Repository navigation
Close contributor pull requests that close an issue not assigned to the author #136
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,88 +1,85 @@ | ||
| const { | ||
| BOT_MESSAGE_PULL_REQUEST, | ||
| BOT_MESSAGE_LINK_ISSUE, | ||
| BOT_MESSAGE_PULL_REQUEST_CLOSED, | ||
| LABEL_COMMUNITY_REVIEW, | ||
| RTIBBLESBOT_USERNAME, | ||
| } = require('./constants'); | ||
| const { BOT_MESSAGE_PULL_REQUEST, BOT_MESSAGE_LINK_ISSUE } = require('./constants'); | ||
| const { sendBotMessage, getLinkedIssues, escapeIssueTitleForSlackMessage } = require('./utils'); | ||
| const { linkStatus, planLink, applyLink } = require('./pull-request-link'); | ||
|
|
||
| function slackLine(context, text) { | ||
| const { repo } = context.repo; | ||
| const { html_url: url, title } = context.payload.pull_request; | ||
| return `*[${repo}] ${text}: <${url}|${escapeIssueTitleForSlackMessage(title)}>*`; | ||
| } | ||
|
|
||
| async function sendReply({ github, context, core }, unlinked) { | ||
| const { number, user } = context.payload.pull_request; | ||
| const body = unlinked | ||
| ? `${BOT_MESSAGE_PULL_REQUEST(user.login)}\n\n${BOT_MESSAGE_LINK_ISSUE}` | ||
| : BOT_MESSAGE_PULL_REQUEST(user.login); | ||
| const botMessageUrl = await sendBotMessage(number, body, { github, context, core }); | ||
| core.setOutput( | ||
| 'slack_notification', | ||
| slackLine(context, `<${botMessageUrl}|Reply sent> on pull request`), | ||
| ); | ||
| } | ||
|
|
||
| async function act({ github, context, core }, plan, link) { | ||
| const { owner, repo } = context.repo; | ||
| const { number } = context.payload.pull_request; | ||
| try { | ||
| const closingUrl = await applyLink(owner, repo, number, plan, { github, core }); | ||
| if (plan.action === 'close') { | ||
| const reason = link.status === 'unassigned' ? 'unassigned' : 'assigned'; | ||
| core.setOutput( | ||
| 'slack_notification', | ||
| slackLine( | ||
| context, | ||
| `<${closingUrl}|Closed> pull request on ${reason} issue <${link.issue.url}|#${link.issue.number}>`, | ||
| ), | ||
| ); | ||
| } | ||
|
Comment on lines
+28
to
+37
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Does this need to be in the |
||
| } catch (error) { | ||
| if (plan.action === 'close') { | ||
| core.setOutput('slack_notification', slackLine(context, 'Failed to close pull request')); | ||
| } | ||
| core.setFailed(`Failed to ${plan.action} the pull request: ${error.message}`); | ||
| } | ||
| } | ||
|
|
||
| module.exports = async ({ github, context, core }) => { | ||
| try { | ||
| const owner = context.repo.owner; | ||
| const repo = context.repo.repo; | ||
| const number = context.payload.pull_request.number; | ||
| const url = context.payload.pull_request.html_url; | ||
| const title = escapeIssueTitleForSlackMessage(context.payload.pull_request.title); | ||
| const author = context.payload.pull_request.user.login; | ||
| const { owner, repo } = context.repo; | ||
| const pullRequest = context.payload.pull_request; | ||
| const { number } = pullRequest; | ||
| const author = pullRequest.user.login; | ||
| const opening = context.payload.action !== 'edited'; | ||
| core.setOutput('slack_notification', ''); | ||
|
|
||
| const lookup = await getLinkedIssues(number, { github, context, core }); | ||
| const linkedIssues = lookup || []; | ||
| const authorAssigned = linkedIssues.some(issue => issue.assignees.includes(author)); | ||
| const assignedElsewhere = linkedIssues.find( | ||
| issue => issue.assignees.length > 0 && !issue.assignees.includes(author), | ||
| ); | ||
| if (!opening && pullRequest.state !== 'open') { | ||
| core.info(`Pull request #${number} is ${pullRequest.state}.`); | ||
| return; | ||
| } | ||
|
|
||
| if (!authorAssigned && assignedElsewhere) { | ||
| const botMessageUrl = await sendBotMessage( | ||
| number, | ||
| BOT_MESSAGE_PULL_REQUEST_CLOSED(author, assignedElsewhere.url), | ||
| { github, context, core }, | ||
| ); | ||
| try { | ||
| await github.rest.pulls.update({ | ||
| owner, | ||
| repo, | ||
| pull_number: number, | ||
| state: 'closed', | ||
| }); | ||
| core.setOutput( | ||
| 'slack_notification', | ||
| `*[${repo}] <${botMessageUrl}|Closed> pull request on assigned issue <${assignedElsewhere.url}|#${assignedElsewhere.number}>: <${url}|${title}>*`, | ||
| ); | ||
| } catch (error) { | ||
| core.setOutput( | ||
| 'slack_notification', | ||
| `*[${repo}] Failed to close pull request <${url}|${title}>, but the closing message was posted*`, | ||
| ); | ||
| core.setFailed(`Failed to close the pull request: ${error.message}`); | ||
| const plan = async link => | ||
| planLink(owner, repo, pullRequest, link, { github, core }, { opening }); | ||
|
|
||
| const lookup = await getLinkedIssues(number, { github, context, core }); | ||
| if (lookup === null) { | ||
| if (opening) { | ||
| await sendReply({ github, context, core }, false); | ||
| } | ||
| return; | ||
| } | ||
|
|
||
| const reply = | ||
| lookup !== null && lookup.length === 0 | ||
| ? `${BOT_MESSAGE_PULL_REQUEST(author)}\n\n${BOT_MESSAGE_LINK_ISSUE}` | ||
| : BOT_MESSAGE_PULL_REQUEST(author); | ||
| const botMessageUrl = await sendBotMessage(number, reply, { | ||
| github, | ||
| context, | ||
| core, | ||
| }); | ||
|
|
||
| if (botMessageUrl) { | ||
| const slackMessage = `*[${repo}] <${botMessageUrl}|Reply sent> on pull request: <${url}|${title}>*`; | ||
| core.setOutput('slack_notification', slackMessage); | ||
| } else { | ||
| core.setOutput('slack_notification', ''); | ||
| const link = linkStatus(lookup, author); | ||
| const first = await plan(link); | ||
| if (opening && first.action !== 'close') { | ||
| await sendReply({ github, context, core }, link.status === 'unlinked'); | ||
| } | ||
| await act({ github, context, core }, first, link); | ||
|
|
||
| if (authorAssigned) { | ||
| try { | ||
| await github.rest.pulls.requestReviewers({ | ||
| owner, | ||
| repo, | ||
| pull_number: number, | ||
| reviewers: [RTIBBLESBOT_USERNAME], | ||
| }); | ||
| await github.rest.issues.addLabels({ | ||
| owner, | ||
| repo, | ||
| issue_number: number, | ||
| labels: [LABEL_COMMUNITY_REVIEW], | ||
| }); | ||
| } catch (error) { | ||
| core.setFailed(`Failed to request review or add the label: ${error.message}`); | ||
| if (opening && first.action === 'label') { | ||
| const relookup = await getLinkedIssues(number, { github, context, core }); | ||
| if (relookup?.length) { | ||
| const relink = linkStatus(relookup, author); | ||
| await act({ github, context, core }, await plan(relink), relink); | ||
| } | ||
| } | ||
| } catch (error) { | ||
|
|
||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.