Skip to content

Fix update-discussion API error logging for explicit discussion_number#23340

Merged
pelikhan merged 6 commits intomainfrom
copilot/fix-update-discussion-api-error
Mar 28, 2026
Merged

Fix update-discussion API error logging for explicit discussion_number#23340
pelikhan merged 6 commits intomainfrom
copilot/fix-update-discussion-api-error

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Mar 28, 2026

When update-discussion failed (e.g. due to missing discussions: write permission), the only visible error was the opaque ERR_API: update discussion #N failed (attempt 1) — the actual GraphQL error was buried in the enhanced error chain and invisible in most log views.

Changes

github_api_helpers.cjs (new shared helper)

  • Adds a generalized logGraphQLError(error, operation, hints) function with a GraphQLErrorHints typedef, replacing the duplicated copies that existed in each handler
  • The hints parameter supports domain-specific messages:
    • insufficientScopesHint — shown when INSUFFICIENT_SCOPES error type is present
    • notFoundHint — shown when NOT_FOUND error type is present
    • notFoundPredicate — optional function to further gate the NOT_FOUND hint (e.g. project handlers use /projectV2\b/.test(msg))
  • Also surfaces error.status (HTTP status code) which was missing from the project handler copies

update_discussion.cjs

  • Removes local logGraphQLError copy; imports the shared version from github_api_helpers.cjs
  • Defines DISCUSSION_GRAPHQL_HINTS with discussion-specific permission hints for INSUFFICIENT_SCOPES (→ add permissions: discussions: write) and NOT_FOUND (→ check discussion number / token read access)
  • Wraps both the fetch-discussion query and the updateDiscussion mutation in try/catch that calls logGraphQLError before re-throwing, so diagnostics fire at the point of failure

update_project.cjs, create_project.cjs, create_project_status_update.cjs

  • Remove their local logGraphQLError copies; each now imports from github_api_helpers.cjs and passes a module-level PROJECT_GRAPHQL_HINTS constant with project-specific messages

actions/setup/setup.sh

  • Adds github_api_helpers.cjs to SAFE_OUTPUTS_FILES so the new shared helper is deployed to the safe-outputs runtime directory

update_discussion.test.cjs

  • Adds debug: vi.fn() to mock core (required by error_recovery.cjs)
  • Adds 3 tests covering INSUFFICIENT_SCOPES, FORBIDDEN, and NOT_FOUND error paths

github_api_helpers.test.cjs

  • Adds 9 tests for the new shared logGraphQLError covering: operation/message logging, errors array details, HTTP status, request/response data, hint display conditions, notFoundPredicate filtering, and no-hints invocation

Example of what is now emitted on an INSUFFICIENT_SCOPES failure:

GraphQL error during: fetch discussion #45 from owner/repo
Message: Request failed due to following response errors
This looks like a token permission problem. The GitHub token requires 'discussions: write' permission. Add 'permissions: discussions: write' to your workflow, or set 'safe-outputs.update-discussion.github-token' to a PAT with the appropriate scopes.
Errors array (1 error(s)):
  [1] Your token has not been granted the required scopes.
      Type: INSUFFICIENT_SCOPES
      Path: ["repository","discussion"]
HTTP status: 401

✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.



✨ PR Review Safe Output Test - Run 23685128220

💥 [THE END] — Illustrated by Smoke Claude ·

Add logGraphQLError helper to update_discussion.cjs that surfaces
the full GraphQL error details (errors array with type/path/locations,
HTTP status, and actionable permission hints for INSUFFICIENT_SCOPES
and NOT_FOUND) when a discussion fetch or mutation fails.

Previously, failures only reported ERR_API: update discussion #N failed
(attempt 1) with the root cause buried in a multi-line enhanced error.
Now every GraphQL failure also emits structured diagnostic output that
enables fast root-cause analysis without a debug re-run.

Wrap both the fetch-discussion query and the updateDiscussion mutation
in try/catch blocks that call logGraphQLError before re-throwing.

