Skip to content

[code-simplifier] refactor(js): simplify add_reaction.cjs payload validation and error handling#48271

Merged
pelikhan merged 3 commits into
mainfrom
code-simplifier/simplify-add-reaction-cjs-dff0fe3463c3c099
Jul 27, 2026
Merged

[code-simplifier] refactor(js): simplify add_reaction.cjs payload validation and error handling#48271
pelikhan merged 3 commits into
mainfrom
code-simplifier/simplify-add-reaction-cjs-dff0fe3463c3c099

Conversation

@github-actions

@github-actions github-actions Bot commented Jul 27, 2026

Copy link
Copy Markdown
Contributor

Summary

Refactors payload validation and error handling in actions/setup/js/add_reaction.cjs for clarity and maintainability. No functional API changes.

Changes

actions/setup/js/add_reaction.cjs

  • Extracted requirePayloadField() helper — centralises null-check validation with consistent error messaging (ERR_NOT_FOUND / ERR_VALIDATION), replacing four identical inline blocks in resolveRestEndpoint().
  • Consolidated handleGraphQLOrUnknownEvent() — restructured to resolve subjectId in a single switch, then call addDiscussionReaction() once, eliminating duplicate try-catch blocks per event type.
  • Restored discussion error handling — error path for discussion events that was inadvertently removed in a prior commit is now re-applied under the unified catch block.

Impact

Area Change
Behaviour None — same logic, same error codes
Error messages Unchanged format
Breaking No

Commits

  • d0741419f refactor(js): simplify add_reaction.cjs payload validation and error handling
  • 31a21c174 fix: restore add_reaction discussion error handling
  • 73a185909 chore: start PR finisher triage

Generated by PR Description Updater for #48271 · sonnet46 · 24.7 AIC · ⌖ 4.6 AIC · ⊞ 4.8K ·

…handling

- Extract requirePayloadField() helper to eliminate 4 repeated
  null-check + setFailed patterns in resolveRestEndpoint
- Consolidate duplicate try/catch blocks in handleGraphQLOrUnknownEvent:
  resolve subjectId per-case, then call addDiscussionReaction once
  with a single shared error handler

Behavior is unchanged. No new exports added.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@pelikhan
pelikhan marked this pull request as ready for review July 27, 2026 04:42
Copilot AI review requested due to automatic review settings July 27, 2026 04:42
@github-actions

github-actions Bot commented Jul 27, 2026

Copy link
Copy Markdown
Contributor Author

🧠 Matt Pocock Skills Reviewer has completed the skills-based review. ✅

@github-actions

github-actions Bot commented Jul 27, 2026

Copy link
Copy Markdown
Contributor Author

Design Decision Gate 🏗️ completed the design decision gate check.

No ADR enforcement needed: PR #48271 does not have the 'implementation' label and has 0 new lines of code in business logic directories (threshold: 100).

@github-actions

github-actions Bot commented Jul 27, 2026

Copy link
Copy Markdown
Contributor Author

Test Quality Sentinel completed test quality analysis.

No test files were added or modified in this PR. Test Quality Sentinel skipped.

@github-actions

github-actions Bot commented Jul 27, 2026

Copy link
Copy Markdown
Contributor Author

PR Code Quality Reviewer completed the code quality review.

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

Refactors reaction payload validation and consolidates GraphQL reaction handling.

Changes:

  • Extracts shared payload-field validation.
  • Consolidates discussion reaction error handling.
  • Introduces a regression where discussion lookup errors escape uncaught.
Show a summary per file
File Description
actions/setup/js/add_reaction.cjs Simplifies validation and GraphQL reaction flow.

Review details

Tip

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

  • Files reviewed: 1/1 changed files
  • Comments generated: 1
  • Review effort level: Medium

Comment thread actions/setup/js/add_reaction.cjs Outdated
}
return;
if (!requirePayloadField(payload, discussionNumber, "Discussion number", ERR_NOT_FOUND)) return;
subjectId = await getDiscussionNodeId(owner, repo, discussionNumber);

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.

