|
| 1 | +/* eslint-disable no-await-in-loop */ |
1 | 2 | import {Command} from '@oclif/core' |
2 | 3 | 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' |
5 | 5 | 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' |
6 | 9 |
|
7 | 10 | export default class SetVars extends Command { |
8 | | - static description = 'Set Variables for a list of repositories' |
| 11 | + static description = 'describe the command here' |
9 | 12 |
|
10 | 13 | static examples = [ |
11 | 14 | ` |
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 |
14 | 18 | `, |
15 | 19 | ] |
16 | 20 |
|
17 | | - static usage='set-vars -r REPOS -n NAMES -x VALUES' |
| 21 | + static usage='set-secret -r REPOS -n NAMES -x VALUES' |
18 | 22 |
|
19 | | - static strict = true |
| 23 | + static strict = false |
20 | 24 |
|
21 | 25 | static flags = secretVarsFlags |
22 | 26 |
|
23 | 27 | 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}` : ''}`)) |
41 | 39 | } |
42 | 40 | } |
43 | 41 | } |
|
0 commit comments