/
repo_github.go
65 lines (56 loc) · 1.65 KB
/
repo_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
package cmd
import (
"github.com/google/go-github/v20/github"
"github.com/naveego/bosun/pkg/bosun"
"github.com/spf13/cobra"
)
var _ = addCommand(
repoCmd,
&cobra.Command{
Use: "configure-github [repo] [repo...]",
Short: "Pulls the repo(s).",
SilenceUsage: true,
SilenceErrors: true,
RunE: func(cmd *cobra.Command, args []string) error {
client := mustGetGithubClient()
return forEachRepo(args, func(ctx bosun.BosunContext, repo *bosun.Repo) error {
log := ctx.WithLogField("repo", repo.Name).Log()
ref, err := repo.GetRef()
if err != nil {
return err
}
g, err := repo.LocalRepo.Git()
if err != nil {
return err
}
err = g.Fetch()
if err != nil {
return err
}
log.Infof("Setting default branch to %q...", repo.Branching.Develop)
_, _, err = client.Repositories.Edit(ctx.Ctx(), ref.Org, ref.Repo, &github.Repository{
DefaultBranch: &repo.Branching.Develop,
})
if err != nil {
return err
}
for _, branch := range []string{repo.Branching.Develop, repo.Branching.Master} {
log.Infof("Adding branch protection for %q...", branch)
_, _, err = client.Repositories.UpdateBranchProtection(ctx.Ctx(), ref.Org, ref.Repo, branch, &github.ProtectionRequest{
RequiredPullRequestReviews: &github.PullRequestReviewsEnforcementRequest{
DismissStaleReviews: true,
RequiredApprovingReviewCount: 1,
},
Restrictions: &github.BranchRestrictionsRequest{
Users: []string{"chriscerk", "roehlerw"},
Teams: []string{},
},
})
if err != nil {
return err
}
}
return err
})
},
})