Skip to content

Commit 0212441

Browse files
authored
Merge pull request #140 from Giorgiosaud/feat/mv-branch-name
feat: mv branch name
2 parents 8510ebd + c4a8921 commit 0212441

File tree

2 files changed

+86
-0
lines changed

2 files changed

+86
-0
lines changed

src/commands/mv-branch/index.ts

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
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+
}

src/repositories/octokit-repository.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ type removeEnvironmentResponse=Endpoints['DELETE /repos/{owner}/{repo}/environme
2121
type getPublicKeyResponse=Endpoints['GET /repositories/{repository_id}/environments/{environment_name}/secrets/public-key']['response'];
2222
type updateSecretResponse=Endpoints['PUT /repositories/{repository_id}/environments/{environment_name}/secrets/{secret_name}']['response'];
2323
type removeSecretResponse=Endpoints['DELETE /repositories/{repository_id}/environments/{environment_name}/secrets/{secret_name}']['response'];
24+
type renameBranchResponse=Endpoints['POST /repos/{owner}/{repo}/branches/{branch}/rename']['response'];
25+
type getBranchResponse=Endpoints['GET /repos/{owner}/{repo}/branches/{branch}']['response'];
2426
export default {
2527
async setEnvironmentVariable({owner, repo, name, environment_name, value}:{owner:string, repo:string, name:string, environment_name:string, value: string}):Promise<postVariableResponse> {
2628
const octokit = await octokitClient({org: owner})
@@ -312,4 +314,30 @@ export default {
312314
return this.setGlobalVariable({owner, repo, name, value})
313315
}
314316
},
317+
async getBranch({owner, repo, branch}:{owner:string, repo:string, branch:string}):Promise<getBranchResponse> {
318+
const octokit = await octokitClient({org: owner})
319+
320+
return octokit.request('GET /repos/{owner}/{repo}/branches/{branch}', {
321+
owner,
322+
repo,
323+
branch,
324+
headers: {
325+
'X-GitHub-Api-Version': '2022-11-28',
326+
},
327+
})
328+
},
329+
async renameBranch({owner, repo, branch, new_name}:{owner:string, repo:string, branch:string, new_name:string}):Promise<renameBranchResponse> {
330+
const octokit = await octokitClient({org: owner})
331+
332+
return octokit.request('POST /repos/{owner}/{repo}/branches/{branch}/rename', {
333+
owner,
334+
repo,
335+
branch,
336+
new_name,
337+
headers: {
338+
'X-GitHub-Api-Version': '2022-11-28',
339+
},
340+
})
341+
},
342+
315343
}

0 commit comments

Comments
 (0)