Skip to content

Refactor duplicated reaction setup into shared helper#43483

Open
pelikhan with Copilot wants to merge 5 commits into
mainfrom
copilot/duplicate-code-reaction-validation-dispatch
Open

Refactor duplicated reaction setup into shared helper#43483
pelikhan with Copilot wants to merge 5 commits into
mainfrom
copilot/duplicate-code-reaction-validation-dispatch

Conversation

Copilot AI commented Jul 5, 2026

Copy link
Copy Markdown
Contributor

The reaction-only and reaction-plus-comment action scripts duplicated the same setup path (read GH_AW_REACTION, validate allowed reactions, resolve invocation context), creating drift risk between entrypoints. This change consolidates that setup into one helper and keeps each entrypoint focused on its distinct side effects.

  • Problem summary

    • Both add_reaction.cjs and add_reaction_and_edit_comment.cjs implemented identical reaction initialization/validation/context-resolution logic.
    • Duplication increased maintenance risk when reaction rules or invocation-context behavior change.
  • Refactor: shared reaction setup

    • Added actions/setup/js/reaction_setup_helpers.cjs:
      • exports VALID_REACTIONS
      • exports resolveReactionSetup(context) returning { reaction, invocationContext } or null after core.setFailed(...)
    • Centralizes:
      • default reaction fallback (eyes)
      • reaction allowlist validation
      • invocation context resolution
  • Entrypoint updates

    • actions/setup/js/add_reaction.cjs
      • replaced inline setup/validation/context code with resolveReactionSetup(context).
    • actions/setup/js/add_reaction_and_edit_comment.cjs
      • removed duplicated setup block and VALID_REACTIONS constant
      • now imports shared setup + allowlist from reaction_setup_helpers.cjs.
  • Targeted coverage

    • Added actions/setup/js/reaction_setup_helpers.test.cjs to cover:
      • default reaction behavior
      • environment override behavior
      • invalid reaction failure path
// shared setup used by both entrypoints
const setup = resolveReactionSetup(context);
if (!setup) return;

const { reaction, invocationContext } = setup;

Copilot AI and others added 2 commits July 5, 2026 06:43
Co-authored-by: pelikhan <4175913+pelikhan@users.noreply.github.com>
Co-authored-by: pelikhan <4175913+pelikhan@users.noreply.github.com>
Copilot AI changed the title [WIP] Refactor duplicate reaction validation and dispatch code Refactor duplicated reaction setup into shared helper Jul 5, 2026
Copilot AI requested a review from pelikhan July 5, 2026 06:46
@pelikhan pelikhan marked this pull request as ready for review July 5, 2026 06:47
Copilot AI review requested due to automatic review settings July 5, 2026 06:47

Copilot AI left a comment

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.

Pull request overview

This PR refactors duplicated reaction initialization/validation and invocation-context resolution logic used by the setup action scripts into a shared helper, reducing drift risk between the reaction entrypoints.

Changes:

  • Added a new reaction_setup_helpers.cjs module that centralizes the reaction allowlist and the “reaction + invocation context” setup logic.
  • Updated both reaction entrypoints (add_reaction.cjs and add_reaction_and_edit_comment.cjs) to use the shared setup helper instead of duplicating setup/validation logic.
  • Added targeted unit tests for the new helper covering default reaction, env override, and invalid reaction handling.
Show a summary per file
File Description
actions/setup/js/reaction_setup_helpers.cjs Introduces shared reaction setup/validation + invocation-context resolution helper.
actions/setup/js/add_reaction.cjs Refactors reaction-only entrypoint to use the shared setup helper.
actions/setup/js/add_reaction_and_edit_comment.cjs Refactors reaction+comment entrypoint to use the shared setup helper and import the shared allowlist.
actions/setup/js/reaction_setup_helpers.test.cjs Adds unit coverage for shared helper behavior (default, override, invalid).

Review details

Tip

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

  • Files reviewed: 4/4 changed files
  • Comments generated: 2
  • Review effort level: Low

Comment on lines +15 to +23
function resolveReactionSetup(rawContext) {
const reaction = process.env.GH_AW_REACTION || "eyes";
const invocationContext = resolveInvocationContext(rawContext);
if (!VALID_REACTIONS.includes(reaction)) {
core.setFailed(`${ERR_VALIDATION}: Invalid reaction type: ${reaction}. Valid reactions are: ${VALID_REACTIONS.join(", ")}`);
return null;
}
return { reaction, invocationContext };
}
Comment on lines +47 to +60
it("fails and returns null for invalid reaction", async () => {
process.env.GH_AW_REACTION = "invalid";
const { resolveReactionSetup } = await importHelpers();

const result = resolveReactionSetup({
eventName: "issues",
repo: { owner: "testowner", repo: "testrepo" },
payload: { issue: { number: 123 } },
});

expect(result).toBeNull();
expect(mockCore.setFailed).toHaveBeenCalledWith(expect.stringContaining("Invalid reaction type: invalid"));
});
});
@pelikhan

pelikhan commented Jul 5, 2026

Copy link
Copy Markdown
Collaborator

@copilot invoke pr-finisher skill

@gh-aw-bot

Copy link
Copy Markdown
Collaborator

@copilot please run the pr-finisher skill, review the existing PR feedback, make any needed follow-up changes, and rerun checks if code changes are required.

Generated by 👨‍🍳 PR Sous Chef · 12.3 AIC · ⌖ 5.1 AIC · ⊞ 3.5K ·
Comment /souschef to run again

…ION_MAP sync test

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

Copilot AI commented Jul 5, 2026

Copy link
Copy Markdown
Contributor Author

@copilot invoke pr-finisher skill

Addressed both reviewer threads in commit fix: validate reaction before resolving invocation context; add REACTION_MAP sync test:

  1. Order fix (reaction_setup_helpers.cjs): moved reaction validation before resolveInvocationContext so an invalid GH_AW_REACTION produces core.setFailed(...) rather than a potential uncaught exception from context parsing.

  2. Sync test (reaction_setup_helpers.test.cjs): added a test that imports REACTION_MAP from add_reaction.cjs and asserts [...VALID_REACTIONS].sort() equals Object.keys(REACTION_MAP).sort(), catching any future drift between the two.

All 4 unit tests pass locally.

@github-actions

github-actions Bot commented Jul 5, 2026

Copy link
Copy Markdown
Contributor

🤖 PR Triage — Run #7

Field Value
Category refactor
Risk 🟢 low
Score 40 / 100
Action 📋 batch_review
Batch pr-batch:js-refactor

Score breakdown: impact 16 + urgency 12 + quality 12

Extracts shared resolveReactionSetup helper from add_reaction.cjs and add_reaction_and_edit_comment.cjs. Adds targeted test coverage. 4 files, +97/-22. Review alongside #43480 as part of the JS refactor batch.

Generated by 🔧 PR Triage Agent · 56.3 AIC · ⌖ 8.84 AIC · ⊞ 5.5K ·

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.

[duplicate-code] Duplicate Code: Reaction Validation and Dispatch

4 participants