Skip to content

Commit

Permalink
feat: ✨ Automatically link pull-request with issue (#786)
Browse files Browse the repository at this point in the history
  • Loading branch information
robvanderleek authored Nov 30, 2023
1 parent 551efaf commit 1861e60
Show file tree
Hide file tree
Showing 5 changed files with 579 additions and 7 deletions.
32 changes: 26 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -339,23 +339,43 @@ gitReplaceChars: 'ab/'

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

## Automatically link pull request with issue

This app can automatically link a pull request to the issue for which the issue
branch (of the pull request) was created. You can enable this feature with:

```yaml
autoLinkIssue: true
```

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

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

## Automatically close issues after a pull request merge

This app can close issues automatically for you when a pull request for an issue branch is merged. You can enable this
feature with:
This app can close issues automatically for you when a pull request for an
issue branch is merged. You can enable this feature with:

```yaml
autoCloseIssue: true
```

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

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

## Default source branch

Expand Down
5 changes: 5 additions & 0 deletions src/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,10 @@ function isExperimentalBranchNameArgument (config) {
config.experimental.branchNameArgument === true)
}

function autoLinkIssue (config) {
return 'autoLinkIssue' in config && config.autoLinkIssue === true
}

function autoCloseIssue (config) {
return 'autoCloseIssue' in config && config.autoCloseIssue === true
}
Expand Down Expand Up @@ -243,6 +247,7 @@ module.exports = {
isModeImmediate: isModeImmediate,
isModeChatOps: isModeChatOps,
getChatOpsCommandArgument: getChatOpsCommandArgument,
autoLinkIssue: autoLinkIssue,
autoCloseIssue: autoCloseIssue,
isSilent: isSilent,
isChatOpsCommand: isChatOpsCommand,
Expand Down
11 changes: 10 additions & 1 deletion src/github.js
Original file line number Diff line number Diff line change
Expand Up @@ -438,6 +438,14 @@ async function updatePrTitle (app, ctx, config, pr, issueTitle, labels) {
}
}

async function updatePrBody (app, ctx, config, pr, body) {
const owner = context.getRepoOwnerLogin(ctx)
const repo = context.getRepoName(ctx)
const pullNumber = pr.number
app.log.info(`Updating body for PR #${pullNumber} in ${owner}/${repo}`)
await ctx.octokit.pulls.update({ owner: owner, repo: repo, pull_number: pullNumber, body: body })
}

module.exports = {
createIssueBranch: createIssueBranch,
addComment: addComment,
Expand All @@ -452,5 +460,6 @@ module.exports = {
getBranchName: getBranchName,
createBranch: createBranch,
createPr: createPr,
updatePrTitle: updatePrTitle
updatePrTitle: updatePrTitle,
updatePrBody: updatePrBody
}
15 changes: 15 additions & 0 deletions src/webhooks/pull-request.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,21 @@ async function handle (app, ctx) {
}
}
}
if (Config.autoLinkIssue(config)) {
const pr = ctx.payload.pull_request
const branchName = pr.head.ref
const issueNumber = github.getIssueNumberFromBranchName(branchName)
if (issueNumber) {
const body = pr.body
const linkText = `closes #${issueNumber}`
if (!body) {
await github.updatePrBody(app, ctx, config, pr, linkText)
} else if (!body.includes(`closes #${issueNumber}`)) {
const updatedBody = body.length === 0 ? linkText : `${body}\n${linkText}`
await github.updatePrBody(app, ctx, config, pr, updatedBody)
}
}
}
}

module.exports = {
Expand Down
Loading

1 comment on commit 1861e60

@vercel
Copy link

@vercel vercel bot commented on 1861e60 Nov 30, 2023

Choose a reason for hiding this comment

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

Please sign in to comment.