Skip to content

Commit

Permalink
fix: handle error when branch already exists
Browse files Browse the repository at this point in the history
fixes #16
  • Loading branch information
sinchang committed Jan 16, 2019
1 parent b03b402 commit 2f683cd
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 10 deletions.
28 changes: 19 additions & 9 deletions src/Repository/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
const { ResourceNotFoundError } = require('../utils/errors')
const {
ResourceNotFoundError,
ReferenceExistsError,
} = require('../utils/errors')

class Repository {
constructor({ repo, owner, github }) {
Expand Down Expand Up @@ -72,14 +75,21 @@ class Repository {

async createBranch({ branchName, defaultBranch }) {
const fromSha = await this.getHeadRef(defaultBranch)

// https://octokit.github.io/rest.js/#api-Git-createRef
await this.github.git.createRef({
owner: this.owner,
repo: this.repo,
ref: `refs/heads/${branchName}`,
sha: fromSha,
})
try {
// https://octokit.github.io/rest.js/#api-Git-createRef
await this.github.git.createRef({
owner: this.owner,
repo: this.repo,
ref: `refs/heads/${branchName}`,
sha: fromSha,
})
} catch (error) {
if (error.code === 422) {
throw new ReferenceExistsError()
} else {
throw error
}
}
}

async updateFile({ filePath, content, branchName, originalSha }) {
Expand Down
8 changes: 8 additions & 0 deletions src/utils/errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,16 @@ class UserNotFoundError extends AllContributorBotError {
}
}

class ReferenceExistsError extends AllContributorBotError {
constructor() {
super(`A branch or Pull Request is already open.`)
this.name = this.constructor.name
}
}

module.exports = {
AllContributorBotError,
ResourceNotFoundError,
UserNotFoundError,
ReferenceExistsError,
}
7 changes: 7 additions & 0 deletions test/Repository/__snapshots__/index.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,10 @@ Object {
"title": "Pull request title",
}
`;

exports[`Repository createPullRequest with files but a branch or pull request already exists 1`] = `
Object {
"ref": "refs/heads/all-contributors/add-jakebolam",
"sha": "aa218f56b14c9653891f9e74264a383fa43fefbd",
}
`;
42 changes: 41 additions & 1 deletion test/Repository/index.test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
const nock = require('nock')

const Repository = require('../../src/Repository')

const { rejectionOf } = require('../testUtils')
const { ReferenceExistsError } = require('../../src/utils/errors')
const gitGetRefdata = require('../../test/fixtures/git.getRef.json')
const gitCreateRefdata = require('../../test/fixtures/git.createRef.json')
const reposUpdateFiledata = require('../../test/fixtures/repos.updateFile.json')
Expand Down Expand Up @@ -101,4 +102,43 @@ describe('Repository', () => {
'https://github.com/all-contributors/all-contributors-bot/pull/1347',
)
})

test('createPullRequest with files but a branch or pull request already exists', async () => {
nock('https://api.github.com')
.get(
`/repos/all-contributors/all-contributors-bot/git/refs/heads/master`,
)
.reply(200, gitGetRefdata)

nock('https://api.github.com')
.post(
`/repos/all-contributors/all-contributors-bot/git/refs`,
verifyBody,
)
.reply(422)

const error = await rejectionOf(
repository.createPullRequestFromFiles({
title: 'Pull request title',
body: 'Pull request body',
filesByPath: {
'.all-contributorsrc': {
content: JSON.stringify({ test: 'test content' }),
originalSha: 'aa218f56b14c9653891f9e74264a383fa43fefbe',
},
'README.md': {
content: 'Updated list',
originalSha: 'aa218f56b14c9653891f9e74264a383fa43fefbl',
},
'/nested-folder/SOME-DOC.md': {
content: 'Updated list in nested folder',
originalSha: 'aa218f56b14de653891f9e74264a383fa43fefbd',
},
},
branchName: 'all-contributors/add-jakebolam',
defaultBranch: 'master',
}),
)
expect(error instanceof ReferenceExistsError).toBeTruthy()
})
})

0 comments on commit 2f683cd

Please sign in to comment.