Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: handle error when branch already exists #48

Merged
merged 12 commits into from
Jan 26, 2019
56 changes: 33 additions & 23 deletions src/Repository/index.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,25 @@
const { ResourceNotFoundError } = require('../utils/errors')
const {
ResourceNotFoundError,
AllContributorBotError,
} = require('../utils/errors')

class Repository {
constructor({ repo, owner, github }) {
constructor({ repo, owner, github, defaultBranch }) {
this.github = github
this.repo = repo
this.owner = owner
this.defaultBranch = defaultBranch
this.basedBranch
}

getFullname() {
return `${this.owner}/${this.repo}`
}

setBasedBranch(branchName) {
this.basedBranch = branchName
}

async getFile(filePath) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder how we can take the latest master file and overwrite the current branch :think: like a rebase

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We probably shouldn't address this here. This is probably a whole ticket in itself. #62.
I'm interested in hearing how you would solve this problem tho.

// https://octokit.github.io/rest.js/#api-Repos-getContents
let file
Expand All @@ -19,6 +28,7 @@ class Repository {
owner: this.owner,
repo: this.repo,
path: filePath,
ref: this.basedBranch,
})
} catch (error) {
if (error.status === 404) {
Expand Down Expand Up @@ -61,17 +71,17 @@ class Repository {
return multipleFilesByPath
}

async getHeadRef(defaultBranch) {
async getHeadRef(branchName) {
sinchang marked this conversation as resolved.
Show resolved Hide resolved
const result = await this.github.git.getRef({
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of checking 404 above when called.

Can we throw an error here? (BranchAlreadyExists) and catch that

owner: this.owner,
repo: this.repo,
ref: `heads/${defaultBranch}`,
ref: `heads/${branchName}`,
})
return result.data.object.sha
}

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

// https://octokit.github.io/rest.js/#api-Git-createRef
await this.github.git.createRef({
Expand Down Expand Up @@ -140,41 +150,41 @@ class Repository {
await Promise.all(createOrUpdateFilesMultiple)
}

async createPullRequest({ title, body, branchName, defaultBranch }) {
async createPullRequest({ title, body, branchName }) {
const result = await this.github.pulls.create({
owner: this.owner,
repo: this.repo,
title,
body,
head: branchName,
base: defaultBranch,
base: this.defaultBranch,
maintainer_can_modify: true,
})
return result.data.html_url
}

async createPullRequestFromFiles({
title,
body,
filesByPath,
branchName,
defaultBranch,
}) {
await this.createBranch({ branchName, defaultBranch })
async createPullRequestFromFiles({ title, body, filesByPath, branchName }) {
if (this.basedBranch === this.defaultBranch)
sinchang marked this conversation as resolved.
Show resolved Hide resolved
await this.createBranch(branchName)

await this.createOrUpdateFiles({
filesByPath,
branchName,
})

const pullRequestURL = await this.createPullRequest({
title,
body,
branchName,
defaultBranch,
})
try {
const pullRequestURL = await this.createPullRequest({
title,
body,
branchName,
})
return pullRequestURL
} catch (error) {
Copy link
Contributor

@jakebolam jakebolam Jan 19, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This catch and error thrown should be done in the createPullRequest method

if (error.status === 422)
throw new AllContributorBotError('Pull request is already open')
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we add a link?
I've updated the [pull request](INSERT_LINK_HERE) you have open.

I don't think this should error too, could we handle this as a reply case here: (https://github.com/all-contributors/all-contributors-bot/blob/master/src/processIssueComment.js#L65-L67) e.g. can throw the error, but catch it here


return pullRequestURL
throw error
}
}
}

Expand Down
27 changes: 18 additions & 9 deletions src/processIssueComment.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ async function processAddContributor({
optionsConfig,
who,
contributions,
defaultBranch,
}) {
const { name, avatar_url, profile } = await getUserDetails({
github: context.github,
Expand Down Expand Up @@ -59,7 +58,6 @@ async function processAddContributor({
)}.\n\nThis was requested by ${commentReply.replyingToWho()} [in this comment](${commentReply.replyingToWhere()})`,
filesByPath: filesByPathToUpdate,
branchName: `all-contributors/add-${safeWho}`,
defaultBranch,
})

commentReply.reply(
Expand All @@ -68,14 +66,29 @@ async function processAddContributor({
}

async function processIssueComment({ context, commentReply }) {
const commentBody = context.payload.comment.body
const { who, action, contributions } = parseComment(commentBody)
const branchName = `all-contributors/add-${who}`
const defaultBranch = context.payload.repository.default_branch
const repository = new Repository({
...context.repo(),
github: context.github,
defaultBranch,
})

try {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This block should happen after fetching config IMO

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This block should happen inside the processAddContributor block. (For future support of other actions)

await repository.getHeadRef(branchName)
repository.setBasedBranch(branchName)
} catch (error) {
if (error.status !== 404) throw error
Copy link
Contributor

@jakebolam jakebolam Jan 19, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See other comment and consider using here:
if (error instance of BranchAlreadyExists)

repository.setBasedBranch(defaultBranch)
}

const optionsConfig = new OptionsConfig({
repository,
commentReply,
})

try {
await optionsConfig.fetch()
} catch (error) {
Expand All @@ -86,18 +99,14 @@ async function processIssueComment({ context, commentReply }) {
}
}

const commentBody = context.payload.comment.body
const parsedComment = parseComment(commentBody)

if (parsedComment.action === 'add') {
if (action === 'add') {
await processAddContributor({
context,
commentReply,
repository,
optionsConfig,
who: parsedComment.who,
contributions: parsedComment.contributions,
defaultBranch: context.payload.repository.default_branch,
who,
contributions,
})
return
}
Expand Down
6 changes: 4 additions & 2 deletions test/Repository/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ describe('Repository', () => {
repo: 'all-contributors-bot',
owner: 'all-contributors',
github: mockGithub,
defaultBranch: 'master',
})

const verifyBody = body => {
Expand All @@ -36,9 +37,11 @@ describe('Repository', () => {
})

test('createPullRequest with files', async () => {
const basedBranch = 'master'
repository.setBasedBranch(basedBranch)
nock('https://api.github.com')
.get(
`/repos/all-contributors/all-contributors-bot/git/refs/heads/master`,
`/repos/all-contributors/all-contributors-bot/git/refs/heads/${basedBranch}`,
)
.reply(200, gitGetRefdata)

Expand Down Expand Up @@ -95,7 +98,6 @@ describe('Repository', () => {
},
},
branchName: 'all-contributors/add-jakebolam',
defaultBranch: 'master',
})
expect(pullRequestNumber).toEqual(
'https://github.com/all-contributors/all-contributors-bot/pull/1347',
Expand Down
38 changes: 38 additions & 0 deletions test/__snapshots__/index-e2e.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,44 @@ I've put up [a pull request](https://github.com/all-contributors/all-contributor
}
`;

exports[`All Contributors app - End to end Happy path, add correct new contributor, but branch exists 1`] = `
Object {
"branch": "all-contributors/add-jakebolam",
"content": "IyBBbGxDb250cmlidXRvcnNCb3QKQSBib3QgZm9yIGF1dG9tYXRpY2FsbHkgYWRkaW5nIGFsbC1jb250cmlidXRvcnMuIPCfpJYKClshW0J1aWxkXShodHRwczovL2ltZy5zaGllbGRzLmlvL2NpcmNsZWNpL3Byb2plY3QvZ2l0aHViL2FsbC1jb250cmlidXRvcnMvYWxsLWNvbnRyaWJ1dG9ycy1ib3QvbWFzdGVyLnN2ZyldKGh0dHBzOi8vY2lyY2xlY2kuY29tL2doL2FsbC1jb250cmlidXRvcnMvYWxsLWNvbnRyaWJ1dG9ycy1ib3QpClshW0NvdmVyYWdlXShodHRwczovL2ltZy5zaGllbGRzLmlvL2NvZGVjb3YvYy9naXRodWIvYWxsLWNvbnRyaWJ1dG9ycy9hbGwtY29udHJpYnV0b3JzLWJvdC5zdmcpXShodHRwczovL2NvZGVjb3YuaW8vZ2l0aHViL2FsbC1jb250cmlidXRvcnMvYWxsLWNvbnRyaWJ1dG9ycy1ib3QpClshW0FsbCBDb250cmlidXRvcnNdKGh0dHBzOi8vaW1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92PTQiIHdpZHRoPSIxMDBweDsiIGFsdD0iSmFrZSBCb2xhbSIvPjxiciAvPjxzdWI+PGI+SmFrZSBCb2xhbTwvYj48L3N1Yj5dKGh0dHBzOi8vamFrZWJvbGFtLmNvbSk8YnIgLz5b8J+Su10oaHR0cHM6Ly9naXRodWIuY29tL2FsbC1jb250cmlidHVvcnMvYm90L2NvbW1pdHM/YXV0aG9yPWpha2Vib2xhbSAiQ29kZSIpIFvwn5qHXSgjaW5mcmEtamFrZWJvbGFtICJJbmZyYXN0cnVjdHVyZSAoSG9zdGluZywgQnVpbGQtVG9vbHMsIGV0YykiKSB8CnwgOi0tLTogfAo8IS0tIEFMTC1DT05UUklCVVRPUlMtTElTVDpFTkQgLS0+CgpUaGlzIHByb2plY3QgZm9sbG93cyB0aGUgW2FsbC1jb250cmlidXRvcnNdKGh0dHBzOi8vZ2l0aHViLmNvbS9hbGwtY29udHJpYnV0b3JzL2FsbC1jb250cmlidXRvcnMpIHNwZWNpZmljYXRpb24uIENvbnRyaWJ1dGlvbnMgb2YgYW55IGtpbmQgd2VsY29tZQoKIyMgTElDRU5TRQoKW01JVF0oTElDRU5TRSkK",
"message": "docs: update README.md",
"sha": "bfce087f5fbed22257de1ee5056b20de63da0a13",
}
`;

exports[`All Contributors app - End to end Happy path, add correct new contributor, but branch exists 2`] = `
Object {
"branch": "all-contributors/add-jakebolam",
"content": "ewogICJwcm9qZWN0TmFtZSI6ICJib3QiLAogICJwcm9qZWN0T3duZXIiOiAiYWxsLWNvbnRyaWJ0dW9ycyIsCiAgInJlcG9UeXBlIjogImdpdGh1YiIsCiAgInJlcG9Ib3N0IjogImh0dHBzOi8vZ2l0aHViLmNvbSIsCiAgImZpbGVzIjogWwogICAgIlJFQURNRS5tZCIKICBdLAogICJpbWFnZVNpemUiOiAxMDAsCiAgImNvbW1pdCI6IGZhbHNlLAogICJjb250cmlidXRvcnMiOiBbCiAgICB7CiAgICAgICJsb2dpbiI6ICJqYWtlYm9sYW0iLAogICAgICAibmFtZSI6ICJKYWtlIEJvbGFtIiwKICAgICAgImF2YXRhcl91cmwiOiAiaHR0cHM6Ly9hdmF0YXJzMi5naXRodWJ1c2VyY29udGVudC5jb20vdS8zNTM0MjM2P3Y9NCIsCiAgICAgICJwcm9maWxlIjogImh0dHBzOi8vamFrZWJvbGFtLmNvbSIsCiAgICAgICJjb250cmlidXRpb25zIjogWwogICAgICAgICJjb2RlIiwKICAgICAgICAiaW5mcmEiCiAgICAgIF0KICAgIH0KICBdLAogICJjb250cmlidXRvcnNQZXJMaW5lIjogNwp9Cg==",
"message": "docs: update .all-contributorsrc",
"sha": "dff34f715bca51114c0336a49381456a926806d5",
}
`;

exports[`All Contributors app - End to end Happy path, add correct new contributor, but branch exists 3`] = `
Object {
"base": "master",
"body": "Adds @jakebolam as a contributor for code, doc, infra.

This was requested by jakebolam [in this comment](https://github.com/all-contributors/all-contributors-bot/pull/1#issuecomment-453012966)",
"head": "all-contributors/add-jakebolam",
"maintainer_can_modify": true,
"title": "docs: add jakebolam as a contributor",
}
`;

exports[`All Contributors app - End to end Happy path, add correct new contributor, but branch exists 4`] = `
Object {
"body": "@jakebolam

I've put up [a pull request](https://github.com/all-contributors/all-contributors-bot/pull/1347) to add @jakebolam! :tada:",
}
`;

exports[`All Contributors app - End to end Happy path, add correct new contributor, no allcontributors file (repo needs init first) 1`] = `
Object {
"ref": "refs/heads/all-contributors/add-jakebolam",
Expand Down
Loading