-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathprotection.go
70 lines (61 loc) · 1.85 KB
/
protection.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
package main
import (
"context"
"fmt"
"strings"
"github.com/google/go-github/v33/github"
)
func createProtection(org, repoName, protectionPattern string, minApprove int, dismissStalePrApprovals, codeOwner bool,
requireBranchesUptodate, includeAdmins bool,
canDismiss, canDismissTeams, canPush, canPushTeams string,
requiredStatusChecks string) {
ctx := context.Background()
client := createGithubClient(ctx)
preq := &github.ProtectionRequest{
RequiredPullRequestReviews: &github.PullRequestReviewsEnforcementRequest{
RequireCodeOwnerReviews: codeOwner,
RequiredApprovingReviewCount: minApprove,
DismissStaleReviews: dismissStalePrApprovals,
},
RequiredStatusChecks: &github.RequiredStatusChecks{
Strict: requireBranchesUptodate,
Contexts: []string{},
},
EnforceAdmins: includeAdmins,
}
dismissUsers := splitArgs(canDismiss)
dismissTeams := splitArgs(canDismissTeams)
if len(dismissUsers)+len(dismissTeams) > 0 {
preq.RequiredPullRequestReviews.DismissalRestrictionsRequest = &github.DismissalRestrictionsRequest{
Users: &dismissUsers,
Teams: &dismissTeams,
}
}
pushUsers := splitArgs(canPush)
pushTeams := splitArgs(canPushTeams)
if len(pushUsers)+len(pushTeams) > 0 {
preq.Restrictions = &github.BranchRestrictionsRequest{
Users: pushUsers,
Teams: pushTeams, // TODO: teams not working
}
}
if len(requiredStatusChecks) > 0 {
preq.RequiredStatusChecks.Contexts = splitArgs(requiredStatusChecks)
}
_, _, err := client.Repositories.UpdateBranchProtection(ctx, org, repoName, protectionPattern, preq)
if err == nil {
fmt.Println(protectionPattern)
} else {
fmt.Println(err)
}
}
func splitArgs(arg string) []string {
splitted := strings.Split(arg, ",")
result := []string{}
for _, s := range splitted {
if len(strings.TrimSpace(s)) != 0 {
result = append(result, s)
}
}
return result
}