Val Town is a social website to write and deploy JavaScript.
Build APIs and schedule functions from your browser.
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
// This approach uses the GitHub API without authentication, which is subject to rate limiting.
// We'll fetch and process the GitHub activity data, and render it as a nicely designed website
// using Vue for templating and Tailwind for styling.
// Tradeoff: May hit rate limits, but doesn't require setting up a personal access token.
import { html } from "https://deno.land/x/html/mod.ts";
const GITHUB_USERNAME = "ejfox";
async function fetchGitHubEvents(username: string) {
const response = await fetch(`https://api.github.com/users/${username}/events/public`, {
headers: {
"User-Agent": "Val-Town-Script",
},
});
if (!response.ok) {
throw new Error(`GitHub API request failed: ${response.status}`);
}
return await response.json();
}
function processEvents(events: any[]) {
return events
.filter(event => {
const eventDate = new Date(event.created_at);
const oneWeekAgo = new Date();
oneWeekAgo.setDate(oneWeekAgo.getDate() - 7);
return eventDate >= oneWeekAgo;
})
.map(event => {
const baseInfo = {
type: event.type,
repo: event.repo.name,
created_at: event.created_at,
};
switch (event.type) {
case "PushEvent":
return {
...baseInfo,
commits: event.payload.commits.map((commit: any) => ({
message: commit.message,
sha: commit.sha,
})),
};
case "IssuesEvent":
return {
...baseInfo,
action: event.payload.action,
issue: {
title: event.payload.issue.title,
number: event.payload.issue.number,
},
};
case "PullRequestEvent":
return {
...baseInfo,
action: event.payload.action,
pullRequest: {
title: event.payload.pull_request.title,
number: event.payload.pull_request.number,
},
};
case "CreateEvent":
return {
...baseInfo,
ref_type: event.payload.ref_type,
};
case "DeleteEvent":
return {
...baseInfo,
ref_type: event.payload.ref_type,
};
case "GistEvent":
return {
...baseInfo,
action: event.payload.action,
gist: {
id: event.payload.gist.id,
description: event.payload.gist.description,
},
};
default:
return baseInfo;
}
})
.sort((a, b) => new Date(b.created_at).getTime() - new Date(a.created_at).getTime());
}
export default async function main(req: Request): Promise<Response> {
try {
const events = await fetchGitHubEvents(GITHUB_USERNAME);
const recentActivity = processEvents(events);
const template = html`
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
ejfox-githubactivity.web.val.run
August 16, 2024