fix(github): handle invalid GitHub event payload JSON - #2464
Open
ghoullier wants to merge 1 commit into
Open
Conversation
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Contributor
There was a problem hiding this comment.
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/catchto treatSyntaxError(invalid JSON) as an empty payload fallback. - Emit a diagnostic message when
GITHUB_EVENT_PATHexists 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() | ||
| }) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
This PR makes
@actions/githubresilient 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 causedContextconstruction to throwSyntaxError: Unexpected end of JSON input, failing the action before it could run.Changes
SyntaxErrorraised while parsing theGITHUB_EVENT_PATHpayload.Validation
npm --prefix packages/github test -- --runInBand __tests__/lib.test.tsnpm --prefix packages/github run tscnpx prettier --check packages/github/src/context.ts packages/github/__tests__/lib.test.tsnpx eslint packages/github/src/context.ts packages/github/__tests__/lib.test.ts