-
Notifications
You must be signed in to change notification settings - Fork 58
/
github.go
357 lines (293 loc) · 8.83 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
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
package resource
import (
"context"
"crypto/tls"
"errors"
"fmt"
"io"
"net/http"
"net/url"
"os"
"strings"
"github.com/google/go-github/v66/github"
"github.com/shurcooL/githubv4"
"golang.org/x/oauth2"
)
// Last run with counterfeiter v6
//go:generate counterfeiter -o fakes/fake_git_hub.go . GitHub
type GitHub interface {
ListReleases() ([]*github.RepositoryRelease, error)
GetReleaseByTag(tag string) (*github.RepositoryRelease, error)
GetRelease(id int) (*github.RepositoryRelease, error)
CreateRelease(release github.RepositoryRelease) (*github.RepositoryRelease, error)
UpdateRelease(release github.RepositoryRelease) (*github.RepositoryRelease, error)
ListReleaseAssets(release github.RepositoryRelease) ([]*github.ReleaseAsset, error)
UploadReleaseAsset(release github.RepositoryRelease, name string, file *os.File) error
DeleteReleaseAsset(asset github.ReleaseAsset) error
DownloadReleaseAsset(asset github.ReleaseAsset) (io.ReadCloser, error)
GetTarballLink(tag string) (*url.URL, error)
GetZipballLink(tag string) (*url.URL, error)
ResolveTagToCommitSHA(tag string) (string, error)
}
type GitHubClient struct {
client *github.Client
clientV4 *githubv4.Client
isEnterprise bool
owner string
repository string
accessToken string
}
func NewGitHubClient(source Source) (*GitHubClient, error) {
var httpClient = &http.Client{}
var ctx = context.TODO()
if source.Insecure {
httpClient.Transport = &http.Transport{
Proxy: http.ProxyFromEnvironment,
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
}
ctx = context.WithValue(ctx, oauth2.HTTPClient, httpClient)
}
if source.AccessToken != "" {
var err error
httpClient, err = oauthClient(ctx, source)
if err != nil {
return nil, err
}
}
client := github.NewClient(httpClient)
clientV4 := githubv4.NewClient(httpClient)
var isEnterprise bool
if source.GitHubAPIURL != "" {
var err error
if !strings.HasSuffix(source.GitHubAPIURL, "/") {
source.GitHubAPIURL += "/"
}
client.BaseURL, err = url.Parse(source.GitHubAPIURL)
if err != nil {
return nil, err
}
client.UploadURL, err = url.Parse(source.GitHubAPIURL)
if err != nil {
return nil, err
}
var v4URL string
if strings.HasSuffix(source.GitHubAPIURL, "/v3/") {
v4URL = strings.TrimSuffix(source.GitHubAPIURL, "/v3/") + "/graphql"
} else {
v4URL = source.GitHubAPIURL + "graphql"
}
clientV4 = githubv4.NewEnterpriseClient(v4URL, httpClient)
isEnterprise = true
}
if source.GitHubV4APIURL != "" {
clientV4 = githubv4.NewEnterpriseClient(source.GitHubV4APIURL, httpClient)
isEnterprise = true
}
if source.GitHubUploadsURL != "" {
var err error
client.UploadURL, err = url.Parse(source.GitHubUploadsURL)
if err != nil {
return nil, err
}
}
owner := source.Owner
if source.User != "" {
owner = source.User
}
return &GitHubClient{
client: client,
clientV4: clientV4,
isEnterprise: isEnterprise,
owner: owner,
repository: source.Repository,
accessToken: source.AccessToken,
}, nil
}
func (g *GitHubClient) ListReleases() ([]*github.RepositoryRelease, error) {
if g.accessToken != "" {
if g.isEnterprise {
return g.listReleasesV4EnterPrice()
}
return g.listReleasesV4()
}
opt := &github.ListOptions{PerPage: 100}
var allReleases []*github.RepositoryRelease
for {
releases, res, err := g.client.Repositories.ListReleases(context.TODO(), g.owner, g.repository, opt)
if err != nil {
return []*github.RepositoryRelease{}, err
}
allReleases = append(allReleases, releases...)
if res.NextPage == 0 {
err = res.Body.Close()
if err != nil {
return nil, err
}
break
}
opt.Page = res.NextPage
}
return allReleases, nil
}
func (g *GitHubClient) GetReleaseByTag(tag string) (*github.RepositoryRelease, error) {
release, res, err := g.client.Repositories.GetReleaseByTag(context.TODO(), g.owner, g.repository, tag)
if err != nil {
return &github.RepositoryRelease{}, err
}
err = res.Body.Close()
if err != nil {
return nil, err
}
return release, nil
}
func (g *GitHubClient) GetRelease(id int) (*github.RepositoryRelease, error) {
release, res, err := g.client.Repositories.GetRelease(context.TODO(), g.owner, g.repository, int64(id))
if err != nil {
return &github.RepositoryRelease{}, err
}
err = res.Body.Close()
if err != nil {
return nil, err
}
return release, nil
}
func (g *GitHubClient) CreateRelease(release github.RepositoryRelease) (*github.RepositoryRelease, error) {
createdRelease, res, err := g.client.Repositories.CreateRelease(context.TODO(), g.owner, g.repository, &release)
if err != nil {
return &github.RepositoryRelease{}, err
}
err = res.Body.Close()
if err != nil {
return nil, err
}
return createdRelease, nil
}
func (g *GitHubClient) UpdateRelease(release github.RepositoryRelease) (*github.RepositoryRelease, error) {
if release.ID == nil {
return nil, errors.New("release did not have an ID: has it been saved yet?")
}
updatedRelease, res, err := g.client.Repositories.EditRelease(context.TODO(), g.owner, g.repository, *release.ID, &release)
if err != nil {
return &github.RepositoryRelease{}, err
}
err = res.Body.Close()
if err != nil {
return nil, err
}
return updatedRelease, nil
}
func (g *GitHubClient) ListReleaseAssets(release github.RepositoryRelease) ([]*github.ReleaseAsset, error) {
opt := &github.ListOptions{PerPage: 100}
assets, res, err := g.client.Repositories.ListReleaseAssets(context.TODO(), g.owner, g.repository, *release.ID, opt)
if err != nil {
return nil, err
}
err = res.Body.Close()
if err != nil {
return nil, err
}
return assets, nil
}
func (g *GitHubClient) UploadReleaseAsset(release github.RepositoryRelease, name string, file *os.File) error {
_, res, err := g.client.Repositories.UploadReleaseAsset(
context.TODO(),
g.owner,
g.repository,
*release.ID,
&github.UploadOptions{
Name: name,
},
file,
)
if err != nil {
return err
}
return res.Body.Close()
}
func (g *GitHubClient) DeleteReleaseAsset(asset github.ReleaseAsset) error {
res, err := g.client.Repositories.DeleteReleaseAsset(context.TODO(), g.owner, g.repository, *asset.ID)
if err != nil {
return err
}
return res.Body.Close()
}
func (g *GitHubClient) DownloadReleaseAsset(asset github.ReleaseAsset) (io.ReadCloser, error) {
bodyReader, redirectURL, err := g.client.Repositories.DownloadReleaseAsset(context.TODO(), g.owner, g.repository, *asset.ID, nil)
if err != nil {
return nil, err
}
if redirectURL == "" {
return bodyReader, err
}
req, err := g.client.NewRequest("GET", redirectURL, nil)
if err != nil {
return nil, err
}
req.Header.Set("Accept", "application/octet-stream")
if g.accessToken != "" && req.URL.Host == g.client.BaseURL.Host {
req.Header.Set("Authorization", "Bearer "+g.accessToken)
}
httpClient := &http.Client{}
resp, err := httpClient.Do(req)
if err != nil {
return nil, err
}
if resp.StatusCode < 200 || resp.StatusCode > 299 {
resp.Body.Close()
return nil, fmt.Errorf("redirect URL %q responded with bad status code: %d", redirectURL, resp.StatusCode)
}
return resp.Body, nil
}
func (g *GitHubClient) GetTarballLink(tag string) (*url.URL, error) {
opt := &github.RepositoryContentGetOptions{Ref: tag}
u, res, err := g.client.Repositories.GetArchiveLink(context.TODO(), g.owner, g.repository, github.Tarball, opt, 10)
if err != nil {
return nil, err
}
res.Body.Close()
return u, nil
}
func (g *GitHubClient) GetZipballLink(tag string) (*url.URL, error) {
opt := &github.RepositoryContentGetOptions{Ref: tag}
u, res, err := g.client.Repositories.GetArchiveLink(context.TODO(), g.owner, g.repository, github.Zipball, opt, 10)
if err != nil {
return nil, err
}
res.Body.Close()
return u, nil
}
func (g *GitHubClient) ResolveTagToCommitSHA(tagName string) (string, error) {
ref, res, err := g.client.Git.GetRef(context.TODO(), g.owner, g.repository, "tags/"+tagName)
if err != nil {
return "", err
}
res.Body.Close()
// Lightweight tag
if *ref.Object.Type == "commit" {
return *ref.Object.SHA, nil
}
// Fail if we're not pointing to a annotated tag
if *ref.Object.Type != "tag" {
return "", fmt.Errorf("could not resolve tag %q to commit: returned type is not 'commit' or 'tag'", tagName)
}
// Resolve tag to commit sha
tag, res, err := g.client.Git.GetTag(context.TODO(), g.owner, g.repository, *ref.Object.SHA)
if err != nil {
return "", err
}
res.Body.Close()
if *tag.Object.Type != "commit" {
return "", fmt.Errorf("could not resolve tag %q to commit: returned type is not 'commit'", tagName)
}
return *tag.Object.SHA, nil
}
func oauthClient(ctx context.Context, source Source) (*http.Client, error) {
ts := oauth2.StaticTokenSource(&oauth2.Token{
AccessToken: source.AccessToken,
})
oauthClient := oauth2.NewClient(ctx, ts)
githubHTTPClient := &http.Client{
Transport: oauthClient.Transport,
}
return githubHTTPClient, nil
}