Skip to content
Merged
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
24 changes: 12 additions & 12 deletions actions/setup/js/add_workflow_run_comment.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good rename from issueNumber to number — the variable is now consistent across all event handler cases, reducing cognitive overhead when reading the switch block.

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;
}

Expand Down
52 changes: 52 additions & 0 deletions actions/setup/js/add_workflow_run_comment.test.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down Expand Up @@ -423,4 +424,55 @@ describe("add_workflow_run_comment", () => {
expect(mockCore.setOutput).toHaveBeenCalledWith("comment-repo", "testowner/testrepo");
});
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good addition: the activationComments: false test ensures the early-exit path is covered. The env cleanup in afterEach (line 53) should handle GH_AW_SAFE_OUTPUT_MESSAGES — confirmed it's now deleted there too.

});

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"),
})
);
});
});
});
Loading