Skip to content

Commit a22134d

Browse files
add functionality to error if branch already exists
1 parent 3dcd834 commit a22134d

File tree

2 files changed

+53
-7
lines changed

2 files changed

+53
-7
lines changed

__tests__/create-branch.test.ts

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import {createBranch} from '../src/create-branch'
22
import { readFileSync } from 'fs';
3+
import { context } from '@actions/github';
34

45
describe('Create a branch based on the input', () => {
56

@@ -12,6 +13,9 @@ describe('Create a branch based on the input', () => {
1213
octokitMock = {
1314
git: {
1415
createRef: jest.fn()
16+
},
17+
repos: {
18+
getBranch: jest.fn()
1519
}
1620
}
1721
})
@@ -30,8 +34,20 @@ describe('Create a branch based on the input', () => {
3034
process.env.GITHUB_TOKEN = 'token'
3135
})
3236

37+
it('gets a branch', async () => {
38+
octokitMock.repos.getBranch.mockRejectedValue(new HttpError())
39+
process.env.GITHUB_REPOSITORY = 'peterjgrainger/test-action-changelog-reminder'
40+
await createBranch(githubMock, context, branch)
41+
expect(octokitMock.repos.getBranch).toHaveBeenCalledWith({
42+
repo: 'test-action-changelog-reminder',
43+
owner: 'peterjgrainger',
44+
branch
45+
})
46+
})
47+
3348

34-
it('Create new branch', async () => {
49+
it('Create new branch if not already there', async () => {
50+
octokitMock.repos.getBranch.mockRejectedValue(new HttpError())
3551
await createBranch(githubMock, contextMock, branch)
3652
expect(octokitMock.git.createRef).toHaveBeenCalledWith({
3753
ref: 'refs/heads/release-v1',
@@ -48,4 +64,18 @@ describe('Create a branch based on the input', () => {
4864
expect(error).toEqual(new ReferenceError('No token defined in the environment variables'))
4965
}
5066
})
67+
68+
it('fails if branch already exists', async() => {
69+
try {
70+
await createBranch(githubMock, contextMock, branch)
71+
throw Error('should error')
72+
} catch (error) {
73+
expect(error).toEqual(new ReferenceError('Branch already exists'))
74+
}
75+
})
5176
});
77+
78+
class HttpError extends Error {
79+
name = 'HttpError'
80+
status = 404
81+
}

src/create-branch.ts

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,28 @@ import { Context } from "@actions/github/lib/context";
22
import { GitHub } from "@actions/github";
33

44
export async function createBranch(github: any, context: Context, branch: string) {
5-
const toolkit : GitHub = new github(githubToken())
6-
await toolkit.git.createRef({
7-
ref: `refs/heads/${branch}`,
8-
sha: context.sha,
9-
...context.repo
10-
})
5+
const toolkit : GitHub = new github(githubToken());
6+
let branchExists;
7+
// throws HttpError if branch already exists.
8+
try {
9+
await toolkit.repos.getBranch({
10+
...context.repo,
11+
branch
12+
})
13+
14+
branchExists = true;
15+
} catch(error) {
16+
if(error.name === 'HttpError' && error.status === 404) {
17+
await toolkit.git.createRef({
18+
ref: `refs/heads/${branch}`,
19+
sha: context.sha,
20+
...context.repo
21+
})
22+
} else {
23+
throw Error(error)
24+
}
25+
}
26+
if (branchExists) throw ReferenceError('Branch already exists')
1127
}
1228

1329
function githubToken(): string {

0 commit comments

Comments
 (0)