const CAMPSITE_API_KEY = Deno.env.get("CAMPSITE_API_KEY");
const PR_ALERTS_PROJECT_ID = "izu9x0b54xy2";
export default async function server(request: Request): Promise<Response> {
if (request.method !== "POST") {
return new Response("Method not allowed", { status: 405 });
}
try {
const payload = await request.json();
if ((payload.action === "opened" || payload.action === "closed") && payload.pull_request) {
const prName = payload.pull_request.title;
const author = payload.pull_request.user.login;
const prUrl = payload.pull_request.html_url;
const action = payload.action;
const campsiteData = {
title: `PR ${action}: "${prName}" by ${author}`,
content_markdown: `${action === "opened" ? "New" : "Closed"} PR: ${prUrl}`,
project_id: PR_ALERTS_PROJECT_ID,
};
const campsiteResponse = await fetch("https://api.campsite.com/v2/posts", {
method: "POST",
headers: {
"Content-Type": "application/json",
"Authorization": `Bearer ${CAMPSITE_API_KEY}`,
},
body: JSON.stringify(campsiteData),
});
if (!campsiteResponse.ok) {
console.error(`Campsite API responded with ${campsiteResponse.status}`);
return new Response(`Error posting to Campsite: ${campsiteResponse.status}`, { status: 500 });
}
console.log(`Posted to Campsite: PR ${action} "${prName}" by ${author}`);
return new Response("Webhook received and processed", { status: 200 });
}
return new Response("Webhook received but no action taken", { status: 200 });
} catch (error) {
console.error("Error processing webhook:", error);
return new Response("Error processing webhook", { status: 500 });
}
}