3
3
* Licensed under the MIT License. See License.txt in the project root for license information.
4
4
*--------------------------------------------------------------------------------------------*/
5
5
6
- import { EventEmitter , LogOutputChannel , Memento , Uri , workspace } from 'vscode' ;
6
+ import { authentication , EventEmitter , LogOutputChannel , Memento , Uri , workspace } from 'vscode' ;
7
7
import { Repository as GitHubRepository , RepositoryRuleset } from '@octokit/graphql-schema' ;
8
- import { getOctokitGraphql } from './auth' ;
8
+ import { AuthenticationError , getOctokitGraphql } from './auth' ;
9
9
import { API , BranchProtection , BranchProtectionProvider , BranchProtectionRule , Repository } from './typings/git' ;
10
10
import { DisposableStore , getRepositoryFromUrl } from './util' ;
11
11
@@ -163,10 +163,12 @@ export class GithubBranchProtectionProvider implements BranchProtectionProvider
163
163
}
164
164
165
165
// Repository details
166
+ this . logger . trace ( `Fetching repository details for "${ repository . owner } /${ repository . repo } ".` ) ;
166
167
const repositoryDetails = await this . getRepositoryDetails ( repository . owner , repository . repo ) ;
167
168
168
169
// Check repository write permission
169
170
if ( repositoryDetails . viewerPermission !== 'ADMIN' && repositoryDetails . viewerPermission !== 'MAINTAIN' && repositoryDetails . viewerPermission !== 'WRITE' ) {
171
+ this . logger . trace ( `Skipping branch protection for "${ repository . owner } /${ repository . repo } " due to missing repository write permission.` ) ;
170
172
continue ;
171
173
}
172
174
@@ -189,8 +191,20 @@ export class GithubBranchProtectionProvider implements BranchProtectionProvider
189
191
190
192
// Save branch protection to global state
191
193
await this . globalState . update ( this . globalStateKey , branchProtection ) ;
194
+ this . logger . trace ( `Branch protection for "${ this . repository . rootUri . toString ( ) } ": ${ JSON . stringify ( branchProtection ) } .` ) ;
192
195
} catch ( err ) {
193
- // noop
196
+ 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
+ }
207
+
194
208
this . logger . warn ( `Failed to update repository branch protection: ${ err . message } ` ) ;
195
209
}
196
210
}
0 commit comments