Skip to content

Commit 90841af

Browse files
authored
fix: when a pull request is already open, link to it (#109)
* wip * logging
1 parent ab0857b commit 90841af

File tree

7 files changed

+60
-14
lines changed

7 files changed

+60
-14
lines changed

serverless.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ plugins:
88
custom:
99
stage: ${opt:stage, self:provider.stage}
1010
appId:
11-
dev: '23544'
11+
dev: ${env:APP_ID}
1212
sandbox: '24310'
1313
prod: '23186'
1414
logLevel:

src/tasks/processIssueComment/Repository/index.js

Lines changed: 37 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
11
const {
2+
AllContributorBotError,
23
BranchNotFoundError,
34
ResourceNotFoundError,
45
} = require('../utils/errors')
56

67
class Repository {
7-
constructor({ repo, owner, github, defaultBranch }) {
8+
constructor({ repo, owner, github, defaultBranch, log }) {
89
this.github = github
910
this.repo = repo
1011
this.owner = owner
1112
this.defaultBranch = defaultBranch
1213
this.baseBranch = defaultBranch
14+
this.log = log
1315
}
1416

1517
getFullname() {
@@ -154,6 +156,24 @@ class Repository {
154156
await Promise.all(createOrUpdateFilesMultiple)
155157
}
156158

159+
async getPullRequestURL({ branchName }) {
160+
try {
161+
const results = await this.github.pulls.list({
162+
owner: this.owner,
163+
repo: this.repo,
164+
state: 'open',
165+
head: branchName,
166+
})
167+
return results.data[0].html_url
168+
} catch (error) {
169+
// Hard fail, but recoverable (not ideal for UX)
170+
this.log.error(error)
171+
throw new AllContributorBotError(
172+
`A pull request is already open for the branch \`${branchName}\`.`,
173+
)
174+
}
175+
}
176+
157177
async createPullRequest({ title, body, branchName }) {
158178
try {
159179
const result = await this.github.pulls.create({
@@ -165,11 +185,23 @@ class Repository {
165185
base: this.defaultBranch,
166186
maintainer_can_modify: true,
167187
})
168-
return result.data.html_url
188+
return {
189+
pullRequestURL: result.data.html_url,
190+
pullCreated: true,
191+
}
169192
} catch (error) {
170193
if (error.status === 422) {
171-
console.log('Pull request already open') // eslint-disable-line no-console
172-
return error.data.html_url
194+
this.log.debug(error)
195+
this.log.info(
196+
'Pull request is already open, finding pull request...',
197+
)
198+
const pullRequestURL = await this.getPullRequestURL({
199+
branchName,
200+
})
201+
return {
202+
pullRequestURL,
203+
pullCreated: false,
204+
}
173205
} else {
174206
throw error
175207
}
@@ -187,12 +219,11 @@ class Repository {
187219
branchName,
188220
})
189221

190-
const pullRequestURL = await this.createPullRequest({
222+
return await this.createPullRequest({
191223
title,
192224
body,
193225
branchName,
194226
})
195-
return pullRequestURL
196227
}
197228
}
198229

src/tasks/processIssueComment/probot-processIssueComment.js

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,10 @@ async function processAddContributor({
5050
originalSha: optionsConfig.getOriginalSha(),
5151
}
5252

53-
const pullRequestURL = await repository.createPullRequestFromFiles({
53+
const {
54+
pullRequestURL,
55+
pullCreated,
56+
} = await repository.createPullRequestFromFiles({
5457
title: `docs: add ${who} as a contributor`,
5558
body: `Adds @${who} as a contributor for ${contributions.join(
5659
', ',
@@ -59,8 +62,15 @@ async function processAddContributor({
5962
branchName,
6063
})
6164

65+
if (pullCreated) {
66+
commentReply.reply(
67+
`I've put up [a pull request](${pullRequestURL}) to add @${who}! :tada:`,
68+
)
69+
return
70+
}
71+
// Updated
6272
commentReply.reply(
63-
`I've put up [a pull request](${pullRequestURL}) to add @${who}! :tada:`,
73+
`I've updated [the pull request](${pullRequestURL}) to add @${who}! :tada:`,
6474
)
6575
}
6676

@@ -70,6 +80,7 @@ async function setupRepository({ context, branchName }) {
7080
...context.repo(),
7181
github: context.github,
7282
defaultBranch,
83+
log: context.log,
7384
})
7485

7586
try {

src/tasks/processIssueComment/utils/errors.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ class BranchNotFoundError extends AllContributorBotError {
2323

2424
class UserNotFoundError extends AllContributorBotError {
2525
constructor(username) {
26-
super(`Could not find the user ${username} on github.`)
26+
super(`Could not find the user \`${username}\` on github.`)
2727
this.name = this.constructor.name
2828
}
2929
}

test/tasks/processIssueComment/Repository/index.test.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,10 @@ describe('Repository', () => {
7878
)
7979
.reply(201, pullsCreatedata)
8080

81-
const pullRequestNumber = await repository.createPullRequestFromFiles({
81+
const {
82+
pullRequestURL,
83+
pullCreated,
84+
} = await repository.createPullRequestFromFiles({
8285
title: 'Pull request title',
8386
body: 'Pull request body',
8487
filesByPath: {
@@ -97,8 +100,9 @@ describe('Repository', () => {
97100
},
98101
branchName: 'all-contributors/add-jakebolam',
99102
})
100-
expect(pullRequestNumber).toEqual(
103+
expect(pullRequestURL).toEqual(
101104
'https://github.com/all-contributors/all-contributors-bot/pull/1347',
102105
)
106+
expect(pullCreated).toBe(true)
103107
})
104108
})

test/tasks/processIssueComment/__snapshots__/probot-app-e2e.test.js.snap

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ exports[`All Contributors app - End to end 4:Fail path, no such user 1`] = `
101101
Object {
102102
"body": "@jakebolam
103103
104-
Could not find the user jakebolam on github.",
104+
Could not find the user \`jakebolam\` on github.",
105105
}
106106
`;
107107

test/tasks/processIssueComment/utils/__snapshots__/getUserDetails.test.js.snap

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,4 @@ Object {
88
}
99
`;
1010

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

0 commit comments

Comments
 (0)