|
| 1 | +import * as vscode from "vscode"; |
| 2 | +import { GitHubRepoContext } from "../../git/repository"; |
| 3 | +import { encodeSecret } from "../../secrets"; |
| 4 | + |
| 5 | +interface AddSecretCommandArgs { |
| 6 | + gitHubRepoContext: GitHubRepoContext; |
| 7 | +} |
| 8 | + |
| 9 | +export function registerAddSecret(context: vscode.ExtensionContext) { |
| 10 | + context.subscriptions.push( |
| 11 | + vscode.commands.registerCommand( |
| 12 | + "github-actions.settings.secret.add", |
| 13 | + async (args: AddSecretCommandArgs) => { |
| 14 | + const gitHubContext = args.gitHubRepoContext; |
| 15 | + |
| 16 | + const name = await vscode.window.showInputBox({ |
| 17 | + prompt: "Enter name for new secret", |
| 18 | + }); |
| 19 | + |
| 20 | + if (!name) { |
| 21 | + return; |
| 22 | + } |
| 23 | + |
| 24 | + const value = await vscode.window.showInputBox({ |
| 25 | + prompt: "Enter the new secret value", |
| 26 | + }); |
| 27 | + |
| 28 | + if (value) { |
| 29 | + try { |
| 30 | + const keyResponse = |
| 31 | + await gitHubContext.client.actions.getRepoPublicKey({ |
| 32 | + owner: gitHubContext.owner, |
| 33 | + repo: gitHubContext.name, |
| 34 | + }); |
| 35 | + |
| 36 | + const key_id = keyResponse.data.key_id; |
| 37 | + const key = keyResponse.data.key; |
| 38 | + |
| 39 | + await gitHubContext.client.actions.createOrUpdateRepoSecret({ |
| 40 | + owner: gitHubContext.owner, |
| 41 | + repo: gitHubContext.name, |
| 42 | + secret_name: name, |
| 43 | + key_id: key_id, |
| 44 | + encrypted_value: encodeSecret(key, value), |
| 45 | + }); |
| 46 | + } catch (e) { |
| 47 | + vscode.window.showErrorMessage(e.message); |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + vscode.commands.executeCommand("github-actions.explorer.refresh"); |
| 52 | + } |
| 53 | + ) |
| 54 | + ); |
| 55 | +} |
0 commit comments