mirror of
https://github.com/hanzo-apps/hanzo.ai.git
synced 2026-10-29 07:20:43 +00:00
Public repository totals stay as they were. On top of them, the grid now adds zeekay's and zooqueen's private commits, by day, with no private repository name kept. That is 6,688 commits. The year is 131,059. [email protected] on Hanzo repositories is zeekay's address and is already inside that count. Co-authored-by: z <[email protected]>
108 lines
4.3 KiB
JavaScript
108 lines
4.3 KiB
JavaScript
/**
|
|
* Private commits this token can read, by day, with no repository name kept.
|
|
*
|
|
* GitHub will not list the private repositories, so they never enter the
|
|
* public census. It will say, for zeekay and zooqueen, which days those
|
|
* private commits landed. Those days are added to the public grid. A public
|
|
* commit is not added here: it is already in the repository totals.
|
|
*
|
|
* [email protected] is zeekay's address on the Hanzo repositories. It is inside
|
|
* zeekay's count. The address also appears on commits by other people; those
|
|
* are not ours and are not added.
|
|
*/
|
|
import { readFileSync, writeFileSync } from "node:fs";
|
|
import { dirname, join } from "node:path";
|
|
import { fileURLToPath } from "node:url";
|
|
|
|
export const PEOPLE = ["zeekay", "zooqueen"];
|
|
const QUERY = `query($login:String!,$from:DateTime!,$to:DateTime!){
|
|
user(login:$login){
|
|
contributionsCollection(from:$from,to:$to){
|
|
commitContributionsByRepository(maxRepositories:100){
|
|
repository { isPrivate }
|
|
contributions(first:100){ nodes { commitCount occurredAt } }
|
|
}
|
|
}
|
|
}
|
|
}`;
|
|
|
|
async function week(token, login, from, to) {
|
|
const res = await fetch("https://api.github.com/graphql", {
|
|
method: "POST",
|
|
headers: { authorization: `Bearer ${token}`, "content-type": "application/json", "user-agent": "hanzo-oss" },
|
|
body: JSON.stringify({ query: QUERY, variables: { login, from, to } }),
|
|
});
|
|
if (!res.ok) throw new Error(`${login} ${res.status}`);
|
|
const body = await res.json();
|
|
if (body.errors) throw new Error(body.errors.map((e) => e.message).join("; "));
|
|
const rows = body.data.user.contributionsCollection.commitContributionsByRepository;
|
|
const days = {};
|
|
for (const row of rows) {
|
|
if (!row.repository.isPrivate) continue;
|
|
for (const node of row.contributions.nodes ?? []) {
|
|
const day = node.occurredAt.slice(0, 10);
|
|
days[day] = (days[day] ?? 0) + node.commitCount;
|
|
}
|
|
}
|
|
return days;
|
|
}
|
|
|
|
/** Private commit counts by UTC day, between fromIso and toIso inclusive. */
|
|
export async function privateDaily(token, fromIso, toIso) {
|
|
const added = {};
|
|
const byLogin = {};
|
|
if (!token) return { added, byLogin, commits: 0 };
|
|
const start = Date.parse(`${fromIso}T00:00:00Z`);
|
|
const end = Date.parse(`${toIso}T00:00:00Z`) + 86400000;
|
|
for (const login of PEOPLE) {
|
|
let n = 0;
|
|
for (let t = start; t < end; t += 7 * 86400000) {
|
|
const next = Math.min(t + 7 * 86400000, end);
|
|
const days = await week(token, login, new Date(t).toISOString(), new Date(next).toISOString());
|
|
for (const [day, c] of Object.entries(days)) {
|
|
added[day] = (added[day] ?? 0) + c;
|
|
n += c;
|
|
}
|
|
}
|
|
byLogin[login] = n;
|
|
console.log(` private: ${login} ${n}`);
|
|
}
|
|
const commits = Object.values(added).reduce((a, b) => a + b, 0);
|
|
return { added, byLogin, commits };
|
|
}
|
|
|
|
const here = dirname(fileURLToPath(import.meta.url));
|
|
if (process.argv[1]?.endsWith("private-days.mjs")) {
|
|
const token = process.env.GITHUB_TOKEN || process.env.GH_TOKEN;
|
|
if (!token) {
|
|
console.error("GITHUB_TOKEN is not set");
|
|
process.exit(1);
|
|
}
|
|
const root = join(here, "..", "lib", "data");
|
|
const activity = JSON.parse(readFileSync(join(root, "oss-activity.json"), "utf8"));
|
|
const stats = JSON.parse(readFileSync(join(root, "oss-stats.json"), "utf8"));
|
|
const { added, byLogin, commits } = await privateDaily(token, activity.from, activity.to);
|
|
for (const [day, n] of Object.entries(added)) activity.daily[day] = (activity.daily[day] ?? 0) + n;
|
|
const sum = (days) => {
|
|
let n = 0;
|
|
const end = Date.parse(`${activity.to}T00:00:00Z`);
|
|
for (let i = 0; i < days; i++) {
|
|
const day = new Date(end - i * 86400000).toISOString().slice(0, 10);
|
|
n += activity.daily[day] ?? 0;
|
|
}
|
|
return n;
|
|
};
|
|
const year = Object.entries(activity.daily)
|
|
.filter(([day]) => day >= activity.from && day <= activity.to)
|
|
.reduce((a, [, n]) => a + n, 0);
|
|
stats.commits.year = year;
|
|
stats.commits.month = sum(30);
|
|
stats.commits.week = sum(7);
|
|
stats.private = { commits, people: byLogin };
|
|
stats.read = new Date().toISOString();
|
|
activity.read = stats.read;
|
|
writeFileSync(join(root, "oss-activity.json"), `${JSON.stringify(activity, null, 2)}\n`);
|
|
writeFileSync(join(root, "oss-stats.json"), `${JSON.stringify(stats, null, 2)}\n`);
|
|
console.log(` private: ${commits} commits added; year is now ${year}`);
|
|
}
|