@@ -8,11 +8,13 @@ import (
88 "io/ioutil"
99 "net/http"
1010 "os"
11+ "strconv"
1112)
1213
1314const (
1415 gitHubUrl string = "https://api.github.com/repos/"
1516 gitHubCredentialsVariable string = "GITHUB_ACCESS_KEY"
17+ debugMessagesVariable string = "DEBUG_MESSAGES_ENABLED"
1618 getReleasesMethodType string = "GET"
1719 editReleaseMethodType string = "PATCH"
1820 defaultVersion string = "latest"
@@ -21,12 +23,13 @@ const (
2123)
2224
2325var (
24- owner string
25- repo string
26- branch string
27- version string
28- description string
29- credentials string = os .Getenv (gitHubCredentialsVariable )
26+ owner string
27+ repo string
28+ branch string
29+ version string
30+ description string
31+ credentials string = os .Getenv (gitHubCredentialsVariable )
32+ debugMessages bool
3033)
3134
3235type IdAndTag struct {
@@ -63,10 +66,12 @@ func sendRequest(methodType string, url string, body io.Reader, bodySize int64)
6366 req .Header .Set ("Accept" , "application/vnd.github.v3+json" )
6467 req .ContentLength = bodySize
6568
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 ))
69+ if debugMessages {
70+ if body == nil {
71+ os .Stdout .WriteString (fmt .Sprintf ("Sending request to %s: %+v\n " , url , req ))
72+ } else {
73+ os .Stdout .WriteString (fmt .Sprintf ("Sending request to %s with data %s: %+v\n " , url , body , req ))
74+ }
7075 }
7176
7277 resp , err := http .DefaultClient .Do (req )
@@ -83,7 +88,8 @@ func sendRequest(methodType string, url string, body io.Reader, bodySize int64)
8388}
8489
8590func getReleaseId () (int64 , error ) {
86- code , bodyBytes , err := sendRequest (getReleasesMethodType , getReleaseApiUrl (), bytes .NewBuffer ([]byte {}), 0 )
91+ url := getReleaseApiUrl ()
92+ code , bodyBytes , err := sendRequest (getReleasesMethodType , url , bytes .NewBuffer ([]byte {}), 0 )
8793
8894 if err != nil {
8995 return 0 , err
@@ -92,20 +98,20 @@ func getReleaseId() (int64, error) {
9298 if code == http .StatusOK {
9399 fmt .Printf (fmt .Sprintf ("Got releases on branch %s: %s\n " , branch , string (bodyBytes )))
94100 } else {
95- return 0 , fmt .Errorf ("error getting releases for version %s with response code %d" , getReleaseApiUrl () , code )
101+ return 0 , fmt .Errorf ("error getting releases for version %s with response code %d" , url , code )
96102 }
97103
98104 var releases []IdAndTag
99105 json .Unmarshal (bodyBytes , & releases )
100- os .Stdout .WriteString (fmt .Sprintf ("Response from %s: %v\n " , getReleaseApiUrl () , releases ))
106+ os .Stdout .WriteString (fmt .Sprintf ("Response from %s: %v\n " , url , releases ))
101107
102108 for _ , release := range releases {
103109 if release .TagName == version {
104110 return release .Id , err
105111 }
106112 }
107113
108- return 0 , err
114+ return 0 , fmt . Errorf ( "No ID found from %s using version %s" , url , version )
109115}
110116
111117func editRelease (id int64 , body io.Reader , bodySize int64 ) (int , []byte , error ) {
@@ -116,7 +122,11 @@ func publishDraftRelease() error {
116122 id , err := getReleaseId ()
117123
118124 if err != nil {
119- return fmt .Errorf ("error getting releases on %s" , getReleaseApiUrl ())
125+ return fmt .Errorf ("error getting releases: %s" , err )
126+ }
127+
128+ if id == 0 {
129+ return fmt .Errorf ("error getting releases - no ID found" )
120130 }
121131
122132 release := Release {
@@ -197,7 +207,15 @@ func main() {
197207 description = os .Args [5 ]
198208 }
199209
200- err := publishDraftRelease ()
210+ var err error
211+
212+ debugMessages , err = strconv .ParseBool (os .Getenv (debugMessagesVariable ))
213+
214+ if err != nil {
215+ debugMessages = false
216+ }
217+
218+ err = publishDraftRelease ()
201219
202220 if err != nil {
203221 os .Stderr .WriteString (fmt .Sprintf ("Failed to publish draft release - %s\n " , err ))
0 commit comments