: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(eng-analytics): open pasted pull requests without a source param (#114737)
Raúl Negrón-Otero80b5af03bf31
diff --git a/products/engineering_analytics/frontend/components/PullRequestJumpInput.tsx b/products/engineering_analytics/frontend/components/PullRequestJumpInput.tsxindex d19bc0cfc730..e646af100beb 100644--- a/products/engineering_analytics/frontend/components/PullRequestJumpInput.tsx+++ b/products/engineering_analytics/frontend/components/PullRequestJumpInput.tsx@@ -14,7 +14,7 @@ const FAILURE_MESSAGE: Record<PullRequestJumpFailure, string> = {/** Opens one pull request in the CI explorer from a link or a number, whether or not the list holds it. */export function PullRequestJumpInput(): JSX.Element {- const { pullRequestJumpText, pullRequestJumpFailure } = useValues(engineeringAnalyticsLogic)+ const { pullRequestJumpText, pullRequestJumpFailure, githubSourcesLoading } = useValues(engineeringAnalyticsLogic)const { setPullRequestJumpText, submitPullRequestJump } = useActions(engineeringAnalyticsLogic)return (@@ -35,6 +35,7 @@ export function PullRequestJumpInput(): JSX.Element {type="secondary"size="small"onClick={() => submitPullRequestJump()}+ disabledReason={githubSourcesLoading ? 'Loading repositories' : undefined}data-attr="engineering-analytics-pull-request-jump-submit">View in CI explorerdiff --git a/products/engineering_analytics/frontend/lib/pullRequestReference.test.ts b/products/engineering_analytics/frontend/lib/pullRequestReference.test.tsindex 1018db506aa9..22e987fee97d 100644--- a/products/engineering_analytics/frontend/lib/pullRequestReference.test.ts+++ b/products/engineering_analytics/frontend/lib/pullRequestReference.test.ts@@ -1,4 +1,3 @@-import type { GitHubSourceApi } from '../generated/api.schemas'import {type PullRequestReference,type PullRequestTarget,@@ -9,17 +8,12 @@ import {const LINK: PullRequestReference = { kind: 'link', owner: 'PostHog', repo: 'posthog', number: 123 }const NUMBER: PullRequestReference = { kind: 'number', number: 123 }-const POSTHOG: GitHubSourceApi = { id: 'src-posthog', repo: 'PostHog/posthog', prefix: '', synced: true }-const WEBSITE: GitHubSourceApi = { id: 'src-website', repo: 'PostHog/posthog.com', prefix: '', synced: true }-describe('pullRequestReference', () => {it.each<[string, string, PullRequestReference | null]>([['a link', 'https://github.com/PostHog/posthog/pull/123', LINK],['a link with no scheme', 'github.com/PostHog/posthog/pull/123', LINK],['a link with a www host in capitals', 'HTTP://WWW.GitHub.com/PostHog/posthog/pull/123', LINK],['a link to the files tab', 'https://github.com/PostHog/posthog/pull/123/files', LINK],- ['a link with a query', 'https://github.com/PostHog/posthog/pull/123?diff=split', LINK],- ['a link to a review comment', 'https://github.com/PostHog/posthog/pull/123#discussion_r42', LINK],['a link inside other text', 'see https://github.com/PostHog/posthog/pull/123, please', LINK],['a link to a repository with a dot in its name',@@ -30,12 +24,12 @@ describe('pullRequestReference', () => {['#n', '#123', NUMBER],['digits', '123', NUMBER],['digits in whitespace', ' 123\n', NUMBER],- ['a link on another host', 'https://gitlab.com/PostHog/posthog/pull/123', null],['a host that only ends in github.com', 'https://notgithub.com/PostHog/posthog/pull/123', null],['a GitHub address in the path of another site', 'https://example.com/github.com/a/b/pull/123', null],['an issue link', 'https://github.com/PostHog/posthog/issues/123', null],- ['a commit link', 'https://github.com/PostHog/posthog/commit/123', null],['a link to pull request zero', 'https://github.com/PostHog/posthog/pull/0', null],+ ['a link whose repository is a dot segment', 'https://github.com/PostHog/../pull/5', null],+ ['owner/repo#n whose repository is a dot segment', 'PostHog/..#5', null],['zero', '#0', null],['digits with a suffix', '123abc', null],['words', 'not a pr', null],@@ -44,44 +38,33 @@ describe('pullRequestReference', () => {expect(parsePullRequestReference(text)).toEqual(expected)})- it.each<[string, PullRequestReference, GitHubSourceApi[], GitHubSourceApi | null, PullRequestTarget | null]>([+ it.each<[string, PullRequestReference, string | null, PullRequestTarget | null]>([[- 'a bare number to the picked repository',+ 'a bare number to the repository in scope',NUMBER,- [POSTHOG, WEBSITE],- WEBSITE,- { owner: 'PostHog', repo: 'posthog.com', number: 123, sourceId: 'src-website' },+ 'PostHog/posthog.com',+ { owner: 'PostHog', repo: 'posthog.com', number: 123, inScope: true },],+ ['a bare number to nothing when no repository is in scope', NUMBER, null, null],[- 'a bare number to the only connected repository',- NUMBER,- [POSTHOG],- null,- { owner: 'PostHog', repo: 'posthog', number: 123, sourceId: 'src-posthog' },- ],- ['a bare number to nothing when several repositories are connected', NUMBER, [POSTHOG, WEBSITE], null, null],- [- 'a bare number to nothing when the only source reports no repository',- NUMBER,- [{ ...POSTHOG, repo: '' }],- null,- null,- ],- [- 'a link to its own repository, in the casing of the source, when another is picked',+ 'a link to the repository in scope, in the casing of the scope',{ kind: 'link', owner: 'posthog', repo: 'POSTHOG', number: 123 },- [POSTHOG, WEBSITE],- WEBSITE,- { owner: 'PostHog', repo: 'posthog', number: 123, sourceId: 'src-posthog' },+ 'PostHog/posthog',+ { owner: 'PostHog', repo: 'posthog', number: 123, inScope: true },],[- 'a link to a repository with no connected source as typed',+ 'a link to another repository as typed, out of scope',{ kind: 'link', owner: 'keajs', repo: 'kea', number: 123 },- [POSTHOG],- POSTHOG,- { owner: 'keajs', repo: 'kea', number: 123, sourceId: null },+ 'PostHog/posthog',+ { owner: 'keajs', repo: 'kea', number: 123, inScope: false },+ ],+ [+ 'a link with no repository in scope as typed',+ LINK,+ null,+ { owner: 'PostHog', repo: 'posthog', number: 123, inScope: false },],- ])('resolves %s', (_label, reference, sources, pickedSource, expected) => {- expect(resolvePullRequestTarget(reference, sources, pickedSource)).toEqual(expected)+ ])('resolves %s', (_label, reference, scopedRepo, expected) => {+ expect(resolvePullRequestTarget(reference, scopedRepo)).toEqual(expected)})})diff --git a/products/engineering_analytics/frontend/lib/pullRequestReference.ts b/products/engineering_analytics/frontend/lib/pullRequestReference.tsindex 6316f144f0fd..bbbd8e4e58a7 100644--- a/products/engineering_analytics/frontend/lib/pullRequestReference.ts+++ b/products/engineering_analytics/frontend/lib/pullRequestReference.ts@@ -1,5 +1,3 @@-import type { GitHubSourceApi } from '../generated/api.schemas'-/** A pull request that free text names. A bare number names no repository. */export type PullRequestReference =| { kind: 'link' | 'repo_number'; owner: string; repo: string; number: number }@@ -9,8 +7,8 @@ export interface PullRequestTarget {owner: stringrepo: stringnumber: number- /** The connected source that syncs the repository. Null when no connected source does. */- sourceId: string | null+ /** False when the pull request is in another repository than the one the page is scoped to. */+ inScope: boolean}// The host must start the text or follow a character that a host name or a path cannot contain. Without@@ -23,6 +21,10 @@ export function parsePullRequestReference(text: string): PullRequestReference |const trimmed = text.trim()const link = LINK.exec(trimmed)const named = link ?? REPO_NUMBER.exec(trimmed)+ // GitHub reserves `.` and `..` as repository names, and a URL path treats them as dot segments.+ if (named && /^\.+$/.test(named[2])) {+ return null+ }const digits = named?.[3] ?? NUMBER.exec(trimmed)?.[1]const number = digits ? Number(digits) : 0// Past the safe integer range a number prints in exponent form, which is not a pull request number.@@ -34,21 +36,20 @@ export function parsePullRequestReference(text: string): PullRequestReference |: { kind: 'number', number }}-/** Where a reference opens. A bare number takes the repository of `pickedSource`, else of the only connected- * source. The result is null when a bare number has neither. */+/** Where a reference opens. A bare number opens in `scopedRepo`, the 'owner/name' the page is scoped to. The+ * result is null when a bare number has no such repository. */export function resolvePullRequestTarget(reference: PullRequestReference,- sources: GitHubSourceApi[],- pickedSource: GitHubSourceApi | null+ scopedRepo: string | null): PullRequestTarget | null {+ const [owner, repo] = scopedRepo?.split('/') ?? []+ const scoped = owner && repo ? { owner, repo, number: reference.number, inScope: true } : nullif (reference.kind === 'number') {- const source = pickedSource ?? (sources.length === 1 ? sources[0] : null)- const [owner, repo] = source?.repo.split('/') ?? []- return source && owner && repo ? { owner, repo, number: reference.number, sourceId: source.id } : null+ return scoped}- // GitHub ignores case in repository names, so match that way and keep the casing the source reports.- const fullName = `${reference.owner}/${reference.repo}`.toLowerCase()- const source = sources.find(({ repo }) => repo.toLowerCase() === fullName)- const [owner, repo] = source ? source.repo.split('/') : [reference.owner, reference.repo]- return { owner, repo, number: reference.number, sourceId: source?.id ?? null }+ // GitHub ignores case in repository names, so match that way and keep the casing the scope reports.+ const named = `${reference.owner}/${reference.repo}`.toLowerCase() === scopedRepo?.toLowerCase()+ return named && scoped+ ? scoped+ : { owner: reference.owner, repo: reference.repo, number: reference.number, inScope: false }}diff --git a/products/engineering_analytics/frontend/scenes/engineeringAnalyticsLogic.test.ts b/products/engineering_analytics/frontend/scenes/engineeringAnalyticsLogic.test.tsindex 94c5e40f5acc..08f9847ec42c 100644--- a/products/engineering_analytics/frontend/scenes/engineeringAnalyticsLogic.test.ts+++ b/products/engineering_analytics/frontend/scenes/engineeringAnalyticsLogic.test.ts@@ -489,8 +489,11 @@ describe('engineeringAnalyticsLogic', () => {expect(mockCiCards.mock.calls[0][1]).toMatchObject({ source_id: 'src-newer' })})- const openPullRequestList = async (searchParams: Record<string, string>): Promise<jest.SpyInstance> => {- mockSources.mockResolvedValue(SOURCES)+ const openPullRequestList = async (+ searchParams: Record<string, string>,+ sources: GitHubSourceApi[] = SOURCES+ ): Promise<jest.SpyInstance> => {+ mockSources.mockResolvedValue(sources)router.actions.push(urls.engineeringAnalyticsPullRequestList(), searchParams)logic = engineeringAnalyticsLogic()logic.mount()@@ -499,40 +502,52 @@ describe('engineeringAnalyticsLogic', () => {}const jumpEvents = (capture: jest.SpyInstance): unknown[] =>capture.mock.calls- .filter(([event]) => event === 'ci explorer opened from pull request jump')+ .filter(([event]) => event === 'pull request jump submitted').map(([, properties]) => properties)const routedPath = (): string => removeProjectIdIfPresent(router.values.location.pathname)- it.each([+ const PICKED = { source: 'src-older', repo: 'posthog/posthog', date_from: '-30d' }++ it.each<[string, Record<string, string>, string, string, Record<string, string>, string]>([[- 'a link in the repository it names, not the one in scope',+ 'a link to the repository in scope with no source when none is picked',+ {},+ 'https://github.com/PostHog/posthog/pull/42',+ urls.engineeringAnalyticsCIExplorer('posthog', 'posthog', 42),+ {},+ 'link',+ ],+ [+ 'a link to another repository without the source and repo of this one',+ PICKED,'https://github.com/PostHog/posthog.com/pull/42/files',- urls.engineeringAnalyticsCIExplorer('posthog', 'posthog.com', 42),- { source: 'src-newer', repo: 'posthog/posthog.com' },+ urls.engineeringAnalyticsCIExplorer('PostHog', 'posthog.com', 42),+ { date_from: '-30d' },'link',],['a bare number in the picked repository',+ PICKED,'#42',urls.engineeringAnalyticsCIExplorer('posthog', 'posthog', 42),- { source: 'src-older', repo: 'posthog/posthog' },+ PICKED,'number',],- ])('the pull request jump opens %s once', async (_label, text, pathname, scope, inputKind) => {- const capture = await openPullRequestList({ source: 'src-older', repo: 'posthog/posthog', date_from: '-30d' })+ ])('the pull request jump opens %s once', async (_label, opened, text, pathname, searchParams, inputKind) => {+ const capture = await openPullRequestList(opened)logic.actions.setPullRequestJumpText(text)logic.actions.submitPullRequestJump()logic.actions.submitPullRequestJump()expect(routedPath()).toBe(pathname)- expect(router.values.searchParams).toEqual({ date_from: '-30d', ...scope })- expect(jumpEvents(capture)).toEqual([{ input_kind: inputKind }])+ expect(router.values.searchParams).toEqual(searchParams)+ expect(jumpEvents(capture)).toEqual([{ outcome: 'opened', input_kind: inputKind }])expect(logic.values.pullRequestJumpFailure).toBeNull()})it('the pull request jump says why it cannot open, until the cause is fixed', async () => {- const capture = await openPullRequestList({})+ const capture = await openPullRequestList({}, [{ ...SOURCES[0], repo: '' }])logic.actions.setPullRequestJumpText('not a pr')logic.actions.submitPullRequestJump()@@ -543,12 +558,15 @@ describe('engineeringAnalyticsLogic', () => {logic.actions.submitPullRequestJump()expect(logic.values.pullRequestJumpFailure).toBe('needs_repository')expect(routedPath()).toBe(urls.engineeringAnalyticsPullRequestList())- expect(jumpEvents(capture)).toEqual([])+ expect(jumpEvents(capture)).toEqual([+ { outcome: 'invalid', input_kind: null },+ { outcome: 'needs_repository', input_kind: 'number' },+ ])- logic.actions.setScope('src-newer', 'posthog/posthog.com')+ logic.actions.setPullRequestJumpText('PostHog/posthog#42')expect(logic.values.pullRequestJumpFailure).toBeNull()logic.actions.submitPullRequestJump()- expect(routedPath()).toBe(urls.engineeringAnalyticsCIExplorer('posthog', 'posthog.com', 42))+ expect(routedPath()).toBe(urls.engineeringAnalyticsCIExplorer('PostHog', 'posthog', 42))})it.each([diff --git a/products/engineering_analytics/frontend/scenes/engineeringAnalyticsLogic.ts b/products/engineering_analytics/frontend/scenes/engineeringAnalyticsLogic.tsindex a544fccf9909..c76c4191f783 100644--- a/products/engineering_analytics/frontend/scenes/engineeringAnalyticsLogic.ts+++ b/products/engineering_analytics/frontend/scenes/engineeringAnalyticsLogic.ts@@ -32,12 +32,12 @@ import type { GitHubSourceApi, PullRequestListItemApi, PushCISampleApi } from '.import { CIStatus, ciStatusOf } from '../lib/ci'import { parsePullRequestReference, resolvePullRequestTarget } from '../lib/pullRequestReference'import { type FleetSummary, computeFleetSummary } from '../lib/runHealth'-import { scopeToValue, withScope } from '../lib/scope'+import { scopeToValue, withCurrentScope, withScope } from '../lib/scope'import { engineeringAnalyticsFiltersLogic } from './engineeringAnalyticsFiltersLogic'import type { RunScopeParams } from './engineeringAnalyticsFiltersLogic'// pinned: analytics event name. Renaming it breaks the insights built on it.-const EVENT_PULL_REQUEST_JUMP_OPENED = 'ci explorer opened from pull request jump'+const EVENT_PULL_REQUEST_JUMP_SUBMITTED = 'pull request jump submitted'// Mirrors the endpoint's server-side limit.export const PR_TABLE_LIMIT = 1000@@ -958,6 +958,7 @@ export const engineeringAnalyticsLogic: LogicWrapper<engineeringAnalyticsLogicTyfailPullRequestJump: (_, { failure }) => failure,setPullRequestJumpText: () => null,setScope: () => null,+ setSourceId: () => null,},],cardsStatus: [@@ -1256,32 +1257,34 @@ export const engineeringAnalyticsLogic: LogicWrapper<engineeringAnalyticsLogicTyactions.setThrashOnly(target === 'thrash')},submitPullRequestJump: () => {- // A bare number needs the sources to find its repository, so do nothing until they load.+ // The repository in scope comes from the sources, so do nothing until they load.if (!values.pullRequestJumpText.trim() || values.githubSourcesLoading) {return}const reference = parsePullRequestReference(values.pullRequestJumpText)- const pickedSource =- values.githubSources.find(- (source) => scopeToValue(source.id, source.repo) === values.selectedScope- ) ?? null- const target = reference && resolvePullRequestTarget(reference, values.githubSources, pickedSource)- if (!reference || !target) {- actions.failPullRequestJump(reference ? 'needs_repository' : 'invalid')+ const target = reference && resolvePullRequestTarget(reference, values.activeSource?.repo || null)+ const capture = (outcome: 'opened' | PullRequestJumpFailure): void => {+ posthog.capture(EVENT_PULL_REQUEST_JUMP_SUBMITTED, {+ outcome,+ input_kind: reference?.kind ?? null,+ })+ }+ if (!target) {+ const failure = reference ? 'needs_repository' : 'invalid'+ capture(failure)+ actions.failPullRequestJump(failure)return}- const { owner, repo, number, sourceId } = target- posthog.capture(EVENT_PULL_REQUEST_JUMP_OPENED, { input_kind: reference.kind })+ capture('opened')// The next scene can take a moment to load. An empty box makes a second submit do nothing.actions.setPullRequestJumpText('')+ const explorerUrl = urls.engineeringAnalyticsCIExplorer(target.owner, target.repo, target.number)router.actions.push(- withScope(- urls.engineeringAnalyticsCIExplorer(owner, repo, number),- // The `repo` param of this page names the repository in scope here, which can- // differ from the repository of a pasted link.- { ...router.values.searchParams, repo: sourceId ? `${owner}/${repo}` : undefined },- sourceId- )+ target.inScope+ ? withCurrentScope(explorerUrl, values.sourceId)+ : // The source and repo in scope belong to another repository. Without them the+ // explorer reads from the source that syncs the pull request's own repository.+ withScope(explorerUrl, { ...router.values.searchParams, repo: undefined }, null))},})),