Skip to content

Commit 8727503

Browse files
committed
wip: working set secrets and vars
1 parent 4305a05 commit 8727503

File tree

4 files changed

+51
-30
lines changed

4 files changed

+51
-30
lines changed
Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,11 @@
11
/* eslint-disable no-await-in-loop */
22
import {Command} from '@oclif/core'
33
import {info} from '../../helpers/logger'
4-
import {validateRepoNames, validateSecrets} from '../../helpers/validations'
5-
import secretVarsFlags from '../../helpers/set-vars-helpers/secret-vars-flags'
4+
import {validateRepoNames} from '../../helpers/validations'
65
import repositoryFactory from '../../repositories/repository-factory'
7-
import encryptSecret from '../../set-secret-helpers/encrypt-secret'
8-
import {getPublicKey} from '../../set-secret-helpers/get-public-key'
96
import RmSecretFlags from '../../helpers/rm-secret-helpers/rm-secret-flags'
107

11-
export default class SetSecret extends Command {
8+
export default class RmSecret extends Command {
129
static description = 'describe the command here'
1310

1411
static examples = [
@@ -26,7 +23,7 @@ export default class SetSecret extends Command {
2623
static flags = RmSecretFlags
2724

2825
async run(): Promise<void> {
29-
const {flags: {organization, repositories, secrets, environment}} = await this.parse(SetSecret)
26+
const {flags: {organization, repositories, secrets, environment}} = await this.parse(RmSecret)
3027
validateRepoNames(repositories)
3128
const octoFactory = repositoryFactory.get('octokit')
3229
for (const repo of repositories) {

src/commands/set-vars/index.ts

Lines changed: 22 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,41 @@
1+
/* eslint-disable no-await-in-loop */
12
import {Command} from '@oclif/core'
23
import {info} from '../../helpers/logger'
3-
import {validateEqualLengths, validateRepoNames} from '../../helpers/validations'
4-
import updateVars from '../../helpers/set-vars-helpers/update-vars'
4+
import {validateRepoNames, validateSecrets} from '../../helpers/validations'
55
import secretVarsFlags from '../../helpers/set-vars-helpers/secret-vars-flags'
6+
import repositoryFactory from '../../repositories/repository-factory'
7+
import encryptSecret from '../../set-secret-helpers/encrypt-secret'
8+
import {getPublicKey} from '../../set-secret-helpers/get-public-key'
69

710
export default class SetVars extends Command {
8-
static description = 'Set Variables for a list of repositories'
11+
static description = 'describe the command here'
912

1013
static examples = [
1114
`
12-
$ github-automation set-vars -r OWNER/NAME1 OWNER/NAME2 ... OWNER/NAMEn --secret-name SECRET_NAME1 SECRET_NAME2 ... SECRET_NAMEN --secret-value SECRETVALUE1 SECRETVALUE2 ... SECRETVALUEN
13-
$ github-automation set-vars -r OWNER/NAME1 OWNER/NAME2 ... OWNER/NAMEn -n SECRET_NAME1 SECRET_NAME2 ... SECRET_NAMEN -x SECRETVALUE1 SECRETVALUE2 ... SECRETVALUEN
15+
you must have a personal github token to set the first time that uses this tool
16+
$ github-automation set-secret -r OWNER/NAME1 OWNER/NAME2 ... OWNER/NAMEn --secrets SECRET_NAME1:SECRET_VALUE_1 SECRET_NAME2:SECRET_VALUE_2 ... SECRET_NAMEN:SECRET_VALUE_N
17+
$ github-automation set-secret -r OWNER/NAME1 OWNER/NAME2 ... OWNER/NAMEn -s SECRET_NAME1:SECRET_VALUE_1 SECRET_NAME2 ... SECRET_NAMEN -x SECRETVALUE1 SECRETVALUE2:SECRET_VALUE_2 ... SECRETVALUEN:SECRET_VALUE_N
1418
`,
1519
]
1620

17-
static usage='set-vars -r REPOS -n NAMES -x VALUES'
21+
static usage='set-secret -r REPOS -n NAMES -x VALUES'
1822

19-
static strict = true
23+
static strict = false
2024

2125
static flags = secretVarsFlags
2226

2327
async run(): Promise<void> {
24-
const {flags} = await this.parse(SetVars)
25-
validateEqualLengths(flags['secret-name'], flags['secret-value'])
26-
validateRepoNames(flags.repositories)
27-
28-
// const token = await getGithubToken(flags.organization)
29-
30-
const varsToSet = []
31-
for (const repo of flags.repositories) {
32-
for (const [index, secret] of flags['secret-value'].entries()) {
33-
varsToSet.push(updateVars({value: secret, owner: flags.organization, repo, name: flags['secret-name'][index], environment_name: flags.environment}))
34-
}
35-
}
36-
37-
await Promise.all(varsToSet)
38-
for (const repo of flags.repositories) {
39-
for (const [index, secret] of flags['secret-value'].entries()) {
40-
this.log(info(`Updated var ${flags['secret-name'][index]} with value ${secret} in org: ${flags.organization} in repo: ${repo}`))
28+
const {flags: {organization, repositories, secrets, environment}} = await this.parse(SetVars)
29+
validateSecrets(secrets)
30+
validateRepoNames(repositories)
31+
const octoFactory = repositoryFactory.get('octokit')
32+
for (const repo of repositories) {
33+
this.log(info(`Updating secrets in org: ${organization} in repo: ${repo}`))
34+
for (const secret of secrets) {
35+
const [name, value] = secret.split(':')
36+
this.log(info(`Updating variables ${name} with value ${value} in org: ${organization} in repo: ${repo} ${environment ? `in environment: ${environment}` : ''}`))
37+
await octoFactory.updateVariables({owner: organization, repo, name, value, environment})
38+
this.log(info(`Updated variable ${name} with value ${value} in org: ${organization} in repo: ${repo} ${environment ? `in environment: ${environment}` : ''}`))
4139
}
4240
}
4341
}

src/repositories/octokit-repository.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -285,5 +285,31 @@ export default {
285285
},
286286
})
287287
},
288+
async updateVariables({owner, repo, name, value, environment}:{owner:string, repo:string, name:string, value:string, environment?:string}):Promise<postVariableResponse|patchVariableResponse> {
289+
if (environment) {
290+
const {data: {environments}} = await this.getEnvironments({organization: owner, repository: repo})
291+
if (!environments?.find(env => env.name === environment)) {
292+
const confirm = await ux.confirm('The environment does not exist. Would you like to create it? (yes/no)')
293+
if (confirm) {
294+
await this.defineEnvironment({owner, repo, environment_name: environment})
295+
} else {
296+
throw new Error(`Environment ${environment} does not exist`)
297+
}
298+
}
288299

300+
try {
301+
await this.getEnvironmentVariable({owner, repo, name, environment_name: environment})
302+
return this.patchEnvironmentVariable({owner, repo, name, value, environment_name: environment})
303+
} catch {
304+
return this.setEnvironmentVariable({owner, repo, name, value, environment_name: environment})
305+
}
306+
}
307+
308+
try {
309+
await this.getGlobalVariable({owner, repo, name})
310+
return this.patchGlobalVariable({owner, repo, name, value})
311+
} catch {
312+
return this.setGlobalVariable({owner, repo, name, value})
313+
}
314+
},
289315
}

0 commit comments

Comments
 (0)