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
5 changes: 5 additions & 0 deletions .changeset/patch-standardize-safe-output-error-codes.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 4 additions & 3 deletions actions/setup/js/add_comment.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ const { getMessages } = require("./messages_core.cjs");
const { sanitizeContent } = require("./sanitize_content.cjs");
const { MAX_COMMENT_LENGTH, MAX_MENTIONS, MAX_LINKS, enforceCommentLimits } = require("./comment_limit_helpers.cjs");
const { logStagedPreviewInfo } = require("./staged_preview.cjs");
const { ERR_NOT_FOUND } = require("./error_codes.cjs");

/** @type {string} Safe output type handled by this module */
const HANDLER_TYPE = "add_comment";
Expand Down Expand Up @@ -229,7 +230,7 @@ async function commentOnDiscussion(github, owner, repo, discussionNumber, messag
);

if (!repository || !repository.discussion) {
throw new Error(`Discussion #${discussionNumber} not found in ${owner}/${repo}`);
throw new Error(`${ERR_NOT_FOUND}: Discussion #${discussionNumber} not found in ${owner}/${repo}`);
}

const discussionId = repository.discussion.id;
Expand Down Expand Up @@ -522,7 +523,7 @@ async function main(config = {}) {

const discussionId = queryResult?.repository?.discussion?.id;
if (!discussionId) {
throw new Error(`Discussion #${itemNumber} not found in ${itemRepo}`);
throw new Error(`${ERR_NOT_FOUND}: Discussion #${itemNumber} not found in ${itemRepo}`);
}

comment = await commentOnDiscussion(github, repoParts.owner, repoParts.repo, itemNumber, processedBody, null);
Expand Down Expand Up @@ -592,7 +593,7 @@ async function main(config = {}) {

const discussionId = queryResult?.repository?.discussion?.id;
if (!discussionId) {
throw new Error(`Discussion #${itemNumber} not found in ${itemRepo}`);
throw new Error(`${ERR_NOT_FOUND}: Discussion #${itemNumber} not found in ${itemRepo}`);
}

core.info(`Found discussion #${itemNumber}, adding comment...`);
Expand Down
7 changes: 4 additions & 3 deletions actions/setup/js/add_copilot_reviewer.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
/// <reference types="@actions/github-script" />

const { getErrorMessage } = require("./error_helpers.cjs");
const { ERR_CONFIG, ERR_NOT_FOUND, ERR_VALIDATION } = require("./error_codes.cjs");

/**
* Add Copilot as a reviewer to a pull request.
Expand All @@ -22,13 +23,13 @@ async function main() {
const prNumberStr = process.env.PR_NUMBER?.trim();

if (!prNumberStr) {
core.setFailed("PR_NUMBER environment variable is required but not set");
core.setFailed(`${ERR_CONFIG}: PR_NUMBER environment variable is required but not set`);
return;
}

const prNumber = parseInt(prNumberStr, 10);
if (isNaN(prNumber) || prNumber <= 0) {
core.setFailed(`Invalid PR_NUMBER: ${prNumberStr}. Must be a positive integer.`);
core.setFailed(`${ERR_VALIDATION}: Invalid PR_NUMBER: ${prNumberStr}. Must be a positive integer.`);
return;
}

Expand All @@ -55,7 +56,7 @@ Successfully added Copilot as a reviewer to PR #${prNumber}.`
} catch (error) {
const errorMessage = getErrorMessage(error);
core.error(`Failed to add Copilot as reviewer: ${errorMessage}`);
core.setFailed(`Failed to add Copilot as reviewer to PR #${prNumber}: ${errorMessage}`);
core.setFailed(`${ERR_NOT_FOUND}: Failed to add Copilot as reviewer to PR #${prNumber}: ${errorMessage}`);
}
}

Expand Down
5 changes: 3 additions & 2 deletions actions/setup/js/add_copilot_reviewer.test.cjs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { describe, it, expect, beforeEach, vi } from "vitest";
const { ERR_CONFIG } = require("./error_codes.cjs");

// Mock the global objects that GitHub Actions provides
const mockCore = {
Expand Down Expand Up @@ -76,7 +77,7 @@ describe("add_copilot_reviewer", () => {

await runScript();

expect(mockCore.setFailed).toHaveBeenCalledWith("PR_NUMBER environment variable is required but not set");
expect(mockCore.setFailed).toHaveBeenCalledWith(`${ERR_CONFIG}: PR_NUMBER environment variable is required but not set`);
expect(mockGithub.rest.pulls.requestReviewers).not.toHaveBeenCalled();
});

Expand All @@ -85,7 +86,7 @@ describe("add_copilot_reviewer", () => {

await runScript();

expect(mockCore.setFailed).toHaveBeenCalledWith("PR_NUMBER environment variable is required but not set");
expect(mockCore.setFailed).toHaveBeenCalledWith(`${ERR_CONFIG}: PR_NUMBER environment variable is required but not set`);
expect(mockGithub.rest.pulls.requestReviewers).not.toHaveBeenCalled();
});

Expand Down
23 changes: 12 additions & 11 deletions actions/setup/js/add_reaction.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
/// <reference types="@actions/github-script" />

const { getErrorMessage, isLockedError } = require("./error_helpers.cjs");
const { ERR_API, ERR_NOT_FOUND, ERR_VALIDATION } = require("./error_codes.cjs");

/**
* Add a reaction to the triggering item (issue, PR, comment, or discussion).
Expand All @@ -18,7 +19,7 @@ async function main() {
// Validate reaction type
const validReactions = ["+1", "-1", "laugh", "confused", "heart", "hooray", "rocket", "eyes"];
if (!validReactions.includes(reaction)) {
core.setFailed(`Invalid reaction type: ${reaction}. Valid reactions are: ${validReactions.join(", ")}`);
core.setFailed(`${ERR_VALIDATION}: Invalid reaction type: ${reaction}. Valid reactions are: ${validReactions.join(", ")}`);
return;
}

Expand All @@ -33,7 +34,7 @@ async function main() {
case "issues":
const issueNumber = context.payload?.issue?.number;
if (!issueNumber) {
core.setFailed("Issue number not found in event payload");
core.setFailed(`${ERR_NOT_FOUND}: Issue number not found in event payload`);
return;
}
reactionEndpoint = `/repos/${owner}/${repo}/issues/${issueNumber}/reactions`;
Expand All @@ -42,7 +43,7 @@ async function main() {
case "issue_comment":
const commentId = context.payload?.comment?.id;
if (!commentId) {
core.setFailed("Comment ID not found in event payload");
core.setFailed(`${ERR_VALIDATION}: Comment ID not found in event payload`);
return;
}
reactionEndpoint = `/repos/${owner}/${repo}/issues/comments/${commentId}/reactions`;
Expand All @@ -51,7 +52,7 @@ async function main() {
case "pull_request":
const prNumber = context.payload?.pull_request?.number;
if (!prNumber) {
core.setFailed("Pull request number not found in event payload");
core.setFailed(`${ERR_NOT_FOUND}: Pull request number not found in event payload`);
return;
}
// PRs are "issues" for the reactions endpoint
Expand All @@ -61,7 +62,7 @@ async function main() {
case "pull_request_review_comment":
const reviewCommentId = context.payload?.comment?.id;
if (!reviewCommentId) {
core.setFailed("Review comment ID not found in event payload");
core.setFailed(`${ERR_VALIDATION}: Review comment ID not found in event payload`);
return;
}
reactionEndpoint = `/repos/${owner}/${repo}/pulls/comments/${reviewCommentId}/reactions`;
Expand All @@ -70,7 +71,7 @@ async function main() {
case "discussion":
const discussionNumber = context.payload?.discussion?.number;
if (!discussionNumber) {
core.setFailed("Discussion number not found in event payload");
core.setFailed(`${ERR_NOT_FOUND}: Discussion number not found in event payload`);
return;
}
// Discussions use GraphQL API - get the node ID
Expand All @@ -81,14 +82,14 @@ async function main() {
case "discussion_comment":
const commentNodeId = context.payload?.comment?.node_id;
if (!commentNodeId) {
core.setFailed("Discussion comment node ID not found in event payload");
core.setFailed(`${ERR_NOT_FOUND}: Discussion comment node ID not found in event payload`);
return;
}
await addDiscussionReaction(commentNodeId, reaction);
return; // Early return for discussion comment events

default:
core.setFailed(`Unsupported event type: ${eventName}`);
core.setFailed(`${ERR_VALIDATION}: Unsupported event type: ${eventName}`);
return;
}

Expand All @@ -107,7 +108,7 @@ async function main() {

// For other errors, fail as before
core.error(`Failed to add reaction: ${errorMessage}`);
core.setFailed(`Failed to add reaction: ${errorMessage}`);
core.setFailed(`${ERR_API}: Failed to add reaction: ${errorMessage}`);
}
}

Expand Down Expand Up @@ -154,7 +155,7 @@ async function addDiscussionReaction(subjectId, reaction) {

const reactionContent = reactionMap[reaction];
if (!reactionContent) {
throw new Error(`Invalid reaction type for GraphQL: ${reaction}`);
throw new Error(`${ERR_VALIDATION}: Invalid reaction type for GraphQL: ${reaction}`);
}

const result = await github.graphql(
Expand Down Expand Up @@ -197,7 +198,7 @@ async function getDiscussionId(owner, repo, discussionNumber) {
);

if (!repository || !repository.discussion) {
throw new Error(`Discussion #${discussionNumber} not found in ${owner}/${repo}`);
throw new Error(`${ERR_NOT_FOUND}: Discussion #${discussionNumber} not found in ${owner}/${repo}`);
}

return {
Expand Down
15 changes: 8 additions & 7 deletions actions/setup/js/add_reaction.test.cjs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// @ts-check
import { describe, it, expect, beforeEach, vi } from "vitest";
const { ERR_NOT_FOUND, ERR_VALIDATION } = require("./error_codes.cjs");

// Mock the global objects that GitHub Actions provides
const mockCore = {
Expand Down Expand Up @@ -139,7 +140,7 @@ describe("add_reaction", () => {

await runScript();

expect(mockCore.setFailed).toHaveBeenCalledWith("Issue number not found in event payload");
expect(mockCore.setFailed).toHaveBeenCalledWith(`${ERR_NOT_FOUND}: Issue number not found in event payload`);
expect(mockGithub.request).not.toHaveBeenCalled();
});
});
Expand All @@ -166,7 +167,7 @@ describe("add_reaction", () => {

await runScript();

expect(mockCore.setFailed).toHaveBeenCalledWith("Comment ID not found in event payload");
expect(mockCore.setFailed).toHaveBeenCalledWith(`${ERR_VALIDATION}: Comment ID not found in event payload`);
});
});

Expand All @@ -192,7 +193,7 @@ describe("add_reaction", () => {

await runScript();

expect(mockCore.setFailed).toHaveBeenCalledWith("Pull request number not found in event payload");
expect(mockCore.setFailed).toHaveBeenCalledWith(`${ERR_NOT_FOUND}: Pull request number not found in event payload`);
});
});

Expand All @@ -218,7 +219,7 @@ describe("add_reaction", () => {

await runScript();

expect(mockCore.setFailed).toHaveBeenCalledWith("Review comment ID not found in event payload");
expect(mockCore.setFailed).toHaveBeenCalledWith(`${ERR_VALIDATION}: Review comment ID not found in event payload`);
});
});

Expand Down Expand Up @@ -268,7 +269,7 @@ describe("add_reaction", () => {

await runScript();

expect(mockCore.setFailed).toHaveBeenCalledWith("Discussion number not found in event payload");
expect(mockCore.setFailed).toHaveBeenCalledWith(`${ERR_NOT_FOUND}: Discussion number not found in event payload`);
});

it("should handle discussion not found error", async () => {
Expand Down Expand Up @@ -317,7 +318,7 @@ describe("add_reaction", () => {

await runScript();

expect(mockCore.setFailed).toHaveBeenCalledWith("Discussion comment node ID not found in event payload");
expect(mockCore.setFailed).toHaveBeenCalledWith(`${ERR_NOT_FOUND}: Discussion comment node ID not found in event payload`);
});
});

Expand Down Expand Up @@ -360,7 +361,7 @@ describe("add_reaction", () => {

await runScript();

expect(mockCore.setFailed).toHaveBeenCalledWith("Unsupported event type: push");
expect(mockCore.setFailed).toHaveBeenCalledWith(`${ERR_VALIDATION}: Unsupported event type: push`);
expect(mockGithub.request).not.toHaveBeenCalled();
});
});
Expand Down
Loading
Loading