import { difference } from "https://deno.land/std@0.214.0/datetime/mod.ts";
import { decodeBase64 } from "https://deno.land/std@0.214.0/encoding/base64.ts";
import { fetchJSON } from "https://esm.town/v/stevekrouse/fetchJSON?v=41";
export default async function(req: Request): Promise<Response> {
console.log(req.headers, req.headers.get("Authorization"));
if (!req.headers.get("Authorization")) {
return Response.json({ error: "Unauthorized" }, {
status: 401,
});
}
if (decodeBase64(req.headers.get("Authorization")) !== Deno.env.BASIC_PW) {
return Response.json({ error: "Forbidden" }, {
status: 403,
});
}
const release = await fetchJSON(
`https://api.github.com/repos/aryeohq/aryeo/releases/latest`,
);
const now = new Date();
const lastReleaseTime = new Date(release.published_at);
const lastReleaseLabel = `${release.name} by @${release.author.login}`;
const lastReleaseSecAgo = difference(lastReleaseTime, now, { units: "seconds" });
let color;
switch (lastReleaseSecAgo) {
case lastReleaseSecAgo > 0 && lastReleaseSecAgo < 15 * 60:
color = "green";
case lastReleaseSecAgo > 15 * 60 && lastReleaseSecAgo < 1 * 60 * 60:
color = "yellow";
default:
color = "default";
}
return Response.json({
color,
data: {
"name": lastReleaseLabel,
"dateValue": lastReleaseTime.toISOString(),
"dateFormat": "YYYY-MM-DDTHH:mm:ss.sssZ",
},
});
}