From 6bc7c079df729ea30d89369c16de624dc8ec86d0 Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" <41898282+github-actions[bot]@users.noreply.github.com> Date: Wed, 1 Jul 2026 05:00:07 +0000 Subject: [PATCH] simplify: reduce duplication in discussion comment handlers - add_workflow_run_comment.cjs parseObject: eliminate nesting and dead return path by collapsing JSON-parse branch to a ternary - add_workflow_run_comment.cjs postDiscussionComment: remove the let-result intermediate; select mutation and variables via ternary (consistent with add_comment.cjs commentOnDiscussion pattern) - add_reaction_and_edit_comment.cjs addCommentWithWorkflowLink: merge the near-identical discussion / discussion_comment branches into one, deriving replyToId conditionally and choosing the mutation accordingly Behavior is unchanged; no interface modifications. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- .../js/add_reaction_and_edit_comment.cjs | 57 ++++++------------- actions/setup/js/add_workflow_run_comment.cjs | 38 +++---------- 2 files changed, 26 insertions(+), 69 deletions(-) diff --git a/actions/setup/js/add_reaction_and_edit_comment.cjs b/actions/setup/js/add_reaction_and_edit_comment.cjs index ff6738d35bd..eebbf137156 100644 --- a/actions/setup/js/add_reaction_and_edit_comment.cjs +++ b/actions/setup/js/add_reaction_and_edit_comment.cjs @@ -274,50 +274,27 @@ async function addCommentWithWorkflowLink(endpoint, runUrl, eventName, invocatio ]; const commentBody = commentParts.join("\n\n"); - if (eventName === "discussion") { - // Parse discussion number from special format: "discussion:NUMBER" + if (eventName === "discussion" || eventName === "discussion_comment") { + // Parse discussion number from special format: "discussion:NUMBER" or "discussion_comment:NUMBER:COMMENT_ID" const discussionNumber = parseInt(endpoint.split(":")[1], 10); const { id: discussionId } = await getDiscussionId(eventRepo.owner, eventRepo.repo, discussionNumber); - - const result = await github.graphql( - ` - mutation($dId: ID!, $body: String!) { - addDiscussionComment(input: { discussionId: $dId, body: $body }) { - comment { - id - url + // For discussion_comment events, thread the reply under the triggering comment. + // GitHub Discussions only supports two nesting levels, so resolve the top-level parent node ID. + const replyToId = eventName === "discussion_comment" + ? await resolveTopLevelDiscussionCommentId(github, eventPayload?.comment?.node_id) + : null; + const mutation = replyToId + ? `mutation($dId: ID!, $body: String!, $replyToId: ID!) { + addDiscussionComment(input: { discussionId: $dId, body: $body, replyToId: $replyToId }) { + comment { id url } } - } - }`, - { dId: discussionId, body: commentBody } - ); - - const comment = result.addDiscussionComment.comment; - setCommentOutputs(comment.id, comment.url, eventRepo); - return; - } else if (eventName === "discussion_comment") { - // Parse discussion number from special format: "discussion_comment:NUMBER:COMMENT_ID" - const discussionNumber = parseInt(endpoint.split(":")[1], 10); - const { id: discussionId } = await getDiscussionId(eventRepo.owner, eventRepo.repo, discussionNumber); - - // Get the comment node ID to use as the parent for threading. - // GitHub Discussions only supports two nesting levels, so if the triggering comment is - // itself a reply, we resolve the top-level parent's node ID. - const commentNodeId = await resolveTopLevelDiscussionCommentId(github, eventPayload?.comment?.node_id); - - const result = await github.graphql( - ` - mutation($dId: ID!, $body: String!, $replyToId: ID!) { - addDiscussionComment(input: { discussionId: $dId, body: $body, replyToId: $replyToId }) { - comment { - id - url + }` + : `mutation($dId: ID!, $body: String!) { + addDiscussionComment(input: { discussionId: $dId, body: $body }) { + comment { id url } } - } - }`, - { dId: discussionId, body: commentBody, replyToId: commentNodeId } - ); - + }`; + const result = await github.graphql(mutation, { dId: discussionId, body: commentBody, ...(replyToId ? { replyToId } : {}) }); const comment = result.addDiscussionComment.comment; setCommentOutputs(comment.id, comment.url, eventRepo); return; diff --git a/actions/setup/js/add_workflow_run_comment.cjs b/actions/setup/js/add_workflow_run_comment.cjs index b3a29c9a349..f22cf860f0a 100644 --- a/actions/setup/js/add_workflow_run_comment.cjs +++ b/actions/setup/js/add_workflow_run_comment.cjs @@ -84,23 +84,16 @@ function setCommentOutputs(commentId, commentUrl, eventRepo = context.repo, opti * @returns {Record|null} */ function parseObject(value) { - if (!value) { - return null; - } + if (!value) return null; if (typeof value === "string") { const trimmed = value.trim(); - if (!trimmed) { - return null; - } + if (!trimmed) return null; try { const parsed = JSON.parse(trimmed); - if (parsed && typeof parsed === "object" && !Array.isArray(parsed)) { - return parsed; - } + return parsed && typeof parsed === "object" && !Array.isArray(parsed) ? parsed : null; } catch { return null; } - return null; } if (typeof value === "object" && !Array.isArray(value)) { return /** @type {Record} */ value; @@ -396,31 +389,18 @@ function buildCommentBody(eventName, runUrl, workflowNameOverride) { */ async function postDiscussionComment(discussionNumber, commentBody, replyToNodeId = null, eventRepo = context.repo) { const discussionId = await getDiscussionNodeId(discussionNumber, eventRepo); - - /** @type {any} */ - let result; - if (replyToNodeId) { - result = await github.graphql( - ` - mutation($dId: ID!, $body: String!, $replyToId: ID!) { + const mutation = replyToNodeId + ? `mutation($dId: ID!, $body: String!, $replyToId: ID!) { addDiscussionComment(input: { discussionId: $dId, body: $body, replyToId: $replyToId }) { comment { id url } } - }`, - { dId: discussionId, body: commentBody, replyToId: replyToNodeId } - ); - } else { - result = await github.graphql( - ` - mutation($dId: ID!, $body: String!) { + }` + : `mutation($dId: ID!, $body: String!) { addDiscussionComment(input: { discussionId: $dId, body: $body }) { comment { id url } } - }`, - { dId: discussionId, body: commentBody } - ); - } - + }`; + const result = await github.graphql(mutation, { dId: discussionId, body: commentBody, ...(replyToNodeId ? { replyToId: replyToNodeId } : {}) }); const comment = result.addDiscussionComment.comment; return setCommentOutputs(comment.id, comment.url, eventRepo); }