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
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",
},
});
}