Skip to content

Commit

Permalink
Only handle notifying discord in webhook
Browse files Browse the repository at this point in the history
  • Loading branch information
Hacksore committed Sep 15, 2024
1 parent 6e3ac98 commit e88cdc9
Showing 1 changed file with 14 additions and 130 deletions.
144 changes: 14 additions & 130 deletions apps/api/src/handlers/webhook.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import { Artifacts, Bindings, Request, WorkflowRuns } from "../types.js";
import { GITHUB_REPO, GITHUB_USER } from "../constants.js";
import { Bindings, Request } from "../types.js";
import { Hono } from "hono/quick";
import { ReleaseCreatedEvent, WorkflowRunEvent } from "@octokit/webhooks-types";
import { WorkflowRunEvent } from "@octokit/webhooks-types";
import { Webhooks } from "@octokit/webhooks";
import { filenameToPlatform, getLatestRelease, isProd } from "../utils.js";
import { isProd } from "../utils.js";

const app = new Hono<{ Bindings: Bindings }>();

Expand Down Expand Up @@ -35,141 +34,28 @@ app.post("/webhook", async (c) => {
}

if (payload.workflow.name === "Canary") {
return await uploadCanaryArtifact({ c });
return await handleCanaryRun({ c, payload });
}
}

if ("release" in body) {
console.log("handling release event");
const payload = body as ReleaseCreatedEvent;
if (payload.action === "created") {
try {
return await uploadStableArtifacts({ c });
} catch (e) {
return c.json({ error: "Failed to upload stable artifact" });
}
}

return c.json({ error: "Not a release create event" });
}

return c.json({ error: "Unable to handle webhook" });
});

async function uploadStableArtifacts({ c }: { c: Request }) {
const releases = await getLatestRelease(c.env.GITHUB_TOKEN);
const latesetVersion = releases.tag_name;

console.log("Latest version", latesetVersion);

const versions = releases.assets
.map((asset) => ({
name: asset.name,
url: asset.browser_download_url,
platform: filenameToPlatform(asset.name),
}))
.filter((asset) => asset.name.match(/\.(dmg|msi|AppImage)$/));

// upload all the files to r2 in the stable folder
for (const asset of versions) {
console.log(`Downloading ${asset.name}`, asset.url);
const fileData = await fetch(asset.url).then((res) => res.arrayBuffer());
await c.env.BUCKET.put(`stable/${latesetVersion}/${asset.name}`, fileData);
}

return c.json({ success: true, updated: new Date().toISOString() });
}

// TODO: https://developers.cloudflare.com/workers/runtime-apis/streams/
async function uploadCanaryArtifact({ c }: { c: Request }) {
const canaryWorkflowRunsResponse = await fetch(
`https://api.github.com/repos/${GITHUB_USER}/${GITHUB_REPO}/actions/workflows/canary.yaml/runs`,
{
cf: {
cacheTtl: 300,
cacheEverything: true,
},
headers: {
Authorization: `token ${c.env.GITHUB_TOKEN}`,
"User-Agent": "overlayed-updater v1",
},
},
);

// all workflow runs for canary
const workflowRuns =
(await canaryWorkflowRunsResponse.json()) as WorkflowRuns;

// find the most recent successful run
const successfulRun = workflowRuns.workflow_runs.find(
(run) => run.conclusion === "success",
);

if (!successfulRun?.artifacts_url) {
return c.json(
{
error: "No artifacts found",
},
500,
);
}

const uploadedArtifacts = (await fetch(successfulRun.artifacts_url, {
cf: {
cacheTtl: 300,
cacheEverything: true,
},
headers: {
Authorization: `token ${c.env.GITHUB_TOKEN}`,
"User-Agent": "overlayed-updater v1",
},
}).then((res) => res.json())) as Artifacts;

const uploaded = [];
// download all the files and upload them to r2
for (const artifact of uploadedArtifacts.artifacts) {
console.log(`Downloading ${artifact.name}`, artifact.archive_download_url);
const signedUrlResponse = await fetch(artifact.archive_download_url, {
headers: {
Accept: "application/vnd.github+json",
Authorization: `Bearer ${c.env.GITHUB_TOKEN}`,
"X-GitHub-Api-Version": "2022-11-28",
"User-Agent": "overlayed-updater v1",
},
redirect: "follow",
});

// NOTE: this issue forces us to do this
// https://github.com/cloudflare/workerd/issues/2223
if (signedUrlResponse.redirected) {
const fileData = await fetch(signedUrlResponse.url).then((res) =>
res.arrayBuffer(),
);
await c.env.BUCKET.put(`canary/${artifact.name}`, fileData);

uploaded.push(artifact.name);
}
}

// upload a manifest with the last update and "version"
await c.env.BUCKET.put(
"canary/latest.json",
JSON.stringify({
updated: new Date().toISOString(),
latestVersion: successfulRun.head_sha,
}),
);

console.log("uploaded manifest", uploaded);

// inform discord role that we have a new canary version
async function handleCanaryRun({
c,
payload,
}: {
c: Request;
payload: WorkflowRunEvent;
}) {
const sha = payload.workflow_run.head_sha;
await fetch(c.env.CANARY_WEBHOOK_URL, {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({
content: `<@&1266782388168560952> New Overlayed Canary version uploaded [${successfulRun.head_sha}](<https://github.com/overlayeddev/overlayed/commit/${successfulRun.head_sha}>)
content: `<@&1266782388168560952> New Overlayed Canary version uploaded [${sha}](<https://github.com/overlayeddev/overlayed/commit/${sha}>)
You can download it on [overlayed.dev/canary](<https://overlayed.dev/canary>)
`,
Expand All @@ -178,9 +64,7 @@ You can download it on [overlayed.dev/canary](<https://overlayed.dev/canary>)

return c.json(
{
uploaded,
updated: new Date().toISOString(),
latestVersion: successfulRun.head_sha,
status: "success",
},
200,
);
Expand Down

0 comments on commit e88cdc9

Please sign in to comment.