Skip to content

Commit 7841c9d

Browse files
authored
Add environment variable support for automation (#8515)
1 parent ed516d3 commit 7841c9d

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

src/github/credentials.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,32 @@ export class CredentialStore extends Disposable {
130130
await this.context.globalState.update(LAST_USED_SCOPES_GITHUB_KEY, this._scopes);
131131
await this.context.globalState.update(LAST_USED_SCOPES_ENTERPRISE_KEY, this._scopesEnterprise);
132132
}
133+
134+
private async tryInitializeFromEnvironmentToken(authProviderId: AuthProvider): Promise<AuthResult | undefined> {
135+
if (isEnterprise(authProviderId)) {
136+
return undefined;
137+
}
138+
const token = process.env.GITHUB_OAUTH_TOKEN;
139+
if (!token) {
140+
return undefined;
141+
}
142+
Logger.debug('Attempting authentication using GITHUB_OAUTH_TOKEN environment variable.', CredentialStore.ID);
143+
try {
144+
const github = await this.createHub(token, authProviderId);
145+
this._githubAPI = github;
146+
this._sessionId = 'environment-token';
147+
if (!this._isInitialized) {
148+
this._isInitialized = true;
149+
this._onDidInitialize.fire();
150+
}
151+
Logger.appendLine('Successfully authenticated using GITHUB_OAUTH_TOKEN environment variable.', CredentialStore.ID);
152+
return { canceled: false };
153+
} catch (e) {
154+
Logger.error(`Failed to authenticate using GITHUB_OAUTH_TOKEN: ${e.message}`, CredentialStore.ID);
155+
return undefined;
156+
}
157+
}
158+
133159
private async initialize(authProviderId: AuthProvider, getAuthSessionOptions: vscode.AuthenticationGetSessionOptions = {}, scopes: string[] = (!isEnterprise(authProviderId) ? this._scopes : this._scopesEnterprise), requireScopes?: boolean): Promise<AuthResult> {
134160
Logger.debug(`Initializing GitHub${getGitHubSuffix(authProviderId)} authentication provider.`, 'Authentication');
135161
if (isEnterprise(authProviderId)) {
@@ -139,6 +165,11 @@ export class CredentialStore extends Disposable {
139165
}
140166
}
141167

168+
const envResult = await this.tryInitializeFromEnvironmentToken(authProviderId);
169+
if (envResult) {
170+
return envResult;
171+
}
172+
142173
if (getAuthSessionOptions.createIfNone === undefined && getAuthSessionOptions.forceNewSession === undefined) {
143174
getAuthSessionOptions.createIfNone = false;
144175
}

0 commit comments

Comments
 (0)