forked from c4milo/github-release
-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
300 lines (249 loc) · 7.86 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
// This Source Code Form is subject to the terms of the Mozilla Public
// License, version 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
package main
import (
"bytes"
"encoding/json"
"flag"
"fmt"
"io"
"io/ioutil"
"log"
"net/http"
"net/http/httputil"
"os"
"path/filepath"
"strconv"
"strings"
"sync"
)
var (
githubToken string
githubUser string
githubRepo string
githubAPIEndpoint string
// Version gets initialized in compilation time.
Version string
debug bool
)
// Release represents a Github Release.
type Release struct {
UploadURL string `json:"upload_url,omitempty"`
TagName string `json:"tag_name"`
Branch string `json:"target_commitish"`
Name string `json:"name"`
Body string `json:"body"`
Draft bool `json:"draft"`
Prerelease bool `json:"prerelease"`
}
var verFlag bool
var prereleaseFlag bool
var draftFlag bool
func init() {
log.SetFlags(0)
debug, _ = strconv.ParseBool(os.Getenv("DEBUG"))
githubToken = os.Getenv("GITHUB_TOKEN")
githubUser = os.Getenv("GITHUB_USER")
githubRepo = os.Getenv("GITHUB_REPO")
githubAPIEndpoint = os.Getenv("GITHUB_API")
if githubAPIEndpoint == "" {
githubAPIEndpoint = "https://api.github.com"
}
flag.BoolVar(&verFlag, "version", false, "-version")
flag.BoolVar(&prereleaseFlag, "prerelease", false, "-prerelease")
flag.BoolVar(&draftFlag, "draft", false, "-draft")
flag.Parse()
}
var usage = `Github command line release tool.
Usage:
github-release <user/repo> <tag> <branch> <description> "<files>"
Parameters:
<user/repo>: Github user and repository
<tag>: Used to created the release. It is also used as the release's name
<branch>: Reference from where to create the provided <tag>, if it does not exist
<description>: The release description
<files>: Glob pattern describing the list of files to include in the release.
Make sure you enclose it in quotes to avoid the shell expanding the glob pattern.
Options:
-version: Displays version
-prerelease: Identify the release as a prerelease
-draft: Save as draft, don't publish
Environment variables:
DEBUG: Allows you to run github-release in debugging mode. DO NOT do this if you are attempting to upload big files.
GITHUB_TOKEN: Must be set in order to interact with Github's API
GITHUB_USER: Just in case you want an alternative way of providing your github user
GITHUB_REPO: Just in case you want an alternative way of providing your github repo
GITHUB_API: Github API endpoint. Set to https://api.github.com/repos/:github-user/:github-repo by default
Before using this tool make sure you set the environment variable GITHUB_TOKEN
with a valid Github token and correct authorization scopes to allow you to create releases
in your project. For more information about creating Github tokens please read the
official Github documentation at https://help.github.com/articles/creating-an-access-token-for-command-line-use/
Author: https://github.com/c4milo
License: http://mozilla.org/MPL/2.0/
`
func main() {
if verFlag {
log.Println(Version)
return
}
if flag.NArg() != 5 {
log.Printf("Error: Invalid number of arguments (got %d, expected 5)\n\n", flag.NArg())
log.Fatal(usage)
}
userRepo := strings.Split(flag.Arg(0), "/")
if len(userRepo) != 2 {
log.Printf("Error: Invalid format used for username and repository: %s\n\n", flag.Arg(0))
log.Fatal(usage)
}
if githubToken == "" {
log.Fatal(`Error: GITHUB_TOKEN environment variable is not set.
Please refer to https://help.github.com/articles/creating-an-access-token-for-command-line-use/ for more help`)
}
githubUser = userRepo[0]
githubRepo = userRepo[1]
githubAPIEndpoint = fmt.Sprintf("%s/repos/%s/%s", githubAPIEndpoint, githubUser, githubRepo)
if debug {
log.Println("Glob pattern received: ")
log.Println(flag.Arg(4))
}
filepaths, err := filepath.Glob(flag.Arg(4))
if err != nil {
log.Fatalf("Error: Invalid glob pattern: %s\n", flag.Arg(4))
}
if debug {
log.Println("Expanded glob pattern: ")
log.Printf("%v\n", filepaths)
}
tag := flag.Arg(1)
branch := flag.Arg(2)
desc := flag.Arg(3)
release := Release{
TagName: tag,
Name: tag,
Prerelease: prereleaseFlag,
Draft: draftFlag,
Branch: branch,
Body: desc,
}
publishRelease(release, filepaths)
log.Println("Done")
}
func uploadFile(uploadURL, path string) {
file, err := os.Open(path)
if err != nil {
log.Printf("Error: %s\n", err.Error())
return
}
defer file.Close()
size, err := fileSize(file)
if err != nil {
log.Printf("Error: %s\n", err.Error())
return
}
filename := filepath.Base(file.Name())
log.Printf("Uploading %s...\n", filename)
body, err := doRequest("POST", uploadURL+"?name="+filename, "application/octet-stream", file, size)
if err != nil {
log.Printf("Error: %s\n", err.Error())
}
if debug {
log.Println("========= UPLOAD RESPONSE ===========")
log.Println(string(body[:]))
}
}
// CreateRelease creates a Github Release, attaching the given files as release assets
// If a release already exist, up in Github, this function will attempt to attach the given files to it.
func CreateRelease(tag, branch, desc string, filepaths []string) {
release := Release{
TagName: tag,
Name: tag,
Prerelease: false,
Draft: false,
Branch: branch,
Body: desc,
}
publishRelease(release, filepaths)
}
func publishRelease(release Release, filepaths []string) {
endpoint := fmt.Sprintf("%s/releases", githubAPIEndpoint)
releaseData, err := json.Marshal(release)
if err != nil {
log.Fatalln(err)
}
releaseBuffer := bytes.NewBuffer(releaseData)
data, err := doRequest("POST", endpoint, "application/json", releaseBuffer, int64(releaseBuffer.Len()))
if err != nil && data != nil {
log.Println(err)
log.Println("Trying again assuming release already exists.")
endpoint = fmt.Sprintf("%s/releases/tags/%s", githubAPIEndpoint, release.TagName)
data, err = doRequest("GET", endpoint, "application/json", nil, int64(0))
}
if err != nil {
log.Fatalln(err)
}
// Gets the release Upload URL from the returned JSON data
err = json.Unmarshal(data, &release)
if err != nil {
log.Fatalln(err)
}
// Upload URL comes like this https://uploads.github.com/repos/octocat/Hello-World/releases/1/assets{?name}
// So we need to remove the {?name} part
uploadURL := strings.Split(release.UploadURL, "{")[0]
var wg sync.WaitGroup
for i := range filepaths {
wg.Add(1)
func(index int) {
uploadFile(uploadURL, filepaths[index])
wg.Done()
}(i)
}
wg.Wait()
}
func fileSize(file *os.File) (int64, error) {
stat, err := file.Stat()
if err != nil {
return 0, err
}
return stat.Size(), nil
}
// Sends HTTP request to Github API
func doRequest(method, url, contentType string, reqBody io.Reader, bodySize int64) ([]byte, error) {
req, err := http.NewRequest(method, url, reqBody)
if err != nil {
return nil, err
}
req.Header.Set("Authorization", fmt.Sprintf("token %s", githubToken))
req.Header.Set("Content-type", contentType)
req.Header.Set("Accept", "application/vnd.github.v3+json")
req.ContentLength = bodySize
if debug {
log.Println("================ REQUEST DUMP ==================")
dump, err := httputil.DumpRequestOut(req, true)
if err != nil {
log.Println(err.Error())
}
log.Println(string(dump[:]))
}
resp, err := http.DefaultClient.Do(req)
if debug {
log.Println("================ RESPONSE DUMP ==================")
dump, err := httputil.DumpResponse(resp, true)
if err != nil {
log.Println(err.Error())
}
log.Println(string(dump[:]))
}
if err != nil {
return nil, err
}
defer resp.Body.Close()
respBody, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
if resp.StatusCode != http.StatusOK && resp.StatusCode != http.StatusCreated {
return respBody, fmt.Errorf("Github returned an error:\n Code: %s. \n Body: %s", resp.Status, respBody)
}
return respBody, nil
}