Skip to content

[jsweep] jsweep: Clean add_comment.cjs#24020

Merged
pelikhan merged 2 commits intomainfrom
jsweep/clean-add-comment-bdee5831fb080319
Apr 2, 2026
Merged

[jsweep] jsweep: Clean add_comment.cjs#24020
pelikhan merged 2 commits intomainfrom
jsweep/clean-add-comment-bdee5831fb080319

Conversation

@github-actions
Copy link
Copy Markdown
Contributor

@github-actions github-actions bot commented Apr 2, 2026

Summary

Cleaned actions/setup/js/add_comment.cjs — a github-script context handler for adding comments to issues, PRs, and discussions.

Changes

add_comment.cjs

Extracted shared helper matchesWorkflowId(body, workflowId)

  • Both findCommentsWithTrackerId and findDiscussionCommentsWithTrackerId duplicated the same multi-line filter predicate checking for workflow ID markers in HTML comments
  • Extracted into a single documented helper, reducing ~10 duplicate lines to one .filter(comment => matchesWorkflowId(comment.body, workflowId)) call each

Removed unused GraphQL fields

  • The commentOnDiscussion function fetched body and createdAt in both mutation responses, but only id and url are ever used
  • Removed these fields from both mutation strings

Simplified variables construction

  • Changed replyToId ? { dId, body, replyToId } : { dId, body } to { dId, body, ...(replyToId ? { replyToId } : {}) } — single expression, cleaner

Simplified resolvedTemporaryIds merge

  • Changed guarded if (resolvedTemporaryIds) { for...of } to for...of Object.entries(resolvedTemporaryIds ?? {}) — uses nullish coalescing

add_comment.test.cjs

Added 6 new tests (59 → 65 tests) covering previously untested paths:

Test Coverage
Staged mode via env var GH_AW_SAFE_OUTPUTS_STAGED isStagedMode() env path
Staged mode via config.staged: true for discussions isStagedMode() config path
Max count enforcement (max: 2 skips 3rd comment) maxCount guard
Footer disabled (footer: false) — only XML marker, no attribution text includeFooter false branch
Hide skipped when no GH_AW_WORKFLOW_ID is set workflowId empty guard
Hide skipped when hide_older_comments is false (default) Default config behavior

Validation ✅

  • Formatting: npm run format:cjs
  • Linting: npm run lint:cjs
  • Type checking: npm run typecheck
  • Tests: npm run test:js -- add_comment.test.cjs → 65 passed ✓

Generated by jsweep - JavaScript Unbloater ·

  • expires on Apr 4, 2026, 4:51 AM UTC

- Extract matchesWorkflowId() helper to eliminate duplicate comment filter logic
  shared between findCommentsWithTrackerId and findDiscussionCommentsWithTrackerId
- Remove unused body and createdAt fields from GraphQL discussion comment mutations
- Simplify variables construction with spread operator in commentOnDiscussion
- Simplify resolvedTemporaryIds merge using nullish coalescing (??)
- Add 6 new tests covering staged mode, max count, footer disabled, and
  hide-older-comments edge cases (no workflow ID, disabled by default)

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@pelikhan pelikhan marked this pull request as ready for review April 2, 2026 04:52
Copilot AI review requested due to automatic review settings April 2, 2026 04:52
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Refactors the add_comment safe-output handler to remove duplicated workflow-ID matching logic, streamline discussion comment GraphQL mutations, and add test coverage for previously untested branches.

Changes:

  • Extracted a shared matchesWorkflowId(body, workflowId) helper used by both issue/PR and discussion comment filtering.
  • Simplified commentOnDiscussion GraphQL mutation payloads (removed unused fields) and cleaned up variable construction / temp-id merge logic.
  • Added new tests covering staged mode, max-count enforcement, footer disabling, and hide-older-comments skip conditions.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

File Description
actions/setup/js/add_comment.cjs Refactors workflow-id matching + trims unused GraphQL fields and simplifies a few expressions.
actions/setup/js/add_comment.test.cjs Adds coverage for staged mode/config paths and additional config guards (max/footer/hide).
Comments suppressed due to low confidence (2)

