Skip to content
Merged
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
83 changes: 42 additions & 41 deletions actions/setup/js/add_reaction.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,21 @@ async function main() {
}
}

/**
* Extract a required field from the event payload, calling setFailed if missing.
* @param {unknown} value - The extracted value (already resolved by caller)
* @param {string} fieldName - Human-readable field name for error messages
* @param {string} errorCode - Error code prefix (ERR_NOT_FOUND or ERR_VALIDATION)
* @returns {boolean} true if valid, false if missing (setFailed already called)
*/
function requirePayloadField(value, fieldName, errorCode) {
if (value == null) {
core.setFailed(`${errorCode}: ${fieldName} not found in event payload`);
Comment thread
pelikhan marked this conversation as resolved.
return false;
}
return true;
}

/**
* Resolve the REST API route and params for non-discussion events.
* Returns null for discussion/discussion_comment/pull_request_review/unsupported events (handled separately).
Expand All @@ -73,38 +88,26 @@ function resolveRestEndpoint(eventName, owner, repo, payload) {
switch (eventName) {
case "issues": {
const issueNumber = payload?.issue?.number;
if (!issueNumber) {
core.setFailed(`${ERR_NOT_FOUND}: Issue number not found in event payload`);
return null;
}
if (!requirePayloadField(issueNumber, "Issue number", ERR_NOT_FOUND)) return null;
return { route: "POST /repos/{owner}/{repo}/issues/{issue_number}/reactions", params: { owner, repo, issue_number: issueNumber } };
}

case "issue_comment": {
const commentId = payload?.comment?.id;
if (!commentId) {
core.setFailed(`${ERR_VALIDATION}: Comment ID not found in event payload`);
return null;
}
if (!requirePayloadField(commentId, "Comment ID", ERR_VALIDATION)) return null;
return { route: "POST /repos/{owner}/{repo}/issues/comments/{comment_id}/reactions", params: { owner, repo, comment_id: commentId } };
}

case "pull_request": {
const prNumber = payload?.pull_request?.number;
if (!prNumber) {
core.setFailed(`${ERR_NOT_FOUND}: Pull request number not found in event payload`);
return null;
}
if (!requirePayloadField(prNumber, "Pull request number", ERR_NOT_FOUND)) return null;
// PRs are "issues" for the reactions endpoint
return { route: "POST /repos/{owner}/{repo}/issues/{issue_number}/reactions", params: { owner, repo, issue_number: prNumber } };
}

case "pull_request_review_comment": {
const reviewCommentId = payload?.comment?.id;
if (!reviewCommentId) {
core.setFailed(`${ERR_VALIDATION}: Review comment ID not found in event payload`);
return null;
}
if (!requirePayloadField(reviewCommentId, "Review comment ID", ERR_VALIDATION)) return null;
return { route: "POST /repos/{owner}/{repo}/pulls/comments/{comment_id}/reactions", params: { owner, repo, comment_id: reviewCommentId } };
}

Expand Down Expand Up @@ -134,38 +137,36 @@ function isRestReactionEvent(eventName) {
* @param {string} reaction
*/
async function handleGraphQLOrUnknownEvent(eventName, owner, repo, payload, reaction) {
switch (eventName) {
case "discussion": {
const discussionNumber = payload?.discussion?.number;
if (!discussionNumber) {
core.setFailed(`${ERR_NOT_FOUND}: Discussion number not found in event payload`);
return;
let subjectId;
try {
switch (eventName) {
case "discussion": {
const discussionNumber = payload?.discussion?.number;
if (!requirePayloadField(discussionNumber, "Discussion number", ERR_NOT_FOUND)) return;
subjectId = await getDiscussionNodeId(owner, repo, discussionNumber);
break;
}
try {
const discussionNodeId = await getDiscussionNodeId(owner, repo, discussionNumber);
await addDiscussionReaction(discussionNodeId, reaction);
} catch (error) {
handleReactionError(error);

case "discussion_comment": {
const commentNodeId = payload?.comment?.node_id;
if (!requirePayloadField(commentNodeId, "Discussion comment node ID", ERR_NOT_FOUND)) return;
subjectId = commentNodeId;
break;
}
return;
}

case "discussion_comment": {
const commentNodeId = payload?.comment?.node_id;
if (!commentNodeId) {
core.setFailed(`${ERR_NOT_FOUND}: Discussion comment node ID not found in event payload`);
default:
core.setFailed(`${ERR_VALIDATION}: Unsupported event type: ${eventName}`);
return;
}
try {
await addDiscussionReaction(commentNodeId, reaction);
} catch (error) {
handleReactionError(error);
}
}
Comment thread
pelikhan marked this conversation as resolved.
Comment thread
pelikhan marked this conversation as resolved.

if (subjectId == null) {
core.setFailed(`${ERR_VALIDATION}: subjectId could not be resolved for event: ${eventName}`);
return;
}

default:
core.setFailed(`${ERR_VALIDATION}: Unsupported event type: ${eventName}`);
await addDiscussionReaction(subjectId, reaction);
} catch (error) {
handleReactionError(error);
}
}

Expand Down
Loading