-
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
Changes from 4 commits
9876b7a
23e7e40
dbe84c4
234b1d7
a8407ec
1c43a5e
a5d4e5a
2ec6ac6
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,60 @@ | ||
| 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; | ||
|
|
||
| async initialize(context: vscode.ExtensionContext): Promise<void> { | ||
| this.registerListeners(context); | ||
| this.setOctokit(); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This line needs an You may find it useful to add the
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That's a good idea. We can use the |
||
| } | ||
|
|
||
| private async setOctokit() { | ||
|
shati-patel marked this conversation as resolved.
Outdated
|
||
| // 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 }); | ||
|
|
||
| if (session) { | ||
| this.octokit = new Octokit.Octokit({ | ||
| auth: session.accessToken | ||
| }); | ||
|
|
||
| return; | ||
| } | ||
|
|
||
| this.octokit = undefined; | ||
|
shati-patel marked this conversation as resolved.
Outdated
|
||
| } | ||
|
|
||
| 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(); | ||
| } | ||
| })); | ||
| } | ||
|
|
||
| async getOctokit(): Promise<Octokit.Octokit> { | ||
| if (this.octokit) { | ||
| return this.octokit; | ||
| } | ||
|
|
||
| const session = await vscode.authentication.getSession(GITHUB_AUTH_PROVIDER_ID, SCOPES, { createIfNone: true }); | ||
|
shati-patel marked this conversation as resolved.
Outdated
|
||
| this.octokit = new Octokit.Octokit({ | ||
| auth: session.accessToken | ||
| }); | ||
|
shati-patel marked this conversation as resolved.
Outdated
|
||
| return this.octokit; | ||
| } | ||
| } | ||
|
|
||
|
|
||
| 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, | ||
| } | ||
| } | ||
| ] | ||
| } | ||
| } |
Uh oh!
There was an error while loading. Please reload this page.