Fixed in 31a21c1. getDiscussionNodeId() is back inside the shared try so lookup-time GraphQL/not-found errors flow through handleReactionError again.

@github-actions github-actions Bot left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Review: simplify add_reaction.cjs

The refactor is clear and the requirePayloadField extraction is a genuine improvement. Two blocking issues found:

  1. Unused payload parameter in requirePayloadField — dead code, should be removed.
  2. getDiscussionNodeId() moved outside try/catch — behavioral regression; errors from that async call are no longer handled by handleReactionError and will propagate as unhandled rejections.

See inline comments for details.

🧵 Reviewed using Impeccable skills by Impeccable Skills Reviewer · sonnet46 · 17.1 AIC · ⌖ 4.66 AIC · ⊞ 5K

Comment thread actions/setup/js/add_reaction.cjs Outdated
* @returns {boolean} true if valid, false if missing (setFailed already called)
*/
function requirePayloadField(payload, value, fieldName, errorCode) {
if (!value) {

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

The payload parameter is declared in the function signature but never used inside the function body — it is dead code.

Consider removing it:

function requirePayloadField(value, fieldName, errorCode) {

And update all call sites to drop the payload argument.

@copilot please address this.

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.

Fixed in 31a21c1. I removed the unused payload parameter from requirePayloadField() and updated all call sites.

Comment thread actions/setup/js/add_reaction.cjs
@github-actions github-actions Bot mentioned this pull request Jul 27, 2026

@github-actions github-actions Bot left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Skills-Based Review 🧠

Applied /codebase-design — requesting changes on three issues in the refactored code.

📋 Key Themes & Highlights

Issues Found

  1. Unused payload parameter (line 71) — dead code in the function signature.
  2. Falsy !value check (line 73) — incorrectly rejects numeric 0 (valid GitHub ID); prefer value == null.
  3. Unguarded subjectId (line 162) — uninitialised let before the switch; a future case that forgets assignment silently passes undefined to addDiscussionReaction.

Positive Highlights

  • ✅ Clean extraction of requirePayloadField eliminates 4 repeated 3-line blocks.
  • ✅ Single shared try/catch in handleGraphQLOrUnknownEvent is a clear readability win.
  • ✅ Net −3 lines with no functional regression.

🧠 Reviewed using Matt Pocock's skills by Matt Pocock Skills Reviewer · sonnet46 · 31.6 AIC · ⌖ 4.84 AIC · ⊞ 6.7K
Comment /matt to run again

Comment thread actions/setup/js/add_reaction.cjs Outdated
* @param {string} errorCode - Error code prefix (ERR_NOT_FOUND or ERR_VALIDATION)
* @returns {boolean} true if valid, false if missing (setFailed already called)
*/
function requirePayloadField(payload, value, fieldName, errorCode) {

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

[/codebase-design] The payload parameter is declared but never used inside requirePayloadField — it is dead code in the signature.

💡 Suggested fix

Remove the unused payload parameter and update call sites:

function requirePayloadField(value, fieldName, errorCode) {
  if (!value) {
    core.setFailed(`${errorCode}: ${fieldName} not found in event payload`);
    return false;
  }
  return true;
}

Call sites become e.g. requirePayloadField(issueNumber, "Issue number", ERR_NOT_FOUND).

@copilot please address this.

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.

Fixed in 31a21c1. requirePayloadField() now only accepts the resolved value, field name, and error code; the dead payload argument is gone.

Comment thread actions/setup/js/add_reaction.cjs
Comment thread actions/setup/js/add_reaction.cjs Outdated
return;
}

try {

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

[/codebase-design] subjectId is declared with let before the switch but is never explicitly initialised. If a future developer adds a new case branch and forgets to assign subjectId, the code will silently call addDiscussionReaction(undefined, reaction) rather than failing fast.

💡 Suggested fix

Initialise to undefined explicitly and add a post-switch guard, or restructure with early returns as the original code did:

let subjectId = undefined;
// ... switch ...
if (subjectId == null) {
  core.setFailed(`${ERR_VALIDATION}: subjectId could not be resolved for event: ${eventName}`);
  return;
}
try {
  await addDiscussionReaction(subjectId, reaction);

This makes the contract explicit and is defensive against new case additions.

@copilot please address this.

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.

Addressed in 31a21c1. handleGraphQLOrUnknownEvent() now validates that subjectId was resolved before calling addDiscussionReaction().

@github-actions github-actions Bot left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

REQUEST_CHANGES — one correctness regression introduced by the refactor, plus two issues with the new helper.

### Blocking Issues
  1. getDiscussionNodeId uncaught (high): Moving this await outside the try/catch is a behavioral regression — errors from GraphQL/network failures now propagate uncaught instead of going through handleReactionError.

  2. !value falsy guard (medium): Rejects 0 and "" which are semantically valid-but-empty IDs. Should use value == null.

  3. Dead payload parameter (medium): requirePayloadField(payload, ...) accepts payload but never reads it. Misleads callers and will trigger linter warnings.

🔎 Code quality review by PR Code Quality Reviewer · sonnet46 · 32.3 AIC · ⌖ 4.9 AIC · ⊞ 5.7K
Comment /review to run again

Comment thread actions/setup/js/add_reaction.cjs
Comment thread actions/setup/js/add_reaction.cjs Outdated
* @param {string} errorCode - Error code prefix (ERR_NOT_FOUND or ERR_VALIDATION)
* @returns {boolean} true if valid, false if missing (setFailed already called)
*/
function requirePayloadField(payload, value, fieldName, errorCode) {

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Falsy !value check silently rejects numeric 0 — while GitHub numbers are never 0 in practice, this guard is fragile and semantically wrong for an ID validator.

💡 Details and fix

!value is true for 0, "", false, null, and undefined. For numeric IDs the dangerous case is 0, and for string node IDs an empty string "" would also be silently treated as missing. Use an explicit null check:

function requirePayloadField(value, fieldName, errorCode) {
  if (value == null) {
    core.setFailed(`${errorCode}: ${fieldName} not found in event payload`);
    return false;
  }
  return true;
}

Note: the payload parameter is also unused — see related 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.

Fixed in 31a21c1. The helper now uses value == null, so it only treats missing values as absent.

Comment thread actions/setup/js/add_reaction.cjs Outdated

/**
* Extract a required field from the event payload, calling setFailed if missing.
* @param {Record<string, any>} payload

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Dead parameter: payload is accepted but never used inside requirePayloadField — removes from the caller's perspective any hint that the function inspects the payload, but it actually doesn't.

💡 Suggested fix

Drop the parameter entirely:

function requirePayloadField(value, fieldName, errorCode) {
  if (!value) {
    core.setFailed(`${errorCode}: ${fieldName} not found in event payload`);
    return false;
  }
  return true;
}

Update all call sites to remove the payload argument. The JSDoc @param {Record<string, any>} payload block also needs to be removed.

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.

Fixed in 31a21c1. I removed the unused payload parameter and its JSDoc entry from requirePayloadField().

@pelikhan

Copy link
Copy Markdown
Collaborator

@copilot run pr-finisher skill

Copilot AI and others added 2 commits July 27, 2026 05:17
Co-authored-by: pelikhan <4175913+pelikhan@users.noreply.github.com>
Co-authored-by: pelikhan <4175913+pelikhan@users.noreply.github.com>

Copilot AI commented Jul 27, 2026

Copy link
Copy Markdown
Contributor

@copilot run pr-finisher skill

Ran it. Addressed the add_reaction.cjs review items in 31a21c1, replied on the review threads, and pushed the update.

Copilot AI requested a review from pelikhan July 27, 2026 05:31
@pelikhan
pelikhan merged commit 7e0913a into main Jul 27, 2026
8 checks passed
@pelikhan
pelikhan deleted the code-simplifier/simplify-add-reaction-cjs-dff0fe3463c3c099 branch July 27, 2026 05:35
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