actions/setup/js/add_comment.test.cjs:1939

  • This test sets process.env.GH_AW_WORKFLOW_NAME and later deletes it. For isolation (and to avoid breaking runs where this env var is already set), restore the previous value in a finally block rather than unconditional delete.
      process.env.GH_AW_WORKFLOW_NAME = "No-Footer Workflow";

      let capturedBody = null;
      mockGithub.rest.issues.createComment = async params => {
        capturedBody = params.body;
        return { data: { id: 1, html_url: "https://github.com/owner/repo/issues/8535#issuecomment-1" } };
      };

actions/setup/js/add_comment.test.cjs:1964

  • These tests mutate process.env.GH_AW_WORKFLOW_ID via delete/assignment but don’t restore any pre-existing value, and cleanup won’t run if an assertion throws. Please save the original value and restore it in finally to keep the suite hermetic.
      delete process.env.GH_AW_WORKFLOW_ID;

      let listCommentsCalled = false;
      mockGithub.rest.issues.listComments = async () => {
        listCommentsCalled = true;
        return { data: [] };
      };

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +1861 to +1880
process.env.GH_AW_SAFE_OUTPUTS_STAGED = "true";

let createCommentCalled = false;
mockGithub.rest.issues.createComment = async () => {
createCommentCalled = true;
return { data: { id: 1, html_url: "https://github.com/owner/repo/issues/8535#issuecomment-1" } };
};

const handler = await eval(`(async () => { ${addCommentScript}; return await main({}); })()`);

const message = { type: "add_comment", body: "Staged comment preview" };
const result = await handler(message, {});

expect(result.success).toBe(true);
expect(result.staged).toBe(true);
expect(result.previewInfo).toBeDefined();
expect(result.previewInfo.itemNumber).toBe(8535);
expect(createCommentCalled).toBe(false);

delete process.env.GH_AW_SAFE_OUTPUTS_STAGED;
Copy link

Copilot AI Apr 2, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test mutates process.env.GH_AW_SAFE_OUTPUTS_STAGED but only deletes it at the end; that can leak state if the variable was already set, and it won’t run if the test fails mid-way. Please snapshot the original value and restore it in a try/finally (see actions/setup/js/add_reviewer.test.cjs:247-269 for the established pattern).

This issue also appears in the following locations of the same file:

  • line 1933
  • line 1958
Suggested change
process.env.GH_AW_SAFE_OUTPUTS_STAGED = "true";
let createCommentCalled = false;
mockGithub.rest.issues.createComment = async () => {
createCommentCalled = true;
return { data: { id: 1, html_url: "https://github.com/owner/repo/issues/8535#issuecomment-1" } };
};
const handler = await eval(`(async () => { ${addCommentScript}; return await main({}); })()`);
const message = { type: "add_comment", body: "Staged comment preview" };
const result = await handler(message, {});
expect(result.success).toBe(true);
expect(result.staged).toBe(true);
expect(result.previewInfo).toBeDefined();
expect(result.previewInfo.itemNumber).toBe(8535);
expect(createCommentCalled).toBe(false);
delete process.env.GH_AW_SAFE_OUTPUTS_STAGED;
const originalStaged = process.env.GH_AW_SAFE_OUTPUTS_STAGED;
process.env.GH_AW_SAFE_OUTPUTS_STAGED = "true";
try {
let createCommentCalled = false;
mockGithub.rest.issues.createComment = async () => {
createCommentCalled = true;
return { data: { id: 1, html_url: "https://github.com/owner/repo/issues/8535#issuecomment-1" } };
};
const handler = await eval(`(async () => { ${addCommentScript}; return await main({}); })()`);
const message = { type: "add_comment", body: "Staged comment preview" };
const result = await handler(message, {});
expect(result.success).toBe(true);
expect(result.staged).toBe(true);
expect(result.previewInfo).toBeDefined();
expect(result.previewInfo.itemNumber).toBe(8535);
expect(createCommentCalled).toBe(false);
} finally {
if (originalStaged === undefined) {
delete process.env.GH_AW_SAFE_OUTPUTS_STAGED;
} else {
process.env.GH_AW_SAFE_OUTPUTS_STAGED = originalStaged;
}
}

Copilot uses AI. Check for mistakes.
Copy link
Copy Markdown
Collaborator

@pelikhan pelikhan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@copilot review comments

