Skip to content
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

fix(success): PRs not recognized as resolved issues #960

Merged
merged 7 commits into from
Nov 6, 2024
31 changes: 24 additions & 7 deletions lib/success.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ export default async function success(pluginConfig, context, { Octokit }) {
for (const { nodes, pageInfo } of responseAssociatedPRs) {
if (nodes.length === 0) continue;

responsePRs.push(...buildIssuesOrPRsFromResponseNode(nodes, "PR"));
responsePRs.push(...buildIssuesOrPRsFromResponseNode(nodes));
if (pageInfo.hasNextPage) {
let cursor = pageInfo.endCursor;
let hasNextPage = true;
Expand Down Expand Up @@ -171,7 +171,7 @@ export default async function success(pluginConfig, context, { Octokit }) {
if (!isEmpty(parsedIssues)) {
const uniqueParsedIssues = uniqBy(flatten(parsedIssues), "number");

// Get relatedIssues
// Get relatedIssues (or relatedPRs i.e. Issues/PRs that are closed by an associatedPR)
issues = await inChunks(uniqueParsedIssues, 100, async (chunk) => {
const { repository } = await octokit.graphql(
buildRelatedIssuesQuery(chunk.map((issue) => issue.number)),
Expand Down Expand Up @@ -360,6 +360,7 @@ async function inChunks(items, chunkSize, callback) {
* Fields common accross PRs and Issue
*/
const baseFields = `
__typename
id
title
body
Expand Down Expand Up @@ -420,8 +421,25 @@ function buildRelatedIssuesQuery(numbers) {
repository(owner: $owner, name: $repo) {
${numbers
.map((num) => {
return `issue${num}: issue(number: ${num}) {
${baseFields}
return `issue${num}: issueOrPullRequest(number: ${num}) {
... on Issue {
${baseFields}
}
... on PullRequest {
${baseFields}
mergeable
changedFiles
mergedAt
isDraft
mergedBy {
login
avatarUrl
url
}
commits {
totalCount
}
}
}`;
})
.join("")}
Expand Down Expand Up @@ -513,10 +531,9 @@ const loadSingleCommitAssociatedPRs = `#graphql
/**
* Build associatedPRs or RelatedIssues object (into issue-like object with `pull_request` property) from the GraphQL repository response
* @param {object} responseNodes
* @param {"ISSUE" | "PR"} type
* @returns {object[]}
*/
function buildIssuesOrPRsFromResponseNode(responseNodes, type = "ISSUE") {
function buildIssuesOrPRsFromResponseNode(responseNodes) {
const resultArray = [];
for (const node of responseNodes) {
let baseProps = {
Expand Down Expand Up @@ -569,7 +586,7 @@ function buildIssuesOrPRsFromResponseNode(responseNodes, type = "ISSUE") {

let result = baseProps;

if (type === "PR") {
if (node.__typename === "PullRequest") {
const prProps = {
pull_request: true,
mergeable: node.mergeable,
Expand Down
2 changes: 1 addition & 1 deletion test/integration.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -433,7 +433,7 @@ test("Comment and add labels on PR included in the releases", async (t) => {
const repo = "test_repo";
const env = { GITHUB_TOKEN: "github_token" };
const failTitle = "The automated release is failing 🚨";
const prs = [{ number: 1, pull_request: true, state: "closed" }];
const prs = [{ number: 1, __typename: "PullRequest", state: "closed" }];
const options = {
repositoryUrl: `https://github.com/${owner}/${repo}.git`,
};
Expand Down
80 changes: 48 additions & 32 deletions test/success.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ test("Add comment and labels to PRs associated with release commits and issues s
const failTitle = "The automated release is failing 🚨";
const pluginConfig = { failTitle };
const prs = [
{ number: 1, pull_request: true, state: "closed" },
{ number: 2, pull_request: true, body: "Fixes #3", state: "closed" },
{ number: 1, __typename: "PullRequest", state: "closed" },
{ number: 2, __typename: "PullRequest", body: "Fixes #3", state: "closed" },
];
const options = {
branch: "master",
Expand Down Expand Up @@ -103,6 +103,7 @@ test("Add comment and labels to PRs associated with release commits and issues s
data: {
repository: {
issue3: {
__typename: "Issue",
id: "I_kw",
title: "issue title",
body: "",
Expand Down Expand Up @@ -139,6 +140,7 @@ test("Add comment and labels to PRs associated with release commits and issues s
locked: false,
},
issue4: {
__typename: "Issue",
id: "I_kw",
title: "issue title",
body: "",
Expand Down Expand Up @@ -317,10 +319,10 @@ test("Add comment and labels to PRs associated with release commits and issues (
const failTitle = "The automated release is failing 🚨";
const pluginConfig = { failTitle };
const prs = [
{ number: 1, pull_request: true, state: "closed" },
{ number: 2, pull_request: true, body: "Fixes #3", state: "closed" },
{ number: 5, pull_request: true, state: "closed" },
{ number: 6, pull_request: true, state: "closed" },
{ number: 1, __typename: "PullRequest", state: "closed" },
{ number: 2, __typename: "PullRequest", body: "Fixes #3", state: "closed" },
{ number: 5, __typename: "PullRequest", state: "closed" },
{ number: 6, __typename: "PullRequest", state: "closed" },
];
const options = {
branch: "master",
Expand Down Expand Up @@ -414,6 +416,7 @@ test("Add comment and labels to PRs associated with release commits and issues (
data: {
repository: {
issue3: {
__typename: "Issue",
id: "I_kw",
title: "issue title",
body: "",
Expand Down Expand Up @@ -450,6 +453,7 @@ test("Add comment and labels to PRs associated with release commits and issues (
locked: false,
},
issue4: {
__typename: "Issue",
id: "I_kw",
title: "issue title",
body: "",
Expand Down Expand Up @@ -607,8 +611,8 @@ test("Add comment and labels to PRs associated with release commits and issues c
const failTitle = "The automated release is failing 🚨";
const pluginConfig = { failTitle };
const prs = [
{ number: 1, pull_request: true, state: "closed" },
{ number: 2, pull_request: true, body: "Fixes #3", state: "closed" },
{ number: 1, __typename: "PullRequest", state: "closed" },
{ number: 2, __typename: "PullRequest", body: "Fixes #3", state: "closed" },
];
const options = {
branch: "master",
Expand Down Expand Up @@ -671,6 +675,7 @@ test("Add comment and labels to PRs associated with release commits and issues c
data: {
repository: {
issue3: {
__typename: "Issue",
id: "I_kw",
title: "issue title",
body: "",
Expand Down Expand Up @@ -707,6 +712,7 @@ test("Add comment and labels to PRs associated with release commits and issues c
locked: false,
},
issue4: {
__typename: "Issue",
id: "I_kw",
title: "issue title",
body: "",
Expand Down Expand Up @@ -905,12 +911,12 @@ test("Make multiple search queries if necessary", async (t) => {
const failTitle = "The automated release is failing 🚨";
const pluginConfig = { failTitle };
const prs = [
{ number: 1, pull_request: true, state: "closed" },
{ number: 2, pull_request: true, state: "closed" },
{ number: 3, pull_request: true, state: "closed" },
{ number: 4, pull_request: true, state: "closed" },
{ number: 5, pull_request: true, state: "closed" },
{ number: 6, pull_request: true, state: "closed" },
{ number: 1, __typename: "PullRequest", state: "closed" },
{ number: 2, __typename: "PullRequest", state: "closed" },
{ number: 3, __typename: "PullRequest", state: "closed" },
{ number: 4, __typename: "PullRequest", state: "closed" },
{ number: 5, __typename: "PullRequest", state: "closed" },
{ number: 6, __typename: "PullRequest", state: "closed" },
];
const options = {
branch: "master",
Expand Down Expand Up @@ -1210,8 +1216,8 @@ test("Do not add comment and labels for unrelated PR returned by search (compare
const failTitle = "The automated release is failing 🚨";
const pluginConfig = { failTitle };
const prs = [
{ number: 1, pull_request: true, state: "closed" },
{ number: 2, pull_request: true, state: "closed" },
{ number: 1, __typename: "PullRequest", state: "closed" },
{ number: 2, __typename: "PullRequest", state: "closed" },
];
const options = {
branch: "master",
Expand Down Expand Up @@ -1677,9 +1683,9 @@ test("Ignore missing and forbidden issues/PRs", async (t) => {
const failTitle = "The automated release is failing 🚨";
const pluginConfig = { failTitle };
const prs = [
{ number: 1, pull_request: true, state: "closed" },
{ number: 2, pull_request: true, body: "Fixes #4", state: "closed" },
{ number: 3, pull_request: true, body: "Fixes #5", state: "closed" },
{ number: 1, __typename: "PullRequest", state: "closed" },
{ number: 2, __typename: "PullRequest", body: "Fixes #4", state: "closed" },
{ number: 3, __typename: "PullRequest", body: "Fixes #5", state: "closed" },
];
const options = {
branch: "master",
Expand Down Expand Up @@ -1750,6 +1756,7 @@ test("Ignore missing and forbidden issues/PRs", async (t) => {
data: {
repository: {
issue4: {
__typename: "Issue",
id: "I_kw",
title: "issue title",
body: "",
Expand Down Expand Up @@ -1786,6 +1793,7 @@ test("Ignore missing and forbidden issues/PRs", async (t) => {
locked: false,
},
issue5: {
__typename: "Issue",
id: "I_kw",
title: "issue title",
body: "",
Expand Down Expand Up @@ -1822,6 +1830,7 @@ test("Ignore missing and forbidden issues/PRs", async (t) => {
locked: false,
},
issue1: {
__typename: "Issue",
id: "I_kw",
title: "issue title",
body: "",
Expand Down Expand Up @@ -2008,7 +2017,7 @@ test("Add custom comment and labels", async (t) => {
],
};
const prs = [
{ number: 1, prop: "PR prop", pull_request: true, state: "closed" },
{ number: 1, prop: "PR prop", __typename: "PullRequest", state: "closed" },
];
const options = { repositoryUrl: `https://github.com/${owner}/${repo}.git` };
const lastRelease = { version: "1.0.0" };
Expand Down Expand Up @@ -2129,7 +2138,7 @@ test("Add custom label", async (t) => {
const env = { GITHUB_TOKEN: "github_token" };
const failTitle = "The automated release is failing 🚨";
const pluginConfig = { releasedLabels: ["custom label"], failTitle };
const prs = [{ number: 1, pull_request: true, state: "closed" }];
const prs = [{ number: 1, __typename: "PullRequest", state: "closed" }];
const options = { repositoryUrl: `https://github.com/${owner}/${repo}.git` };
const lastRelease = { version: "1.0.0" };
const commits = [{ hash: "123", message: "Commit 1 message" }];
Expand Down Expand Up @@ -2242,7 +2251,7 @@ test("Comment on issue/PR without ading a label", async (t) => {
const env = { GITHUB_TOKEN: "github_token" };
const failTitle = "The automated release is failing 🚨";
const pluginConfig = { releasedLabels: false, failTitle };
const prs = [{ number: 1, pull_request: true, state: "closed" }];
const prs = [{ number: 1, __typename: "PullRequest", state: "closed" }];
const options = { repositoryUrl: `https://github.com/${owner}/${repo}.git` };
const lastRelease = { version: "1.0.0" };
const commits = [{ hash: "123", message: "Commit 1 message" }];
Expand Down Expand Up @@ -2347,7 +2356,7 @@ test("Editing the release to include all release links at the bottom", async (t)
const env = { GITHUB_TOKEN: "github_token" };
const failTitle = "The automated release is failing 🚨";
const pluginConfig = { releasedLabels: false, addReleases: "bottom" };
const prs = [{ number: 1, pull_request: true, state: "closed" }];
const prs = [{ number: 1, __typename: "PullRequest", state: "closed" }];
const options = {
repositoryUrl: `https://github.com/${owner}/${repo}.git`,
};
Expand Down Expand Up @@ -2478,7 +2487,7 @@ test("Editing the release to include all release links at the top", async (t) =>
const env = { GITHUB_TOKEN: "github_token" };
const failTitle = "The automated release is failing 🚨";
const pluginConfig = { releasedLabels: false, addReleases: "top" };
const prs = [{ number: 1, pull_request: true, state: "closed" }];
const prs = [{ number: 1, __typename: "PullRequest", state: "closed" }];
const options = {
repositoryUrl: `https://github.com/${owner}/${repo}.git`,
};
Expand Down Expand Up @@ -2609,7 +2618,7 @@ test("Editing the release to include all release links with no additional releas
const env = { GITHUB_TOKEN: "github_token" };
const failTitle = "The automated release is failing 🚨";
const pluginConfig = { releasedLabels: false, addReleases: "top" };
const prs = [{ number: 1, pull_request: true, state: "closed" }];
const prs = [{ number: 1, __typename: "PullRequest", state: "closed" }];
const options = {
repositoryUrl: `https://github.com/${owner}/${repo}.git`,
};
Expand Down Expand Up @@ -2726,7 +2735,7 @@ test("Editing the release to include all release links with no additional releas
const env = { GITHUB_TOKEN: "github_token" };
const failTitle = "The automated release is failing 🚨";
const pluginConfig = { releasedLabels: false, addReleases: "bottom" };
const prs = [{ number: 1, pull_request: true, state: "closed" }];
const prs = [{ number: 1, __typename: "PullRequest", state: "closed" }];
const options = {
repositoryUrl: `https://github.com/${owner}/${repo}.git`,
};
Expand Down Expand Up @@ -2843,7 +2852,7 @@ test("Editing the release to include all release links with no releases", async
const env = { GITHUB_TOKEN: "github_token" };
const failTitle = "The automated release is failing 🚨";
const pluginConfig = { releasedLabels: false, addReleases: "bottom" };
const prs = [{ number: 1, pull_request: true, state: "closed" }];
const prs = [{ number: 1, __typename: "PullRequest", state: "closed" }];
const options = {
repositoryUrl: `https://github.com/${owner}/${repo}.git`,
};
Expand Down Expand Up @@ -2953,7 +2962,7 @@ test("Editing the release with no ID in the release", async (t) => {
const env = { GITHUB_TOKEN: "github_token" };
const failTitle = "The automated release is failing 🚨";
const pluginConfig = { releasedLabels: false, addReleases: "bottom" };
const prs = [{ number: 1, pull_request: true, state: "closed" }];
const prs = [{ number: 1, __typename: "PullRequest", state: "closed" }];
const options = { repositoryUrl: `https://github.com/${owner}/${repo}.git` };
const nextRelease = {
version: "2.0.0",
Expand Down Expand Up @@ -3071,8 +3080,8 @@ test("Ignore errors when adding comments and closing issues", async (t) => {
{ number: 3, body: `Issue 3 body\n\n${ISSUE_ID}`, title: failTitle },
];
const prs = [
{ number: 1, pull_request: true, state: "closed" },
{ number: 2, pull_request: true, state: "closed" },
{ number: 1, __typename: "PullRequest", state: "closed" },
{ number: 2, __typename: "PullRequest", state: "closed" },
];
const options = {
repositoryUrl: `https://github.com/${owner}/${repo}.git`,
Expand Down Expand Up @@ -3524,6 +3533,7 @@ test('Add comment and label to found issues/associatedPR using the "successComme

const prs = [
{
__typename: "PullRequest",
id: "PR_kwDOMLlZj85z_R2M",
title: "fix: will semantic-release recognize the associated issue ",
body: "",
Expand Down Expand Up @@ -3574,6 +3584,7 @@ test('Add comment and label to found issues/associatedPR using the "successComme
},
},
{
__typename: "PullRequest",
id: "PR_kwDOMLlZj85z_R2M",
title: "fix: will semantic-release recognize the associated issue ",
body: "",
Expand Down Expand Up @@ -3775,6 +3786,7 @@ test('Does not comment/label associatedPR and relatedIssues created by "Bots"',
];
const prs = [
{
__typename: "PullRequest",
number: 2,
id: "PR_kwDOMLlZj851SZzc",
title: "pr title",
Expand Down Expand Up @@ -3823,6 +3835,7 @@ test('Does not comment/label associatedPR and relatedIssues created by "Bots"',
},
},
{
__typename: "PullRequest",
number: 3,
id: "PR_kwDOMLlZj851SZzc",
title: "pr title",
Expand Down Expand Up @@ -3916,6 +3929,7 @@ test('Does not comment/label associatedPR and relatedIssues created by "Bots"',
data: {
repository: {
issue4: {
__typename: "Issue",
id: "I_kw",
title: "issue title",
body: "",
Expand Down Expand Up @@ -3952,6 +3966,7 @@ test('Does not comment/label associatedPR and relatedIssues created by "Bots"',
locked: false,
},
issue5: {
__typename: "Issue",
id: "I_kw",
title: "issue title",
body: "",
Expand Down Expand Up @@ -4120,8 +4135,8 @@ test('Does not comment/label "associatedPR" when "successCommentCondition" disab
},
];
const prs = [
{ number: 2, pull_request: true, body: "Fixes #4", state: "closed" },
{ number: 3, pull_request: true, state: "closed" },
{ number: 2, __typename: "PullRequest", body: "Fixes #4", state: "closed" },
{ number: 3, __typename: "PullRequest", state: "closed" },
];

const fetch = fetchMock
Expand Down Expand Up @@ -4168,6 +4183,7 @@ test('Does not comment/label "associatedPR" when "successCommentCondition" disab
data: {
repository: {
issue4: {
__typename: "Issue",
id: "I_kw",
title: "issue title",
body: "",
Expand Down