From b2bd38699b0595d0a35b501f20251f8715a9fd1c Mon Sep 17 00:00:00 2001 From: Paul Gschwendtner Date: Thu, 4 Jun 2020 18:55:52 +0200 Subject: [PATCH] fix(dev-infra): properly determine oauth scopes for git client token (#37439) We recently added a better reporting mechanism for oauth tokens in the dev-infra git util. Unfortunately the logic broke as part of addressing PR review feedback. Right now, always the empty promise from `oauthScopes` will be used as `getAuthScopes` considers it as the already-requested API value. This is not the case as the default promise is also truthy. We should just fix this by making the property nullable. PR Close #37439 --- dev-infra/pr/merge/git.ts | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/dev-infra/pr/merge/git.ts b/dev-infra/pr/merge/git.ts index e91d443f27..b89cccb953 100644 --- a/dev-infra/pr/merge/git.ts +++ b/dev-infra/pr/merge/git.ts @@ -49,7 +49,7 @@ export class GitClient { api: Octokit; /** The OAuth scopes available for the provided Github token. */ - private _oauthScopes = Promise.resolve([]); + private _oauthScopes: Promise|null = null; /** Regular expression that matches the provided Github token. */ private _tokenRegex = new RegExp(this._githubToken, 'g'); @@ -131,7 +131,7 @@ export class GitClient { */ async hasOauthScopes(...requestedScopes: string[]): Promise { const missingScopes: string[] = []; - const scopes = await this.getAuthScopes(); + const scopes = await this.getAuthScopesForToken(); requestedScopes.forEach(scope => { if (!scopes.includes(scope)) { missingScopes.push(scope); @@ -158,21 +158,20 @@ export class GitClient { /** - * Retrieves the OAuth scopes for the loaded Github token, returning the already retrived - * list of OAuth scopes if available. + * Retrieves the OAuth scopes for the loaded Github token, returning the already + * retrieved list of OAuth scopes if available. **/ - private getAuthScopes() { + private async getAuthScopesForToken() { // If the OAuth scopes have already been loaded, return the Promise containing them. - if (this._oauthScopes) { + if (this._oauthScopes !== null) { return this._oauthScopes; } // OAuth scopes are loaded via the /rate_limit endpoint to prevent // usage of a request against that rate_limit for this lookup. - this._oauthScopes = this.api.rateLimit.get().then(_response => { + return this._oauthScopes = this.api.rateLimit.get().then(_response => { const response = _response as RateLimitResponseWithOAuthScopeHeader; const scopes: string = response.headers['x-oauth-scopes'] || ''; return scopes.split(',').map(scope => scope.trim()); }); - return this._oauthScopes; } }