Add debug: vi.fn() to the test mock core and 3 new tests covering
INSUFFICIENT_SCOPES, FORBIDDEN, and NOT_FOUND error scenarios.

Agent-Logs-Url: https://github.com/github/gh-aw/sessions/b7ebe3f3-899f-4f11-9dff-5f93dce4189e

Co-authored-by: pelikhan <4175913+pelikhan@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix update-discussion API error when targeting explicit discussion number Fix update-discussion API error logging for explicit discussion_number Mar 28, 2026
Copilot AI requested a review from pelikhan March 28, 2026 12:12
* @param {Error & { errors?: Array<{ type?: string, message: string, path?: unknown, locations?: unknown }>, request?: unknown, data?: unknown, status?: number }} error - GraphQL error
* @param {string} operation - Human-readable description of the failing operation
*/
function logGraphQLError(error, operation) {
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

  • refactor into graphql helpers file / function with generalized logging

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.

Done in af8d72f. Moved logGraphQLError into github_api_helpers.cjs as a shared, generalized function. It accepts an optional GraphQLErrorHints parameter (insufficientScopesHint, notFoundHint, notFoundPredicate) for domain-specific messages. All four files that had local copies (update_discussion.cjs, update_project.cjs, create_project.cjs, create_project_status_update.cjs) now import and call the shared version with a module-level hints constant. Nine new tests added to github_api_helpers.test.cjs.

@pelikhan pelikhan marked this pull request as ready for review March 28, 2026 12:18
Copilot AI review requested due to automatic review settings March 28, 2026 12:18
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

Improves diagnostics for update-discussion GraphQL failures by surfacing actionable error details directly in logs (especially for explicit discussion_number updates).

Changes:

  • Added logGraphQLError helper to emit GraphQL error details (errors array metadata, HTTP status, permission hints, and request/response when present).
  • Wrapped the fetch-discussion query and updateDiscussion mutation with try/catch to log diagnostics at the failure point before rethrowing.
  • Extended unit tests to cover INSUFFICIENT_SCOPES, FORBIDDEN, and NOT_FOUND error logging paths (and added a core.debug mock required by shared error recovery).

Reviewed changes

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

File Description
actions/setup/js/update_discussion.cjs Adds detailed GraphQL error logging and applies it to discussion fetch + mutation paths.
actions/setup/js/update_discussion.test.cjs Adds mocks and tests validating error logging behavior for common GraphQL failure types.

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

* @param {string} operation - Human-readable description of the failing operation
*/
function logGraphQLError(error, operation) {
core.info(`GraphQL error during: ${operation}`);
Copy link

Copilot AI Mar 28, 2026

Choose a reason for hiding this comment

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

The log prefix here (GraphQL error during:) is inconsistent with the existing pattern used elsewhere in this repo (e.g., actions/setup/js/update_project.cjs:46, create_project.cjs:17) which logs GraphQL Error during:. For consistent grepping/parsing across actions, consider aligning this string (including capitalization) with the established prefix.

Suggested change
core.info(`GraphQL error during: ${operation}`);
core.info(`GraphQL Error during: ${operation}`);

Copilot uses AI. Check for mistakes.
Comment on lines +645 to +650
// Verify logGraphQLError was called: it emits the operation name, message, and permission hint
expect(mockCore.infos.some(msg => msg.includes("GraphQL error during:"))).toBe(true);
expect(mockCore.infos.some(msg => msg.includes("fetch discussion #45"))).toBe(true);
expect(mockCore.infos.some(msg => msg.includes("Request failed"))).toBe(true);
expect(mockCore.infos.some(msg => msg.includes("discussions: write"))).toBe(true);
expect(mockCore.infos.some(msg => msg.includes("INSUFFICIENT_SCOPES"))).toBe(true);
Copy link

Copilot AI Mar 28, 2026

Choose a reason for hiding this comment

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

This test asserts the new log prefix GraphQL error during:. If you align the runtime log message with the repo’s existing convention (GraphQL Error during: as in update_project.cjs / create_project.cjs), this assertion should be updated accordingly to avoid brittle divergence between handlers.

Copilot uses AI. Check for mistakes.
Move the duplicated logGraphQLError helper from update_discussion.cjs,
update_project.cjs, create_project.cjs, and create_project_status_update.cjs
into a single generalized implementation in github_api_helpers.cjs.

The shared function accepts an optional `hints` (GraphQLErrorHints) parameter
for domain-specific INSUFFICIENT_SCOPES / NOT_FOUND messages and an optional
notFoundPredicate to gate the NOT_FOUND hint on the error message content
(used by project callers to match /projectV2\b/).

Each caller defines a module-level *_GRAPHQL_HINTS constant and passes it to
logGraphQLError. The shared function also surfaces error.status (HTTP status
code), which was previously only logged by update_discussion.cjs.

Tests added to github_api_helpers.test.cjs covering: operation/message
logging, errors array details, HTTP status, request/response data,
insufficientScopesHint, notFoundHint with and without notFoundPredicate,
and no-hints invocation.

Agent-Logs-Url: https://github.com/github/gh-aw/sessions/fd81583a-3cf5-4b0c-89d0-94d252259614

Co-authored-by: pelikhan <4175913+pelikhan@users.noreply.github.com>
Copilot AI requested a review from pelikhan March 28, 2026 12:24
@pelikhan
Copy link
Copy Markdown
Collaborator

@copilot

  • merge main
  • update setup.sh with required files

Copy link
Copy Markdown
Contributor

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

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

💥 Automated smoke test review - all systems nominal!

💥 [THE END] — Illustrated by Smoke Claude

}

if (error.status) core.info(`HTTP status: ${error.status}`);
if (error.request) core.info(`Request: ${JSON.stringify(error.request, null, 2)}`);
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.

The GraphQLErrorHints typedef is well-structured and enables clear domain-specific error guidance. Consider adding a JSDoc @example block showing typical usage to help future maintainers.

repo: context.repo.repo,
number: discussionNumber,
});
} catch (fetchError) {
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.

Good use of the shared logGraphQLError helper here. The DISCUSSION_GRAPHQL_HINTS constant at module level is clean. The notFoundHint message is clear and actionable for debugging.

Copilot AI and others added 2 commits March 28, 2026 12:32
…scussion-api-error

# Conflicts:
#	actions/setup/js/update_discussion.cjs

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

Copilot AI commented Mar 28, 2026

@copilot

  • merge main...

Done in 40f1d68. Merged main (resolved conflict in update_discussion.cjs preserving both the logGraphQLError import and the new resolveNumberFromTemporaryId import from main), and added github_api_helpers.cjs to SAFE_OUTPUTS_FILES in actions/setup/setup.sh.

@pelikhan
Copy link
Copy Markdown
Collaborator

Copy link
Copy Markdown
Contributor Author

Copilot AI commented Mar 28, 2026

@copilot Fix https://github.com/github/gh-aw/actions/runs/23685313925/job/69003796899

Fixed in 62dd016. The TypeScript errors were caused by catch (fetchError) / catch (mutationError) — in strict TypeScript, caught variables are typed as unknown, so the inline /** @type {any} */ fetchError cast without parentheses was rejected. Changed both catch blocks to use catch (err) + const fetchError = /** @type {any} */ (err) (matching the pattern already used in update_project.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)
  • 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 odules/npm/node_-b git bran�� -M main k/gh-aw/gh-aw/actions/setup/js/node_modules/.bin/git user.name Test User 0/x64/lib/node_magent-change.txt git (dns block)

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

@pelikhan pelikhan merged commit 21e679c into main Mar 28, 2026
53 checks passed
@pelikhan pelikhan deleted the copilot/fix-update-discussion-api-error branch March 28, 2026 16:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[plan] Fix update-discussion API error when targeting explicit discussion_number

3 participants