Skip to content

Commit

Permalink
Handle failure in create branch function gracefully
Browse files Browse the repository at this point in the history
  • Loading branch information
robvanderleek committed Oct 14, 2019
1 parent e84d506 commit d467781
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 7 deletions.
19 changes: 12 additions & 7 deletions probot.js
Expand Up @@ -11,8 +11,7 @@ module.exports = app => {
app.log('Branch already exists')
} else {
const sha = await getSourceBranchHeadSha(ctx, config, app.log)
await createBranch(ctx, owner, repo, branchName, sha)
app.log(`Branch created: ${branchName}`)
await createBranch(ctx, owner, repo, branchName, sha, app.log)
}
})
}
Expand Down Expand Up @@ -81,11 +80,16 @@ async function getBranchHeadSha (ctx, branch) {
}
}

async function createBranch (ctx, owner, repo, branchName, sha) {
const res = await ctx.github.gitdata.createRef({
'owner': owner, 'repo': repo, 'ref': `refs/heads/${branchName}`, 'sha': sha
})
return res
async function createBranch (ctx, owner, repo, branchName, sha, log) {
try {
const res = await ctx.github.gitdata.createRef({
'owner': owner, 'repo': repo, 'ref': `refs/heads/${branchName}`, 'sha': sha
})
log(`Branch created: ${branchName}`)
return res
} catch (e) {
log.warn(`Could not create branch (${e.message})`)
}
}

function getIssueBranchConfig (ctx, config) {
Expand Down Expand Up @@ -143,4 +147,5 @@ module.exports.getFullBranchNameFromIssue = getFullBranchNameFromIssue
module.exports.getBranchNameFromIssue = getBranchNameFromIssue
module.exports.getIssueBranchConfig = getIssueBranchConfig
module.exports.getIssueBranchPrefix = getIssueBranchPrefix
module.exports.createBranch = createBranch
module.exports.interpolate = interpolate
18 changes: 18 additions & 0 deletions test/probot.test.js
Expand Up @@ -304,3 +304,21 @@ test('get branch name from issue with only branch prefix configured', async () =
let config = { branchName: 'short', branches: [{ label: 'enhancement', prefix: 'feature/' }] }
expect(await myProbotApp.getBranchNameFromIssue(ctx, config)).toBe('feature/issue-12')
})

test('handle branch already exist', async () => {
const ctx = {
github: {
gitdata: {
createRef: () => {
const error = { message: 'Oops, branch already exists' }
throw error
}
}
}
}
const log = { warn: jest.fn() }

await myProbotApp.createBranch(ctx, 'robvanderleek', 'create-issue-branch', 'issue-1', '1234abcd', log)

expect(log.warn).toBeCalled()
})

0 comments on commit d467781

Please sign in to comment.