Skip to content

feat: issue-PR linking plugin to comment on issues when PRs reference them in body #226

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

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ const plugins = require("./plugins");
const enabledPlugins = new Set([
"autoAssign",
"commitMessage",
"issuePrLink",
"needsInfo",
"recurringIssues",
"releaseMonitor",
Expand Down
1 change: 1 addition & 0 deletions src/plugins/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
module.exports = {
autoAssign: require("./auto-assign"),
commitMessage: require("./commit-message"),
issuePrLink: require("./issue-pr-link"),
needsInfo: require("./needs-info"),
recurringIssues: require("./recurring-issues"),
releaseMonitor: require("./release-monitor"),
Expand Down
169 changes: 169 additions & 0 deletions src/plugins/issue-pr-link/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,169 @@
/**
* @fileoverview Comment on issues when PRs are created/edited to fix them
* @author ESLint GitHub Bot Contributors
*/

"use strict";

//-----------------------------------------------------------------------------
// Type Definitions
//-----------------------------------------------------------------------------

/** @typedef {import("probot").Context} ProbotContext */

//-----------------------------------------------------------------------------
// Helpers
//-----------------------------------------------------------------------------

/**
* Regex to find issue references in PR bodies
* Matches patterns like: "Fix #123", "Fixes #123", "Closes #123", "Resolves #123", etc.
*/
const ISSUE_REFERENCE_REGEX = /\b(?:fix|fixes|fixed|close|closes|closed|resolve|resolves|resolved)\b:?[ \t]+#(?<issueNumber>\d+)/giu;

/**
* Maximum number of issues to comment on per PR to prevent abuse
*/
const MAX_ISSUES_PER_PR = 3;

/**
* Extract issue numbers from PR body
* @param {string} body PR body
* @returns {number[]} Array of issue numbers
* @private
*/
function extractIssueNumbers(body) {
const matches = [];
let match;

// Reset regex lastIndex to ensure we start from the beginning
ISSUE_REFERENCE_REGEX.lastIndex = 0;

while ((match = ISSUE_REFERENCE_REGEX.exec(body)) !== null && matches.length < MAX_ISSUES_PER_PR) {
const issueNumber = parseInt(match.groups.issueNumber, 10);
if (!matches.includes(issueNumber)) {
matches.push(issueNumber);
}
}

return matches;
}

/**
* Create the comment message for the issue
* @param {string} prUrl URL of the pull request
* @param {string} prAuthor Author of the pull request
* @returns {string} comment message
* @private
*/
function createCommentMessage(prUrl, prAuthor) {
return `👋 Hi! This issue is being addressed in pull request ${prUrl}. Thanks, @${prAuthor}!

[//]: # (issue-pr-link)`;
}

/**
* Check if an issue exists and is open
* @param {ProbotContext} context Probot context object
* @param {number} issueNumber Issue number to check
* @returns {Promise<boolean>} True if issue exists and is open
* @private
*/
async function isIssueOpenAndExists(context, issueNumber) {
try {
const { data: issue } = await context.octokit.issues.get(
context.repo({ issue_number: issueNumber })
);
return issue.state === "open";
} catch {
// Issue doesn't exist or we don't have access
return false;
}
}

/**
* Check if we already commented on this issue for this PR
* @param {ProbotContext} context Probot context object
* @param {number} issueNumber Issue number
* @param {number} prNumber PR number
* @returns {Promise<boolean>} True if we already commented
* @private
*/
async function hasExistingComment(context, issueNumber, prNumber) {
try {
const { data: comments } = await context.octokit.issues.listComments(
context.repo({ issue_number: issueNumber })
);

const botComments = comments.filter(comment =>
comment.user.type === "Bot" &&
comment.body.includes("[//]: # (issue-pr-link)") &&
comment.body.includes(`/pull/${prNumber}`)
);

return botComments.length > 0;
} catch {
// If we can't check comments, assume we haven't commented
return false;
}
}

/**
* Comment on issues referenced in the PR body
* @param {ProbotContext} context Probot context object
* @returns {Promise<void>}
* @private
*/
async function commentOnReferencedIssues(context) {
const { payload } = context;
const pr = payload.pull_request;

if (!pr || !pr.body) {
return;
}

const issueNumbers = extractIssueNumbers(pr.body);

if (issueNumbers.length === 0) {
return;
}

const prUrl = pr.html_url;
const prAuthor = pr.user.login;
const prNumber = pr.number;

// Comment on each referenced issue
for (const issueNumber of issueNumbers) {
try {
// Check if issue exists and is open
if (!(await isIssueOpenAndExists(context, issueNumber))) {
continue;
}

// Check if we already commented on this issue for this PR
if (await hasExistingComment(context, issueNumber, prNumber)) {
continue;
}

// Create the comment
await context.octokit.issues.createComment(
context.repo({
issue_number: issueNumber,
body: createCommentMessage(prUrl, prAuthor)
})
);
} catch (error) {
// Log error but continue with other issues
// eslint-disable-next-line no-console -- Logging errors is intentional
console.error(`Failed to comment on issue #${issueNumber}:`, error);
}
}
}

//-----------------------------------------------------------------------------
// Robot
//-----------------------------------------------------------------------------

module.exports = robot => {
robot.on(["pull_request.opened", "pull_request.edited"], commentOnReferencedIssues);
};
Loading