From e4879e0c90997af4f419b5ff65d4b592e1875285 Mon Sep 17 00:00:00 2001 From: draarivpatel-ui <287273626+draarivpatel-ui@users.noreply.github.com> Date: Wed, 25 Nov 2026 11:24:09 -0600 Subject: [PATCH] Find pull requests merged by a release merge commit --- lib/success.js | 61 +++++++++++++++++- test/success.test.js | 144 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 203 insertions(+), 2 deletions(-) diff --git a/lib/success.js b/lib/success.js index 68abe094..8e4ba508 100644 --- a/lib/success.js +++ b/lib/success.js @@ -97,7 +97,32 @@ export default async function success(pluginConfig, context, { Octokit }) { ); }); - const uniqueAssociatedPRs = uniqBy(associatedPRs, "number"); + const mergeCommitShas = [ + ...new Set( + associatedPRs + .map((pr) => pr.merge_commit_sha) + .filter((sha) => shas.includes(sha)), + ), + ]; + const stackedPRs = mergeCommitShas.length + ? buildIssuesOrPRsFromResponseNode( + Object.values( + await octokit.graphql( + buildMergeCommitPRsQuery(mergeCommitShas, owner, repo), + { + owner, + repo, + }, + ), + ) + .flatMap((result) => result.nodes) + .filter(isFromThisRepository), + ) + : []; + const uniqueAssociatedPRs = uniqBy( + [...associatedPRs, ...stackedPRs], + "number", + ); const prs = await pFilter(uniqueAssociatedPRs, async ({ number }) => { const commits = await octokit.paginate( @@ -434,6 +459,9 @@ const prFields = ` mergeable changedFiles mergedAt + mergeCommit { + oid + } isDraft mergedBy { login @@ -465,7 +493,7 @@ function buildRelatedIssuesQuery(numbers) { } }`; }) - .join("")} + .join("\n")} } } `; @@ -503,6 +531,34 @@ function buildAssociatedPRsQuery(shas) { `; } +/** + * Builds GraphQL query for finding pull requests merged by the same merge commit + * @param {Array} shas + * @param {string} owner + * @param {string} repo + * @returns {string} + */ +function buildMergeCommitPRsQuery(shas, owner, repo) { + return `#graphql + query getMergeCommitPRs($owner: String!, $repo: String!) { + ${shas + .map( + ( + sha, + index, + ) => `search${index}: search(query: "repo:${owner}/${repo} is:pr is:merged ${sha}", type: ISSUE, first: 100) { + nodes { + ... on PullRequest { + ${prFields} + } + } + }`, + ) + .join("\n")} + } + `; +} + /** * GraphQL Query to fetch additional associatedPR for commits that has more than 100 associatedPRs */ @@ -592,6 +648,7 @@ function buildIssuesOrPRsFromResponseNode(responseNodes) { changed_files: node.changedFiles, commits: node.commits?.totalCount, merged_at: node.mergedAt, + merge_commit_sha: node.mergeCommit?.oid, draft: node.isDraft, merged_by: { login: node.mergedBy?.login, diff --git a/test/success.test.js b/test/success.test.js index a7b570f6..039d3068 100644 --- a/test/success.test.js +++ b/test/success.test.js @@ -1228,6 +1228,150 @@ test("Make multiple search queries if necessary", async (t) => { t.true(fetch.done()); }); +test("Add comment and labels to every pull request in a merged stack", async (t) => { + const owner = "test_user"; + const repo = "test_repo"; + const env = { GITHUB_TOKEN: "github_token" }; + const pluginConfig = { failTitle: "The automated release is failing" }; + const mergeCommit = { oid: "123" }; + const bottomPR = { + number: 1, + __typename: "PullRequest", + state: "closed", + repository: { nameWithOwner: `${owner}/${repo}` }, + mergeCommit, + }; + const upperPR = { + number: 2, + __typename: "PullRequest", + state: "closed", + repository: { nameWithOwner: `${owner}/${repo}` }, + mergeCommit, + }; + const options = { + branch: "master", + repositoryUrl: `https://github.com/${owner}/${repo}.git`, + }; + const commits = [{ hash: "123", message: "Merge pull request stack" }]; + const nextRelease = { version: "1.0.0" }; + const releases = [ + { name: "GitHub release", url: "https://github.com/release" }, + ]; + + const fetch = fetchMock + .sandbox() + .getOnce(`https://api.github.local/repos/${owner}/${repo}`, { + full_name: `${owner}/${repo}`, + clone_url: `https://api.github.local/${owner}/${repo}.git`, + }) + .postOnce( + (url, { body }) => + url === "https://api.github.local/graphql" && + JSON.parse(body).query.includes("query getAssociatedPRs("), + { + data: { + repository: { + commit123: { + oid: "123", + associatedPullRequests: { + pageInfo: { endCursor: "NI", hasNextPage: false }, + nodes: [bottomPR], + }, + }, + }, + }, + }, + ) + .postOnce( + (url, { body }) => + url === "https://api.github.local/graphql" && + JSON.parse(body).query.includes("query getMergeCommitPRs("), + { + data: { + search0: { nodes: [bottomPR, upperPR] }, + }, + }, + ) + .getOnce( + `https://api.github.local/repos/${owner}/${repo}/pulls/1/commits`, + [{ sha: "rebased_sha" }], + ) + .getOnce(`https://api.github.local/repos/${owner}/${repo}/pulls/1`, { + merge_commit_sha: "123", + }) + .getOnce( + `https://api.github.local/repos/${owner}/${repo}/pulls/2/commits`, + [{ sha: "rebased_sha" }], + ) + .getOnce(`https://api.github.local/repos/${owner}/${repo}/pulls/2`, { + merge_commit_sha: "123", + }) + .postOnce( + `https://api.github.local/repos/${owner}/${repo}/issues/1/comments`, + { + html_url: "https://github.com/successcomment-1", + }, + ) + .postOnce( + `https://api.github.local/repos/${owner}/${repo}/issues/1/labels`, + {}, + { body: ["released"] }, + ) + .postOnce( + `https://api.github.local/repos/${owner}/${repo}/issues/2/comments`, + { + html_url: "https://github.com/successcomment-2", + }, + ) + .postOnce( + `https://api.github.local/repos/${owner}/${repo}/issues/2/labels`, + {}, + { body: ["released"] }, + ) + .postOnce( + (url, { body }) => + url === "https://api.github.local/graphql" && + JSON.parse(body).query.includes("query getSRIssues("), + { + data: { repository: { issues: { nodes: [] } } }, + }, + ); + + await success( + pluginConfig, + { + env, + options, + commits, + nextRelease, + releases, + logger: t.context.logger, + }, + { + Octokit: TestOctokit.defaults((options) => ({ + ...options, + request: { ...options.request, fetch }, + })), + }, + ); + + t.true( + t.context.log.calledWith( + "Added comment to PR #%d: %s", + 1, + "https://github.com/successcomment-1", + ), + ); + t.true( + t.context.log.calledWith( + "Added comment to PR #%d: %s", + 2, + "https://github.com/successcomment-2", + ), + ); + t.true(fetch.done()); +}); + test("Do not add comment and labels for unrelated PR returned by search (compare sha and merge_commit_sha)", async (t) => { const owner = "test_user"; const repo = "test_repo";