|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "encoding/json" |
| 6 | + "fmt" |
| 7 | + "io" |
| 8 | + "io/ioutil" |
| 9 | + "net/http" |
| 10 | + "os" |
| 11 | + "strconv" |
| 12 | +) |
| 13 | + |
| 14 | +const ( |
| 15 | + gitHubUrl string = "https://api.github.com/repos/" |
| 16 | + gitHubCredentialsVariable string = "GITHUB_ACCESS_KEY" |
| 17 | + debugMessagesVariable string = "DEBUG_MESSAGES_ENABLED" |
| 18 | + getReleasesMethodType string = "GET" |
| 19 | + editReleaseMethodType string = "PATCH" |
| 20 | + defaultVersion string = "latest" |
| 21 | + defaultBranch string = "master" |
| 22 | + minimumNumberOfCommandLineArgs int = 3 |
| 23 | +) |
| 24 | + |
| 25 | +var ( |
| 26 | + owner string |
| 27 | + repo string |
| 28 | + branch string |
| 29 | + version string |
| 30 | + description string |
| 31 | + credentials string = os.Getenv(gitHubCredentialsVariable) |
| 32 | + debugMessages bool |
| 33 | +) |
| 34 | + |
| 35 | +type IdAndTag struct { |
| 36 | + Id int64 `json:"id"` |
| 37 | + TagName string `json:"tag_name"` |
| 38 | +} |
| 39 | + |
| 40 | +type Release struct { |
| 41 | + TagName string `json:"tag_name"` |
| 42 | + Branch string `json:"target_commitish"` |
| 43 | + Name string `json:"name"` |
| 44 | + Body string `json:"body"` |
| 45 | + Draft bool `json:"draft"` |
| 46 | + Prerelease bool `json:"prerelease"` |
| 47 | +} |
| 48 | + |
| 49 | +func getReleaseApiUrl() string { |
| 50 | + return fmt.Sprintf("%s%s/%s/releases", gitHubUrl, owner, repo) |
| 51 | +} |
| 52 | + |
| 53 | +func getEditReleaseApiUrl(id int64) string { |
| 54 | + return fmt.Sprintf("%s/%d", getReleaseApiUrl(), id) |
| 55 | +} |
| 56 | + |
| 57 | +func sendRequest(methodType string, url string, body io.Reader, bodySize int64) (int, []byte, error) { |
| 58 | + req, err := http.NewRequest(methodType, url, body) |
| 59 | + |
| 60 | + if err != nil { |
| 61 | + return 0, nil, err |
| 62 | + } |
| 63 | + |
| 64 | + req.Header.Set("Authorization", fmt.Sprintf("token %s", credentials)) |
| 65 | + req.Header.Set("Content-Type", "application/json") |
| 66 | + req.Header.Set("Accept", "application/vnd.github.v3+json") |
| 67 | + req.ContentLength = bodySize |
| 68 | + |
| 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 | + } |
| 75 | + } |
| 76 | + |
| 77 | + resp, err := http.DefaultClient.Do(req) |
| 78 | + |
| 79 | + if err != nil { |
| 80 | + return 0, nil, err |
| 81 | + } |
| 82 | + |
| 83 | + defer resp.Body.Close() |
| 84 | + |
| 85 | + bodyBytes, err := ioutil.ReadAll(resp.Body) |
| 86 | + |
| 87 | + return resp.StatusCode, bodyBytes, err |
| 88 | +} |
| 89 | + |
| 90 | +func getReleaseId() (int64, error) { |
| 91 | + url := getReleaseApiUrl() |
| 92 | + code, bodyBytes, err := sendRequest(getReleasesMethodType, url, bytes.NewBuffer([]byte{}), 0) |
| 93 | + |
| 94 | + if err != nil { |
| 95 | + return 0, err |
| 96 | + } |
| 97 | + |
| 98 | + if code == http.StatusOK { |
| 99 | + fmt.Printf(fmt.Sprintf("Got releases on branch %s: %s\n", branch, string(bodyBytes))) |
| 100 | + } else { |
| 101 | + return 0, fmt.Errorf("error getting releases for version %s with response code %d", url, code) |
| 102 | + } |
| 103 | + |
| 104 | + var releases []IdAndTag |
| 105 | + json.Unmarshal(bodyBytes, &releases) |
| 106 | + os.Stdout.WriteString(fmt.Sprintf("Response from %s: %v\n", url, releases)) |
| 107 | + |
| 108 | + for _, release := range releases { |
| 109 | + if release.TagName == version { |
| 110 | + return release.Id, err |
| 111 | + } |
| 112 | + } |
| 113 | + |
| 114 | + return 0, fmt.Errorf("No ID found from %s using version %s", url, version) |
| 115 | +} |
| 116 | + |
| 117 | +func editRelease(id int64, body io.Reader, bodySize int64) (int, []byte, error) { |
| 118 | + return sendRequest(editReleaseMethodType, getEditReleaseApiUrl(id), body, bodySize) |
| 119 | +} |
| 120 | + |
| 121 | +func publishDraftRelease() error { |
| 122 | + id, err := getReleaseId() |
| 123 | + |
| 124 | + if err != nil { |
| 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") |
| 130 | + } |
| 131 | + |
| 132 | + release := Release{ |
| 133 | + TagName: version, |
| 134 | + Branch: branch, |
| 135 | + Name: version, |
| 136 | + Body: description, |
| 137 | + Draft: false, |
| 138 | + Prerelease: false, |
| 139 | + } |
| 140 | + |
| 141 | + releaseData, err := json.Marshal(release) |
| 142 | + |
| 143 | + if err != nil { |
| 144 | + return fmt.Errorf("error setting JSON data %s when publishing draft release to %s due to %s", releaseData, getReleaseApiUrl(), err) |
| 145 | + } |
| 146 | + |
| 147 | + fmt.Printf(fmt.Sprintf("Publishing draft release of version %s on branch %s\n", version, branch)) |
| 148 | + |
| 149 | + releaseBuffer := bytes.NewBuffer(releaseData) |
| 150 | + |
| 151 | + code, _, err := editRelease(id, releaseBuffer, int64(releaseBuffer.Len())) |
| 152 | + |
| 153 | + if err != nil { |
| 154 | + return fmt.Errorf("error publishing draft release to %s", getReleaseApiUrl()) |
| 155 | + } |
| 156 | + |
| 157 | + if code == http.StatusOK { |
| 158 | + fmt.Printf(fmt.Sprintf("Published draft release of id %d for version %s on branch %s\n", id, version, branch)) |
| 159 | + } else { |
| 160 | + return fmt.Errorf("error publishing draft release to %s with response code %d", getReleaseApiUrl(), code) |
| 161 | + } |
| 162 | + |
| 163 | + return nil |
| 164 | +} |
| 165 | + |
| 166 | +func main() { |
| 167 | + if credentials == "" { |
| 168 | + os.Stderr.WriteString("Must provide GitHub credentials via GITHUB_ACCESS_KEY\n") |
| 169 | + os.Exit(1) |
| 170 | + } |
| 171 | + |
| 172 | + numberOfCommandLineArgs := len(os.Args) |
| 173 | + |
| 174 | + if numberOfCommandLineArgs < minimumNumberOfCommandLineArgs { |
| 175 | + os.Stderr.WriteString(fmt.Sprintf("Only found %d arguments - Must provide owner and repo\n", numberOfCommandLineArgs)) |
| 176 | + os.Exit(1) |
| 177 | + } |
| 178 | + |
| 179 | + owner = os.Args[1] |
| 180 | + repo = os.Args[2] |
| 181 | + |
| 182 | + if owner == "" { |
| 183 | + os.Stderr.WriteString("Must provide owner as the first argument\n") |
| 184 | + os.Exit(1) |
| 185 | + } |
| 186 | + |
| 187 | + if repo == "" { |
| 188 | + os.Stderr.WriteString("Must provide repo as the second argument\n") |
| 189 | + os.Exit(1) |
| 190 | + } |
| 191 | + |
| 192 | + if numberOfCommandLineArgs < 4 { |
| 193 | + version = defaultVersion |
| 194 | + } else { |
| 195 | + version = os.Args[3] |
| 196 | + } |
| 197 | + |
| 198 | + if numberOfCommandLineArgs < 5 { |
| 199 | + branch = defaultBranch |
| 200 | + } else { |
| 201 | + branch = os.Args[4] |
| 202 | + } |
| 203 | + |
| 204 | + if numberOfCommandLineArgs < 6 { |
| 205 | + description = version |
| 206 | + } else { |
| 207 | + description = os.Args[5] |
| 208 | + } |
| 209 | + |
| 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() |
| 219 | + |
| 220 | + if err != nil { |
| 221 | + os.Stderr.WriteString(fmt.Sprintf("Failed to publish draft release - %s\n", err)) |
| 222 | + } |
| 223 | +} |
0 commit comments