forked from github/vscode-github-actions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathauth.ts
48 lines (36 loc) · 1.3 KB
/
auth.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import * as vscode from "vscode";
import {orgFeaturesEnabled, updateOrgFeaturesEnabled} from "../configuration/configuration";
import {resetGitHubContext} from "../git/repository";
const AUTH_PROVIDER_ID = "github";
const DEFAULT_SCOPES = ["repo", "workflow"];
const ORG_SCOPES = [...DEFAULT_SCOPES, "admin:org"];
export function registerListeners(context: vscode.ExtensionContext): void {
context.subscriptions.push(
vscode.authentication.onDidChangeSessions(async e => {
if (e.provider.id === AUTH_PROVIDER_ID) {
await enableOrgFeatures();
}
})
);
}
export async function getSession(): Promise<vscode.AuthenticationSession> {
const existingSession = await vscode.authentication.getSession(AUTH_PROVIDER_ID, getScopes(), {
createIfNone: true
});
if (!existingSession) {
throw new Error("Could not get token from the GitHub authentication provider. \nPlease sign-in and allow access.");
}
return existingSession;
}
export async function enableOrgFeatures() {
await updateOrgFeaturesEnabled(true);
await resetGitHubContext();
// TODO: CS: There has be a better way :)
await vscode.commands.executeCommand("github-actions.explorer.refresh");
}
function getScopes(): string[] {
if (orgFeaturesEnabled()) {
return ORG_SCOPES;
}
return DEFAULT_SCOPES;
}