Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 0 additions & 27 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

91 changes: 91 additions & 0 deletions src/inngest/functions/process-pr-event.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -270,3 +270,94 @@ describe('processPrEvent - awardRecommendedMerge XP capping', () => {
);
});
});

describe('processPrEvent - linkPrToClaim issues relation array', () => {
beforeEach(() => {
vi.clearAllMocks();
});

const setupMock = (issuesArray: unknown) => {
const recommendationsMock = sb({
is: vi.fn().mockResolvedValue({
data: [
{
id: 1,
issue_id: 101,
issues: issuesArray,
},
],
}),
update: vi.fn().mockResolvedValue({ error: null }),
});

const profilesMock = sb({
maybeSingle: vi.fn().mockResolvedValue({ data: { id: 'contributor-id' } }),
});

const pullRequestsMock = sb({
upsert: vi.fn().mockResolvedValue({ error: null }),
});

const installationRepositoriesMock = sb({
maybeSingle: vi.fn().mockResolvedValue({ data: { repo_full_name: 'owner/repo' } }),
});

wire({
recommendations: recommendationsMock,
profiles: profilesMock,
pull_requests: pullRequestsMock,
installation_repositories: installationRepositoriesMock,
});

return { recommendationsMock };
};

const evOpened = () => ({
data: {
payload: {
action: 'opened',
pull_request: {
id: 1234,
number: 1,
html_url: 'https://github.com/owner/repo/pull/1',
title: 'Fix issue',
body: 'Closes #123',
state: 'open',
draft: false,
merged: false,
merged_at: null,
closed_at: null,
created_at: '2026-01-01T00:00:00Z',
updated_at: '2026-01-01T00:00:00Z',
user: { login: 'contributor' },
base: { repo: { full_name: 'owner/repo' } },
},
},
},
});

it('handles issues relation returned as an array', async () => {
const { recommendationsMock } = setupMock([
{ repo_full_name: 'owner/repo', github_issue_number: 123 },
]);

await prRun({ event: evOpened(), step });

expect(recommendationsMock.update).toHaveBeenCalledWith(
expect.objectContaining({ linked_pr_url: 'https://github.com/owner/repo/pull/1' }),
);
});

it('handles issues relation returned as a single object', async () => {
const { recommendationsMock } = setupMock({
repo_full_name: 'owner/repo',
github_issue_number: 123,
});

await prRun({ event: evOpened(), step });

expect(recommendationsMock.update).toHaveBeenCalledWith(
expect.objectContaining({ linked_pr_url: 'https://github.com/owner/repo/pull/1' }),
);
});
});
16 changes: 8 additions & 8 deletions src/inngest/functions/process-pr-event.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import { XP_SOURCE, xpForMerge, refIds, XP_REWARDS } from '@/lib/xp/sources';
import { cacheDelByPrefix } from '@/lib/cache';
import { buildPrRow, type IngestiblePr } from '@/lib/maintainer/pr-ingest';
import { unwrapJoin } from '@/lib/supabase/inner-join';

/**
* Webhook handler for GitHub `pull_request` events.
Expand Down Expand Up @@ -170,13 +171,13 @@
.is('linked_pr_url', null);

for (const claim of claims ?? []) {
const raw = (claim as unknown as { issues: unknown }).issues;
const issue = Array.isArray(raw)
? (raw[0] as { repo_full_name?: string; github_issue_number?: number } | undefined)
: (raw as { repo_full_name?: string; github_issue_number?: number } | undefined);
const issue = unwrapJoin<{ repo_full_name?: string; github_issue_number?: number }>(
(claim as unknown as { issues: unknown }).issues,
);

if (!issue?.repo_full_name || typeof issue.github_issue_number !== 'number') continue;
if (issue.repo_full_name === repo && issueRefs.includes(issue.github_issue_number)) {
await sb.from('recommendations').update({ linked_pr_url: prUrl }).eq('id', claim.id);

Check failure on line 180 in src/inngest/functions/process-pr-event.ts

View workflow job for this annotation

GitHub Actions / check

src/inngest/functions/process-pr-event.test.ts > processPrEvent - linkPrToClaim issues relation array > handles issues relation returned as a single object

TypeError: sb.from(...).update(...).eq is not a function ❯ linkPrToClaim src/inngest/functions/process-pr-event.ts:180:73 ❯ src/inngest/functions/process-pr-event.ts:87:16 ❯ src/inngest/functions/process-pr-event.test.ts:357:5

Check failure on line 180 in src/inngest/functions/process-pr-event.ts

View workflow job for this annotation

GitHub Actions / check

src/inngest/functions/process-pr-event.test.ts > processPrEvent - linkPrToClaim issues relation array > handles issues relation returned as an array

TypeError: sb.from(...).update(...).eq is not a function ❯ linkPrToClaim src/inngest/functions/process-pr-event.ts:180:73 ❯ src/inngest/functions/process-pr-event.ts:87:16 ❯ src/inngest/functions/process-pr-event.test.ts:344:5
return { linked: true, recId: claim.id };
}
}
Expand Down Expand Up @@ -317,10 +318,9 @@
for (const claim of claims ?? []) {
// Supabase types the joined `issues` field as an array even for a
// single-row !inner join. Normalise.
const raw = (claim as unknown as { issues: unknown }).issues;
const issue = Array.isArray(raw)
? (raw[0] as { repo_full_name?: string; github_issue_number?: number } | undefined)
: (raw as { repo_full_name?: string; github_issue_number?: number } | undefined);
const issue = unwrapJoin<{ repo_full_name?: string; github_issue_number?: number }>(
(claim as unknown as { issues: unknown }).issues,
);
if (!issue?.repo_full_name || typeof issue.github_issue_number !== 'number') continue;
if (issue.repo_full_name === repo && issueRefs.includes(issue.github_issue_number)) {
return (claim as { id: number }).id;
Expand Down
7 changes: 5 additions & 2 deletions src/inngest/functions/recommendations-build.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { inngest } from '../client';
import { getServiceSupabase } from '@/lib/supabase/service';
import { filterAndRank, type ScoredIssue, type SkipCounts } from '@/lib/pipeline/recommend';
import { unwrapJoin } from '@/lib/supabase/inner-join';
import { SKIP_HISTORY_WINDOW_DAYS } from '@/lib/pipeline/constants';

/**
Expand Down Expand Up @@ -75,10 +76,12 @@ export const recommendationsBuild = inngest.createFunction(
const skipHistoryMap: Record<string, SkipCounts> = {};
for (const row of skipsData ?? []) {
const userId = row.user_id;
const issue = row.issues as unknown as {
const issue = unwrapJoin<{
repo_full_name: string;
repo_language: string | null;
};
}>((row as unknown as { issues: unknown }).issues);

if (!issue?.repo_full_name) continue;

if (!skipHistoryMap[userId]) {
skipHistoryMap[userId] = { byRepo: {}, byLanguage: {} };
Expand Down
3 changes: 3 additions & 0 deletions src/lib/supabase/inner-join.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export function unwrapJoin<T>(raw: unknown): T | undefined {
return Array.isArray(raw) ? (raw[0] as T) : (raw as T);
}
Loading