Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save Siddhant-K-code/83a22b3f9988394193e81c8a33b2335e to your computer and use it in GitHub Desktop.
Save Siddhant-K-code/83a22b3f9988394193e81c8a33b2335e to your computer and use it in GitHub Desktop.
Daily GitHub Activity of a user in an organization
// npm install ocotokit dotenv
// Configure GITHUB_API_KEY with `repo` scope
// node main.js
// Output fill we saved in {today's date}.md file
import { Octokit } from "octokit";
import fs from "fs";
import dotenv from "dotenv";
import util from "util";
dotenv.config();
util.inspect.defaultOptions.depth = null;
const apiKey = process.env.GITHUB_API_KEY;
const octokit = new Octokit({ auth: apiKey });
// The org that the contains the events you want to include
const org = "gitpod-io";
const today = new Date()
.toLocaleString("sv", { timeZoneName: "short" })
.substring(0, 10);
let userLogin = "";
async function main() {
if (!apiKey) {
console.log("Please set the GITHUB_API_KEY environment variable");
} else {
const {
data: { login },
} = await octokit.rest.users.getAuthenticated();
userLogin = login;
let page = 1;
let data = await getListItems(page);
// GH rate limits after 4 pages, but it's probably always enough to cover the current day
while (page < 4) {
const nextData = await getListItems(page);
data = [...data, ...nextData];
page++;
}
const list = data.reduce((acc, curr) => {
const item = formatEvent(curr);
if (item && !acc.includes(item)) {
acc += `${item}\n`;
}
return acc;
}, "");
fs.writeFile(`./${today}.md`, list, (err) => {
if (err) {
console.log(err);
}
});
console.log("Here is your GitHub activity for the day: \n");
console.log(list);
}
}
async function getListItems(page) {
const result = await octokit.request("GET /users/{username}/events", {
username: userLogin,
per_page: 100,
page: page,
});
const data = result.data.filter((event) => {
return event.repo.name.includes(org) && event.created_at.includes(today);
});
return data;
}
function formatEvent(event) {
const { type, payload } = event;
switch (type) {
case "IssuesEvent":
if (payload.action === "opened") {
return `:issue-new: Opened - ${payload.issue.title}: ${payload.issue.html_url}`;
}
case "PullRequestReviewEvent":
if (payload.action === "created") {
return `:mag: Reviewed - ${payload.pull_request.title}: ${payload.pull_request.html_url}`;
}
case "PullRequestEvent":
if (payload.action === "opened") {
return `:pr-open: Opened - ${payload.pull_request.title}: ${payload.pull_request.html_url}`;
} else if (
payload.action === "closed" &&
payload.pull_request &&
payload.pull_request.merged
) {
return `:pr-merged: Merged - ${payload.pull_request.title}: ${payload.pull_request.html_url}`;
} else if (
payload.action === "closed" &&
payload.pull_request &&
!payload.pull_request.merged
) {
return `:pr-closed: Closed - ${payload.pull_request.title}: ${payload.pull_request.html_url}`;
}
}
}
main();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment