Repository navigation
Expand file tree
/
Copy pathsync-github.js
More file actions
57 lines (50 loc) · 2.42 KB
/
Copy pathsync-github.js
File metadata and controls
57 lines (50 loc) · 2.42 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
// Pulls stars, last push and language for every project with a public repo,
// and writes github.json for the deck's ops lines. Uses GITHUB_TOKEN if set.
const path = require('path');
const fs = require('fs');
const ROOT = path.join(__dirname, '..');
const OWNER = '00xJS';
const manifest = JSON.parse(fs.readFileSync(path.join(ROOT, 'projects.json'), 'utf8'));
async function syncGithub() {
const headers = { Accept: 'application/vnd.github+json', 'User-Agent': 'observation-deck' };
if (process.env.GITHUB_TOKEN) headers.Authorization = `Bearer ${process.env.GITHUB_TOKEN}`;
const out = { generatedAt: new Date().toISOString(), repos: {} };
const repos = [...new Set(manifest.projects.filter((p) => p.repo).map((p) => p.repo))];
for (const repo of repos) {
try {
const res = await fetch(`https://api.github.com/repos/${OWNER}/${repo}`, { headers });
if (!res.ok) throw new Error(`HTTP ${res.status}`);
const r = await res.json();
out.repos[repo] = {
stars: r.stargazers_count,
pushedAt: r.pushed_at,
language: r.language,
archived: r.archived,
};
console.log(` ${repo}: ★${r.stargazers_count}, pushed ${r.pushed_at}`);
} catch (err) {
console.error(` Error fetching ${repo}: ${err.message}`);
}
}
// Contributions in the last year: the same figure as the profile's graph. Private
// contributions are included because the profile shows them, so any token works
// (GraphQL just needs one).
if (headers.Authorization) {
try {
const query = `{ user(login: "${OWNER}") { contributionsCollection { contributionCalendar { totalContributions } } } }`;
const res = await fetch('https://api.github.com/graphql', { method: 'POST', headers, body: JSON.stringify({ query }) });
if (!res.ok) throw new Error(`HTTP ${res.status}`);
const body = await res.json();
if (body.errors) throw new Error(body.errors[0].message);
out.contributions = { total: body.data.user.contributionsCollection.contributionCalendar.totalContributions };
console.log(` contributions in the last year: ${out.contributions.total}`);
} catch (err) {
console.error(` Error counting contributions: ${err.message}`);
}
} else {
console.log(' no GITHUB_TOKEN, skipping contributions');
}
fs.writeFileSync(path.join(ROOT, 'github.json'), JSON.stringify(out, null, 2) + '\n');
console.log('Wrote github.json');
}
syncGithub();