-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
71 lines (61 loc) · 1.81 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
import { promises as fs } from "fs";
import path from "path";
import fetch from "node-fetch";
const year = new Date().getFullYear();
const main = async () => {
const template = await (
await fs.readFile(path.join(process.cwd(), "/README.template.md"))
).toString("utf-8");
const getGhCommits = async () => {
try {
const response = await fetch(`https://api.github.com/user/emails`, {
method: "GET",
headers: {
"Content-Type": "application/json",
Accept: "application/vnd.github.cloak-preview",
Authorization: `bearer ${process.env.JASHN_GH_PCT}`
}
});
let emailsList = await response.json();
const commits = await Promise.all(
emailsList.map((x) => getCommitsByEmail(x.email))
);
const commitCount = commits.reduce((acc, curr) => acc + curr);
let committedTreesText = `Committed trees(${year}): `;
if (!!commitCount) {
const readme = template.replace(
"{committed_trees}",
`${committedTreesText}${commitCount} \n ${Array(commitCount)
.fill("🌳")
.join("")}`
);
await fs.writeFile("README.md", readme);
}
} catch (err) {
console.log(err);
}
};
getGhCommits();
};
const getCommitsByEmail = async (email) => {
try {
const res = await fetch(
`https://api.github.com/search/commits?q=author-email:${email}+author-date:>${
year - 1
}`,
{
method: "GET",
headers: {
"Content-Type": "application/json",
Accept: "application/vnd.github.cloak-preview",
Authorization: `bearer ${process.env.JASHN_GH_PCT}`
}
}
);
const myCommits = await res.json();
return myCommits.total_count;
} catch (err) {
console.log(err);
}
};
main();