* @param {string} workflowId - Workflow ID to match
* @returns {boolean}
*/
function matchesWorkflowId(body, workflowId) {
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@copilot move helper to footer helper file with other generated footer related functions. Look for duplicate

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moved matchesWorkflowId into generate_footer.cjs alongside the related generateWorkflowIdMarker and getWorkflowIdMarkerContent functions, and updated add_comment.cjs to import it from there (35a4f6f).

…d add try/finally for env var restoration

Agent-Logs-Url: https://github.com/github/gh-aw/sessions/504727d8-8965-469b-a4d2-21a060b97cc5

Co-authored-by: pelikhan <4175913+pelikhan@users.noreply.github.com>
Copy link
Copy Markdown
Contributor

Copilot AI commented Apr 2, 2026

@copilot review comments

Addressed all review comments in 35a4f6f: wrapped all 4 env-var–mutating tests in try/finally with snapshot/restore, and moved matchesWorkflowId to generate_footer.cjs.

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/repos/github/gh-aw/contents/.github%2Fworkflows%2Faudit-workflows.md
    • Triggering command: /opt/hostedtoolcache/node/24.14.0/x64/bin/node /opt/hostedtoolcache/node/24.14.0/x64/bin/node --experimental-import-meta-resolve --require /home/REDACTED/work/gh-aw/gh-aw/actions/setup/js/node_modules/vitest/suppress-warnings.cjs --conditions node --conditions development /home/REDACTED/work/gh-aw/gh-aw/actions/setup/js/node_modules/vitest/dist/workers/forks.js (http block)
    • Triggering command: /opt/hostedtoolcache/node/24.14.0/x64/bin/node /opt/hostedtoolcache/node/24.14.0/x64/bin/node --experimental-import-meta-resolve --require /home/REDACTED/work/gh-aw/gh-aw/actions/setup/js/node_modules/vitest/suppress-warnings.cjs --conditions node --conditions development /home/REDACTED/work/gh-aw/gh-aw/actions/setup/js/node_modules/vitest/dist/workers/forks.js 55e3bddb0e73128brun 55e3bddb0e73128b/tmp/go-handler-test-alGpwY/test-cwd.go ode_modules/vitest/dist/workers/forks.js /hom�� 1dc3ed6b881fffe1122073e6..HEAD .cfg 64/pkg/tool/linux_amd64/vet * origin k/gh-aw/node_modnpm run typecheck &amp;&amp; vitest run --no-file-parallelism --no-file-parallelism 64/pkg/tool/linux_amd64/vet (http block)
    • Triggering command: /opt/hostedtoolcache/node/24.14.0/x64/bin/node /opt/hostedtoolcache/node/24.14.0/x64/bin/node --experimental-import-meta-resolve --require /home/REDACTED/work/gh-aw/gh-aw/actions/setup/js/node_modules/vitest/suppress-warnings.cjs --conditions node --conditions development /home/REDACTED/work/gh-aw/gh-aw/actions/setup/js/node_modules/vitest/dist/workers/forks.js main 64/bin/git git chec�� 0a65618b..HEAD st/suppress-warn--conditions ode_modules/.bindevelopment HEAD^ ere k/gh-aw/gh-aw/acnpm run typecheck &amp;&amp; vitest run --no-file-parallelism --no-file-parallelism st/dist/workers/forks.js (http block)
  • invalid.example.invalid
    • Triggering command: /usr/lib/git-core/git-remote-https /usr/lib/git-core/git-remote-https origin https://invalid.example.invalid/nonexistent-repo.git git conf�� user.name lure tions/setup/js/node_modules/.bin/git -M main /usr/sbin/git git init�� --bare --initial-branch=main k/gh-aw/gh-aw/actions/setup/js/node_modules/.bin/git &#39;/tmp/bare-incregit &#39;/tmp/bare-increadd cal/bin/git git (dns block)

If you need me to access, download, or install something from one of these locations, you can either:

Copilot AI requested a review from pelikhan April 2, 2026 05:19
@pelikhan pelikhan merged commit 1043bcd into main Apr 2, 2026
56 checks passed
@pelikhan pelikhan deleted the jsweep/clean-add-comment-bdee5831fb080319 branch April 2, 2026 05:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants