web/src/components/things/ItemGitHubMetrics.astro
145 lines · 4116 bytes
1---
2import FontAwesome from '@components/form/FontAwesome.svelte';
3import { error } from '@utils/logger';
4import { safeFetch } from '@utils/safe-fetch';
5import { fetchGitHubStats } from '@utils/fetch-repo-info';
6
7const { github } = Astro.props;
8
9interface RepoMetrics {
10 stars: number;
11 forks: number;
12 openIssues: number;
13 language: string;
14 licenseSpdx: string;
15 licenseName: string;
16}
17
18const normalizeRepo = (repo: string) =>
19 repo.replace(/^https?:\/\/github\.com\//, '').replace(/\/+$/, '');
20
21// Fallback straight to the GitHub API when our API is unavailable.
22const fromGitHub = async (repo: string): Promise<RepoMetrics | null> => {
23 const headers: Record<string, string> = { 'User-Agent': 'awesome-privacy' };
24 const apiKey = import.meta.env.GITHUB_API_KEY;
25 if (apiKey) headers['Authorization'] = `token ${apiKey}`;
26 try {
27 const res = await safeFetch(`https://api.github.com/repos/${repo}`, { headers });
28 if (!res.ok) {
29 error('GitHub API', `HTTP ${res.status} for ${repo}`);
30 return null;
31 }
32 const data = await res.json();
33 return {
34 stars: data.stargazers_count ?? 0,
35 forks: data.forks_count ?? 0,
36 openIssues: data.open_issues_count ?? 0,
37 language: data.language ?? '',
38 licenseSpdx: data.license?.spdx_id ?? '',
39 licenseName: data.license?.name ?? '',
40 };
41 } catch (e) {
42 error('GitHub API', `Network error for ${repo}: ${e}`);
43 return null;
44 }
45};
46
47// Prefer our unified API, then fall back to the GitHub API directly.
48const getMetrics = async (repo: string): Promise<RepoMetrics | null> => {
49 const stats = await fetchGitHubStats(repo);
50 if (stats) {
51 const { info } = stats;
52 return {
53 stars: info.starCount,
54 forks: info.forksCount,
55 openIssues: info.openIssues,
56 language: info.language,
57 licenseSpdx: info.license,
58 licenseName: info.licenseName,
59 };
60 }
61 return fromGitHub(normalizeRepo(repo));
62};
63
64const formatLicense = (spdx: string) => {
65 if (!spdx) return 'Unknown';
66 if (spdx === 'NOASSERTION') return 'No License';
67 return spdx;
68};
69
70const formatBigNumber = (num: number | undefined) => {
71 if (num == null) return 0;
72 if (num > 1000) {
73 return `${(num / 1000).toFixed(1)}k`;
74 }
75 return num;
76};
77
78const metrics = await getMetrics(github);
79---
80
81<div class="github-metrics">
82 <span title={`GitHub repository: ${github}`}>
83 <a href={`https://github.com/${github}`} target="_blank"><FontAwesome iconName="github" /> {github}</a>
84 </span>
85 { metrics && (
86 <span title={`GitHub Star Count: ${metrics.stars}`}>
87 <FontAwesome iconName="stars" /> {formatBigNumber(metrics.stars)}
88 </span>
89 <span title={`GitHub Fork Count: ${metrics.forks}`}>
90 <FontAwesome iconName="forks" /> {formatBigNumber(metrics.forks)}
91 </span>
92 <span title={`Open Issues / Bugs: ${metrics.openIssues}`}>
93 <FontAwesome iconName="issues" /> {formatBigNumber(metrics.openIssues)}
94 </span>
95 <span title={`Code licensed under ${metrics.licenseName}`}>
96 <FontAwesome iconName="license" /> {formatLicense(metrics.licenseSpdx)}
97 </span>
98 <span title={`Primary programming language ${metrics.language}`}>
99 <FontAwesome iconName="language" /> {metrics.language}
100 </span>
101 )}
102</div>
103
104
105<style lang="scss">
106.github-metrics {
107 display: flex;
108 align-items: center;
109 gap: var(--space-md);
110 font-size: var(--text-sm);
111 opacity: var(--opacity-muted);
112 flex-wrap: wrap;
113 span {
114 display: flex;
115 align-items: center;
116 justify-content: center;
117 color: var(--foreground);
118 gap: var(--space-xs);
119 padding: var(--space-sm) 0;
120
121 a {
122 color: var(--foreground);
123 text-decoration: none;
124 align-items: center;
125 display: flex;
126 gap: var(--space-xs);
127 transition: var(--transition-normal);
128 color: var(--accent-3-text);
129 &:hover {
130 color: var(--accent-3-text);
131 text-decoration: underline;
132 }
133 }
134
135 img {
136 border-radius: var(--curve-md);
137 }
138 }
139 :global(svg) {
140 color: var(--foreground);
141 width: var(--text-md);
142 height: var(--text-md);
143 }
144}
145</style>