Val Town is a social website to write and deploy JavaScript.
Build APIs and schedule functions from your browser.
Readme

This val creates a GitHub webhook listener that posts pull request information to Campsite when a pull request is opened or closed.

Campsite API docs: https://campsite.com/docs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
/**
* GitHub PR Alerts
*
* This val creates a GitHub webhook listener that posts pull request information
* to Campsite when a pull request is opened or closed.
*
* To use this in your organization:
* 1. Fork this val
* 2. Create a custom integration in your Campsite organization settings [1]
* 3. Add the API token as an environment variable named `CAMPSITE_API_TOKEN` in Val Town
* 4. Add the project ID where you want to create alerts as `PR_ALERTS_PROJECT_ID`
* 5. Copy the HTTP endpoint for this Val and add it as a webhook in your GitHub settings [2]
* 6. Optionally customize the output in `campsiteData`
*
* [1] https://app.campsite.co/<org-slug>/settings/integrations
* [2] https://github.com/<username>/<repo>/settings/hooks
*
* Campsite API docs:
* https://campsite.com/docs
*/
// These environment variables are required:
const CAMPSITE_API_KEY = Deno.env.get("CAMPSITE_API_KEY");
// Set these to your own values:
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;
// Prepare the data for Campsite API
const campsiteData = {
title: `PR ${action}: "${prName}" by ${author}`,
content_markdown: `${action === "opened" ? "New" : "Closed"} PR: ${prUrl}`,
project_id: PR_ALERTS_PROJECT_ID,
};
// Send POST request to Campsite API
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 });
}
}
campsite-githubpralert.web.val.run
September 18, 2024