Skip to content
Merged
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
34 changes: 33 additions & 1 deletion src/inngest/functions/process-installation-event.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import { describe, it, expect, vi, beforeEach } from 'vitest';
import { processInstallationEvent } from './process-installation-event';
import {
processInstallationEvent,
processInstallationReposEvent,
} from './process-installation-event';
import { sb, wire, step } from './__tests__/test-helpers';

// Mock external dependencies.
Expand All @@ -13,6 +16,11 @@ vi.mock('../client', () => ({
},
}));

const reposRun = processInstallationReposEvent as unknown as (ctx: {
event: { data: { payload: Record<string, unknown> } };
step: typeof step;
}) => Promise<unknown>;

// Handler references.
const installRun = processInstallationEvent as unknown as (ctx: {
event: { data: { payload: Record<string, unknown> } };
Expand Down Expand Up @@ -121,4 +129,28 @@ describe('processInstallationEvent', () => {
}),
);
});
it('repositories_added uses upsert to support webhook replays', async () => {
const repos = sb({
upsert: vi.fn().mockResolvedValue({ error: null }),
});

wire({
installation_repositories: repos,
});

await reposRun({
event: {
data: {
payload: {
action: 'added',
installation: { id: 100 },
repositories_added: [{ full_name: 'myorg/repo-a' }],
},
},
},
step,
});

expect(repos.upsert).toHaveBeenCalled();
});
});
5 changes: 4 additions & 1 deletion src/inngest/functions/process-installation-event.ts
Original file line number Diff line number Diff line change
Expand Up @@ -220,11 +220,14 @@ export const processInstallationReposEvent = inngest.createFunction(
if (!sb) throw new Error('service role missing');

if (payload.repositories_added?.length) {
await sb.from('installation_repositories').insert(
await sb.from('installation_repositories').upsert(
payload.repositories_added.map((r) => ({
installation_id: payload.installation.id,
repo_full_name: r.full_name,
})),
{
onConflict: 'installation_id,repo_full_name',
},
);
// Fan-out a per-repo backfill for each new repo so the maintainer
// queue picks them up without waiting for the next cron tick.
Expand Down
Loading