-
-
Notifications
You must be signed in to change notification settings - Fork 258
Expand file tree
/
Copy pathupdate-github-stats.mjs
More file actions
114 lines (88 loc) · 2.96 KB
/
update-github-stats.mjs
File metadata and controls
114 lines (88 loc) · 2.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
import { mkdir, readFile, writeFile } from 'node:fs/promises'
import { dirname, join } from 'node:path'
import { fileURLToPath } from 'node:url'
const __dirname = dirname(fileURLToPath(import.meta.url))
const outputPath = join(__dirname, '../.vitepress/_data/github-stats.json')
const repository = 'massCodeIO/massCode'
const startYear = 2019
async function readExistingStats() {
try {
return JSON.parse(await readFile(outputPath, 'utf-8'))
}
catch {
return {
repository,
stars: 0,
releaseDownloads: 0,
activeDevelopmentStartYear: startYear,
activeDevelopmentYears: new Date().getUTCFullYear() - startYear,
updatedAt: null,
}
}
}
function createHeaders() {
const headers = {
'Accept': 'application/vnd.github+json',
'User-Agent': 'masscode-docs-build',
'X-GitHub-Api-Version': '2022-11-28',
}
// eslint-disable-next-line node/prefer-global/process
const token = process.env.GITHUB_TOKEN || process.env.GH_TOKEN
if (token)
headers.Authorization = `Bearer ${token}`
return headers
}
async function fetchJson(url) {
const response = await fetch(url, { headers: createHeaders() })
if (!response.ok)
throw new Error(`GitHub API returned ${response.status} for ${url}`)
return response.json()
}
async function fetchAllReleases() {
const releases = []
let page = 1
while (true) {
const batch = await fetchJson(`https://api.github.com/repos/${repository}/releases?per_page=100&page=${page}`)
if (!Array.isArray(batch) || batch.length === 0)
break
releases.push(...batch)
if (batch.length < 100)
break
page += 1
}
return releases
}
function getReleaseDownloads(releases) {
return releases.reduce((total, release) => {
if (release.draft)
return total
return total + release.assets.reduce((assetTotal, asset) => assetTotal + asset.download_count, 0)
}, 0)
}
async function updateStats() {
const existingStats = await readExistingStats()
try {
const [repo, releases] = await Promise.all([
fetchJson(`https://api.github.com/repos/${repository}`),
fetchAllReleases(),
])
const stats = {
repository,
stars: repo.stargazers_count,
releaseDownloads: getReleaseDownloads(releases),
activeDevelopmentStartYear: startYear,
activeDevelopmentYears: new Date().getUTCFullYear() - startYear,
updatedAt: new Date().toISOString(),
}
await mkdir(dirname(outputPath), { recursive: true })
await writeFile(outputPath, `${JSON.stringify(stats, null, 2)}\n`)
console.log(`Updated GitHub stats: ${stats.stars} stars, ${stats.releaseDownloads} release downloads`)
}
catch (error) {
const message = error instanceof Error ? error.message : String(error)
console.warn(`Could not update GitHub stats, using existing values: ${message}`)
await mkdir(dirname(outputPath), { recursive: true })
await writeFile(outputPath, `${JSON.stringify(existingStats, null, 2)}\n`)
}
}
await updateStats()