@@ -5,28 +5,35 @@ import (
55 "encoding/json"
66 "fmt"
77 "io"
8+ "io/ioutil"
89 "net/http"
910 "os"
1011)
1112
1213const (
13- gitHubUrl string = "https://github.com/"
14+ gitHubUrl string = "https://api. github.com/repos /"
1415 gitHubCredentialsVariable string = "GITHUB_ACCESS_KEY"
16+ getReleasesMethodType string = "GET"
17+ editReleaseMethodType string = "PATCH"
1518 defaultVersion string = "latest"
1619 defaultBranch string = "master"
1720 minimumNumberOfCommandLineArgs int = 3
1821)
1922
2023var (
21- releaseApiUrl string = gitHubUrl + "repos/" + owner + "/" + repo + "/releases/"
22- owner string
23- repo string
24- branch string
25- version string
26- description string
27- credentials string = os .Getenv (gitHubCredentialsVariable )
24+ owner string
25+ repo string
26+ branch string
27+ version string
28+ description string
29+ credentials string = os .Getenv (gitHubCredentialsVariable )
2830)
2931
32+ type IdAndTag struct {
33+ Id int64 `json:"id"`
34+ TagName string `json:"tag_name"`
35+ }
36+
3037type Release struct {
3138 TagName string `json:"tag_name"`
3239 Branch string `json:"target_commitish"`
@@ -37,34 +44,81 @@ type Release struct {
3744}
3845
3946func getReleaseApiUrl () string {
40- return releaseApiUrl + version
47+ return fmt .Sprintf ("%s%s/%s/releases" , gitHubUrl , owner , repo )
48+ }
49+
50+ func getEditReleaseApiUrl (id int64 ) string {
51+ return fmt .Sprintf ("%s/%d" , getReleaseApiUrl (), id )
4152}
4253
43- func sendRequest (body io.Reader , bodySize int64 ) (* http. Response , error ) {
44- req , err := http .NewRequest ("PATCH" , getReleaseApiUrl () , body )
54+ func sendRequest (methodType string , url string , body io.Reader , bodySize int64 ) (int , [] byte , error ) {
55+ req , err := http .NewRequest (methodType , url , body )
4556
4657 if err != nil {
47- return nil , err
58+ return 0 , nil , err
4859 }
4960
5061 req .Header .Set ("Authorization" , fmt .Sprintf ("token %s" , credentials ))
5162 req .Header .Set ("Content-Type" , "application/json" )
5263 req .Header .Set ("Accept" , "application/vnd.github.v3+json" )
5364 req .ContentLength = bodySize
5465
55- os .Stdout .WriteString (fmt .Sprintf ("Sending request to %s with data %s" , getReleaseApiUrl (), body ))
66+ if body == nil {
67+ os .Stdout .WriteString (fmt .Sprintf ("Sending request to %s: %+v\n " , url , req ))
68+ } else {
69+ os .Stdout .WriteString (fmt .Sprintf ("Sending request to %s with data %s: %+v\n " , url , body , req ))
70+ }
5671
5772 resp , err := http .DefaultClient .Do (req )
73+
74+ if err != nil {
75+ return 0 , nil , err
76+ }
77+
5878 defer resp .Body .Close ()
5979
80+ bodyBytes , err := ioutil .ReadAll (resp .Body )
81+
82+ return resp .StatusCode , bodyBytes , err
83+ }
84+
85+ func getReleaseId () (int64 , error ) {
86+ code , bodyBytes , err := sendRequest (getReleasesMethodType , getReleaseApiUrl (), bytes .NewBuffer ([]byte {}), 0 )
87+
6088 if err != nil {
61- return resp , err
89+ return 0 , err
90+ }
91+
92+ if code == http .StatusOK {
93+ fmt .Printf (fmt .Sprintf ("Got releases on branch %s: %s\n " , branch , string (bodyBytes )))
94+ } else {
95+ return 0 , fmt .Errorf ("error getting releases for version %s with response code %d" , getReleaseApiUrl (), code )
6296 }
6397
64- return resp , err
98+ var releases []IdAndTag
99+ json .Unmarshal (bodyBytes , & releases )
100+ os .Stdout .WriteString (fmt .Sprintf ("Response from %s: %v\n " , getReleaseApiUrl (), releases ))
101+
102+ for _ , release := range releases {
103+ if release .TagName == version {
104+ return release .Id , err
105+ }
106+ }
107+
108+ return 0 , err
109+ }
110+
111+ func editRelease (id int64 , body io.Reader , bodySize int64 ) (int , []byte , error ) {
112+ return sendRequest (editReleaseMethodType , getEditReleaseApiUrl (id ), body , bodySize )
65113}
66114
67115func publishDraftRelease () error {
116+ id , err := getReleaseId ()
117+
118+ if err != nil {
119+ return fmt .Errorf ("error getting releases on %s" , getReleaseApiUrl ())
120+ }
121+
68122 release := Release {
69123 TagName : version ,
70124 Branch : branch ,
@@ -80,21 +134,19 @@ func publishDraftRelease() error {
80134 return fmt .Errorf ("error setting JSON data %s when publishing draft release to %s due to %s" , releaseData , getReleaseApiUrl (), err )
81135 }
82136
137+ fmt .Printf (fmt .Sprintf ("Publishing draft release of version %s on branch %s\n " , version , branch ))
138+
83139 releaseBuffer := bytes .NewBuffer (releaseData )
84140
85- resp , err := sendRequest ( releaseBuffer , int64 (releaseBuffer .Len ()))
141+ code , _ , err := editRelease ( id , releaseBuffer , int64 (releaseBuffer .Len ()))
86142
87143 if err != nil {
88- return fmt .Errorf ("error publishing draft release to %s with response %s " , getReleaseApiUrl (), resp )
144+ return fmt .Errorf ("error publishing draft release to %s" , getReleaseApiUrl ())
89145 }
90146
91- if resp == nil {
92- return fmt .Errorf ("error publishing draft release to %s with nil response" , getReleaseApiUrl ())
93- }
94-
95- code := resp .StatusCode
96-
97- if code != http .StatusOK {
147+ if code == http .StatusOK {
148+ fmt .Printf (fmt .Sprintf ("Published draft release of id %d for version %s on branch %s\n " , id , version , branch ))
149+ } else {
98150 return fmt .Errorf ("error publishing draft release to %s with response code %d" , getReleaseApiUrl (), code )
99151 }
100152
@@ -103,20 +155,19 @@ func publishDraftRelease() error {
103155
104156func main () {
105157 if credentials == "" {
106- os .Stderr .WriteString ("Must provide GitHub credentials via GITHUB_CREDENTIALS \n " )
158+ os .Stderr .WriteString ("Must provide GitHub credentials via GITHUB_ACCESS_KEY \n " )
107159 os .Exit (1 )
108160 }
109161
110- if len (os .Args ) != minimumNumberOfCommandLineArgs {
111- os .Stderr .WriteString (fmt .Sprintf ("Only found %d arguments - Must provide owner and repo\n " , len (os .Args )))
162+ numberOfCommandLineArgs := len (os .Args )
163+
164+ if numberOfCommandLineArgs < minimumNumberOfCommandLineArgs {
165+ os .Stderr .WriteString (fmt .Sprintf ("Only found %d arguments - Must provide owner and repo\n " , numberOfCommandLineArgs ))
112166 os .Exit (1 )
113167 }
114168
115169 owner = os .Args [1 ]
116170 repo = os .Args [2 ]
117- version = os .Args [3 ]
118- branch = os .Args [4 ]
119- description = os .Args [5 ]
120171
121172 if owner == "" {
122173 os .Stderr .WriteString ("Must provide owner as the first argument\n " )
@@ -128,16 +179,22 @@ func main() {
128179 os .Exit (1 )
129180 }
130181
131- if version == "" {
182+ if numberOfCommandLineArgs < 4 {
132183 version = defaultVersion
184+ } else {
185+ version = os .Args [3 ]
133186 }
134187
135- if branch == "" {
188+ if numberOfCommandLineArgs < 5 {
136189 branch = defaultBranch
190+ } else {
191+ branch = os .Args [4 ]
137192 }
138193
139- if description == "" {
194+ if numberOfCommandLineArgs < 6 {
140195 description = version
196+ } else {
197+ description = os .Args [5 ]
141198 }
142199
143200 err := publishDraftRelease ()
0 commit comments