-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprme.go
558 lines (514 loc) · 16.4 KB
/
prme.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
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
package prme
import (
"bytes"
"encoding/json"
"errors"
"flag"
"fmt"
"io"
"net/http"
"os"
"os/exec"
"strings"
"time"
)
type Client struct {
token, apiHost string
httpClient *http.Client
}
// clientOption specifies prme client options as functions.
type clientOption func(*Client) error
// WithAPIHost sets the Github API hostname for an instance of the client.
func WithAPIHost(host string) clientOption {
return func(c *Client) error {
c.apiHost = host
return nil
}
}
// WithHTTPClient sets a custom net/http.Client for an instance of the client.
func WithHTTPClient(hc *http.Client) clientOption {
return func(c *Client) error {
c.httpClient = hc
return nil
}
}
func NewClient(token string, options ...clientOption) (*Client, error) {
if token == "" {
return nil, errors.New("the Github token cannot be empty, please specify a personal access token")
}
c := &Client{
token: token,
apiHost: "https://api.github.com",
httpClient: &http.Client{Timeout: time.Second * 10},
}
for _, o := range options {
err := o(c)
if err != nil {
return nil, err
}
}
return c, nil
}
func (c *Client) MakeAPIRequest(method, URI string) (*http.Response, error) {
if !strings.HasPrefix(URI, "/") {
URI = "/" + URI
}
URL := c.apiHost + URI
req, err := http.NewRequest(method, URL, nil)
if err != nil {
return nil, err
}
req.Header.Add("Authorization", fmt.Sprintf("token %s", c.token))
resp, err := c.httpClient.Do(req)
if err != nil {
return nil, err
}
return resp, nil
}
func (c *Client) MakeAPIRequestWithData(method, URI string, body []byte) (*http.Response, error) {
if strings.HasPrefix(URI, "/") == false {
URI = "/" + URI
}
URL := c.apiHost + URI
req, err := http.NewRequest(method, URL, bytes.NewReader(body))
if err != nil {
return nil, err
}
req.Header.Add("Authorization", fmt.Sprintf("token %s", c.token))
resp, err := c.httpClient.Do(req)
if err != nil {
return nil, err
}
return resp, nil
}
func RunGitCommand(workingDir string, arg string, extraArgs ...string) (string, error) {
args := append([]string{arg}, extraArgs...)
cmd := exec.Command("git", args...)
cmd.Dir = workingDir
output, err := cmd.CombinedOutput()
if err != nil {
return "", fmt.Errorf("command %q returned error %w and output: %s", cmd, err, output)
}
return strings.TrimSuffix(string(output), "\n"), nil
}
type repo struct {
Client *Client
ownerAndName string
}
func (r repo) String() string {
return r.ownerAndName
}
func NewRepo(ownerAndName, token string, clientOptions ...clientOption) (*repo, error) {
if ownerAndName == "" {
return nil, errors.New("the repository cannot be empty, please specify a repository of the form OwnerName/RepositoryName")
}
if !strings.Contains(ownerAndName, "/") {
return nil, errors.New("the repository must be of the form OwnerName/RepositoryName")
}
c, err := NewClient(token, clientOptions...)
if err != nil {
return nil, fmt.Errorf("while constructing client for repository: %w", err)
}
return &repo{
Client: c,
ownerAndName: ownerAndName,
}, nil
}
func (r repo) Exists() (bool, error) {
apiURI := fmt.Sprintf("/repos/%s", r)
resp, err := r.Client.MakeAPIRequest(http.MethodGet, apiURI)
if err != nil {
return false, err
}
defer resp.Body.Close()
if resp.StatusCode == http.StatusNotFound {
return false, nil
}
if resp.StatusCode != http.StatusOK {
return false, fmt.Errorf("HTTP %d for %s while getting repository %q", resp.StatusCode, apiURI, r)
}
var repoAPIResp struct {
FullName string `json:"full_name"`
}
err = json.NewDecoder(resp.Body).Decode(&repoAPIResp)
if err != nil {
return false, err
}
if strings.ToLower(repoAPIResp.FullName) != strings.ToLower(r.String()) {
return false, fmt.Errorf("incorrect repository name %q returned while checking if repository %q exists", repoAPIResp.FullName, r)
}
return true, nil
}
func (r repo) CommitExists(ref string) (bool, error) {
apiURI := fmt.Sprintf("/repos/%s/git/commits/%s", r, ref)
resp, err := r.Client.MakeAPIRequest(http.MethodGet, apiURI)
if err != nil {
return false, err
}
defer resp.Body.Close()
if resp.StatusCode == http.StatusNotFound {
return false, nil
}
if resp.StatusCode != http.StatusOK {
return false, fmt.Errorf("HTTP %d for %s while getting commit %q in repository %q", resp.StatusCode, apiURI, ref, r)
}
var commitAPIResp struct{ Sha string }
err = json.NewDecoder(resp.Body).Decode(&commitAPIResp)
if err != nil {
return false, err
}
if commitAPIResp.Sha != ref {
return false, fmt.Errorf("incorrect commit sha %q returned while checking if commit %q exists", commitAPIResp.Sha, ref)
}
return true, nil
}
func (r repo) CreateOrphanBranches(branchNames ...string) error {
if len(branchNames) == 0 {
return errors.New("please supply at least one branch name")
}
for i, branchName := range branchNames {
if branchName == "" {
return fmt.Errorf("branchName[%d] cannot be empty", i)
}
}
repoURL := fmt.Sprintf("ssh://[email protected]/%s", r)
tempDir, err := os.MkdirTemp("", "pr-me-")
if err != nil {
return err
}
defer os.RemoveAll(tempDir)
tempDirWithRepo := tempDir + "/" + r.String()
_, err = RunGitCommand(tempDir, "clone", repoURL, r.String())
if err != nil {
return err
}
commitSha, err := RunGitCommand(tempDirWithRepo, "commit-tree", "4b825dc642cb6eb9a060e54bf8d69288fbee4904", "-m", "empty-tree commit")
if err != nil {
return err
}
if commitSha == "" {
return errors.New("empty commit sha returned after creating empty-tree commit")
}
for _, branchName := range branchNames {
_, err = RunGitCommand(tempDirWithRepo, "branch", branchName, commitSha)
if err != nil {
return err
}
}
gitPushArgs := append([]string{"origin"}, branchNames...)
_, err = RunGitCommand(tempDirWithRepo, "push", gitPushArgs...)
if err != nil {
return err
}
return nil
}
func (r repo) BranchExists(branch string) (bool, error) {
apiURI := fmt.Sprintf("/repos/%s/branches/%s", r, branch)
resp, err := r.Client.MakeAPIRequest(http.MethodGet, apiURI)
if err != nil {
return false, err
}
if resp.StatusCode == http.StatusNotFound {
return false, nil
}
if resp.StatusCode != http.StatusOK {
return false, fmt.Errorf("unexpected HTTP %d for %s while determining if branch %q exists in repository %q", resp.StatusCode, apiURI, branch, r)
}
var branchAPIResp struct{ Name string }
err = json.NewDecoder(resp.Body).Decode(&branchAPIResp)
if err != nil {
return false, err
}
defer resp.Body.Close()
if branchAPIResp.Name != branch {
return false, fmt.Errorf("incorrect name %q returned while checking if branch %q exists", branchAPIResp.Name, branch)
}
return true, nil
}
// MergeBranch merges headBranch into baseBranch in the given repository.
func (r repo) MergeBranch(baseBranch, headBranch string) error {
apiURI := fmt.Sprintf("/repos/%s/merges", r)
mergeJSON := fmt.Sprintf(`{"base":"%s","head":"%s"}`, baseBranch, headBranch)
resp, err := r.Client.MakeAPIRequestWithData(http.MethodPost, apiURI, []byte(mergeJSON))
if err != nil {
return err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusCreated {
return fmt.Errorf("HTTP %d for %s while merging branch %q into %q in repository %q", resp.StatusCode, apiURI, headBranch, baseBranch, r)
}
return nil
}
// CreatePullRequest creates a pull request using the specified properties.
// returning the PR URL.
func (r repo) CreatePullRequest(title, body, baseBranch, headBranch string) (PRURL string, err error) {
apiURI := fmt.Sprintf("/repos/%s/pulls", r)
PRJSON := fmt.Sprintf(`{"title":"%s","body":"%s","base":"%s","head":"%s"}`, title, body, baseBranch, headBranch)
resp, err := r.Client.MakeAPIRequestWithData(http.MethodPost, apiURI, []byte(PRJSON))
if err != nil {
return "", err
}
if resp.StatusCode != http.StatusCreated {
return "", fmt.Errorf("HTTP %d for %s while creating pull request in repository %q, base branch %q, and head branch %q", resp.StatusCode, apiURI, r, baseBranch, headBranch)
}
var PRAPIResp struct {
HTMLURL *string `json:"html_url"`
}
err = json.NewDecoder(resp.Body).Decode(&PRAPIResp)
if err != nil {
return "", err
}
defer resp.Body.Close()
if PRAPIResp.HTMLURL == nil {
return "", errors.New("the Github API did not return a pull request HTML URL")
}
return *PRAPIResp.HTMLURL, nil
}
type FullPullRequestCreator struct {
Token, Repo, FullRepoBranch, Title, Body, BaseBranch, HeadBranch string
}
type fullPullRequestCreatorOption func(*FullPullRequestCreator) error
func WithToken(token string) fullPullRequestCreatorOption {
return func(f *FullPullRequestCreator) error {
if token == "" {
return errors.New("token cannot be empty, please specify a Github personal access token")
}
f.Token = token
return nil
}
}
func WithFullRepoBranch(branch string) fullPullRequestCreatorOption {
return func(f *FullPullRequestCreator) error {
if branch == "" {
return errors.New("the full repo branch cannot be empty")
}
f.FullRepoBranch = branch
return nil
}
}
func WithTitle(title string) fullPullRequestCreatorOption {
return func(f *FullPullRequestCreator) error {
if title == "" {
return errors.New("the title cannot be empty")
}
f.Title = title
return nil
}
}
func WithBody(body string) fullPullRequestCreatorOption {
return func(f *FullPullRequestCreator) error {
if body == "" {
return errors.New("the body cannot be empty")
}
f.Body = body
return nil
}
}
func WithBaseBranchName(branch string) fullPullRequestCreatorOption {
return func(f *FullPullRequestCreator) error {
if branch == "" {
return errors.New("the base branch name cannot be empty")
}
f.BaseBranch = branch
return nil
}
}
func WithHeadBranchName(branch string) fullPullRequestCreatorOption {
return func(f *FullPullRequestCreator) error {
if branch == "" {
return errors.New("the head branch name cannot be empty")
}
f.HeadBranch = branch
return nil
}
}
func NewFullPullRequestCreator(repo string, options ...fullPullRequestCreatorOption) (*FullPullRequestCreator, error) {
if repo == "" {
return nil, errors.New("repo cannot be empty")
}
f := &FullPullRequestCreator{
Repo: repo,
Token: "",
Title: "Full Review",
Body: "A full review of the entire repository. When this PR is complete, be sure to manually merge its head branch into the main branch for this repository.",
BaseBranch: "prme-full-review",
HeadBranch: "prme-full-content",
FullRepoBranch: "main",
}
for _, option := range options {
err := option(f)
if err != nil {
return nil, err
}
}
return f, nil
}
func (f FullPullRequestCreator) Create() (string, error) {
if f.FullRepoBranch == "" {
return "", errors.New("the full repo branch cannot be empty")
}
if f.BaseBranch == "" {
return "", errors.New("the base branch cannot be empty")
}
if f.HeadBranch == "" {
return "", errors.New("the head branch cannot be empty")
}
if f.Title == "" {
return "", errors.New("the title cannot be empty")
}
if f.Body == "" {
return "", errors.New("the body cannot be empty")
}
r, err := NewRepo(f.Repo, f.Token)
if err != nil {
return "", err
}
ok, err := r.Exists()
if err != nil {
return "", err
}
if !ok {
return "", fmt.Errorf("repository %q does not exist or the access token does not provide access", r)
}
ok, err = r.BranchExists(f.FullRepoBranch)
if err != nil {
return "", err
}
if !ok {
return "", fmt.Errorf("full repository branch %q does not exist in repository %q", f.FullRepoBranch, r)
}
ok, err = r.BranchExists(f.BaseBranch)
if err != nil {
return "", err
}
if ok {
return "", fmt.Errorf("base branch %q already exists in repository %q", f.BaseBranch, r)
}
ok, err = r.BranchExists(f.HeadBranch)
if err != nil {
return "", err
}
if ok {
return "", fmt.Errorf("head branch %q already exists in repository %q", f.HeadBranch, r)
}
err = r.CreateOrphanBranches(f.BaseBranch, f.HeadBranch)
if err != nil {
return "", err
}
err = r.MergeBranch(f.HeadBranch, f.FullRepoBranch)
if err != nil {
return "", err
}
PRURL, err := r.CreatePullRequest(f.Title, f.Body, f.BaseBranch, f.HeadBranch)
if err != nil {
return "", err
}
return PRURL, nil
}
func flagOrEnvValue(f *flag.Flag) {
envVarName := "PRME_" + strings.ToUpper(f.Name)
envVarValue := os.Getenv(envVarName)
if envVarValue != "" && f.Value.String() == f.DefValue {
_ = f.Value.Set(envVarValue)
}
}
func NewFullPullRequestCreatorFromArgs(args []string, output, errOutput io.Writer) (*FullPullRequestCreator, error) {
fs := flag.NewFlagSet("prme", flag.ExitOnError)
fs.SetOutput(errOutput)
fs.Usage = func() {
fmt.Fprintf(errOutput, `This program creates a pull request that reviews all content of a Github repository.
The GH_TOKEN environment variable must be set to a Github personal access token. To create a token, see https://github.com/settings/tokens
Usage: %s [flags] <repository>
The <repository> should be of the form OwnerName/RepositoryName
For example:
export GH_TOKEN='ghp_.....'
%s ivanfetch/pr-me
Available command-line flags:
`,
fs.Name(), fs.Name())
fs.PrintDefaults()
fmt.Fprintf(errOutput, `
The following environment variables override defaults. Command-line flags will override everything.
<Environment Variable> <Current Value>
PRME_FBRANCH %q
PRME_TITLE %q
PRME_BODY %q
PRME_BBRANCH %q
PRME_HBRANCH %q
`,
os.Getenv("PRME_FBRANCH"), os.Getenv("PRME_TITLE"), os.Getenv("PRME_BODY"), os.Getenv("PRME_BBRANCH"), os.Getenv("PRME_HBRANCH"))
}
defaultValues, err := NewFullPullRequestCreator("dummyRepo")
if err != nil {
return nil, fmt.Errorf("while getting default values: %w", err)
}
CLIVersion := fs.Bool("version", false, "Display the version and git commit.")
CLIFullRepoBranch := fs.String("fbranch", defaultValues.FullRepoBranch, "The name of the existing branch, such as main or master, containing all repository content. This is also set via the PRME_FBRANCH environment variable.")
CLITitle := fs.String("title", defaultValues.Title, "The title of the pull request. This is also set via the PRME_TITLE environment variable.")
CLIBody := fs.String("body", defaultValues.Body, "The body; first comment of the pull request. This is also set via the PRME_TITLE environment variable.")
CLIBaseBranch := fs.String("bbranch", defaultValues.BaseBranch, "The name of the base orphan branch to create for the pull request.This is also set via the PRME_BBRANCH environment variable.")
CLIHeadBranch := fs.String("hbranch", defaultValues.HeadBranch, "The name of the head review branch to create for the pull request, where review fixes should be pushed. This is also set via the PRME_HBRANCH environment variable.")
err = fs.Parse(args)
if err != nil {
return nil, err
}
fs.VisitAll(flagOrEnvValue)
if *CLIVersion {
return nil, fmt.Errorf("%s version %s, git commit %s\n", fs.Name(), Version, GitCommit)
}
if fs.NArg() == 0 {
return nil, fmt.Errorf(
`Set the GH_TOKEN environment variable to a Github personal access token, then run this program with a repository name for which you would like a pull request that reviews all files.
For example: %s IvanFetch/myproject
Run %s -h for additional help.`,
fs.Name(), fs.Name())
}
if fs.NArg() > 1 {
return nil, fmt.Errorf("Please only specify one repository name, and make sure any command-line flags come first. RUn %s -h for additional help.", fs.Name())
}
repoName := strings.TrimPrefix(fs.Args()[0], "github.com/")
f, err := NewFullPullRequestCreator(repoName)
if err != nil {
return nil, err
}
f.Token = os.Getenv("GH_TOKEN")
if f.Token == "" {
return nil, errors.New("Please set the GH_TOKEN environment variable to a Github personal access token. Tokens can be managed at https://github.com/settings/tokens")
}
f.FullRepoBranch = *CLIFullRepoBranch
f.Title = *CLITitle
f.Body = *CLIBody
f.BaseBranch = *CLIBaseBranch
f.HeadBranch = *CLIHeadBranch
return f, nil
}
func CreateFullPullRequest(repo string, options ...fullPullRequestCreatorOption) (string, error) {
f, err := NewFullPullRequestCreator(repo, options...)
if err != nil {
return "", err
}
PRURL, err := f.Create()
if err != nil {
return "", err
}
return PRURL, nil
}
func CreateFullPullRequestFromArgs(args []string, output, errOutput io.Writer) (string, error) {
FPR, err := NewFullPullRequestCreatorFromArgs(args, output, errOutput)
if err != nil {
return "", err
}
PRURL, err := FPR.Create()
if err != nil {
return "", err
}
return PRURL, nil
}
func RunCLI() {
PRURL, err := CreateFullPullRequestFromArgs(os.Args[1:], os.Stdout, os.Stderr)
if err != nil {
fmt.Fprintf(os.Stderr, "%v\n", err)
os.Exit(1)
}
fmt.Printf("A full pull request has been created at %s\n", PRURL)
}