-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
executable file
·131 lines (113 loc) · 3.92 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
package main
import (
"context"
"fmt"
"log"
"os"
"strings"
"github.com/google/go-github/v25/github"
"github.com/jessevdk/go-flags"
"golang.org/x/oauth2"
)
var appVersion = "v0.0.4"
var opts struct {
Version bool `long:"version" description:"Show version"`
OrgName string `short:"o" long:"org" description:"Organization name"`
RepoNames string `short:"r" long:"repos" description:"Repository names. Can be multiple"`
BranchNames string `short:"b" long:"branches" default:"develop master" description:"Protected branch names. Can be multiple"`
OperationName string `short:"p" long:"operation" default:"add" description:"Operation name [add, remove]"`
UserNames string `short:"u" long:"users" description:"User's login to change settings. Can be multiple"`
}
func main() {
flags.Parse(&opts)
if opts.Version {
fmt.Println(appVersion)
os.Exit(0)
}
token := os.Getenv("GITHUB_TOKEN")
if token == "" {
log.Fatal("Unauthorized: No token present. Please, add GITHUB_TOKEN environment variable")
}
if len(opts.OrgName) == 0 {
log.Fatal("org must be described!")
}
if len(opts.RepoNames) == 0 {
log.Fatal("repos must be described!")
}
if len(opts.BranchNames) == 0 {
log.Fatal("branches must be described!")
}
if len(opts.OperationName) == 0 {
log.Fatal("operation must be described!")
}
if len(opts.UserNames) == 0 {
log.Fatal("users must be described!")
}
ctx := context.Background()
ts := oauth2.StaticTokenSource(&oauth2.Token{AccessToken: token})
tc := oauth2.NewClient(ctx, ts)
client := github.NewClient(tc)
_repos := strings.Split(opts.RepoNames, " ")
_branches := strings.Split(opts.BranchNames, " ")
_users := strings.Split(opts.UserNames, " ")
for _, repoName := range _repos {
fmt.Println(repoName)
branches, _, err := client.Repositories.ListBranches(ctx, opts.OrgName, repoName, &github.ListOptions{PerPage: 100})
if err == nil {
for _, branch := range branches {
if contains(_branches, *branch.Name) {
fmt.Println("\t", *branch.Name)
protection, _, err := client.Repositories.GetBranchProtection(ctx, opts.OrgName, repoName, *branch.Name)
if err == nil {
preq := &github.ProtectionRequest{
RequiredStatusChecks: protection.RequiredStatusChecks,
EnforceAdmins: protection.EnforceAdmins.Enabled,
Restrictions: &github.BranchRestrictionsRequest{
Teams: []string{},
Users: []string{},
},
}
if protection.RequiredPullRequestReviews != nil {
preq.RequiredPullRequestReviews = &github.PullRequestReviewsEnforcementRequest{
DismissStaleReviews: protection.RequiredPullRequestReviews.DismissStaleReviews,
RequireCodeOwnerReviews: protection.RequiredPullRequestReviews.RequireCodeOwnerReviews,
RequiredApprovingReviewCount: protection.RequiredPullRequestReviews.RequiredApprovingReviewCount,
}
}
for _, v := range protection.Restrictions.Users {
preq.Restrictions.Users = append(preq.Restrictions.Users, *v.Login)
}
for _, v := range protection.Restrictions.Teams {
preq.Restrictions.Teams = append(preq.Restrictions.Teams, *v.Slug)
}
if opts.OperationName == "add" {
for _, u := range _users {
preq.Restrictions.Users = append(preq.Restrictions.Users, u)
}
} else if opts.OperationName == "remove" {
fmt.Println("Operation not supported yet: remove")
return
} else {
fmt.Println("Operation not supported: ", opts.OperationName)
return
}
fmt.Println("\t\tpreq.Restrictions.Users ", preq.Restrictions.Users)
_, _, err := client.Repositories.UpdateBranchProtection(ctx, opts.OrgName, repoName, *branch.Name, preq)
if err != nil {
fmt.Println(err)
}
fmt.Println("\t\tDone!")
}
}
}
}
}
}
func contains(a []string, x string) bool {
for _, n := range a {
if x == n {
return true
}
}
return false
}