Skip to content

Commit

Permalink
feat: retrigger check-run using v3 server endpoint (#369)
Browse files Browse the repository at this point in the history
* chore: disable onIssue and onIssueLabel job

* chore: disable issue comment and check run

* chore: disable remaining jobs

* feat: enable custom event

* feat: trigger check run using server api endpoint

* chore: disable custom events

* chore: remove logs
  • Loading branch information
teodorus-nathaniel authored Jan 27, 2025
1 parent 8a70c7a commit 0bfd412
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 8 deletions.
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');
}
}

0 comments on commit 0bfd412

Please sign in to comment.