-
-
Notifications
You must be signed in to change notification settings - Fork 678
fix(github): keep a webhook delivery alive when link metadata will not parse #1526
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
andrejsshell
merged 4 commits into
usekaneo:main
from
luantaraschi:fix/github-webhook-metadata-guards
Aug 15, 2026
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
8f96782
fix(github): keep a webhook delivery alive when link metadata will no…
luantaraschi 32a4447
fix(github): type the metadata the edit handler reads back
luantaraschi cdf3ce2
fix(github): ignore link metadata that parses to something other than…
luantaraschi b4d7d40
Merge branch 'main' into fix/github-webhook-metadata-guards
randoneering File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,49 @@ | ||
| // An external link's metadata is a JSON string in the database, so a row | ||
| // written by an older version, or truncated, is not the caller's fault and | ||
| // should not take the webhook delivery down with it. The Gitea handlers already | ||
| // warn and carry on with an empty object; this is the same behaviour in one | ||
| // place, since the GitHub side needs it in four. | ||
| // The shape is whatever an earlier write left behind, so callers that read | ||
| // named fields say what they expect. The default keeps the untyped reading for | ||
| // the handlers that only spread the value forward. | ||
| export function parseLinkMetadata<T extends object = Record<string, unknown>>( | ||
| raw: string | null | undefined, | ||
| context: { externalLinkId: string; source: string }, | ||
| ): Partial<T> { | ||
| if (!raw) { | ||
| return {}; | ||
| } | ||
|
|
||
| let parsed: unknown; | ||
|
|
||
| try { | ||
| parsed = JSON.parse(raw); | ||
| } catch (error) { | ||
| console.warn("Failed to parse GitHub external link metadata", { | ||
| ...context, | ||
| error, | ||
| }); | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
|
|
||
| return {}; | ||
| } | ||
|
|
||
| // Parsing succeeding is not the same as the row holding metadata. The | ||
| // literal `null` parses to `null`, `"kaneo"` to a string, and either would | ||
| // pass the return type on to a caller that reads a named field off it, which | ||
| // is the crash this helper exists to prevent. An array is not what any writer | ||
| // here produces either, and spreading one forward would turn its indices into | ||
| // keys. Both take the same exit as a row that will not parse. | ||
| if (typeof parsed !== "object" || parsed === null || Array.isArray(parsed)) { | ||
| console.warn( | ||
| "Ignoring GitHub external link metadata that is not an object", | ||
| { | ||
| ...context, | ||
| metadataType: parsed === null ? "null" : typeof parsed, | ||
| }, | ||
| ); | ||
|
|
||
| return {}; | ||
| } | ||
|
|
||
| return parsed as Partial<T>; | ||
| } | ||
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
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
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
66 changes: 66 additions & 0 deletions
66
tests/api/plugins/github/utils/parse-link-metadata.test.ts
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,66 @@ | ||
| import { afterEach, describe, expect, it, vi } from "vitest"; | ||
| import { parseLinkMetadata } from "../../../../../apps/api/src/plugins/github/utils/parse-link-metadata"; | ||
|
|
||
| const context = { externalLinkId: "link-1", source: "issue_closed" }; | ||
|
|
||
| afterEach(() => { | ||
| vi.restoreAllMocks(); | ||
| }); | ||
|
|
||
| describe("parseLinkMetadata", () => { | ||
| it("reads a well formed object", () => { | ||
| expect( | ||
| parseLinkMetadata('{"state":"open","createdFrom":"kaneo"}', context), | ||
| ).toEqual({ | ||
| state: "open", | ||
| createdFrom: "kaneo", | ||
| }); | ||
| }); | ||
|
|
||
| it("treats an absent value as no metadata", () => { | ||
| expect(parseLinkMetadata(null, context)).toEqual({}); | ||
| expect(parseLinkMetadata(undefined, context)).toEqual({}); | ||
| expect(parseLinkMetadata("", context)).toEqual({}); | ||
| }); | ||
|
|
||
| it("warns and carries on when the row cannot be parsed", () => { | ||
| const warn = vi.spyOn(console, "warn").mockImplementation(() => undefined); | ||
|
|
||
| // A row truncated by an older write, which used to throw out of the handler | ||
| // and fail the whole webhook delivery. | ||
| expect(parseLinkMetadata('{"state":"op', context)).toEqual({}); | ||
| expect(warn).toHaveBeenCalledOnce(); | ||
| expect(warn.mock.calls[0][1]).toMatchObject({ | ||
| externalLinkId: "link-1", | ||
| source: "issue_closed", | ||
| }); | ||
| }); | ||
|
|
||
| it("keeps the row out of the warning", () => { | ||
| const warn = vi.spyOn(console, "warn").mockImplementation(() => undefined); | ||
|
|
||
| // The row can hold a task description synced from Kaneo, so the log gets | ||
| // the link id to find it by and not the content itself. | ||
| parseLinkMetadata( | ||
| '{"lastSync":{"description":"segredo do cliente"', | ||
| context, | ||
| ); | ||
| expect(JSON.stringify(warn.mock.calls[0])).not.toContain( | ||
| "segredo do cliente", | ||
| ); | ||
| }); | ||
|
|
||
| it("ignores a row that parses to something other than an object", () => { | ||
| const warn = vi.spyOn(console, "warn").mockImplementation(() => undefined); | ||
|
|
||
| // `JSON.parse` answers these without throwing, so they used to reach the | ||
| // handlers typed as metadata. `null` is the one that bites: reading | ||
| // `metadata.createdFrom` off it throws inside the webhook. | ||
| expect(parseLinkMetadata("null", context)).toEqual({}); | ||
| expect(parseLinkMetadata('"kaneo"', context)).toEqual({}); | ||
| expect(parseLinkMetadata("42", context)).toEqual({}); | ||
| expect(parseLinkMetadata("true", context)).toEqual({}); | ||
| expect(parseLinkMetadata('["kaneo"]', context)).toEqual({}); | ||
| expect(warn).toHaveBeenCalledTimes(5); | ||
| }); | ||
| }); |
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.