-
Notifications
You must be signed in to change notification settings - Fork 89
/
github.go
333 lines (276 loc) · 10.3 KB
/
github.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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
package main
import (
"bytes"
"encoding/json"
"fmt"
"io"
"net/http"
"net/url"
"os"
"regexp"
"strings"
"github.com/dustin/go-humanize"
"github.com/hashicorp/go-version"
"github.com/sirupsen/logrus"
)
type GitHubRepo struct {
Url string // The URL of the GitHub repo
BaseUrl string // The Base URL of the GitHub Instance
ApiUrl string // The API Url of the GitHub Instance
Owner string // The GitHub account name under which the repo exists
Name string // The GitHub repo name
Token string // The personal access token to access this repo (if it's a private repo)
}
type GitHubInstance struct {
BaseUrl string
ApiUrl string
}
// Represents a specific git commit.
// Note that code using GitHub Commit should respect the following hierarchy:
// - CommitSha > BranchName > GitTag
// - Example: GitTag and BranchName are both specified; use the GitTag
// - Example: GitTag and CommitSha are both specified; use the CommitSha
// - Example: BranchName alone is specified; use BranchName
type GitHubCommit struct {
Repo GitHubRepo // The GitHub repo where this release lives
GitRef string // The git reference
GitTag string // The specific git tag for this release
BranchName string // If specified, indicates that this commit should be the latest commit on the given branch
CommitSha string // If specified, indicates that this commit should be exactly this Git Commit SHA.
}
// Modeled directly after the api.github.com response
type GitHubTagsApiResponse struct {
Name string // The tag name
ZipBallUrl string // The URL where a ZIP of the release can be downloaded
TarballUrl string // The URL where a Tarball of the release can be downloaded
Commit GitHubTagsCommitApiResponse
}
// Modeled directly after the api.github.com response
type GitHubTagsCommitApiResponse struct {
Sha string // The SHA of the commit associated with a given tag
Url string // The URL at which additional API information can be found for the given commit
}
// Modeled directly after the api.github.com response (but only includes the fields we care about). For more info, see:
// https://developer.github.com/v3/repos/releases/#get-a-release-by-tag-name
type GitHubReleaseApiResponse struct {
Id int
Url string
Name string
Assets []GitHubReleaseAsset
}
// The "assets" portion of the GitHubReleaseApiResponse. Modeled directly after the api.github.com response (but only
// includes the fields we care about). For more info, see:
// https://developer.github.com/v3/repos/releases/#get-a-release-by-tag-name
type GitHubReleaseAsset struct {
Id int
Url string
Name string
}
func ParseUrlIntoGithubInstance(logger *logrus.Entry, repoUrl string, apiv string) (GitHubInstance, *FetchError) {
var instance GitHubInstance
u, err := url.Parse(repoUrl)
if err != nil {
return instance, newError(githubRepoUrlMalformedOrNotParseable, fmt.Sprintf("GitHub Repo URL %s is malformed.", repoUrl))
}
baseUrl := u.Host
apiUrl := "api.github.com"
if baseUrl != "github.com" && baseUrl != "www.github.com" {
logger.Infof("Assuming GitHub Enterprise since the provided url (%s) does not appear to be for GitHub.com\n", repoUrl)
apiUrl = baseUrl + "/api/" + apiv
}
instance = GitHubInstance{
BaseUrl: baseUrl,
ApiUrl: apiUrl,
}
return instance, nil
}
// Fetch all SemVer tags from the given GitHub repo
func FetchTags(githubRepoUrl string, githubToken string, instance GitHubInstance) ([]string, *FetchError) {
var tagsString []string
repo, err := ParseUrlIntoGitHubRepo(githubRepoUrl, githubToken, instance)
if err != nil {
return tagsString, wrapError(err)
}
// Set per_page to 100, which is the max, to reduce network calls
tagsUrl := formatUrl(repo, createGitHubRepoUrlForPath(repo, "tags?per_page=100"))
for tagsUrl != "" {
resp, err := callGitHubApiRaw(tagsUrl, "GET", repo.Token, map[string]string{})
if err != nil {
return tagsString, err
}
// Convert the response body to a byte array
buf := new(bytes.Buffer)
_, goErr := buf.ReadFrom(resp.Body)
if goErr != nil {
return tagsString, wrapError(goErr)
}
jsonResp := buf.Bytes()
// Extract the JSON into our array of gitHubTagsCommitApiResponse's
var tags []GitHubTagsApiResponse
if err := json.Unmarshal(jsonResp, &tags); err != nil {
return tagsString, wrapError(err)
}
for _, tag := range tags {
// Skip tags that are not semantically versioned so that they don't cause errors. (issue #75)
if _, err := version.NewVersion(tag.Name); err == nil {
tagsString = append(tagsString, tag.Name)
}
}
// Get paginated tags (issue #26 and #46)
tagsUrl = getNextUrl(resp.Header.Get("link"))
}
return tagsString, nil
}
// Convert a URL into a GitHubRepo struct
func ParseUrlIntoGitHubRepo(url string, token string, instance GitHubInstance) (GitHubRepo, *FetchError) {
var gitHubRepo GitHubRepo
regex, regexErr := regexp.Compile("https?://(?:www\\.)?" + instance.BaseUrl + "/(.+?)/(.+?)(?:$|\\?|#|/)")
if regexErr != nil {
return gitHubRepo, newError(githubRepoUrlMalformedOrNotParseable, fmt.Sprintf("GitHub Repo URL %s is malformed.", url))
}
matches := regex.FindStringSubmatch(url)
if len(matches) != 3 {
return gitHubRepo, newError(githubRepoUrlMalformedOrNotParseable, fmt.Sprintf("GitHub Repo URL %s could not be parsed correctly", url))
}
gitHubRepo = GitHubRepo{
Url: url,
BaseUrl: instance.BaseUrl,
ApiUrl: instance.ApiUrl,
Owner: matches[1],
Name: matches[2],
Token: token,
}
return gitHubRepo, nil
}
// Download the release asset with the given id and return its body
func DownloadReleaseAsset(repo GitHubRepo, assetId int, destPath string, withProgress bool) *FetchError {
url := createGitHubRepoUrlForPath(repo, fmt.Sprintf("releases/assets/%d", assetId))
resp, err := callGitHubApi(repo, url, map[string]string{"Accept": "application/octet-stream"})
if err != nil {
return err
}
return writeResonseToDisk(resp, destPath, withProgress)
}
// Get information about the GitHub release with the given tag
func GetGitHubReleaseInfo(repo GitHubRepo, tag string) (GitHubReleaseApiResponse, *FetchError) {
release := GitHubReleaseApiResponse{}
url := createGitHubRepoUrlForPath(repo, fmt.Sprintf("releases/tags/%s", tag))
resp, err := callGitHubApi(repo, url, map[string]string{})
if err != nil {
return release, err
}
// Convert the response body to a byte array
buf := new(bytes.Buffer)
_, goErr := buf.ReadFrom(resp.Body)
if goErr != nil {
return release, wrapError(goErr)
}
jsonResp := buf.Bytes()
if err := json.Unmarshal(jsonResp, &release); err != nil {
return release, wrapError(err)
}
return release, nil
}
// Craft a URL for the GitHub repos API of the form repos/:owner/:repo/:path
func createGitHubRepoUrlForPath(repo GitHubRepo, path string) string {
return fmt.Sprintf("repos/%s/%s/%s", repo.Owner, repo.Name, path)
}
var nextLinkRegex = regexp.MustCompile(`<(.+?)>;\s*rel="next"`)
// Get the next page URL from the given link header returned by the GitHub API. If there is no next page, return an
// empty string. The link header is expected to be of the form:
//
// <url>; rel="next", <url>; rel="last"
func getNextUrl(links string) string {
if len(links) == 0 {
return ""
}
for _, link := range strings.Split(links, ",") {
urlMatches := nextLinkRegex.FindStringSubmatch(link)
if len(urlMatches) == 2 {
return strings.TrimSpace(urlMatches[1])
}
}
return ""
}
// Format a URL for calling the GitHub API for the given repo and path
func formatUrl(repo GitHubRepo, path string) string {
return fmt.Sprintf("https://"+repo.ApiUrl+"/%s", path)
}
// Call the GitHub API at the given path and return the HTTP response
func callGitHubApi(repo GitHubRepo, path string, customHeaders map[string]string) (*http.Response, *FetchError) {
return callGitHubApiRaw(formatUrl(repo, path), "GET", repo.Token, customHeaders)
}
// Call the GitHub API at the given URL, using the given HTTP method, and passing the given token and headers, and
// return the response
func callGitHubApiRaw(url string, method string, token string, customHeaders map[string]string) (*http.Response, *FetchError) {
httpClient := &http.Client{}
request, err := http.NewRequest(method, url, nil)
if err != nil {
return nil, wrapError(err)
}
if token != "" {
request.Header.Set("Authorization", fmt.Sprintf("token %s", token))
}
for headerName, headerValue := range customHeaders {
request.Header.Set(headerName, headerValue)
}
resp, err := httpClient.Do(request)
if err != nil {
return nil, wrapError(err)
}
if resp.StatusCode != http.StatusOK {
// Convert the resp.Body to a string
buf := new(bytes.Buffer)
_, goErr := buf.ReadFrom(resp.Body)
if goErr != nil {
return nil, wrapError(goErr)
}
respBody := buf.String()
// We leverage the HTTP Response Code as our ErrorCode here.
return nil, newError(resp.StatusCode, fmt.Sprintf("Received HTTP Response %d while fetching releases for GitHub URL %s. Full HTTP response: %s", resp.StatusCode, url, respBody))
}
return resp, nil
}
type writeCounter struct {
written uint64
suffix string // contains " / SIZE MB" if size is known, otherwise empty
}
func newWriteCounter(total int64) *writeCounter {
if total > 0 {
return &writeCounter{
suffix: fmt.Sprintf(" / %s", humanize.Bytes(uint64(total))),
}
}
return &writeCounter{}
}
func (wc *writeCounter) Write(p []byte) (int, error) {
n := len(p)
wc.written += uint64(n)
wc.PrintProgress()
return n, nil
}
func (wc writeCounter) PrintProgress() {
// Clear the line by using a character return to go back to the start and remove
// the remaining characters by filling it with spaces
fmt.Printf("\r%s", strings.Repeat(" ", 35))
// Return again and print current status of download
// We use the humanize package to print the bytes in a meaningful way (e.g. 10 MB)
fmt.Printf("\rDownloading... %s%s", humanize.Bytes(wc.written), wc.suffix)
}
// Write the body of the given HTTP response to disk at the given path
func writeResonseToDisk(resp *http.Response, destPath string, withProgress bool) *FetchError {
out, err := os.Create(destPath)
if err != nil {
return wrapError(err)
}
defer out.Close()
defer resp.Body.Close()
var readCloser io.Reader
if withProgress {
readCloser = io.TeeReader(resp.Body, newWriteCounter(resp.ContentLength))
} else {
readCloser = resp.Body
}
_, err = io.Copy(out, readCloser)
return wrapError(err)
}