Skip to content
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

feat: retrigger check-run using v3 server endpoint #369

Merged
merged 10 commits into from
Jan 27, 2025
3 changes: 3 additions & 0 deletions src/lib/server/trigger-dev/jobs/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ config.integrationsList.forEach((org) => {
senderLogin: zod.string()
})
}),
enabled: false,
concurrencyLimit: 1,
integrations: { github },
run: async (payload, io, ctx) =>
Expand Down Expand Up @@ -140,6 +141,7 @@ if (!isDev) {
content: zod.string()
})
}),
enabled: false,
run: async (payload, io) => {
const { content } = payload;

Expand All @@ -161,6 +163,7 @@ if (!isDev) {
title: zod.string()
})
}),
enabled: false,
run: async (payload, io) => {
const { content, title } = payload;

Expand Down
50 changes: 42 additions & 8 deletions src/routes/api/submissions/+server.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { json } from '@sveltejs/kit';
import axios from 'axios';

import { dev } from '$app/environment';
import { TRIGGER_SERVER_SECRET, TRIGGER_SERVER_URL } from '$env/static/private';

import type { RequestHandler } from '@sveltejs/kit';

Expand Down Expand Up @@ -61,14 +63,15 @@ export const POST: RequestHandler = async ({ url, request, cookies }) => {
);

// get last commit
await checkRunFromEvent(
pr.org,
pr.repo,
contributor.id!,
contributor.login!,
pr.number as number
);
await triggerRequestCheckRun({
org: pr.org,
repoName: pr.repo,
senderId: contributor.id!,
senderLogin: contributor.login!,
prNumber: pr.number as number
});
}

return json({
data: await submissions.create(body!)
});
Expand Down Expand Up @@ -136,7 +139,13 @@ export const PATCH: RequestHandler = async ({ request, cookies, url }) => {

if (body!.approval === 'pending') {
// get last commit
await checkRunFromEvent(pr.org, pr.repo, body!.owner_id, user!.login, pr.number as number);
await triggerRequestCheckRun({
org: pr.org,
repoName: pr.repo,
senderId: body!.owner_id,
senderLogin: user!.login,
prNumber: pr.number as number
});
}
}

Expand All @@ -157,3 +166,28 @@ const validateDate = (dateStr: string | null | undefined): Date => {
const date = new Date(dateStr);
return Number.isNaN(date.getTime()) ? new Date() : date;
};

async function triggerRequestCheckRun(data: {
org: string;
repoName: string;
senderId: number;
senderLogin: string;
prNumber: number;
}) {
try {
const url = TRIGGER_SERVER_URL;
const secret = TRIGGER_SERVER_SECRET;

if (!url || !secret) throw new Error('Trigger server not configured');

const res = await axios.post(`${url}/api/submission-event`, data, {
headers: {
'x-trigger-server-secret': secret
}
});

if (res.status !== 200) throw new Error(res.data.message);
} catch (e) {
throw new Error((e as any)?.response?.data || 'Failed to trigger check run');
}
}