diff --git a/actions/setup/js/add_workflow_run_comment.cjs b/actions/setup/js/add_workflow_run_comment.cjs index 72fc38e2aca..8201482241e 100644 --- a/actions/setup/js/add_workflow_run_comment.cjs +++ b/actions/setup/js/add_workflow_run_comment.cjs @@ -84,44 +84,44 @@ async function main() { try { switch (eventName) { case "issues": { - const issueNumber = context.payload?.issue?.number; - if (!issueNumber) { + const number = context.payload?.issue?.number; + if (!number) { core.setFailed(`${ERR_NOT_FOUND}: Issue number not found in event payload`); return; } - commentEndpoint = `/repos/${owner}/${repo}/issues/${issueNumber}/comments`; + commentEndpoint = `/repos/${owner}/${repo}/issues/${number}/comments`; break; } case "issue_comment": { - const issueNumberForComment = context.payload?.issue?.number; - if (!issueNumberForComment) { + const number = context.payload?.issue?.number; + if (!number) { core.setFailed(`${ERR_NOT_FOUND}: Issue number not found in event payload`); return; } // Create new comment on the issue itself, not on the comment - commentEndpoint = `/repos/${owner}/${repo}/issues/${issueNumberForComment}/comments`; + commentEndpoint = `/repos/${owner}/${repo}/issues/${number}/comments`; break; } case "pull_request": { - const prNumber = context.payload?.pull_request?.number; - if (!prNumber) { + const number = context.payload?.pull_request?.number; + if (!number) { core.setFailed(`${ERR_NOT_FOUND}: Pull request number not found in event payload`); return; } - commentEndpoint = `/repos/${owner}/${repo}/issues/${prNumber}/comments`; + commentEndpoint = `/repos/${owner}/${repo}/issues/${number}/comments`; break; } case "pull_request_review_comment": { - const prNumberForReviewComment = context.payload?.pull_request?.number; - if (!prNumberForReviewComment) { + const number = context.payload?.pull_request?.number; + if (!number) { core.setFailed(`${ERR_NOT_FOUND}: Pull request number not found in event payload`); return; } // Create new comment on the PR itself (using issues endpoint since PRs are issues) - commentEndpoint = `/repos/${owner}/${repo}/issues/${prNumberForReviewComment}/comments`; + commentEndpoint = `/repos/${owner}/${repo}/issues/${number}/comments`; break; } diff --git a/actions/setup/js/add_workflow_run_comment.test.cjs b/actions/setup/js/add_workflow_run_comment.test.cjs index 02d265347e6..398848a0c98 100644 --- a/actions/setup/js/add_workflow_run_comment.test.cjs +++ b/actions/setup/js/add_workflow_run_comment.test.cjs @@ -50,6 +50,7 @@ describe("add_workflow_run_comment", () => { delete process.env.GH_AW_TRACKER_ID; delete process.env.GH_AW_LOCK_FOR_AGENT; delete process.env.GITHUB_SERVER_URL; + delete process.env.GH_AW_SAFE_OUTPUT_MESSAGES; // Reset context to default global.context = { @@ -423,4 +424,55 @@ describe("add_workflow_run_comment", () => { expect(mockCore.setOutput).toHaveBeenCalledWith("comment-repo", "testowner/testrepo"); }); }); + + describe("main() - activation comments disabled", () => { + it("should skip comment when activationComments is false", async () => { + process.env.GH_AW_SAFE_OUTPUT_MESSAGES = JSON.stringify({ activationComments: false }); + global.context = { + eventName: "issues", + runId: 12345, + repo: { owner: "testowner", repo: "testrepo" }, + payload: { + issue: { number: 456 }, + repository: { html_url: "https://github.com/testowner/testrepo" }, + }, + }; + + await runScript(); + + expect(mockGithub.request).not.toHaveBeenCalled(); + expect(mockCore.setFailed).not.toHaveBeenCalled(); + }); + }); + + describe("main() - issue_comment missing number", () => { + it("should fail when issue number is missing in issue_comment event", async () => { + global.context = { + eventName: "issue_comment", + runId: 12345, + repo: { owner: "testowner", repo: "testrepo" }, + payload: {}, + }; + + await runScript(); + + expect(mockCore.setFailed).toHaveBeenCalledWith(`${ERR_NOT_FOUND}: Issue number not found in event payload`); + expect(mockGithub.request).not.toHaveBeenCalled(); + }); + }); + + describe("addCommentWithWorkflowLink() - custom workflow name", () => { + it("should use GH_AW_WORKFLOW_NAME in the comment body", async () => { + process.env.GH_AW_WORKFLOW_NAME = "My Custom Workflow"; + + await runAddCommentWithWorkflowLink("/repos/testowner/testrepo/issues/123/comments", "https://github.com/testowner/testrepo/actions/runs/12345", "issues"); + + expect(mockGithub.request).toHaveBeenCalledWith( + expect.stringContaining("POST"), + expect.objectContaining({ + body: expect.stringContaining("My Custom Workflow"), + }) + ); + }); + }); });