Skip to content
This repository was archived by the owner on Sep 17, 2019. It is now read-only.

Commit e4f6885

Browse files
committed
fix protected branches
Signed-off-by: Jess Frazelle <[email protected]>
1 parent bb02be9 commit e4f6885

File tree

1 file changed

+20
-14
lines changed

1 file changed

+20
-14
lines changed

main.go

+20-14
Original file line numberDiff line numberDiff line change
@@ -83,15 +83,16 @@ func main() {
8383
}
8484
}()
8585

86+
ctx := context.Background()
87+
8688
// Create the http client.
8789
ts := oauth2.StaticTokenSource(
8890
&oauth2.Token{AccessToken: token},
8991
)
90-
tc := oauth2.NewClient(oauth2.NoContext, ts)
92+
tc := oauth2.NewClient(ctx, ts)
9193

9294
// Create the github client.
9395
client := github.NewClient(tc)
94-
9596
page := 1
9697
perPage := 100
9798
var affiliation string
@@ -101,27 +102,27 @@ func main() {
101102
affiliation = "owner,collaborator,organization_member"
102103
}
103104
logrus.Debugf("Getting repositories...")
104-
if err := getRepositories(client, page, perPage, affiliation); err != nil {
105+
if err := getRepositories(ctx, client, page, perPage, affiliation); err != nil {
105106
logrus.Fatal(err)
106107
}
107108
}
108109

109-
func getRepositories(client *github.Client, page, perPage int, affiliation string) error {
110+
func getRepositories(ctx context.Context, client *github.Client, page, perPage int, affiliation string) error {
110111
opt := &github.RepositoryListOptions{
111112
Affiliation: affiliation,
112113
ListOptions: github.ListOptions{
113114
Page: page,
114115
PerPage: perPage,
115116
},
116117
}
117-
repos, resp, err := client.Repositories.List(context.Background(), "", opt)
118+
repos, resp, err := client.Repositories.List(ctx, "", opt)
118119
if err != nil {
119120
return err
120121
}
121122

122123
for _, repo := range repos {
123124
logrus.Debugf("Handling repo %s...", *repo.FullName)
124-
if err := handleRepo(client, repo); err != nil {
125+
if err := handleRepo(ctx, client, repo); err != nil {
125126
logrus.Warn(err)
126127
}
127128
}
@@ -132,46 +133,51 @@ func getRepositories(client *github.Client, page, perPage int, affiliation strin
132133
}
133134

134135
page = resp.NextPage
135-
return getRepositories(client, page, perPage, affiliation)
136+
return getRepositories(ctx, client, page, perPage, affiliation)
136137
}
137138

138139
// handleRepo will return nil error if the user does not have access to something.
139-
func handleRepo(client *github.Client, repo *github.Repository) error {
140+
func handleRepo(ctx context.Context, client *github.Client, repo *github.Repository) error {
140141
opt := &github.ListOptions{
141142
PerPage: 100,
142143
}
143-
collabs, resp, err := client.Repositories.ListCollaborators(context.Background(), *repo.Owner.Login, *repo.Name, &github.ListCollaboratorsOptions{ListOptions: *opt})
144+
collabs, resp, err := client.Repositories.ListCollaborators(ctx, *repo.Owner.Login, *repo.Name, &github.ListCollaboratorsOptions{ListOptions: *opt})
144145
if resp.StatusCode == http.StatusNotFound || resp.StatusCode == http.StatusForbidden {
145146
return nil
146147
}
147148
if err != nil {
148149
return err
149150
}
150151

151-
keys, resp, err := client.Repositories.ListKeys(context.Background(), *repo.Owner.Login, *repo.Name, opt)
152+
keys, resp, err := client.Repositories.ListKeys(ctx, *repo.Owner.Login, *repo.Name, opt)
152153
if resp.StatusCode == http.StatusNotFound || resp.StatusCode == http.StatusForbidden {
153154
return nil
154155
}
155156
if err != nil {
156157
return err
157158
}
158159

159-
hooks, resp, err := client.Repositories.ListHooks(context.Background(), *repo.Owner.Login, *repo.Name, opt)
160+
hooks, resp, err := client.Repositories.ListHooks(ctx, *repo.Owner.Login, *repo.Name, opt)
160161
if resp.StatusCode == http.StatusNotFound || resp.StatusCode == http.StatusForbidden {
161162
return nil
162163
}
163164
if err != nil {
164165
return err
165166
}
166167

167-
branches, _, err := client.Repositories.ListBranches(context.Background(), *repo.Owner.Login, *repo.Name, opt)
168+
branches, _, err := client.Repositories.ListBranches(ctx, *repo.Owner.Login, *repo.Name, opt)
168169
if err != nil {
169170
return err
170171
}
171172
protectedBranches := []string{}
172173
for _, branch := range branches {
173-
if branch.GetProtected() {
174-
protectedBranches = append(protectedBranches, *branch.Name)
174+
// we must get the individual branch for the branch protection to work
175+
b, _, err := client.Repositories.GetBranch(ctx, *repo.Owner.Login, *repo.Name, branch.GetName())
176+
if err != nil {
177+
return err
178+
}
179+
if b.GetProtected() {
180+
protectedBranches = append(protectedBranches, b.GetName())
175181
}
176182
}
177183

0 commit comments

Comments
 (0)