-
Notifications
You must be signed in to change notification settings - Fork 22
/
merge.go
106 lines (89 loc) · 3.02 KB
/
merge.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
package main
import (
"context"
"errors"
"flag"
"fmt"
"net/http"
"strings"
"github.com/google/go-github/github"
)
const mergeHelp = `Update all merge settings to allow specific types only.`
func (cmd *mergeCommand) Name() string { return "merge" }
func (cmd *mergeCommand) Args() string { return "[OPTIONS]" }
func (cmd *mergeCommand) ShortHelp() string { return mergeHelp }
func (cmd *mergeCommand) LongHelp() string { return mergeHelp }
func (cmd *mergeCommand) Hidden() bool { return false }
func (cmd *mergeCommand) Register(fs *flag.FlagSet) {
fs.BoolVar(&cmd.commits, "commits", false, "Allow merge commits, add all commits from the head branch to the base branch with a merge commit")
fs.BoolVar(&cmd.squash, "squash", false, "Allow squash merging, combine all commits from the head branch into a single commit in the base branch")
fs.BoolVar(&cmd.rebase, "rebase", false, "Allow rebase merging, add all commits from the head branch onto the base branch individually")
}
type mergeCommand struct {
commits bool
squash bool
rebase bool
}
func (cmd *mergeCommand) Run(ctx context.Context, args []string) error {
return runCommand(ctx, cmd.handleRepoMergeOpt)
}
// handleRepo will return nil error if the user does not have access to something.
func (cmd *mergeCommand) handleRepoMergeOpt(ctx context.Context, client *github.Client, repo *github.Repository) error {
if !cmd.commits && !cmd.squash && !cmd.rebase {
return errors.New("you must choose from commits, squash, and/or rebase")
}
repo, resp, err := client.Repositories.Get(ctx, repo.GetOwner().GetLogin(), repo.GetName())
if resp.StatusCode == http.StatusNotFound || resp.StatusCode == http.StatusForbidden || err != nil {
if _, ok := err.(*github.RateLimitError); ok {
return err
}
return nil
}
if err != nil {
return err
}
willBeUpdated := false
if repo.GetAllowMergeCommit() != cmd.commits {
willBeUpdated = true
}
if repo.GetAllowSquashMerge() != cmd.squash {
willBeUpdated = true
}
if repo.GetAllowRebaseMerge() != cmd.rebase {
willBeUpdated = true
}
opt := []string{}
if cmd.commits {
opt = append(opt, "mergeCommits")
}
if cmd.squash {
opt = append(opt, "squash")
}
if cmd.rebase {
opt = append(opt, "rebase")
}
if dryrun && willBeUpdated {
fmt.Printf("[UPDATE] %s will be changed to %s\n", *repo.FullName, strings.Join(opt, " | "))
return nil
}
if !willBeUpdated {
fmt.Printf("[OK] %s is already set to %s\n", *repo.FullName, strings.Join(opt, " | "))
return nil
}
// Edit the repo settings.
repo.AllowRebaseMerge = &cmd.rebase
repo.AllowSquashMerge = &cmd.squash
repo.AllowMergeCommit = &cmd.commits
repo, resp, err = client.Repositories.Edit(ctx, repo.GetOwner().GetLogin(), repo.GetName(), repo)
if resp.StatusCode == http.StatusNotFound || resp.StatusCode == http.StatusForbidden || err != nil {
if _, ok := err.(*github.RateLimitError); ok {
return err
}
return nil
}
if err != nil {
return err
}
fmt.Printf("[OK] %s is set to %s\n", *repo.FullName, strings.Join(opt, " | "))
return nil
}