/
github.go
293 lines (241 loc) · 6.9 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
package upstream
import (
"context"
"errors"
"fmt"
"net/http"
"net/url"
"path/filepath"
"github.com/google/go-github/v38/github"
"golang.org/x/oauth2"
)
var (
GitHubClientID string
)
// GitHub is a wrapper struct for the GitHub Upstream
type GitHub struct {
}
type GitHubAppAuthQuery struct {
DeviceCode string `json:"device_code"`
UserCode string `json:"user_code"`
VerificationUri string `json:"verification_uri"`
ExpiresIn int `json:"expires_in"`
Interval int `json:"interval"`
}
func init() {
registerUpstream(&GitHub{}, "github.com")
}
func (g *GitHub) Auth(path string) (result Guard, err error) {
// Check if GitHubClientID has not been set.
if GitHubClientID == "" {
return nil, errors.New("required client id is nil")
}
client := github.NewClient(nil)
ctx := context.Background()
defer ctx.Done()
query := &GitHubAppAuthQuery{}
// Construct GitHub HTTP query
req, _ := http.NewRequest("POST", "https://github.com/login/device/code", nil)
req.Header.Add("Accept", "application/json")
// Add parameters to request query
params := req.URL.Query()
params.Add("client_id", GitHubClientID)
params.Add("scope", "public_repo")
// Encode query
req.URL.RawQuery = params.Encode()
// Launch request
_, err = client.Do(ctx, req, query)
if err != nil {
return nil, err
}
return &GitHubGuard{
client: client,
query: query,
},
nil
}
type GitHubGuard struct {
client *github.Client
query *GitHubAppAuthQuery
token string
}
type GitHubAppAuthPoll struct {
AccessToken string `json:"access_token"`
TokenType string `json:"token_type"`
Scope string `json:"scope"`
Error string `json:"error"`
}
func (g *GitHubGuard) GetAccessToken() string {
return g.token
}
func (g *GitHubGuard) GetCode() string {
return g.query.UserCode
}
func (g *GitHubGuard) GetExpireInterval() int {
return g.query.ExpiresIn
}
func (g *GitHubGuard) GetInterval() int {
return g.query.Interval
}
func (g *GitHubGuard) CheckStatus() (status string, err error) {
ctx := context.Background()
defer ctx.Done()
// Construct poll request.
pollReq, _ := http.NewRequest("POST", "https://github.com/login/oauth/access_token", nil)
pollReq.Header.Add("Accept", "application/json")
// Add parameters to poll request.
params := pollReq.URL.Query()
params.Add("client_id", GitHubClientID)
params.Add("device_code", g.query.DeviceCode)
params.Add("grant_type", "urn:ietf:params:oauth:grant-type:device_code")
// Construct poll response.
pollResp := &GitHubAppAuthPoll{}
// Encode request
pollReq.URL.RawQuery = params.Encode()
// Launch request
_, err = g.client.Do(ctx, pollReq, pollResp)
if err != nil {
return "", err
}
// Set token on successful auth.
if pollResp.AccessToken != "" {
g.token = pollResp.AccessToken
}
return pollResp.Error, nil
}
func (g *GitHubGuard) GetUser() (string, error) {
ctx := context.Background()
defer ctx.Done()
tokenSource := oauth2.StaticTokenSource(
&oauth2.Token{AccessToken: g.token},
)
client := github.NewClient(oauth2.NewClient(ctx, tokenSource))
// Construct user request to get the name of the current logged in user.
req, _ := http.NewRequest("GET", "https://api.github.com/user", nil)
user := &github.User{}
// Launch request
_, err := client.Do(ctx, req, user)
if err != nil {
return "", err
}
return *user.Login, nil
}
func (g *GitHub) HaveWriteAccess(token string, url url.URL) (bool, error) {
// Setup GitHub client
ctx := context.Background()
defer ctx.Done()
tokenSource := oauth2.StaticTokenSource(
&oauth2.Token{AccessToken: token},
)
client := github.NewClient(oauth2.NewClient(ctx, tokenSource))
username, err := getUsername(client)
if err != nil {
return false, err
}
// Check a user's given permission level for a repository on GitHub.
perm, resp, err := client.Repositories.GetPermissionLevel(
ctx,
filepath.Base(filepath.Dir(url.Path)),
filepath.Base(url.Path),
username,
)
if resp != nil && resp.Response.StatusCode != 200 {
return false, err
}
return *perm.Permission == "admin" || *perm.Permission == "write", nil
}
func (g *GitHub) Fork(token string, url url.URL) (string, error) {
// Setup GitHub client
ctx := context.Background()
defer ctx.Done()
tokenSource := oauth2.StaticTokenSource(
&oauth2.Token{AccessToken: token},
)
client := github.NewClient(oauth2.NewClient(ctx, tokenSource))
username, err := getUsername(client)
if err != nil {
return "", err
}
// Check for the existence of the fork before attempting to create one
remoteRepo, _, err := client.Repositories.Get(
ctx,
username,
filepath.Base(url.Path),
)
if err != nil {
remoteRepo, response, err := client.Repositories.CreateFork(
ctx,
filepath.Base(filepath.Dir(url.Path)),
filepath.Base(url.Path),
nil,
)
if remoteRepo == nil || response.StatusCode != 202 && response.StatusCode != 200 {
return "", err
}
}
return remoteRepo.GetHTMLURL(), nil
}
func getUsername(client *github.Client) (string, error) {
// Setup GitHub client
ctx := context.Background()
defer ctx.Done()
// Construct user request to get the name of the current logged in user.
req, _ := http.NewRequest("GET", "https://api.github.com/user", nil)
user := &github.User{}
// Launch request
_, err := client.Do(ctx, req, user)
if err != nil {
return "", err
}
return *user.Login, nil
}
// OpenPR opens a pull request from the input branch to the destination branch.
func (g *GitHub) OpenPR(opts PrOpts) (err error) {
ctx := context.Background()
ts := oauth2.StaticTokenSource(
&oauth2.Token{AccessToken: opts.Token},
)
tc := oauth2.NewClient(ctx, ts)
client := github.NewClient(tc)
forkOwner := filepath.Base(filepath.Dir(opts.Fork.Path))
pr := &github.NewPullRequest{
Title: github.String(opts.PrTitle),
Body: github.String(opts.PrBody),
Head: github.String(fmt.Sprintf("%s:%s", forkOwner, opts.PrBranch)),
Base: github.String(opts.MainBranch),
MaintainerCanModify: github.Bool(true),
}
repoOwner := filepath.Base(filepath.Dir(opts.Origin.Path))
repoName := filepath.Base(opts.Origin.Path)
_, _, err = client.PullRequests.Create(ctx, repoOwner, repoName, pr)
return err
}
// SearchPrByBranch checks to see if there is an existing PR based on a specific branch
// and if so returns the name.
func (g *GitHub) SearchPrByBranch(url url.URL, token, branchName string) (err error) {
ctx := context.Background()
ts := oauth2.StaticTokenSource(
&oauth2.Token{AccessToken: token},
)
tc := oauth2.NewClient(ctx, ts)
client := github.NewClient(tc)
result, _, err := client.Search.Issues(
ctx,
fmt.Sprintf(
"head:%s type:pr repo:%s/%s",
branchName,
filepath.Dir(url.Path),
filepath.Base(url.Path),
), &github.SearchOptions{})
if err != nil {
return err
}
if len(result.Issues) > 0 {
for _, issue := range result.Issues {
if issue.GetState() == "open" {
return nil
}
}
}
return errors.New("not found")
}