Public
HTTP (deprecated)
Val Town is a social website to write and deploy JavaScript.
Build APIs and schedule functions from your browser.
Readme

Github Activity Report

Generates an activity report of GitHub events for a specified user. The report groups events by day, repository, and event type, providing an organized view of the user's activities over a specified date range.

Usage

Parameters

  • userName (required): The GitHub username for which to generate the activity report.
  • startDate (optional): The start date of the activity report in YYYY-MM-DD format. If not provided, the report includes all events up to the endDate.
  • endDate (optional): The end date of the activity report in YYYY-MM-DD format. If not provided, the report includes all events from the startDate onwards.

Response Structure

The API returns a JSON response body with the following structure:

{ "YYYY-MM-DD": { "repository_name": { "event_type": [ { "id": "event_id", "created_at": "event_timestamp", "payload": { /* event payload */ } }, // More events... ] }, // More repositories... }, // More dates... }
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
import { parse } from "npm:parse-github-event";
export default async function(req: Request): Promise<Response> {
const url = new URL(req.url);
const userName = url.searchParams.get("userName");
const startDate = url.searchParams.get("startDate");
const endDate = url.searchParams.get("endDate");
if (!userName) {
return new Response(JSON.stringify({ ok: false, error: "userName query parameter is required" }), {
headers: { "Content-Type": "application/json" },
status: 400,
});
}
const response = await fetch(`https://api.github.com/users/${userName}/events`);
const events = await response.json();
if (!events) {
return new Response(JSON.stringify({ ok: false, error: "Failed to fetch events" }), {
headers: { "Content-Type": "application/json" },
status: 500,
});
}
// Convert startDate and endDate to Date objects if provided
const start = startDate ? new Date(startDate) : null;
const end = endDate ? new Date(endDate) : null;
// Filter events based on the date range if start or end date is provided
const filteredEvents = events?.filter(event => {
const eventDate = new Date(event.created_at);
if (start && end) {
return eventDate >= start && eventDate <= end;
} else if (start) {
return eventDate >= start;
} else if (end) {
return eventDate <= end;
}
return true; // No date filter applied, return all events
}) || [];
// // Aggregate the relevant information
// const activityReport = filteredEvents.reduce((report, event) => {
// const eventDate = new Date(event.created_at).toISOString().split("T")[0]; // Get the date part only
// const repoName = event.repo.name;
// const eventType = event.type;
// if (!report[eventDate]) {
// report[eventDate] = {};
// }
// if (!report[eventDate][repoName]) {
// report[eventDate][repoName] = {};
// }
// if (!report[eventDate][repoName][eventType]) {
// report[eventDate][repoName][eventType] = [];
// }
// report[eventDate][repoName][eventType].push({
// id: event.id,
// created_at: event.created_at,
// payload: event.payload,
// });
// return report;
// }, {});
return Response.json({ ok: true, body: parse(filteredEvents) });
}
elliotbraem-githubactivityreport.web.val.run
August 5, 2024