-
-
Notifications
You must be signed in to change notification settings - Fork 27
/
main.go
225 lines (192 loc) · 7.47 KB
/
main.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
package main
import (
"context"
"crypto/tls"
"flag"
"fmt"
"log"
"net/http"
"os"
"strings"
"github.com/google/go-github/v42/github"
)
type roundTripper struct {
accessToken string
insecure bool
}
func (rt roundTripper) RoundTrip(r *http.Request) (*http.Response, error) {
r.Header.Set("Authorization", fmt.Sprintf("token %s", rt.accessToken))
transport := http.Transport{TLSClientConfig: &tls.Config{InsecureSkipVerify: rt.insecure}}
return transport.RoundTrip(r)
}
func isValidState(state string) bool {
validStates := [4]string{"error", "failure", "pending", "success"}
for _, s := range validStates {
if state == s {
return true
}
}
return false
}
var (
action = flag.String("action", os.Getenv("GITHUB_ACTION"), "Action to perform: 'update_state' or 'update_branch_protection'")
token = flag.String("token", os.Getenv("GITHUB_TOKEN"), "Github access token")
owner = flag.String("owner", os.Getenv("GITHUB_OWNER"), "Github repository owner")
repo = flag.String("repo", os.Getenv("GITHUB_REPO"), "Github repository name")
ref = flag.String("ref", os.Getenv("GITHUB_REF"), "Commit SHA, branch name or tag")
state = flag.String("state", os.Getenv("GITHUB_STATE"), "Commit state. Possible values are 'pending', 'success', 'error' or 'failure'")
ctx = flag.String("context", os.Getenv("GITHUB_CONTEXT"), "Status label. Could be the name of a CI environment")
description = flag.String("description", os.Getenv("GITHUB_DESCRIPTION"), "Short high level summary of the status")
url = flag.String("url", os.Getenv("GITHUB_TARGET_URL"), "URL of the page representing the status")
baseURL = flag.String("baseURL", os.Getenv("GITHUB_BASE_URL"), "Base URL of github enterprise")
uploadURL = flag.String("uploadURL", os.Getenv("GITHUB_UPLOAD_URL"), "Upload URL of github enterprise")
insecure = flag.Bool("insecure", strings.ToLower(os.Getenv("GITHUB_INSECURE")) == "true", "Ignore SSL certificate check")
)
func getUserLogins(users []*github.User) []string {
res := []string{}
if users != nil && len(users) > 0 {
for _, user := range users {
if user != nil {
res = append(res, user.GetLogin())
}
}
}
return res
}
func getTeamSlugs(teams []*github.Team) []string {
res := []string{}
if teams != nil && len(teams) > 0 {
for _, team := range teams {
if team != nil {
res = append(res, team.GetSlug())
}
}
}
return res
}
func main() {
flag.Parse()
if *action == "" {
flag.PrintDefaults()
log.Fatal("-action or GITHUB_ACTION required")
}
if *action != "update_state" && *action != "update_branch_protection" {
flag.PrintDefaults()
log.Fatal("-action or GITHUB_ACTION must be 'update_state' or 'update_branch_protection'")
}
if *token == "" {
flag.PrintDefaults()
log.Fatal("-token or GITHUB_TOKEN required")
}
if *owner == "" {
flag.PrintDefaults()
log.Fatal("-owner or GITHUB_OWNER required")
}
if *repo == "" {
flag.PrintDefaults()
log.Fatal("-repo or GITHUB_REPO required")
}
http.DefaultClient.Transport = roundTripper{*token, *insecure}
var githubClient *github.Client
if *baseURL != "" || *uploadURL != "" {
if *baseURL == "" {
flag.PrintDefaults()
log.Fatal("-baseURL or GITHUB_BASE_URL required when using -uploadURL or GITHUB_UPLOAD_URL")
}
if *uploadURL == "" {
flag.PrintDefaults()
log.Fatal("-uploadURL or GITHUB_UPLOAD_URL required when using -baseURL or GITHUB_BASE_URL")
}
githubClient, _ = github.NewEnterpriseClient(*baseURL, *uploadURL, http.DefaultClient)
} else {
githubClient = github.NewClient(http.DefaultClient)
}
// Update status of a commit
if *action == "update_state" {
if *ref == "" {
flag.PrintDefaults()
log.Fatal("-ref or GITHUB_REF is required and must be a commit SHA")
}
if *state == "" {
flag.PrintDefaults()
log.Fatal("-state or GITHUB_STATE required")
}
if !isValidState(*state) {
flag.PrintDefaults()
log.Fatal("-state or GITHUB_STATE must be one of 'error', 'failure', 'pending', 'success'")
}
repoStatus := &github.RepoStatus{}
repoStatus.State = state
if *ctx != "" {
repoStatus.Context = ctx
}
if *description != "" {
repoStatus.Description = description
}
if *url != "" {
repoStatus.TargetURL = url
}
repoStatus, _, err := githubClient.Repositories.CreateStatus(context.Background(), *owner, *repo, *ref, repoStatus)
if err != nil {
log.Fatal(err)
}
fmt.Println("github-status-updater: Updated status", *repoStatus.ID)
} else if *action == "update_branch_protection" {
if *ref == "" {
flag.PrintDefaults()
log.Fatal("-ref or GITHUB_REF is required and must be a branch name")
}
if *ctx == "" {
flag.PrintDefaults()
log.Fatal("-context or GITHUB_CONTEXT required")
}
// https://godoc.org/github.com/google/go-github/github#RepositoriesService.GetBranchProtection
// Get the existing branch protection and copy all the fields (to not override them), add the provided context to the existing contexts
protection, _, err := githubClient.Repositories.GetBranchProtection(context.Background(), *owner, *repo, *ref)
if err != nil {
log.Fatal(err)
}
protectionRequest := &github.ProtectionRequest{}
var requiredStatusChecks *github.RequiredStatusChecks
var requiredPullRequestReviews *github.PullRequestReviewsEnforcement
var enforceAdmins *github.AdminEnforcement
var restrictions *github.BranchRestrictions
if protection != nil {
requiredStatusChecks = protection.GetRequiredStatusChecks()
requiredPullRequestReviews = protection.GetRequiredPullRequestReviews()
enforceAdmins = protection.GetEnforceAdmins()
restrictions = protection.GetRestrictions()
}
if requiredStatusChecks == nil {
requiredStatusChecks = &github.RequiredStatusChecks{Strict: true, Contexts: []string{}}
}
protectionRequest.RequiredStatusChecks = &github.RequiredStatusChecks{Strict: requiredStatusChecks.Strict, Contexts: append(requiredStatusChecks.Contexts, *ctx)}
if requiredPullRequestReviews != nil {
pullRequestReviewsEnforcementRequest := &github.PullRequestReviewsEnforcementRequest{}
users := getUserLogins(requiredPullRequestReviews.DismissalRestrictions.Users)
teams := getTeamSlugs(requiredPullRequestReviews.DismissalRestrictions.Teams)
dismissalRestrictionsRequest := &github.DismissalRestrictionsRequest{
Users: &users,
Teams: &teams,
}
pullRequestReviewsEnforcementRequest.DismissalRestrictionsRequest = dismissalRestrictionsRequest
pullRequestReviewsEnforcementRequest.DismissStaleReviews = requiredPullRequestReviews.DismissStaleReviews
pullRequestReviewsEnforcementRequest.RequireCodeOwnerReviews = requiredPullRequestReviews.RequireCodeOwnerReviews
pullRequestReviewsEnforcementRequest.RequiredApprovingReviewCount = requiredPullRequestReviews.RequiredApprovingReviewCount
protectionRequest.RequiredPullRequestReviews = pullRequestReviewsEnforcementRequest
}
if enforceAdmins != nil {
protectionRequest.EnforceAdmins = enforceAdmins.Enabled
}
if restrictions != nil {
branchRestrictionsRequest := &github.BranchRestrictionsRequest{Users: getUserLogins(restrictions.Users), Teams: getTeamSlugs(restrictions.Teams)}
protectionRequest.Restrictions = branchRestrictionsRequest
}
// https://godoc.org/github.com/google/go-github/github#RepositoriesService.UpdateBranchProtection
_, _, err = githubClient.Repositories.UpdateBranchProtection(context.Background(), *owner, *repo, *ref, protectionRequest)
if err != nil {
log.Fatal(err)
}
fmt.Println("github-status-updater: Updated branch protection")
}
}