11import * as vscode from "vscode" ;
2- import { GitHubRepoContext } from "../../git/repository" ;
32import { encodeSecret } from "../../secrets" ;
3+ import { EnvironmentSecretsCommandArgs } from "../../treeViews/settings/environmentSecretsNode" ;
4+ import { RepoSecretsCommandArgs } from "../../treeViews/settings/repoSecretsNode" ;
5+ import { GitHubRepoContext } from "../../git/repository" ;
46
5- interface AddSecretCommandArgs {
6- gitHubRepoContext : GitHubRepoContext ;
7- }
7+ type AddSecretCommandArgs = RepoSecretsCommandArgs | EnvironmentSecretsCommandArgs ;
88
99export function registerAddSecret ( context : vscode . ExtensionContext ) {
1010 context . subscriptions . push (
1111 vscode . commands . registerCommand ( "github-actions.settings.secret.add" , async ( args : AddSecretCommandArgs ) => {
12- const gitHubContext = args . gitHubRepoContext ;
12+ const { gitHubRepoContext } = args ;
1313
1414 const name = await vscode . window . showInputBox ( {
1515 prompt : "Enter name for new secret"
@@ -23,29 +23,56 @@ export function registerAddSecret(context: vscode.ExtensionContext) {
2323 prompt : "Enter the new secret value"
2424 } ) ;
2525
26- if ( value ) {
27- try {
28- const keyResponse = await gitHubContext . client . actions . getRepoPublicKey ( {
29- owner : gitHubContext . owner ,
30- repo : gitHubContext . name
31- } ) ;
32-
33- const key_id = keyResponse . data . key_id ;
34- const key = keyResponse . data . key ;
35-
36- await gitHubContext . client . actions . createOrUpdateRepoSecret ( {
37- owner : gitHubContext . owner ,
38- repo : gitHubContext . name ,
39- secret_name : name ,
40- key_id : key_id ,
41- encrypted_value : await encodeSecret ( key , value )
42- } ) ;
43- } catch ( e ) {
44- await vscode . window . showErrorMessage ( ( e as Error ) . message ) ;
26+ if ( ! value ) {
27+ return ;
28+ }
29+
30+ try {
31+ if ( "environment" in args ) {
32+ await createOrUpdateEnvSecret ( gitHubRepoContext , args . environment . name , name , value ) ;
33+ } else {
34+ await createOrUpdateRepoSecret ( gitHubRepoContext , name , value ) ;
4535 }
36+ } catch ( e ) {
37+ await vscode . window . showErrorMessage ( ( e as Error ) . message ) ;
4638 }
4739
4840 await vscode . commands . executeCommand ( "github-actions.explorer.refresh" ) ;
4941 } )
5042 ) ;
5143}
44+
45+ export async function createOrUpdateRepoSecret ( context : GitHubRepoContext , name : string , value : string ) {
46+ const keyResponse = await context . client . actions . getRepoPublicKey ( {
47+ owner : context . owner ,
48+ repo : context . name
49+ } ) ;
50+
51+ await context . client . actions . createOrUpdateRepoSecret ( {
52+ owner : context . owner ,
53+ repo : context . name ,
54+ secret_name : name ,
55+ key_id : keyResponse . data . key_id ,
56+ encrypted_value : await encodeSecret ( keyResponse . data . key , value )
57+ } ) ;
58+ }
59+
60+ export async function createOrUpdateEnvSecret (
61+ context : GitHubRepoContext ,
62+ environment : string ,
63+ name : string ,
64+ value : string
65+ ) {
66+ const keyResponse = await context . client . actions . getEnvironmentPublicKey ( {
67+ repository_id : context . id ,
68+ environment_name : environment
69+ } ) ;
70+
71+ await context . client . actions . createOrUpdateEnvironmentSecret ( {
72+ repository_id : context . id ,
73+ environment_name : environment ,
74+ secret_name : name ,
75+ key_id : keyResponse . data . key_id ,
76+ encrypted_value : await encodeSecret ( keyResponse . data . key , value )
77+ } ) ;
78+ }
0 commit comments