:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
fix(signals): handle missing pull request diff data (#130583)
Dylan Martin70eacf95224c
diff --git a/products/signals/frontend/inbox/components/detail/PullRequestDiffPanel.test.tsx b/products/signals/frontend/inbox/components/detail/PullRequestDiffPanel.test.tsxnew file mode 100644index 000000000000..6a5aea4c43fd--- /dev/null+++ b/products/signals/frontend/inbox/components/detail/PullRequestDiffPanel.test.tsx@@ -0,0 +1,83 @@+import '@testing-library/jest-dom'++import { cleanup, render, screen } from '@testing-library/react'+import { expectLogic } from 'kea-test-utils'++import { resumeKeaLoadersErrors, silenceKeaLoadersErrors } from '~/initKea'+import { useMocks } from '~/mocks/jest'+import { initKeaTests } from '~/test/init'++import type { CommitDiffResponseApi } from 'products/signals/frontend/generated/api.schemas'++import { makeReport } from '../../__mocks__/inboxMocks'+import { inboxReportDetailLogic } from '../../logics/inboxReportDetailLogic'+import { CommitContent } from './artefactTypes'+import { PullRequestDiffStat } from './PullRequestDiffPanel'+import { summarizeDiff } from './PullRequestDiffView'+import { PullRequestFilesChanged } from './PullRequestFilesChanged'++jest.mock('@pierre/diffs', () => ({ parsePatchFiles: jest.fn() }), { virtual: true })+jest.mock('@pierre/diffs/react', () => ({ FileDiff: () => null }), { virtual: true })++const commit: CommitContent = {+ repository: 'example/app',+ branch: 'fix-diff',+ commit_sha: 'abc123',+ message: 'Fix diff',+}++describe('PullRequestDiffPanel', () => {+ afterEach(cleanup)++ it.each([undefined, null, 42, {}])('returns no summary for a non-string diff: %p', (diff) => {+ expect(summarizeDiff(diff)).toBeNull()+ })++ it.each([{}, { diff: null }, { diff: 42 }, null])(+ 'shows a load error instead of crashing for an invalid diff response: %p',+ async (response) => {+ useMocks({+ get: {+ '/api/projects/:team_id/signals/reports/:id/feedback_state/': { sentiment: null, note: '' },+ '/api/projects/:team_id/signals/reports/:id/artefacts/': { results: [] },+ '/api/projects/:team_id/signals/reports/:id/signals/': { signals: [] },+ '/api/projects/:team_id/signals/reports/:id/checks/': { results: [] },+ '/api/projects/:team_id/signals/reports/available_reviewers/': [],+ '/api/projects/:team_id/signals/reports/:id/artefacts/:artefact_id/diff/': () => [200, response],+ },+ })+ initKeaTests()+ const report = makeReport()+ const logic = inboxReportDetailLogic({ reportId: report.id, report })+ logic.mount()+ silenceKeaLoadersErrors()+ try {+ if (response !== null) {+ logic.actions.loadReportDiffSuccess(response as unknown as CommitDiffResponseApi)+ const stat = render(<PullRequestDiffStat report={report} commit={commit} />)+ expect(stat.container).toBeEmptyDOMElement()+ stat.unmount()+ logic.actions.loadReportDiffSuccess(null)+ }+ await expectLogic(logic, () => logic.actions.loadReportDiff({ artefactId: 'commit-1' }))+ .toDispatchActions(['loadReportDiffFailure'])+ .toMatchValues({ reportDiff: null, reportDiffLoading: false })++ const stat = render(<PullRequestDiffStat report={report} commit={commit} />)+ expect(stat.container).toBeEmptyDOMElement()+ render(<PullRequestFilesChanged report={report} commit={commit} />)+ expect(+ screen.getByText("Couldn't load the diff. Try again, or open the pull request on GitHub.")+ ).toBeInTheDocument()+ } finally {+ cleanup()+ logic.unmount()+ resumeKeaLoadersErrors()+ }+ }+ )++ it.each(['', ' \n'])('keeps an empty diff distinct from an invalid diff: %p', (diff) => {+ expect(summarizeDiff(diff)).toEqual({ fileCount: 0, additions: 0, deletions: 0 })+ })+})diff --git a/products/signals/frontend/inbox/components/detail/PullRequestDiffView.tsx b/products/signals/frontend/inbox/components/detail/PullRequestDiffView.tsxindex 55cfb095c192..c4c7cd84e076 100644--- a/products/signals/frontend/inbox/components/detail/PullRequestDiffView.tsx+++ b/products/signals/frontend/inbox/components/detail/PullRequestDiffView.tsx@@ -150,7 +150,10 @@ function DiffFileCard({ file, children }: { file: FileDiffMetadata; children: Re}/** Aggregate file/line counts from a unified diff patch — shared by the diff toolbar and file headers. */-export function summarizeDiff(diff: string, cacheKey?: string): DiffSummary | null {+export function summarizeDiff(diff: unknown, cacheKey?: string): DiffSummary | null {+ if (typeof diff !== 'string') {+ return null+ }if (!diff.trim()) {return { fileCount: 0, additions: 0, deletions: 0 }}diff --git a/products/signals/frontend/inbox/logics/inboxReportDetailLogic.ts b/products/signals/frontend/inbox/logics/inboxReportDetailLogic.tsindex 4e3355870ff4..f57c7cdc3ef0 100644--- a/products/signals/frontend/inbox/logics/inboxReportDetailLogic.ts+++ b/products/signals/frontend/inbox/logics/inboxReportDetailLogic.ts@@ -1155,7 +1155,11 @@ export const inboxReportDetailLogic = kea<inboxReportDetailLogicType>([if (!teamId) {return null}- return await signalsReportArtefactsDiff(String(teamId), props.reportId, artefactId)+ const response = await signalsReportArtefactsDiff(String(teamId), props.reportId, artefactId)+ if (typeof response?.diff !== 'string') {+ throw new Error('Invalid diff response')+ }+ return response},},],