forked from tcnksm/ghr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
github.go
268 lines (217 loc) · 7.4 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
package main
import (
"context"
"fmt"
"net/http"
"net/url"
"os"
"path/filepath"
"strings"
"time"
"github.com/Songmu/retry"
"github.com/google/go-github/github"
"github.com/pkg/errors"
"golang.org/x/oauth2"
)
// ErrReleaseNotFound contains the error for when a release is not found
var (
ErrReleaseNotFound = errors.New("release is not found")
)
// GitHub contains the functions necessary for interacting with GitHub release
// objects
type GitHub interface {
CreateRelease(ctx context.Context, req *github.RepositoryRelease) (*github.RepositoryRelease, error)
GetRelease(ctx context.Context, tag string) (*github.RepositoryRelease, error)
EditRelease(ctx context.Context, releaseID int64, req *github.RepositoryRelease) (*github.RepositoryRelease, error)
DeleteRelease(ctx context.Context, releaseID int64) error
DeleteTag(ctx context.Context, tag string) error
UploadAsset(ctx context.Context, releaseID int64, filename string) (*github.ReleaseAsset, error)
DeleteAsset(ctx context.Context, assetID int64) error
ListAssets(ctx context.Context, releaseID int64) ([]*github.ReleaseAsset, error)
SetUploadURL(urlStr string) error
}
// GitHubClient is the client for interacting with the GitHub API
type GitHubClient struct {
Owner, Repo string
*github.Client
}
// NewGitHubClient creates and initializes a new GitHubClient
func NewGitHubClient(owner, repo, token, urlStr string) (GitHub, error) {
if len(owner) == 0 {
return nil, errors.New("missing GitHub repository owner")
}
if len(repo) == 0 {
return nil, errors.New("missing GitHub repository name")
}
if len(token) == 0 {
return nil, errors.New("missing GitHub API token")
}
if len(urlStr) == 0 {
return nil, errors.New("missing GitHub API URL")
}
baseURL, err := url.ParseRequestURI(urlStr)
if err != nil {
return nil, errors.Wrap(err, "failed to parse Github API URL")
}
ts := oauth2.StaticTokenSource(&oauth2.Token{
AccessToken: token,
})
tc := oauth2.NewClient(context.TODO(), ts)
client := github.NewClient(tc)
client.BaseURL = baseURL
return &GitHubClient{
Owner: owner,
Repo: repo,
Client: client,
}, nil
}
// SetUploadURL constructs the upload URL for a release
func (c *GitHubClient) SetUploadURL(urlStr string) error {
i := strings.Index(urlStr, "repos/")
parsedURL, err := url.ParseRequestURI(urlStr[:i])
if err != nil {
return errors.Wrap(err, "failed to parse upload URL")
}
c.UploadURL = parsedURL
return nil
}
// CreateRelease creates a new release object in the GitHub API
func (c *GitHubClient) CreateRelease(ctx context.Context, req *github.RepositoryRelease) (*github.RepositoryRelease, error) {
release, res, err := c.Repositories.CreateRelease(context.TODO(), c.Owner, c.Repo, req)
if err != nil {
return nil, errors.Wrap(err, "failed to create a release")
}
if res.StatusCode != http.StatusCreated {
return nil, errors.Errorf("create release: invalid status: %s", res.Status)
}
return release, nil
}
// GetRelease queries the GitHub API for a specified release object
func (c *GitHubClient) GetRelease(ctx context.Context, tag string) (*github.RepositoryRelease, error) {
// Check Release whether already exists or not
release, res, err := c.Repositories.GetReleaseByTag(context.TODO(), c.Owner, c.Repo, tag)
if err != nil {
if res == nil {
return nil, errors.Wrapf(err, "failed to get release tag: %s", tag)
}
// TODO(tcnksm): Handle invalid token
if res.StatusCode != http.StatusNotFound {
return nil, errors.Wrapf(err,
"get release tag: invalid status: %s", res.Status)
}
return nil, ErrReleaseNotFound
}
return release, nil
}
// EditRelease edit a release object within the GitHub API
func (c *GitHubClient) EditRelease(ctx context.Context, releaseID int64, req *github.RepositoryRelease) (*github.RepositoryRelease, error) {
var release *github.RepositoryRelease
err := retry.Retry(3, 3*time.Second, func() error {
var (
res *github.Response
err error
)
release, res, err = c.Repositories.EditRelease(context.TODO(), c.Owner, c.Repo, releaseID, req)
if err != nil {
return errors.Wrapf(err, "failed to edit release: %d", releaseID)
}
if res.StatusCode != http.StatusOK {
return errors.Errorf("edit release: invalid status: %s", res.Status)
}
return nil
})
return release, err
}
// DeleteRelease deletes a release object within the GitHub API
func (c *GitHubClient) DeleteRelease(ctx context.Context, releaseID int64) error {
res, err := c.Repositories.DeleteRelease(context.TODO(), c.Owner, c.Repo, releaseID)
if err != nil {
return errors.Wrap(err, "failed to delete release")
}
if res.StatusCode != http.StatusNoContent {
return errors.Errorf("delete release: invalid status: %s", res.Status)
}
return nil
}
// DeleteTag deletes a tag from the GitHub API
func (c *GitHubClient) DeleteTag(ctx context.Context, tag string) error {
ref := fmt.Sprintf("tags/%s", tag)
res, err := c.Git.DeleteRef(context.TODO(), c.Owner, c.Repo, ref)
if err != nil {
return errors.Wrapf(err, "failed to delete tag: %s", ref)
}
if res.StatusCode != http.StatusNoContent {
return errors.Errorf("delete tag: invalid status: %s", res.Status)
}
return nil
}
// UploadAsset uploads specified assets to a given release object
func (c *GitHubClient) UploadAsset(ctx context.Context, releaseID int64, filename string) (*github.ReleaseAsset, error) {
filename, err := filepath.Abs(filename)
if err != nil {
return nil, errors.Wrap(err, "failed to get abs path")
}
opts := &github.UploadOptions{
// Use base name by default
Name: filepath.Base(filename),
}
var asset *github.ReleaseAsset
err = retry.Retry(3, 3*time.Second, func() error {
var (
res *github.Response
err error
)
f, err := os.Open(filename)
if err != nil {
return errors.Wrap(err, "failed to open file")
}
defer f.Close()
asset, res, err = c.Repositories.UploadReleaseAsset(context.TODO(), c.Owner, c.Repo, releaseID, opts, f)
if err != nil {
return errors.Wrapf(err, "failed to upload release asset: %s", filename)
}
switch res.StatusCode {
case http.StatusCreated:
return nil
case 422:
return errors.Errorf(
"upload release asset: invalid status code: %s",
"422 (this is probably because the asset already uploaded)")
default:
return errors.Errorf(
"upload release asset: invalid status code: %s", res.Status)
}
})
return asset, err
}
// DeleteAsset deletes assets from a given release object
func (c *GitHubClient) DeleteAsset(ctx context.Context, assetID int64) error {
res, err := c.Repositories.DeleteReleaseAsset(context.TODO(), c.Owner, c.Repo, assetID)
if err != nil {
return errors.Wrap(err, "failed to delete release asset")
}
if res.StatusCode != http.StatusNoContent {
return errors.Errorf("delete release assets: invalid status code: %s", res.Status)
}
return nil
}
// ListAssets lists assets associated with a given release
func (c *GitHubClient) ListAssets(ctx context.Context, releaseID int64) ([]*github.ReleaseAsset, error) {
result := []*github.ReleaseAsset{}
page := 1
for {
assets, res, err := c.Repositories.ListReleaseAssets(context.TODO(), c.Owner, c.Repo, releaseID, &github.ListOptions{Page: page})
if err != nil {
return nil, errors.Wrap(err, "failed to list assets")
}
if res.StatusCode != http.StatusOK {
return nil, errors.Errorf("list release assets: invalid status code: %s", res.Status)
}
result = append(result, assets...)
if res.NextPage <= page {
break
}
page = res.NextPage
}
return result, nil
}