-
Notifications
You must be signed in to change notification settings - Fork 0
/
github.go
158 lines (128 loc) · 3.96 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
package main
import (
"context"
"errors"
"fmt"
"net/http"
"strings"
"github.com/google/go-github/github"
)
type repositoryJobError map[*github.Repository]error
func (e repositoryJobError) Error() string {
l := make([]string, len(e))
for r, err := range e {
l = append(l, fmt.Sprintf("repo [%s] err: %s", r.GetFullName(), err.Error()))
}
return strings.Join(l, "\n")
}
func runCheck(ctx context.Context, gh *github.Client, org string) error {
rs, err := getRepositories(ctx, gh, org)
if err != nil {
return err
}
errs := repositoryJobError{}
for _, r := range rs {
debug("running check on repository %s", r.GetName())
if err := checkRepository(ctx, gh, r); err != nil {
errs[r] = err
}
}
if len(errs) > 0 {
return errs
}
return nil
}
func getGithubClient(u, t string) *github.Client {
return github.NewClient((&github.BasicAuthTransport{
Username: u,
Password: t,
}).Client())
}
func getRepositories(ctx context.Context, gh *github.Client, org string) ([]*github.Repository, error) {
opts := &github.RepositoryListByOrgOptions{}
opts.PerPage = 250
rs, _, err := gh.Repositories.ListByOrg(ctx, org, opts)
if err != nil {
debug("issue fetching repositories %s", err)
} else {
debug("fetched %d repositories for %s organsiation", len(rs), org)
}
return rs, err
}
func checkRepository(ctx context.Context, gh *github.Client, r *github.Repository) error {
if !requiresProtection(r) {
debug("no check required")
return nil
}
b := r.GetDefaultBranch()
if len(b) == 0 {
return errors.New("no default branch")
}
debug("repository default branch: %s", b)
p, res, err := gh.Repositories.GetBranchProtection(ctx, r.Owner.GetLogin(), r.GetName(), b)
if err != nil {
if res.StatusCode != http.StatusNotFound {
debug("unknown error getting branch permissions: %s", err)
return err
}
debug("branch has no permissions, attempting to protect %s branch", b)
return protectBranch(ctx, gh, r, b)
}
if !isValidProtection(p) {
debug("branch permissions are not valid, just going to tinker them")
return fixBranch(ctx, gh, r, b, p)
}
debug("branch permissions perfectly fine")
return nil
}
func requiresProtection(r *github.Repository) bool {
return r.GetPrivate()
}
func isValidProtection(p *github.Protection) bool {
return p.EnforceAdmins.Enabled && p.RequiredPullRequestReviews != nil && p.RequiredPullRequestReviews.DismissStaleReviews
}
func protectBranch(ctx context.Context, gh *github.Client, r *github.Repository, b string) error {
_, _, err := gh.Repositories.UpdateBranchProtection(ctx, r.Owner.GetLogin(), r.GetName(), b, &github.ProtectionRequest{
RequiredPullRequestReviews: &github.PullRequestReviewsEnforcementRequest{
DismissStaleReviews: true,
DismissalRestrictionsRequest: &github.DismissalRestrictionsRequest{
Users: []string{},
Teams: []string{},
},
},
EnforceAdmins: true,
})
return err
}
func fixBranch(ctx context.Context, gh *github.Client, r *github.Repository, b string, p *github.Protection) error {
req := &github.ProtectionRequest{
RequiredStatusChecks: p.RequiredStatusChecks,
RequiredPullRequestReviews: &github.PullRequestReviewsEnforcementRequest{
DismissStaleReviews: true,
DismissalRestrictionsRequest: getDismissalRestrictionsRequestForExistingProtection(p),
},
EnforceAdmins: true,
}
_, _, err := gh.Repositories.UpdateBranchProtection(ctx, r.Owner.GetLogin(), r.GetName(), b, req)
return err
}
func getDismissalRestrictionsRequestForExistingProtection(p *github.Protection) *github.DismissalRestrictionsRequest {
if p.Restrictions != nil {
u := make([]string, len(p.Restrictions.Users))
t := make([]string, len(p.Restrictions.Teams))
for i, user := range p.Restrictions.Users {
u[i] = user.GetLogin()
}
for i, team := range p.Restrictions.Teams {
t[i] = team.GetName()
}
return &github.DismissalRestrictionsRequest{
Users: u,
Teams: t,
}
}
return &github.DismissalRestrictionsRequest{
Users: []string{},
Teams: []string{},
}
}