187 lines · 7.7 KB
import { env } from "cloudflare:workers";
import { all, first } from "./db";
import { branchSha, listRefs } from "./git/refs";
import { withStore } from "./git/store";
import { renderMarkdown } from "./markdown";
import { REPO_SELECT } from "./repos";
import type { RepoRow } from "~/lib/types";

const DAY_MS = 24 * 60 * 60 * 1000;
const GRAPH_WEEKS = 52;
const WEEK_DAYS = 7;
const README_NAMES = new Set(["readme.md", "readme.markdown"]);
const MAX_README_BYTES = 256 * 1024;
const README_CACHE_TTL_SECONDS = 60 * 60 * 24 * 7;
const decoder = new TextDecoder();

export type ContributionBreakdown = { commits: number; pulls: number; issues: number; reviews: number; other: number };

export type ContributionDay = {
  date: string;
  count: number;
  breakdown: ContributionBreakdown | null;
  repos: string[];
};

const MAX_DAY_REPOS = 3;
const MIN_GRAPH_YEAR = 1970;

type Category = keyof ContributionBreakdown;

function categoryOf(kind: string): Category {
  if (kind === "commit") return "commits";
  if (kind.startsWith("pull_")) return "pulls";
  if (kind.startsWith("issue_")) return "issues";
  if (kind === "comment" || kind.startsWith("review")) return "reviews";
  return "other";
}

/** Verified addresses of a user; commits authored with any of them count as theirs. */
const VERIFIED_EMAILS = `SELECT email FROM users WHERE account_id = ?1 AND email_verified_at IS NOT NULL