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
32 changes: 17 additions & 15 deletions actions/setup/js/add_reaction.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -31,25 +31,27 @@ async function main() {

try {
switch (eventName) {
case "issues":
case "issues": {
const issueNumber = context.payload?.issue?.number;
if (!issueNumber) {
core.setFailed(`${ERR_NOT_FOUND}: Issue number not found in event payload`);
return;
}
reactionEndpoint = `/repos/${owner}/${repo}/issues/${issueNumber}/reactions`;
break;
}

case "issue_comment":
case "issue_comment": {
const commentId = context.payload?.comment?.id;
if (!commentId) {
core.setFailed(`${ERR_VALIDATION}: Comment ID not found in event payload`);
return;
}
reactionEndpoint = `/repos/${owner}/${repo}/issues/comments/${commentId}/reactions`;
break;
}

case "pull_request":
case "pull_request": {
const prNumber = context.payload?.pull_request?.number;
if (!prNumber) {
core.setFailed(`${ERR_NOT_FOUND}: Pull request number not found in event payload`);
Expand All @@ -58,35 +60,39 @@ async function main() {
// PRs are "issues" for the reactions endpoint
reactionEndpoint = `/repos/${owner}/${repo}/issues/${prNumber}/reactions`;
break;
}

case "pull_request_review_comment":
case "pull_request_review_comment": {
const reviewCommentId = context.payload?.comment?.id;
if (!reviewCommentId) {
core.setFailed(`${ERR_VALIDATION}: Review comment ID not found in event payload`);
return;
}
reactionEndpoint = `/repos/${owner}/${repo}/pulls/comments/${reviewCommentId}/reactions`;
break;
}

case "discussion":
case "discussion": {
const discussionNumber = context.payload?.discussion?.number;
if (!discussionNumber) {
core.setFailed(`${ERR_NOT_FOUND}: Discussion number not found in event payload`);
return;
}
// Discussions use GraphQL API - get the node ID
const discussion = await getDiscussionId(owner, repo, discussionNumber);
await addDiscussionReaction(discussion.id, reaction);
const discussionNodeId = await getDiscussionNodeId(owner, repo, discussionNumber);
await addDiscussionReaction(discussionNodeId, reaction);
return; // Early return for discussion events
}

case "discussion_comment":
case "discussion_comment": {
const commentNodeId = context.payload?.comment?.node_id;
if (!commentNodeId) {
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(`${ERR_VALIDATION}: Unsupported event type: ${eventName}`);
Expand Down Expand Up @@ -181,16 +187,15 @@ async function addDiscussionReaction(subjectId, reaction) {
* @param {string} owner - Repository owner
* @param {string} repo - Repository name
* @param {number} discussionNumber - Discussion number
* @returns {Promise<{id: string, url: string}>} Discussion details
* @returns {Promise<string>} Discussion node ID
*/
async function getDiscussionId(owner, repo, discussionNumber) {
async function getDiscussionNodeId(owner, repo, discussionNumber) {
const { repository } = await github.graphql(
`
query($owner: String!, $repo: String!, $num: Int!) {
repository(owner: $owner, name: $repo) {
discussion(number: $num) {
id
url
}
}
}`,
Expand All @@ -201,10 +206,7 @@ async function getDiscussionId(owner, repo, discussionNumber) {
throw new Error(`${ERR_NOT_FOUND}: Discussion #${discussionNumber} not found in ${owner}/${repo}`);
}

return {
id: repository.discussion.id,
url: repository.discussion.url,
};
return repository.discussion.id;
}

module.exports = { main };
Loading