-
Notifications
You must be signed in to change notification settings - Fork 226
Add GitHub authentication #874
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
shati-patel
merged 8 commits into
github:qc-development
from
shati-patel:shati-patel/auth
Jun 9, 2021
Merged
Changes from 6 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
9876b7a
Add GitHub authentication
shati-patel 23e7e40
Update eslint packages
shati-patel dbe84c4
Lint fixes from updating eslint packages
shati-patel 234b1d7
Change type back to `Function`
shati-patel a8407ec
Address review comments
shati-patel 1c43a5e
Tweak "createOctokit"
shati-patel a5d4e5a
More tidy-up
shati-patel 2ec6ac6
Add command to "activationEvents"
shati-patel File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,59 @@ | ||
| import * as vscode from 'vscode'; | ||
| import * as Octokit from '@octokit/rest'; | ||
|
|
||
| const GITHUB_AUTH_PROVIDER_ID = 'github'; | ||
|
|
||
| // 'repo' scope should be enough for triggering workflows. For a comprenhensive list, see: | ||
| // https://docs.github.com/apps/building-oauth-apps/understanding-scopes-for-oauth-apps | ||
| const SCOPES = ['repo']; | ||
|
|
||
| /** | ||
| * Handles authentication to GitHub, using the VS Code [authentication API](https://code.visualstudio.com/api/references/vscode-api#authentication). | ||
| */ | ||
| export class Credentials { | ||
| private octokit: Octokit.Octokit | undefined; | ||
|
|
||
| // 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() { } | ||
|
|
||
| static async initialize(context: vscode.ExtensionContext): Promise<Credentials> { | ||
| const c = new Credentials(); | ||
| c.registerListeners(context); | ||
| await c.createOctokit(false); | ||
|
shati-patel marked this conversation as resolved.
Outdated
|
||
| return c; | ||
| } | ||
|
|
||
| private async createOctokit(createIfNone: boolean): Promise<Octokit.Octokit | undefined> { | ||
| const session = await vscode.authentication.getSession(GITHUB_AUTH_PROVIDER_ID, SCOPES, { createIfNone: createIfNone }); | ||
|
shati-patel marked this conversation as resolved.
Outdated
|
||
|
|
||
| return session | ||
| ? new Octokit.Octokit({ | ||
| auth: session.accessToken | ||
| }) : 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) { | ||
| this.octokit = await this.createOctokit(false); | ||
| } | ||
| })); | ||
| } | ||
|
|
||
| async getOctokit(): Promise<Octokit.Octokit> { | ||
| if (this.octokit) { | ||
| return this.octokit; | ||
| } | ||
|
|
||
| this.octokit = await this.createOctokit(true); | ||
| // octokit shouldn't be undefined, since we've set "createIfNone: true". | ||
| // The following block is mainly here to prevent a compiler error. | ||
|
shati-patel marked this conversation as resolved.
|
||
| if (!this.octokit) { | ||
| throw new Error('Did not initialize Octokit.'); | ||
| } | ||
|
shati-patel marked this conversation as resolved.
|
||
| return this.octokit; | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,19 @@ | ||
| module.exports = { | ||
| env: { | ||
| mocha: true | ||
| }, | ||
| rules: { | ||
| "@typescript-eslint/ban-types": [ | ||
| "error", | ||
| { | ||
| // For a full list of the default banned types, see: | ||
| // https://github.com/typescript-eslint/typescript-eslint/blob/master/packages/eslint-plugin/docs/rules/ban-types.md | ||
| extendDefaults: true, | ||
| "types": { | ||
| // Don't complain about the `Function` type in test files. (Default is `true`.) | ||
| "Function": false, | ||
| } | ||
| } | ||
| ] | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.