Skip to content

Commit

Permalink
fix: when a pull request is already open, link to it (#109)
Browse files Browse the repository at this point in the history
* wip

* logging
  • Loading branch information
jakebolam authored Jan 27, 2019
1 parent ab0857b commit 90841af
Show file tree
Hide file tree
Showing 7 changed files with 60 additions and 14 deletions.
2 changes: 1 addition & 1 deletion serverless.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ plugins:
custom:
stage: ${opt:stage, self:provider.stage}
appId:
dev: '23544'
dev: ${env:APP_ID}
sandbox: '24310'
prod: '23186'
logLevel:
Expand Down
43 changes: 37 additions & 6 deletions src/tasks/processIssueComment/Repository/index.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
const {
AllContributorBotError,
BranchNotFoundError,
ResourceNotFoundError,
} = require('../utils/errors')

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

getFullname() {
Expand Down Expand Up @@ -154,6 +156,24 @@ class Repository {
await Promise.all(createOrUpdateFilesMultiple)
}

async getPullRequestURL({ branchName }) {
try {
const results = await this.github.pulls.list({
owner: this.owner,
repo: this.repo,
state: 'open',
head: branchName,
})
return results.data[0].html_url
} catch (error) {
// Hard fail, but recoverable (not ideal for UX)
this.log.error(error)
throw new AllContributorBotError(
`A pull request is already open for the branch \`${branchName}\`.`,
)
}
}

async createPullRequest({ title, body, branchName }) {
try {
const result = await this.github.pulls.create({
Expand All @@ -165,11 +185,23 @@ class Repository {
base: this.defaultBranch,
maintainer_can_modify: true,
})
return result.data.html_url
return {
pullRequestURL: result.data.html_url,
pullCreated: true,
}
} catch (error) {
if (error.status === 422) {
console.log('Pull request already open') // eslint-disable-line no-console
return error.data.html_url
this.log.debug(error)
this.log.info(
'Pull request is already open, finding pull request...',
)
const pullRequestURL = await this.getPullRequestURL({
branchName,
})
return {
pullRequestURL,
pullCreated: false,
}
} else {
throw error
}
Expand All @@ -187,12 +219,11 @@ class Repository {
branchName,
})

const pullRequestURL = await this.createPullRequest({
return await this.createPullRequest({
title,
body,
branchName,
})
return pullRequestURL
}
}

Expand Down
15 changes: 13 additions & 2 deletions src/tasks/processIssueComment/probot-processIssueComment.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,10 @@ async function processAddContributor({
originalSha: optionsConfig.getOriginalSha(),
}

const pullRequestURL = await repository.createPullRequestFromFiles({
const {
pullRequestURL,
pullCreated,
} = await repository.createPullRequestFromFiles({
title: `docs: add ${who} as a contributor`,
body: `Adds @${who} as a contributor for ${contributions.join(
', ',
Expand All @@ -59,8 +62,15 @@ async function processAddContributor({
branchName,
})

if (pullCreated) {
commentReply.reply(
`I've put up [a pull request](${pullRequestURL}) to add @${who}! :tada:`,
)
return
}
// Updated
commentReply.reply(
`I've put up [a pull request](${pullRequestURL}) to add @${who}! :tada:`,
`I've updated [the pull request](${pullRequestURL}) to add @${who}! :tada:`,
)
}

Expand All @@ -70,6 +80,7 @@ async function setupRepository({ context, branchName }) {
...context.repo(),
github: context.github,
defaultBranch,
log: context.log,
})

try {
Expand Down
2 changes: 1 addition & 1 deletion src/tasks/processIssueComment/utils/errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class BranchNotFoundError extends AllContributorBotError {

class UserNotFoundError extends AllContributorBotError {
constructor(username) {
super(`Could not find the user ${username} on github.`)
super(`Could not find the user \`${username}\` on github.`)
this.name = this.constructor.name
}
}
Expand Down
8 changes: 6 additions & 2 deletions test/tasks/processIssueComment/Repository/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,10 @@ describe('Repository', () => {
)
.reply(201, pullsCreatedata)

const pullRequestNumber = await repository.createPullRequestFromFiles({
const {
pullRequestURL,
pullCreated,
} = await repository.createPullRequestFromFiles({
title: 'Pull request title',
body: 'Pull request body',
filesByPath: {
Expand All @@ -97,8 +100,9 @@ describe('Repository', () => {
},
branchName: 'all-contributors/add-jakebolam',
})
expect(pullRequestNumber).toEqual(
expect(pullRequestURL).toEqual(
'https://github.com/all-contributors/all-contributors-bot/pull/1347',
)
expect(pullCreated).toBe(true)
})
})
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ exports[`All Contributors app - End to end 4:Fail path, no such user 1`] = `
Object {
"body": "@jakebolam
Could not find the user jakebolam on github.",
Could not find the user \`jakebolam\` on github.",
}
`;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@ Object {
}
`;

exports[`Get User Details throws error when user does not exists 1`] = `"Could not find the user no-such-user on github."`;
exports[`Get User Details throws error when user does not exists 1`] = `"Could not find the user \`no-such-user\` on github."`;

0 comments on commit 90841af

Please sign in to comment.