返回 CodeWhale
project-pulse.ts
根目录 / web / lib / project-pulse.ts
1 import { fetchRepoStats, readRepoStatsSnapshot, type Face } from "./github";
2 import { getEnv } from "./kv";
3 import { RELEASE_CONTRIBUTORS, RELEASE_HELPERS, UNRELEASED_CONTRIBUTORS } from "./release-credits";
4 import { isExcluded, parseExclusions, repoActivityView, type RepoActivityView } from "./repo-activity";
5 import type { FeedItem, RepoStats } from "./types";
6
7 export interface ProjectPulse {
8 stats: RepoStats | null;
9 merged: FeedItem[];
10 faces: Face[];
11 activity: RepoActivityView | null;
12 }
13
14 export async function loadProjectPulse(mergedLimit = 6, faceLimit = 60): Promise<ProjectPulse> {
15 const env = await getEnv();
16 const [stats] = await Promise.allSettled([fetchRepoStats(env.GITHUB_TOKEN)]);
17
18 const exclusions = parseExclusions();
19 const activity = repoActivityView();
20 const merged: FeedItem[] = (activity?.pulls ?? []).slice(0, mergedLimit).map((pull) => ({
21 kind: "pull",
22 number: pull.number,
23 title: pull.title,
24 url: pull.url,
25 state: "merged",
26 author: pull.login,
27 authorAvatar: pull.avatar,
28 createdAt: pull.mergedAt,
29 updatedAt: pull.mergedAt,
30 eventAt: pull.mergedAt,
31 comments: 0,
32 labels: [],
33 firstTimeContributor: pull.firstTime,
34 }));
35
36 const seen = new Set<string>();
37 const faces: Face[] = [];
38 const recent = (activity?.people ?? []).map((person) => ({ login: person.login, avatar: person.avatar }));
39 const credited = [...UNRELEASED_CONTRIBUTORS, ...RELEASE_CONTRIBUTORS, ...RELEASE_HELPERS]
40 .map((handle) => handle.replace(/^@/, ""))
41 .map((login) => ({ login, avatar: `https://avatars.githubusercontent.com/${login}` }));
42 for (const face of [...recent, ...credited]) {
43 if (!face.login || !face.avatar || seen.has(face.login) || isExcluded(face.login, exclusions)) continue;
44 seen.add(face.login);
45 faces.push(face);
46 }
47
48 const live = stats.status === "fulfilled" && stats.value.stars !== null ? stats.value : null;
49 const known = live ?? readRepoStatsSnapshot();
50 return { stats: known.stars === null ? null : known, merged, faces: faces.slice(0, faceLimit), activity };
51 }
52
52 lines TYPESCRIPT