Clean up add_workflow_run_comment.cjs: eliminate duplication, add tests#16738
Clean up add_workflow_run_comment.cjs: eliminate duplication, add tests#16738
Conversation
Co-authored-by: pelikhan <4175913+pelikhan@users.noreply.github.com>
There was a problem hiding this comment.
Pull request overview
Refactors add_workflow_run_comment.cjs to reduce duplication and adds Vitest coverage for workflow-run comment creation across supported GitHub event types.
Changes:
- Extracted helpers (
getDiscussionNodeId,setCommentOutputs) and simplified event type description selection viaEVENT_TYPE_DESCRIPTIONS. - Added block-scoped switch
casebodies to avoidno-case-declarationsissues. - Added a new
add_workflow_run_comment.test.cjswith coverage for main flows, error paths, and feature flags; exportedaddCommentWithWorkflowLinkto enable direct testing.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
| actions/setup/js/add_workflow_run_comment.cjs | Refactors event handling and comment creation (REST + GraphQL) with shared helpers and exported function for testing. |
| actions/setup/js/add_workflow_run_comment.test.cjs | Adds Vitest coverage for supported event types, failures, and comment body markers/outputs. |
Comments suppressed due to low confidence (2)
actions/setup/js/add_workflow_run_comment.cjs:236
commentNodeIdcan beundefinedfordiscussion_commentpayloads, but it’s passed directly asreplyToIdto the GraphQL mutation. Add an explicit check and produce a clear failure (and add a test for the missing-node_idcase) so this doesn’t fail later with a GraphQL validation error.
This issue also appears on line 130 of the same file.
// Get the comment node ID to use as the parent for threading
const commentNodeId = context.payload?.comment?.node_id;
actions/setup/js/add_workflow_run_comment.cjs:134
- For
discussion_commentevents,addCommentWithWorkflowLinkrelies oncontext.payload.comment.node_idas thereplyToId, but this switch case only validatescomment.id. Ifnode_idis missing, the GraphQL mutation will be invoked with an invalidreplyToIdand will fail later. Please validatecontext.payload.comment.node_idhere (or pass it through the endpoint) and fail early with a specific message when it’s missing.
const discussionCommentNumber = context.payload?.discussion?.number;
const discussionCommentId = context.payload?.comment?.id;
if (!discussionCommentNumber || !discussionCommentId) {
core.setFailed("Discussion or comment information not found in event payload");
return;
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| } | ||
| }`, | ||
| { owner: context.repo.owner, repo: context.repo.repo, num: discussionNumber } | ||
| ); |
There was a problem hiding this comment.
getDiscussionNodeId assumes repository.discussion is always present. If discussions are disabled or the discussion number is invalid, this will throw a generic TypeError when accessing .id, making failures hard to diagnose. Please guard against a missing discussion and throw/return a clearer error (e.g., include owner/repo and discussion number) before accessing .id.
| ); | |
| ); | |
| if (!repository || !repository.discussion) { | |
| throw new Error( | |
| `Could not find discussion #${discussionNumber} in ${context.repo.owner}/${context.repo.repo}. ` + | |
| `Discussions may be disabled for this repository or the discussion number may be invalid.` | |
| ); | |
| } |
Refactors
add_workflow_run_comment.cjsto eliminate code duplication and adds comprehensive test coverage.Changes
Eliminated duplication:
getDiscussionNodeId()helper - GraphQL query was duplicated indiscussionanddiscussion_commentcasessetCommentOutputs()helper - output setting logic was duplicated 3 times across event handlersEVENT_TYPE_DESCRIPTIONSlookup object + optional chainingCode quality:
no-case-declarations)addCommentWithWorkflowLinkto enable testingTest coverage:
Example
Before:
After:
Warning
Firewall rules blocked me from connecting to one or more addresses (expand for details)
I tried to connect to the following addresses, but was blocked by firewall rules:
https://api.github.com/graphql/usr/bin/gh gh run download 22166965257 -n agent-artifacts -D /tmp/agent-artifacts(http block)/usr/bin/gh /usr/bin/gh api graphql -f query=query($owner: String!, $name: String!) { repository(owner: $owner, name: $name) { hasDiscussionsEnabled } } -f owner=github -f name=gh-aw --check 64/bin/go git conf�� --get remote.origin.url(http block)https://api.github.com/repos/actions/checkout/git/ref/tags/v3/usr/bin/gh gh api /repos/actions/checkout/git/ref/tags/v3 --jq .object.sha -json GO111MODULE ache/go/1.25.0/x64/pkg/tool/linux_amd64/vet GOINSECURE GOMOD GOMODCACHE ache/go/1.25.0/x64/pkg/tool/linux_amd64/vet(http block)https://api.github.com/repos/actions/checkout/git/ref/tags/v4/usr/bin/gh gh api /repos/actions/checkout/git/ref/tags/v4 --jq .object.sha blog-auditor.md GO111MODULE ache/go/1.25.0/x64/pkg/tool/linux_amd64/link GOINSECURE GOMOD GOMODCACHE ache/go/1.25.0/x64/pkg/tool/linux_amd64/link -uns�� 3036063/b404/sliceutil.test /tmp/go-build3223036063/b288/vet.cfg 3036063/b404/importcfg.link GOSUMDB GOWORK 64/bin/go -JPGyIl2vB7_F/G9sEDD2pLhLMDAgEAiXu/X6R5OoPF4X40d7UERB47/kI2eOA--JPGyIl2vB7_F(http block)/usr/bin/gh gh api /repos/actions/checkout/git/ref/tags/v4 --jq .object.sha brave.md k/gh-aw/gh-aw/pkg/cli/fileutil/fmain /opt/hostedtoolcache/go/1.25.0/x-lang=go1.25 GOINSECURE GOMOD GOMODCACHE /opt/hostedtoolcache/go/1.25.0/x-dwarf=false -o runs/20260219-040212-30077/test-go1.25.0 -trimpath 3036063/b324/vet.cfg l main -lang=go1.25 /opt/hostedtoolcache/go/1.25.0/x/tmp/go-build3223036063/b421/_testmain.go(http block)/usr/bin/gh gh api /repos/actions/checkout/git/ref/tags/v4 --jq .object.sha /tmp/TestHashConsistency_GoAndJavaScript1091784387/001/test-complex-frontmatter-@{u} resolved$ /usr/bin/git -json GO111MODULE 64/bin/go git init�� GOMODCACHE go /usr/bin/git -json GO111MODULE x_amd64/vet git(http block)https://api.github.com/repos/actions/checkout/git/ref/tags/v5/usr/bin/gh gh api /repos/actions/checkout/git/ref/tags/v5 --jq .object.sha -json GO111MODULE 64/pkg/tool/linu-nolocalimports GOINSECURE GOMOD GOMODCACHE 64/pkg/tool/linu/tmp/go-build3223036063/b412/_testmain.go env -json GO111MODULE 64/pkg/tool/linux_amd64/vet GOINSECURE GOMOD GOMODCACHE 64/pkg/tool/linuREDACTED(http block)/usr/bin/gh gh api /repos/actions/checkout/git/ref/tags/v5 --jq .object.sha ithub/workflows/archie.md g/timeutil/format_test.go ipts.test -errorsas -ifaceassert -nilfunc ipts.test 2230�� xterm-color -tests /usr/bin/git ECai/9kS0RR28kB5git GO111MODULE 64/bin/go git(http block)/usr/bin/gh gh api /repos/actions/checkout/git/ref/tags/v5 --jq .object.sha /tmp/gh-aw-test-runs/20260219-040212-30077/test-1840838544 rev-parse /usr/bin/git @{u} GO111MODULE x_amd64/vet git rev-�� --show-toplevel x_amd64/vet /usr/bin/git -json GO111MODULE x_amd64/vet git(http block)https://api.github.com/repos/actions/github-script/git/ref/tags/v8/usr/bin/gh gh api /repos/actions/github-script/git/ref/tags/v8 --jq .object.sha KJnW/z2r0LREfQX7GOSUMDB GO111MODULE 64/bin/go GOINSECURE GOMOD GOMODCACHE 9152170/b401/imp/tmp/go-build3223036063/b114/vet.cfg /hom�� che/go-build/b1/b136a0101c461533GOINSECURE **/*.cjs 64/bin/go **/*.json --ignore-path run-script/lib/n-bool /opt/hostedtoolc-buildtags(http block)/usr/bin/gh gh api /repos/actions/github-script/git/ref/tags/v8 --jq .object.sha "prettier" --check '**/*.cjs' '*GOINSECURE GOPROXY 64/bin/go GOSUMDB GOWORK 64/bin/go /opt/hostedtoolc/tmp/go-build3223036063/b257/vet.cfg -o /tmp/go-build3199152170/b345/_pkGOINSECURE -trimpath 64/bin/go -p main -lang=go1.25 go(http block)/usr/bin/gh gh api /repos/actions/github-script/git/ref/tags/v8 --jq .object.sha pkg/mod/github.c-errorsas pkg/mod/github.c-ifaceassert 64/bin/go GOSUMDB GOWORK 64/bin/go /opt/hostedtoolc-buildtags -o /tmp/go-build319-errorsas -trimpath 64/bin/go -p testing/internalrev-parse -lang=go1.25 go(http block)https://api.github.com/repos/actions/setup-go/git/ref/tags/v4/usr/bin/gh gh api /repos/actions/setup-go/git/ref/tags/v4 --jq .object.sha vaScript1091784387/001/test-simple-frontmatter.md GO111MODULE Name,createdAt,startedAt,updatedAt,event,headBranch,headSha,displayTitle GOINSECURE GOMOD GOMODCACHE /opt/hostedtoolcache/go/1.25.0/x64/pkg/tool/linux_amd64/vet -ato�� -bool -buildtags 3036063/b338/vet.cfg -errorsas -ifaceassert -nilfunc /opt/hostedtoolcache/go/1.25.0/x64/pkg/tool/linux_amd64/vet(http block)https://api.github.com/repos/actions/setup-node/git/ref/tags/v4/usr/bin/gh gh api /repos/actions/setup-node/git/ref/tags/v4 --jq .object.sha bot-detection.md GO111MODULE 3036063/b176/vet.cfg GOINSECURE GOMOD GOMODCACHE /opt/hostedtoolcache/go/1.25.0/x64/pkg/tool/linu/tmp/go-build3223036063/b108/vet.cfg -ato�� -bool -buildtags 3036063/b334/vet.cfg -errorsas -ifaceassert -nilfunc /opt/hostedtoolcache/go/1.25.0/x64/pkg/tool/linux_amd64/vet(http block)https://api.github.com/repos/github/gh-aw/actions/runs/1/artifacts/usr/bin/gh gh run download 1 --dir test-logs/run-1 GO111MODULE x_amd64/compile GOINSECURE GOMOD GOMODCACHE x_amd64/compile env -json .cfg 64/pkg/tool/linux_amd64/vet GOINSECURE GOMOD GOMODCACHE 64/pkg/tool/linux_amd64/vet(http block)https://api.github.com/repos/github/gh-aw/actions/runs/12345/artifacts/usr/bin/gh gh run download 12345 --dir test-logs/run-12345 GO111MODULE x_amd64/compile GOINSECURE GOMOD GOMODCACHE x_amd64/compile env -json GO111MODULE 64/pkg/tool/linux_amd64/vet GOINSECURE GOMOD GOMODCACHE 64/pkg/tool/linux_amd64/vet(http block)https://api.github.com/repos/github/gh-aw/actions/runs/12346/artifacts/usr/bin/gh gh run download 12346 --dir test-logs/run-12346 GO111MODULE x_amd64/compile GOINSECURE GOMOD GOMODCACHE x_amd64/compile env -json GO111MODULE 64/pkg/tool/linux_amd64/vet GOINSECURE GOMOD GOMODCACHE 64/pkg/tool/linuorigin(http block)https://api.github.com/repos/github/gh-aw/actions/runs/2/artifacts/usr/bin/gh gh run download 2 --dir test-logs/run-2 GO111MODULE 64/pkg/tool/linux_amd64/vet GOINSECURE GOMOD GOMODCACHE 64/pkg/tool/linu-importcfg env -json .cfg 64/pkg/tool/linu-buildmode=exe GOINSECURE GOMOD GOMODCACHE 64/pkg/tool/linu-extld=gcc(http block)https://api.github.com/repos/github/gh-aw/actions/runs/3/artifacts/usr/bin/gh gh run download 3 --dir test-logs/run-3 GO111MODULE 64/pkg/tool/linux_amd64/vet GOINSECURE GOMOD GOMODCACHE 64/pkg/tool/linux_amd64/vet env -json .cfg 64/pkg/tool/linux_amd64/vet GOINSECURE GOMOD GOMODCACHE 64/pkg/tool/linuremote.origin.url(http block)https://api.github.com/repos/github/gh-aw/actions/runs/4/artifacts/usr/bin/gh gh run download 4 --dir test-logs/run-4 GO111MODULE 64/pkg/tool/linux_amd64/compile GOINSECURE GOMOD GOMODCACHE 64/pkg/tool/linux_amd64/compile env g_.a GO111MODULE 64/pkg/tool/linux_amd64/vet GOINSECURE GOMOD GOMODCACHE 64/pkg/tool/linuremote.origin.url(http block)https://api.github.com/repos/github/gh-aw/actions/runs/5/artifacts/usr/bin/gh gh run download 5 --dir test-logs/run-5 GO111MODULE 64/pkg/tool/linux_amd64/vet GOINSECURE GOMOD GOMODCACHE 64/pkg/tool/linux_amd64/vet env -json .cfg 64/pkg/tool/linux_amd64/vet GOINSECURE GOMOD GOMODCACHE 64/pkg/tool/linux_amd64/vet(http block)https://api.github.com/repos/github/gh-aw/actions/workflows/usr/bin/gh gh workflow list --json name,state,path BbC9/PYNcR6J8iK6GOSUMDB GO111MODULE 64/bin/go GOINSECURE GOMOD GOMODCACHE 9152170/b395/imp-buildtags /hom�� che/go-build/dd/-errorsas **/*.cjs 64/bin/go **/*.json --ignore-path ../../../.pretti-bool /opt/hostedtoolc-buildtags(http block)/usr/bin/gh gh run list --json databaseId,number,url,status,conclusion,workflowName,createdAt,startedAt,updatedAt,event,headBranch,headSha,displayTitle --workflow nonexistent-workflow-12345 --limit 100 GOWORK 64/bin/go go env -json GO111MODULE 64/bin/go GOINSECURE GOMOD GOMODCACHE go(http block)/usr/bin/gh gh run list --json databaseId,number,url,status,conclusion,workflowName,createdAt,startedAt,updatedAt,event,headBranch,headSha,displayTitle --workflow nonexistent-workflow-12345 --limit 6 GOMOD GOMODCACHE x_amd64/link env -json GO111MODULE 64/pkg/tool/linux_amd64/vet GOINSECURE GOMOD GOMODCACHE 2l/hKXTtrccq1d6W0qIfHov/c9W7SApZ--json(http block)https://api.github.com/repos/github/gh-aw/git/ref/tags/v1.0.0/usr/bin/gh gh api /repos/github/gh-aw/git/ref/tags/v1.0.0 --jq .object.sha -json GO111MODULE 64/pkg/tool/linux_amd64/vet GOINSECURE GOMOD GOMODCACHE 64/pkg/tool/linux_amd64/vet env 3403055332/.github/workflows .cfg 64/pkg/tool/linux_amd64/vet GOINSECURE GOMOD GOMODCACHE FyPOPt7/hAqieucyA4npJ9bo6g4b(http block)https://api.github.com/repos/nonexistent/action/git/ref/tags/v999.999.999/usr/bin/gh gh api /repos/nonexistent/action/git/ref/tags/v999.999.999 --jq .object.sha -json GO111MODULE 64/pkg/tool/linux_amd64/vet GOINSECURE GOMOD GOMODCACHE 64/pkg/tool/linux_amd64/vet env -json .cfg 64/pkg/tool/linux_amd64/vet GOINSECURE GOMOD GOMODCACHE 64/pkg/tool/linux_amd64/vet(http block)https://api.github.com/repos/nonexistent/repo/actions/runs/12345/usr/bin/gh gh run view 12345 --repo nonexistent/repo --json status,conclusion GOINSECURE GOMOD GOMODCACHE 64/pkg/tool/linux_amd64/vet env -json .cfg 64/pkg/tool/linux_amd64/vet GOINSECURE GOMOD GOMODCACHE 64/pkg/tool/linux_amd64/vet(http block)https://api.github.com/repos/owner/repo/actions/workflows/usr/bin/gh gh workflow list --json name,state,path --repo owner/repo 64/bin/go GOINSECURE GOMOD GOMODCACHE iptables -w -t security 64/bin/go OUTPUT -d run-script/lib/n-bool go(http block)/usr/bin/gh gh workflow list --json name,state,path --repo owner/repo 64/bin/go GOINSECURE GOMOD GOMODCACHE iptables -w -t security 64/bin/go OUTPUT -d 168.63.129.16 go(http block)https://api.github.com/repos/owner/repo/contents/file.md/tmp/go-build3223036063/b368/cli.test /tmp/go-build3223036063/b368/cli.test -test.testlogfile=/tmp/go-build3223036063/b368/testlog.txt -test.paniconexit0 -test.v=true -test.parallel=4 -test.timeout=10m0s -test.run=^Test -test.short=true GOINSECURE GOMOD GOMODCACHE 9152170/b361/imp/tmp/go-build3223036063/b095/vet.cfg -c pkg/mod/github.com/pmezard/go-diGOINSECURE GOPROXY 64/bin/go GOSUMDB GOWORK 64/bin/go /opt/hostedtoolc/tmp/go-build3223036063/b258/vet.cfg(http block)https://api.github.com/repos/test-owner/test-repo/actions/secrets/usr/bin/gh gh api /repos/test-owner/test-repo/actions/secrets --jq .secrets[].name vMPf/0LMLcOs4Pm5GOSUMDB GO111MODULE 64/bin/go GOINSECURE GOMOD GOMODCACHE 9152170/b387/imp/tmp/go-build3223036063/b159/vet.cfg /hom�� 9152170/b387/embedcfg **/*.cjs 64/bin/go **/*.json --ignore-path ../../../.pretti-bool /opt/hostedtoolc-buildtags(http block)If you need me to access, download, or install something from one of these locations, you can either:
Original prompt
This section details on the original issue you should resolve
<issue_title>[jsweep] Clean add_workflow_run_comment.cjs</issue_title>
<issue_description>## Summary
Cleaned
actions/setup/js/add_workflow_run_comment.cjsand added comprehensive tests.Context type: github-script context
Changes Made
add_workflow_run_comment.cjsBlock-scoped switch cases – Added
{}braces to eachcaseblock inmain()to fix lexical declaration issues withconstinside switch cases (ESLintno-case-declarationsrule).Lookup object for event descriptions – Replaced the 7-case switch statement for
eventTypeDescriptionwith a conciseEVENT_TYPE_DESCRIPTIONSlookup object + optional chaining (??), reducing ~18 lines to 9 lines.Extracted
getDiscussionNodeId()helper – The identical GraphQL query to fetch a discussion node ID was duplicated for bothdiscussionanddiscussion_commentevent cases. Extracted to a shared helper function.Extracted
setCommentOutputs()helper – Thecore.setOutputcalls forcomment-id,comment-url, andcomment-repowere duplicated 3 times. Extracted to a shared helper function.Exported
addCommentWithWorkflowLink– Added tomodule.exportsto enable testing.add_workflow_run_comment.test.cjs(new file)Created comprehensive test coverage with 18 test cases:
main()tests (12 cases):issue_commentevent: creates comment on the issuepull_requestevent: creates comment, fails when PR number missingpull_request_review_commentevent: creates commentdiscussionevent: creates GraphQL comment, fails when number missingdiscussion_commentevent: creates threaded comment withreplyToId, fails when fields missingsetFailedaddCommentWithWorkflowLink()tests (6 cases):workflow-idmarker whenGITHUB_WORKFLOWis settracker-idmarker whenGH_AW_TRACKER_IDis setreactioncomment type markerGH_AW_LOCK_FOR_AGENT=truecomment-id,comment-url,comment-repo)Validation ✅
npm run format:cjs✓npm run lint:cjs✓npm run typecheck✓npm run test:js– 18/18 passed ✓To apply the patch locally:
Show patch preview (500 of 627 lines)