Skip to content

Commit

Permalink
fix: rebuild on top of master
Browse files Browse the repository at this point in the history
  • Loading branch information
kristof-mattei committed Dec 26, 2024
1 parent 5e040f4 commit ec38c66
Show file tree
Hide file tree
Showing 2 changed files with 106 additions and 32 deletions.
46 changes: 24 additions & 22 deletions lib/success.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,12 +64,12 @@ export default async function success(pluginConfig, context, { Octokit }) {
if (isEmpty(commits)) {
logger.log("No commits found in release");
}
logger.log("Skip commenting on issues and pull requests.");
logger.log("Skip commenting / adding labels on issues and pull requests.");
logger.warn(
`DEPRECATION: 'false' for 'successComment' is deprecated and will be removed in a future major version. Use 'successCommentCondition' instead.`,
);
} else if (successCommentCondition === false) {
logger.log("Skip commenting on issues and pull requests.");
} else if (successCommentCondition === false && releasedLabels === false) {
logger.log("Skip commenting / adding labels on issues and pull requests.");
} else {
const parser = issueParser(
"github",
Expand Down Expand Up @@ -205,23 +205,25 @@ export default async function success(pluginConfig, context, { Octokit }) {
return;
}

const body = successComment
? template(successComment)({ ...context, issue })
: getSuccessComment(issue, releaseInfos, nextRelease);
try {
const comment = { owner, repo, issue_number: issue.number, body };
debug("create comment: %O", comment);
const {
data: { html_url: url },
} = await octokit.request(
"POST /repos/{owner}/{repo}/issues/{issue_number}/comments",
comment,
);
logger.log(
`Added comment to ${issueOrPR} #%d: %s`,
issue.number,
url,
);
if (successComment !== false) {
const body = successComment
? template(successComment)({ ...context, issue })
: getSuccessComment(issue, releaseInfos, nextRelease);
const comment = { owner, repo, issue_number: issue.number, body };
debug("create comment: %O", comment);
const {
data: { html_url: url },
} = await octokit.request(
"POST /repos/{owner}/{repo}/issues/{issue_number}/comments",
comment,
);
logger.log(
`Added comment to ${issueOrPR} #%d: %s`,
issue.number,
url,
);
}

if (releasedLabels) {
const labels = releasedLabels.map((label) =>
Expand All @@ -245,18 +247,18 @@ export default async function success(pluginConfig, context, { Octokit }) {
} catch (error) {
if (error.status === 403) {
logger.error(
`Not allowed to add a comment to the issue/PR #%d.`,
`Not allowed to add a comment/label to the issue/PR #%d.`,
issue.number,
);
} else if (error.status === 404) {
logger.error(
`Failed to add a comment to the issue/PR #%d as it doesn't exist.`,
`Failed to add a comment/label to the issue/PR #%d as it doesn't exist.`,
issue.number,
);
} else {
errors.push(error);
logger.error(
`Failed to add a comment to the issue/PR #%d.`,
`Failed to add a comment/label to the issue/PR #%d.`,
issue.number,
);
// Don't throw right away and continue to update other issues
Expand Down
92 changes: 82 additions & 10 deletions test/success.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1992,13 +1992,13 @@ test("Ignore missing and forbidden issues/PRs", async (t) => {
);
t.true(
t.context.error.calledWith(
"Failed to add a comment to the issue/PR #%d as it doesn't exist.",
"Failed to add a comment/label to the issue/PR #%d as it doesn't exist.",
2,
),
);
t.true(
t.context.error.calledWith(
"Not allowed to add a comment to the issue/PR #%d.",
"Not allowed to add a comment/label to the issue/PR #%d.",
3,
),
);
Expand Down Expand Up @@ -3225,7 +3225,7 @@ test("Ignore errors when adding comments and closing issues", async (t) => {
t.is(error2.status, 500);
t.true(
t.context.error.calledWith(
"Failed to add a comment to the issue/PR #%d.",
"Failed to add a comment/label to the issue/PR #%d.",
1,
),
);
Expand Down Expand Up @@ -3396,6 +3396,10 @@ test('Skip comment on on issues/PR if "successComment" is "false"', async (t) =>
repository: {
commit123: {
associatedPullRequests: {
pageInfo: {
endCursor: "NI",
hasNextPage: false,
},
nodes: [prs[0]],
},
},
Expand All @@ -3411,6 +3415,32 @@ test('Skip comment on on issues/PR if "successComment" is "false"', async (t) =>
{},
{ labels: ["released"] },
)
.postOnce(
(url, { body }) => {
t.is(url, "https://api.github.local/graphql");
t.regex(JSON.parse(body).query, /query getRelatedIssues\(/);
return true;
},
{
data: {
repository: {},
},
},
)
.postOnce(
(url, { body }) => {
t.is(url, "https://api.github.local/graphql");
t.regex(JSON.parse(body).query, /query getSRIssues\(/);
return true;
},
{
data: {
repository: {
issues: { nodes: [] },
},
},
},
)
.getOnce(
`https://api.github.local/search/issues?q=${encodeURIComponent(
"in:title",
Expand Down Expand Up @@ -3522,7 +3552,9 @@ test('Skip comment on issues/PR and skip label if "successComment" is "false" /
);

t.true(
t.context.log.calledWith("Skip commenting on issues and pull requests."),
t.context.log.calledWith(
"Skip commenting / adding labels on issues and pull requests.",
),
);
t.true(fetch.done());
});
Expand Down Expand Up @@ -3553,13 +3585,53 @@ test('Does not comment/label on issues/PR if "successCommentCondition" is "false
.getOnce(`https://api.github.local/repos/${owner}/${repo}`, {
full_name: `${owner}/${repo}`,
})
.postOnce("https://api.github.local/graphql", {
data: {
repository: {
issues: { nodes: [] },
.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: [],
},
},
},
},
},
})
)
.postOnce(
(url, { body }) => {
t.is(url, "https://api.github.local/graphql");
t.regex(JSON.parse(body).query, /query getRelatedIssues\(/);
return true;
},
{
data: {
repository: {},
},
},
)
.postOnce(
(url, { body }) => {
t.is(url, "https://api.github.local/graphql");
t.regex(JSON.parse(body).query, /query getSRIssues\(/);
return true;
},
{
data: {
repository: {
issues: { nodes: [] },
},
},
},
)
.getOnce(
`https://api.github.local/search/issues?q=${encodeURIComponent(
"in:title",
Expand Down Expand Up @@ -3592,7 +3664,7 @@ test('Does not comment/label on issues/PR if "successCommentCondition" is "false
t.true(fetch.done());
});

test('Add comment and label to found issues/associatedPR using the "successCommentCondition": if specific label is found', async (t) => {
test('Add comment and label to found issues/associated PR using the "successCommentCondition": if specific label is found', async (t) => {
const owner = "test_user";
const repo = "test_repo";
const env = { GITHUB_TOKEN: "github_token" };
Expand Down

0 comments on commit ec38c66

Please sign in to comment.