|
1 | | -import * as keytarType from "keytar"; |
2 | | -import { env } from "vscode"; |
| 1 | +import * as vscode from "vscode"; |
3 | 2 |
|
4 | | -declare const __webpack_require__: typeof require; |
5 | | -declare const __non_webpack_require__: typeof require; |
6 | | -function getNodeModule<T>(moduleName: string): T | undefined { |
7 | | - const r = |
8 | | - typeof __webpack_require__ === "function" |
9 | | - ? __non_webpack_require__ |
10 | | - : require; |
11 | | - try { |
12 | | - return r(`${env.appRoot}/node_modules.asar/${moduleName}`); |
13 | | - } catch (err) { |
14 | | - // Not in ASAR. |
15 | | - } |
16 | | - try { |
17 | | - return r(`${env.appRoot}/node_modules/${moduleName}`); |
18 | | - } catch (err) { |
19 | | - // Not available. |
20 | | - } |
21 | | - return undefined; |
22 | | -} |
23 | | - |
24 | | -function getKeytar(): typeof keytarType { |
25 | | - const keytar = getNodeModule<typeof keytarType>("keytar"); |
26 | | - if (!keytar) { |
27 | | - throw new Error("Keytar module for secure token storage not found"); |
28 | | - } |
| 3 | +const AUTH_PROVIDER_ID = "github"; |
| 4 | +const SCOPES = ["repo", "workflows"]; |
29 | 5 |
|
30 | | - return keytar; |
31 | | -} |
32 | | - |
33 | | -const ServiceName = "VS Code GitHub Actions"; |
| 6 | +let token: Promise<string> | undefined; |
34 | 7 |
|
35 | 8 | export async function getPAT(): Promise<string | null> { |
36 | | - return getKeytar().getPassword(ServiceName, "pat"); |
37 | | -} |
| 9 | + if (token) { |
| 10 | + return token; |
| 11 | + } |
| 12 | + |
| 13 | + return (token = new Promise(async (resolve) => { |
| 14 | + const existingSessions = await vscode.authentication.getSessions( |
| 15 | + AUTH_PROVIDER_ID, |
| 16 | + SCOPES |
| 17 | + ); |
38 | 18 |
|
39 | | -export async function setPAT(token: string): Promise<void> { |
40 | | - return getKeytar().setPassword(ServiceName, "pat", token); |
| 19 | + if (existingSessions.length) { |
| 20 | + resolve(await existingSessions[0].getAccessToken()); |
| 21 | + } else { |
| 22 | + const session = await vscode.authentication.login( |
| 23 | + AUTH_PROVIDER_ID, |
| 24 | + SCOPES |
| 25 | + ); |
| 26 | + resolve(await session.getAccessToken()); |
| 27 | + } |
| 28 | + })); |
41 | 29 | } |
0 commit comments