Skip to content

fix(github): handle invalid GitHub event payload JSON - #2464

Open
ghoullier wants to merge 1 commit into
actions:mainfrom
ghoullier:main
Open

fix(github): handle invalid GitHub event payload JSON#2464
ghoullier wants to merge 1 commit into
actions:mainfrom
ghoullier:main

Conversation

@ghoullier

@ghoullier ghoullier commented Jul 31, 2026

Copy link
Copy Markdown

Summary

This PR makes @actions/github resilient to malformed GitHub event payload files.

GitHub Actions exposes the triggering event through GITHUB_EVENT_PATH. In parallel workflows, a consumer can encounter a partially written or truncated event file. Previously, this caused Context construction to throw SyntaxError: Unexpected end of JSON input, failing the action before it could run.

Changes

  • Catch SyntaxError raised while parsing the GITHUB_EVENT_PATH payload.
  • Preserve the existing empty payload fallback when the JSON is invalid.
  • Emit a clear message identifying the affected event path and invalid JSON condition.
  • Continue propagating non-JSON errors, such as event-file read failures, rather than hiding unexpected I/O issues.
  • Add a regression test using a truncated JSON fixture.

Validation

  • npm --prefix packages/github test -- --runInBand __tests__/lib.test.ts
  • npm --prefix packages/github run tsc
  • npx prettier --check packages/github/src/context.ts packages/github/__tests__/lib.test.ts
  • npx eslint packages/github/src/context.ts packages/github/__tests__/lib.test.ts

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Copilot AI review requested due to automatic review settings July 31, 2026 13:23
@ghoullier
ghoullier requested a review from a team as a code owner July 31, 2026 13:23

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

Improves @actions/github Context initialization robustness by tolerating malformed/truncated GITHUB_EVENT_PATH payload files (common in parallel workflows) while still surfacing non-JSON I/O failures, and adds a regression test to cover the invalid-JSON scenario.

Changes:

  • Wrap event payload parsing in a try/catch to treat SyntaxError (invalid JSON) as an empty payload fallback.
  • Emit a diagnostic message when GITHUB_EVENT_PATH exists but contains invalid JSON.
  • Add a truncated JSON fixture + Jest test asserting the empty payload fallback and emitted message.

Reviewed changes

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

File Description
packages/github/src/context.ts Catch JSON parse SyntaxError and log an invalid-payload message while preserving existing behavior for other errors.
packages/github/tests/lib.test.ts Add regression test covering invalid JSON payload handling and verifying the emitted message.
packages/github/tests/invalid-payload.json Add truncated JSON fixture used by the new regression test.

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

Comment on lines +31 to +42
const write = jest
.spyOn(process.stdout, 'write')
.mockImplementation(() => true)

context = new Context()

expect(context.payload).toEqual({})
expect(write).toHaveBeenCalledWith(
`GITHUB_EVENT_PATH ${process.env.GITHUB_EVENT_PATH} contains invalid JSON${EOL}`
)
write.mockRestore()
})
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.

2 participants