Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 24 additions & 19 deletions extensions/ql-vscode/src/authentication.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,33 +13,38 @@ const SCOPES = ['repo'];
export class Credentials {
private octokit: Octokit.Octokit | undefined;

async initialize(context: vscode.ExtensionContext): Promise<void> {
this.registerListeners(context);
this.setOctokit();
// Explicitly make the constructor private, so that we can't accidentally call the constructor from outside the class
// without also initializing the class.
// eslint-disable-next-line @typescript-eslint/no-empty-function
private constructor() { }
Comment thread
shati-patel marked this conversation as resolved.

static async initialize(context: vscode.ExtensionContext): Promise<Credentials> {
const c = new Credentials();
c.registerListeners(context);
await c.initializeOctokit(false);
return c;
}

private async setOctokit() {
// If `createIfNone` were true, a dialog would pop up asking the user to authenticate as soon as the extension starts.
// Setting `createIfNone` to false for now, so we can have a more "quiet" prompt, i.e. a numbered label on the "accounts"
// icon in the activity bar.
const session = await vscode.authentication.getSession(GITHUB_AUTH_PROVIDER_ID, SCOPES, { createIfNone: false });
private async initializeOctokit(createIfNone: boolean) {
Comment thread
shati-patel marked this conversation as resolved.
Outdated
// If `createIfNone` is true, a dialog pops up asking the user to authenticate as soon as the extension starts.
// Initializing with `createIfNone: false` for now, so we can have a more quiet prompt, i.e. a numbered label on
// the "accounts" icon in the activity bar.
Comment thread
shati-patel marked this conversation as resolved.
Outdated
const session = await vscode.authentication.getSession(GITHUB_AUTH_PROVIDER_ID, SCOPES, { createIfNone: createIfNone });
Comment thread
shati-patel marked this conversation as resolved.
Outdated

if (session) {
this.octokit = new Octokit.Octokit({
auth: session.accessToken
});

return;
} else {
this.octokit = undefined;
}
Comment thread
shati-patel marked this conversation as resolved.
Outdated

this.octokit = undefined;
}

registerListeners(context: vscode.ExtensionContext): void {
// Sessions are changed when a user logs in or logs out.
context.subscriptions.push(vscode.authentication.onDidChangeSessions(async e => {
if (e.provider.id === GITHUB_AUTH_PROVIDER_ID) {
await this.setOctokit();
await this.initializeOctokit(false);
}
}));
}
Expand All @@ -49,12 +54,12 @@ export class Credentials {
return this.octokit;
}

const session = await vscode.authentication.getSession(GITHUB_AUTH_PROVIDER_ID, SCOPES, { createIfNone: true });
this.octokit = new Octokit.Octokit({
auth: session.accessToken
});
await this.initializeOctokit(true);
// octokit shouldn't be undefined, since we've set "createIfNone: true".
// The following block is mainly here to prevent a compiler error.
Comment thread
shati-patel marked this conversation as resolved.
if (!this.octokit) {
throw new Error('Failed to initialize Octokit.');
}
Comment thread
shati-patel marked this conversation as resolved.
return this.octokit;
}
}


3 changes: 1 addition & 2 deletions extensions/ql-vscode/src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -714,8 +714,7 @@ async function activateWithInstalledDistribution(
* Credentials for authenticating to GitHub.
* Currently unused, but will be useful in the future when making API calls.
*/
const credentials = new Credentials();
await credentials.initialize(ctx);
const credentials = await Credentials.initialize(ctx);

ctx.subscriptions.push(
commandRunner('codeQL.authenticateToGitHub', async () => {
Expand Down