|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "encoding/json" |
| 6 | + "fmt" |
| 7 | + "io" |
| 8 | + "net/http" |
| 9 | + "os" |
| 10 | +) |
| 11 | + |
| 12 | +const ( |
| 13 | + gitHubUrl string = "https://github.com/" |
| 14 | + gitHubCredentialsVariable string = "GITHUB_ACCESS_KEY" |
| 15 | + defaultVersion string = "latest" |
| 16 | + defaultBranch string = "master" |
| 17 | + minimumNumberOfCommandLineArgs int = 3 |
| 18 | +) |
| 19 | + |
| 20 | +var ( |
| 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) |
| 28 | +) |
| 29 | + |
| 30 | +type Release struct { |
| 31 | + TagName string `json:"tag_name"` |
| 32 | + Branch string `json:"target_commitish"` |
| 33 | + Name string `json:"name"` |
| 34 | + Body string `json:"body"` |
| 35 | + Draft bool `json:"draft"` |
| 36 | + Prerelease bool `json:"prerelease"` |
| 37 | +} |
| 38 | + |
| 39 | +func getReleaseApiUrl() string { |
| 40 | + return releaseApiUrl + version |
| 41 | +} |
| 42 | + |
| 43 | +func sendRequest(body io.Reader, bodySize int64) (*http.Response, error) { |
| 44 | + req, err := http.NewRequest("PATCH", getReleaseApiUrl(), body) |
| 45 | + |
| 46 | + if err != nil { |
| 47 | + return nil, err |
| 48 | + } |
| 49 | + |
| 50 | + req.Header.Set("Authorization", fmt.Sprintf("token %s", credentials)) |
| 51 | + req.Header.Set("Content-Type", "application/json") |
| 52 | + req.Header.Set("Accept", "application/vnd.github.v3+json") |
| 53 | + req.ContentLength = bodySize |
| 54 | + |
| 55 | + os.Stdout.WriteString(fmt.Sprintf("Sending request to %s with data %s", getReleaseApiUrl(), body)) |
| 56 | + |
| 57 | + resp, err := http.DefaultClient.Do(req) |
| 58 | + defer resp.Body.Close() |
| 59 | + |
| 60 | + if err != nil { |
| 61 | + return resp, err |
| 62 | + } |
| 63 | + |
| 64 | + return resp, err |
| 65 | +} |
| 66 | + |
| 67 | +func publishDraftRelease() error { |
| 68 | + release := Release{ |
| 69 | + TagName: version, |
| 70 | + Branch: branch, |
| 71 | + Name: version, |
| 72 | + Body: description, |
| 73 | + Draft: false, |
| 74 | + Prerelease: false, |
| 75 | + } |
| 76 | + |
| 77 | + releaseData, err := json.Marshal(release) |
| 78 | + |
| 79 | + if err != nil { |
| 80 | + return fmt.Errorf("error setting JSON data %s when publishing draft release to %s due to %s", releaseData, getReleaseApiUrl(), err) |
| 81 | + } |
| 82 | + |
| 83 | + releaseBuffer := bytes.NewBuffer(releaseData) |
| 84 | + |
| 85 | + resp, err := sendRequest(releaseBuffer, int64(releaseBuffer.Len())) |
| 86 | + |
| 87 | + if err != nil { |
| 88 | + return fmt.Errorf("error publishing draft release to %s with response %s", getReleaseApiUrl(), resp) |
| 89 | + } |
| 90 | + |
| 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 { |
| 98 | + return fmt.Errorf("error publishing draft release to %s with response code %d", getReleaseApiUrl(), code) |
| 99 | + } |
| 100 | + |
| 101 | + return nil |
| 102 | +} |
| 103 | + |
| 104 | +func main() { |
| 105 | + if credentials == "" { |
| 106 | + os.Stderr.WriteString("Must provide GitHub credentials via GITHUB_CREDENTIALS\n") |
| 107 | + os.Exit(1) |
| 108 | + } |
| 109 | + |
| 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))) |
| 112 | + os.Exit(1) |
| 113 | + } |
| 114 | + |
| 115 | + owner = os.Args[1] |
| 116 | + repo = os.Args[2] |
| 117 | + version = os.Args[3] |
| 118 | + branch = os.Args[4] |
| 119 | + description = os.Args[5] |
| 120 | + |
| 121 | + if owner == "" { |
| 122 | + os.Stderr.WriteString("Must provide owner as the first argument\n") |
| 123 | + os.Exit(1) |
| 124 | + } |
| 125 | + |
| 126 | + if repo == "" { |
| 127 | + os.Stderr.WriteString("Must provide repo as the second argument\n") |
| 128 | + os.Exit(1) |
| 129 | + } |
| 130 | + |
| 131 | + if version == "" { |
| 132 | + version = defaultVersion |
| 133 | + } |
| 134 | + |
| 135 | + if branch == "" { |
| 136 | + branch = defaultBranch |
| 137 | + } |
| 138 | + |
| 139 | + if description == "" { |
| 140 | + description = version |
| 141 | + } |
| 142 | + |
| 143 | + err := publishDraftRelease() |
| 144 | + |
| 145 | + if err != nil { |
| 146 | + os.Stderr.WriteString(fmt.Sprintf("Failed to publish draft release - %s\n", err)) |
| 147 | + } |
| 148 | +} |
0 commit comments