-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
68 lines (64 loc) · 2.06 KB
/
index.js
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
async function githubActivity(username) {
const response = await fetch(
`https://api.github.com/users/${username}/events`,
{
headers: {
"User-Agent": "node.js",
},
}
);
if (!response.ok) {
if (response.status === 404) {
throw new Error("Username not found. Check username again!");
} else {
throw new Error("Error fetching data: ", response.status);
}
}
return response.json();
}
function displayActivity(events) {
if (events.length === 0) {
console.log("No recent activity found");
return;
}
events.forEach((event) => {
let action;
switch (event.type) {
case "PushEvent":
const commitCount = event.payload.commits.length;
action = `Pushed ${commitCount} commit(s) to ${event.repo.name} on ${new Date(event.created_at).toLocaleDateString("en-US",
{
weekday: "long",
year: "numeric",
month: "long",
day: "numeric" })} at ${new Date(event.created_at).toLocaleTimeString("en-US",
{
hour: "2-digit",
minute: "2-digit",
hour12: true })}`;
break;
case "CreateEvent":
action = `Created ${event.payload.ref_type} in ${event.repo.name}`;
break;
default:
action = `${event.type.replace("Event", "")} in ${
event.repo.name
}`;
break;
}
console.log(`- ${action}`);
});
}
const username = process.argv[2];
if (!username) {
console.error("Please provide a GitHub username.");
process.exit(1);
}
githubActivity(username)
.then((events) => {
displayActivity(events);
})
.catch((error) => {
console.error(error.message);
process.exit(1);
});