-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
index.js
108 lines (94 loc) · 3.37 KB
/
index.js
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
#!/usr/bin/env node
'use strict';
require('dotenv').config();
const { request } = require('@octokit/request');
const { userInfoFetcher, totalCommitsFetcher } = require('./fetch');
const numeral = require('numeral');
const gistId = process.env.GIST_ID;
const githubToken = process.env.GH_TOKEN;
const countAllCommits = process.env.ALL_COMMITS.toString() === 'true';
const kFormat = process.env.K_FORMAT.toString() === 'true';
async function main() {
if (!githubToken) {
throw new Error('GH_TOKEN is not defined');
}
let stats;
try {
stats = await getStats();
console.info('Successfully fetched statistics from GitHub');
console.info(JSON.stringify(stats, null, 2));
} catch (e) {
throw new Error(`cannot retrieve statistics: ${e.message}`);
}
try {
await updateGist(stats);
} catch (e) {
throw new Error(`cannot update gist: ${e.message}`);
}
}
async function getStats() {
const stats = {
name: '',
totalPRs: 0,
totalCommits: 0,
totalIssues: 0,
totalStars: 0,
contributedTo: 0,
};
const user = await userInfoFetcher(githubToken).then((res) => res.data.data.viewer);
stats.name = user.name || user.login;
stats.totalPRs = user.pullRequests.totalCount;
stats.totalIssues = user.issues.totalCount;
stats.contributedTo = user.repositoriesContributedTo.totalCount;
stats.totalStars = user.repositories.nodes.reduce((prev, curr) => {
return prev + curr.stargazers.totalCount;
}, 0);
stats.totalCommits = user.contributionsCollection.totalCommitContributions;
if (countAllCommits) {
stats.totalCommits = await totalCommitsFetcher(user.login, githubToken);
}
return stats;
}
async function updateGist(stats) {
const humanize = (n) => (n >= 1000 ? numeral(n).format(kFormat ? '0.0a' : '0,0') : n);
const gistContent =
[
['⭐', `Total Stars`, humanize(stats.totalStars)],
['➕', countAllCommits ? 'Total Commits' : 'Past Year Commits', humanize(stats.totalCommits)],
['🔀', `Total PRs`, humanize(stats.totalPRs)],
['🚩', `Total Issues`, humanize(stats.totalIssues)],
['📦', `Contributed to`, humanize(stats.contributedTo)],
]
.map((content) => {
let line = `${content[1]}:${content[2]}`;
line = line.replace(':', ':' + ' '.repeat(45 - line.length));
line = `${content[0]} ${line}`;
return line;
})
.join('\n') + '\n';
const gist = await request('GET /gists/:gist_id', {
gist_id: gistId,
headers: { authorization: `token ${githubToken}` },
});
const filename = Object.keys(gist.data.files)[0];
if (gist.data.files[filename].content === gistContent) {
console.info('Nothing to update');
return;
}
return request('PATCH /gists/:gist_id', {
files: {
[filename]: {
filename: `${stats.name}'s GitHub Stats`,
content: gistContent,
},
},
gist_id: gistId,
headers: { authorization: `token ${githubToken}` },
}).then(() => {
console.info(`Updated Gist ${gistId} with the following content:\n${gistContent}`);
});
}
main().catch((err) => {
console.error(err.message);
process.exit(1);
});