@@ -83,15 +83,16 @@ func main() {
83
83
}
84
84
}()
85
85
86
+ ctx := context .Background ()
87
+
86
88
// Create the http client.
87
89
ts := oauth2 .StaticTokenSource (
88
90
& oauth2.Token {AccessToken : token },
89
91
)
90
- tc := oauth2 .NewClient (oauth2 . NoContext , ts )
92
+ tc := oauth2 .NewClient (ctx , ts )
91
93
92
94
// Create the github client.
93
95
client := github .NewClient (tc )
94
-
95
96
page := 1
96
97
perPage := 100
97
98
var affiliation string
@@ -101,27 +102,27 @@ func main() {
101
102
affiliation = "owner,collaborator,organization_member"
102
103
}
103
104
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 {
105
106
logrus .Fatal (err )
106
107
}
107
108
}
108
109
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 {
110
111
opt := & github.RepositoryListOptions {
111
112
Affiliation : affiliation ,
112
113
ListOptions : github.ListOptions {
113
114
Page : page ,
114
115
PerPage : perPage ,
115
116
},
116
117
}
117
- repos , resp , err := client .Repositories .List (context . Background () , "" , opt )
118
+ repos , resp , err := client .Repositories .List (ctx , "" , opt )
118
119
if err != nil {
119
120
return err
120
121
}
121
122
122
123
for _ , repo := range repos {
123
124
logrus .Debugf ("Handling repo %s..." , * repo .FullName )
124
- if err := handleRepo (client , repo ); err != nil {
125
+ if err := handleRepo (ctx , client , repo ); err != nil {
125
126
logrus .Warn (err )
126
127
}
127
128
}
@@ -132,46 +133,51 @@ func getRepositories(client *github.Client, page, perPage int, affiliation strin
132
133
}
133
134
134
135
page = resp .NextPage
135
- return getRepositories (client , page , perPage , affiliation )
136
+ return getRepositories (ctx , client , page , perPage , affiliation )
136
137
}
137
138
138
139
// 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 {
140
141
opt := & github.ListOptions {
141
142
PerPage : 100 ,
142
143
}
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 })
144
145
if resp .StatusCode == http .StatusNotFound || resp .StatusCode == http .StatusForbidden {
145
146
return nil
146
147
}
147
148
if err != nil {
148
149
return err
149
150
}
150
151
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 )
152
153
if resp .StatusCode == http .StatusNotFound || resp .StatusCode == http .StatusForbidden {
153
154
return nil
154
155
}
155
156
if err != nil {
156
157
return err
157
158
}
158
159
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 )
160
161
if resp .StatusCode == http .StatusNotFound || resp .StatusCode == http .StatusForbidden {
161
162
return nil
162
163
}
163
164
if err != nil {
164
165
return err
165
166
}
166
167
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 )
168
169
if err != nil {
169
170
return err
170
171
}
171
172
protectedBranches := []string {}
172
173
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 ())
175
181
}
176
182
}
177
183
0 commit comments