Skip to content

Commit 1861e60

Browse files
feat: ✨ Automatically link pull-request with issue (#786)
1 parent 551efaf commit 1861e60

File tree

5 files changed

+579
-7
lines changed

5 files changed

+579
-7
lines changed

README.md

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -339,23 +339,43 @@ gitReplaceChars: 'ab/'
339339

340340
The above configuration replaces all occurences of the characters 'a', 'b' and '/' in the branch title.
341341

342+
## Automatically link pull request with issue
343+
344+
This app can automatically link a pull request to the issue for which the issue
345+
branch (of the pull request) was created. You can enable this feature with:
346+
347+
```yaml
348+
autoLinkIssue: true
349+
```
350+
351+
Be aware that the app needs to be able to find the issue number in the branch
352+
name, otherwise this feature will not work. This feature only works if one of
353+
the following is true for your app configuration:
354+
355+
- You use the default `branchName` setting
356+
- Your `branchName` setting is `tiny`, `short` or `full`
357+
- Your branch name starts with the issue number
358+
- Your branch name contains the string `issue-` (case insensitive) followed by
359+
the issue number, for example: `Project-A-Issue-123-Rewrite_in_Clojure`
360+
342361
## Automatically close issues after a pull request merge
343362

344-
This app can close issues automatically for you when a pull request for an issue branch is merged. You can enable this
345-
feature with:
363+
This app can close issues automatically for you when a pull request for an
364+
issue branch is merged. You can enable this feature with:
346365

347366
```yaml
348367
autoCloseIssue: true
349368
```
350369

351-
Be aware that the app needs to be able to find the issue number in the branch name, otherwise this feature will not
352-
work. This feature only works if one of the following is true for your app configuration:
370+
Be aware that the app needs to be able to find the issue number in the branch
371+
name, otherwise this feature will not work. This feature only works if one of
372+
the following is true for your app configuration:
353373

354374
- You use the default `branchName` setting
355375
- Your `branchName` setting is `tiny`, `short` or `full`
356376
- Your branch name starts with the issue number
357-
- Your branch name contains the string `issue-` (case insensitive) followed by the issue number, for
358-
example: `Project-A-Issue-123-Rewrite_in_Clojure`
377+
- Your branch name contains the string `issue-` (case insensitive) followed by
378+
the issue number, for example: `Project-A-Issue-123-Rewrite_in_Clojure`
359379

360380
## Default source branch
361381

src/config.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,10 @@ function isExperimentalBranchNameArgument (config) {
7676
config.experimental.branchNameArgument === true)
7777
}
7878

79+
function autoLinkIssue (config) {
80+
return 'autoLinkIssue' in config && config.autoLinkIssue === true
81+
}
82+
7983
function autoCloseIssue (config) {
8084
return 'autoCloseIssue' in config && config.autoCloseIssue === true
8185
}
@@ -243,6 +247,7 @@ module.exports = {
243247
isModeImmediate: isModeImmediate,
244248
isModeChatOps: isModeChatOps,
245249
getChatOpsCommandArgument: getChatOpsCommandArgument,
250+
autoLinkIssue: autoLinkIssue,
246251
autoCloseIssue: autoCloseIssue,
247252
isSilent: isSilent,
248253
isChatOpsCommand: isChatOpsCommand,

src/github.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -438,6 +438,14 @@ async function updatePrTitle (app, ctx, config, pr, issueTitle, labels) {
438438
}
439439
}
440440

441+
async function updatePrBody (app, ctx, config, pr, body) {
442+
const owner = context.getRepoOwnerLogin(ctx)
443+
const repo = context.getRepoName(ctx)
444+
const pullNumber = pr.number
445+
app.log.info(`Updating body for PR #${pullNumber} in ${owner}/${repo}`)
446+
await ctx.octokit.pulls.update({ owner: owner, repo: repo, pull_number: pullNumber, body: body })
447+
}
448+
441449
module.exports = {
442450
createIssueBranch: createIssueBranch,
443451
addComment: addComment,
@@ -452,5 +460,6 @@ module.exports = {
452460
getBranchName: getBranchName,
453461
createBranch: createBranch,
454462
createPr: createPr,
455-
updatePrTitle: updatePrTitle
463+
updatePrTitle: updatePrTitle,
464+
updatePrBody: updatePrBody
456465
}

src/webhooks/pull-request.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,21 @@ async function handle (app, ctx) {
2020
}
2121
}
2222
}
23+
if (Config.autoLinkIssue(config)) {
24+
const pr = ctx.payload.pull_request
25+
const branchName = pr.head.ref
26+
const issueNumber = github.getIssueNumberFromBranchName(branchName)
27+
if (issueNumber) {
28+
const body = pr.body
29+
const linkText = `closes #${issueNumber}`
30+
if (!body) {
31+
await github.updatePrBody(app, ctx, config, pr, linkText)
32+
} else if (!body.includes(`closes #${issueNumber}`)) {
33+
const updatedBody = body.length === 0 ? linkText : `${body}\n${linkText}`
34+
await github.updatePrBody(app, ctx, config, pr, updatedBody)
35+
}
36+
}
37+
}
2338
}
2439

2540
module.exports = {

0 commit comments

Comments
 (0)