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
This commit is contained in:
Paul Gschwendtner 2020-06-04 18:55:52 +02:00 committed by atscott
parent c025357fb8
commit b2bd38699b
1 changed files with 7 additions and 8 deletions

View File

@ -49,7 +49,7 @@ export class GitClient {
api: Octokit;
/** The OAuth scopes available for the provided Github token. */
private _oauthScopes = Promise.resolve<string[]>([]);
private _oauthScopes: Promise<string[]>|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<true|{error: string}> {
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;
}
}