|
| 1 | +/* eslint-disable no-await-in-loop */ |
| 2 | +import {Command, Flags} from '@oclif/core' |
| 3 | +import {validateRepoNames} from '../../helpers/validations' |
| 4 | +import repositoryFactory from '../../repositories/repository-factory' |
| 5 | +import {info} from 'node:console' |
| 6 | +export default class MvBranch extends Command { |
| 7 | + static description = 'Remove environments if exist' |
| 8 | + |
| 9 | + static examples = [ |
| 10 | + ` |
| 11 | + you must have a personal github token to set the first time that uses this tool |
| 12 | + $ github-automation rm-env --organization OWNER --repositories OWNER/NAME1 OWNER/NAME2 ... OWNER/NAMEn --environments ENVIRONMENTA ENVIRONMENTB |
| 13 | + $ github-automation rm-env -o Owner -r OWNER/NAME1 OWNER/NAME2 ... OWNER/NAMEn --environments ENVIRONMENTA ENVIRONMENTB |
| 14 | + `, |
| 15 | + ] |
| 16 | + |
| 17 | + static usage='create-environment -r REPOS -n NAMES -x VALUES' |
| 18 | + |
| 19 | + static strict = false |
| 20 | + |
| 21 | + static flags = { |
| 22 | + organization: Flags.string({ |
| 23 | + char: 'o', |
| 24 | + description: 'A single string containing the organization name', |
| 25 | + required: true, |
| 26 | + }), |
| 27 | + repositories: Flags.string({ |
| 28 | + char: 'r', |
| 29 | + description: 'Can be multiples repositories names', |
| 30 | + required: true, |
| 31 | + multiple: true, |
| 32 | + }), |
| 33 | + branchNaming: Flags.string({ |
| 34 | + char: 'b', |
| 35 | + description: 'branchfrom:branchto', |
| 36 | + required: true, |
| 37 | + }), |
| 38 | + |
| 39 | + help: Flags.help({char: 'h'}), |
| 40 | + } |
| 41 | + |
| 42 | + async run(): Promise<void> { |
| 43 | + const {flags: {organization, repositories, branchNaming}} = await this.parse(MvBranch) |
| 44 | + validateRepoNames(repositories) |
| 45 | + const octoFactory = repositoryFactory.get('octokit') |
| 46 | + for (const repo of repositories) { |
| 47 | + const [branch, new_name] = branchNaming.split(':') |
| 48 | + try { |
| 49 | + console.log(info(`checking if ${branch} exist in ${repo} inside ${organization}`)) |
| 50 | + await octoFactory.getBranch({owner: organization, repo, branch}) |
| 51 | + console.log(info(`Renaming branch ${branch} to ${new_name} in ${repo} inside ${organization}`)) |
| 52 | + await octoFactory.renameBranch({owner: organization, repo, branch, new_name}) |
| 53 | + } catch { |
| 54 | + throw new Error(`Branch ${branch} does not exist in ${repo} inside ${organization}`) |
| 55 | + } |
| 56 | + } |
| 57 | + } |
| 58 | +} |
0 commit comments