@@ -114,25 +114,32 @@ export class GithubBranchProtectionProvider implements BranchProtectionProvider
114
114
// Restore branch protection from global state
115
115
this . branchProtection = this . globalState . get < BranchProtection [ ] > ( this . globalStateKey , [ ] ) ;
116
116
117
- repository . status ( ) . then ( ( ) => this . updateRepositoryBranchProtection ( ) ) ;
117
+ repository . status ( ) . then ( ( ) => {
118
+ authentication . onDidChangeSessions ( e => {
119
+ if ( e . provider . id === 'github' ) {
120
+ this . updateRepositoryBranchProtection ( true ) ;
121
+ }
122
+ } ) ;
123
+ this . updateRepositoryBranchProtection ( ) ;
124
+ } ) ;
118
125
}
119
126
120
127
provideBranchProtection ( ) : BranchProtection [ ] {
121
128
return this . branchProtection ;
122
129
}
123
130
124
- private async getRepositoryDetails ( owner : string , repo : string ) : Promise < GitHubRepository > {
125
- const graphql = await getOctokitGraphql ( ) ;
131
+ private async getRepositoryDetails ( owner : string , repo : string , silent : boolean ) : Promise < GitHubRepository > {
132
+ const graphql = await getOctokitGraphql ( silent ) ;
126
133
const { repository } = await graphql < { repository : GitHubRepository } > ( REPOSITORY_QUERY , { owner, repo } ) ;
127
134
128
135
return repository ;
129
136
}
130
137
131
- private async getRepositoryRulesets ( owner : string , repo : string ) : Promise < RepositoryRuleset [ ] > {
138
+ private async getRepositoryRulesets ( owner : string , repo : string , silent : boolean ) : Promise < RepositoryRuleset [ ] > {
132
139
const rulesets : RepositoryRuleset [ ] = [ ] ;
133
140
134
141
let cursor : string | undefined = undefined ;
135
- const graphql = await getOctokitGraphql ( ) ;
142
+ const graphql = await getOctokitGraphql ( silent ) ;
136
143
137
144
while ( true ) {
138
145
const { repository } = await graphql < { repository : GitHubRepository } > ( REPOSITORY_RULESETS_QUERY , { owner, repo, cursor } ) ;
@@ -151,10 +158,10 @@ export class GithubBranchProtectionProvider implements BranchProtectionProvider
151
158
return rulesets ;
152
159
}
153
160
154
- private async updateRepositoryBranchProtection ( ) : Promise < void > {
155
- try {
156
- const branchProtection : BranchProtection [ ] = [ ] ;
161
+ private async updateRepositoryBranchProtection ( silent = false ) : Promise < void > {
162
+ const branchProtection : BranchProtection [ ] = [ ] ;
157
163
164
+ try {
158
165
for ( const remote of this . repository . state . remotes ) {
159
166
const repository = getRepositoryFromUrl ( remote . pushUrl ?? remote . fetchUrl ?? '' ) ;
160
167
@@ -164,7 +171,7 @@ export class GithubBranchProtectionProvider implements BranchProtectionProvider
164
171
165
172
// Repository details
166
173
this . logger . trace ( `Fetching repository details for "${ repository . owner } /${ repository . repo } ".` ) ;
167
- const repositoryDetails = await this . getRepositoryDetails ( repository . owner , repository . repo ) ;
174
+ const repositoryDetails = await this . getRepositoryDetails ( repository . owner , repository . repo , silent ) ;
168
175
169
176
// Check repository write permission
170
177
if ( repositoryDetails . viewerPermission !== 'ADMIN' && repositoryDetails . viewerPermission !== 'MAINTAIN' && repositoryDetails . viewerPermission !== 'WRITE' ) {
@@ -174,7 +181,7 @@ export class GithubBranchProtectionProvider implements BranchProtectionProvider
174
181
175
182
// Get repository rulesets
176
183
const branchProtectionRules : BranchProtectionRule [ ] = [ ] ;
177
- const repositoryRulesets = await this . getRepositoryRulesets ( repository . owner , repository . repo ) ;
184
+ const repositoryRulesets = await this . getRepositoryRulesets ( repository . owner , repository . repo , silent ) ;
178
185
179
186
for ( const ruleset of repositoryRulesets ) {
180
187
branchProtectionRules . push ( {
@@ -193,19 +200,17 @@ export class GithubBranchProtectionProvider implements BranchProtectionProvider
193
200
await this . globalState . update ( this . globalStateKey , branchProtection ) ;
194
201
this . logger . trace ( `Branch protection for "${ this . repository . rootUri . toString ( ) } ": ${ JSON . stringify ( branchProtection ) } .` ) ;
195
202
} catch ( err ) {
203
+ this . logger . warn ( `Failed to update repository branch protection: ${ err . message } ` ) ;
204
+
196
205
if ( err instanceof AuthenticationError ) {
197
- // Since there is no GitHub authentication session available we need to wait
198
- // until the user signs in with their GitHub account so that we can query the
199
- // repository rulesets
200
- const disposable = authentication . onDidChangeSessions ( e => {
201
- if ( e . provider . id === 'github' ) {
202
- disposable . dispose ( ) ;
203
- this . updateRepositoryBranchProtection ( ) ;
204
- }
205
- } ) ;
206
- }
206
+ // A GitHub authentication session could be missing if the user has not yet
207
+ // signed in with their GitHub account or they have signed out. In this case
208
+ // we have to clear the branch protection information.
209
+ this . branchProtection = branchProtection ;
210
+ this . _onDidChangeBranchProtection . fire ( this . repository . rootUri ) ;
207
211
208
- this . logger . warn ( `Failed to update repository branch protection: ${ err . message } ` ) ;
212
+ await this . globalState . update ( this . globalStateKey , undefined ) ;
213
+ }
209
214
}
210
215
}
211
216
0 commit comments