forked from techinpark/productive-box
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
114 lines (99 loc) 路 3.51 KB
/
index.ts
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 { resolve } from 'path';
import { config } from 'dotenv';
import { Octokit } from '@octokit/rest';
import githubQuery from './githubQuery';
import generateBarChart from './generateBarChart';
import { userInfoQuery, createContributedRepoQuery, createCommittedDateQuery } from './queries';
/**
* get environment variable
*/
config({ path: resolve(__dirname, '../.env') });
interface IRepo {
name: string;
owner: string;
}
(async() => {
/**
* First, get user id
*/
const userResponse = await githubQuery(userInfoQuery)
.catch(error => console.error(`Unable to get username and id\n${error}`));
const { login: username, id } = userResponse?.data?.viewer;
/**
* Second, get contributed repos
*/
const contributedRepoQuery = createContributedRepoQuery(username);
const repoResponse = await githubQuery(contributedRepoQuery)
.catch(error => console.error(`Unable to get the contributed repo\n${error}`));
const repos: IRepo[] = repoResponse?.data?.user?.repositoriesContributedTo?.nodes
.filter(repoInfo => (!repoInfo?.isFork))
.map(repoInfo => ({
name: repoInfo?.name,
owner: repoInfo?.owner?.login,
}));
/**
* Third, get commit time and parse into commit-time/hour diagram
*/
const committedTimeResponseMap = await Promise.all(
repos.map(({name, owner}) => githubQuery(createCommittedDateQuery(id, name, owner)))
).catch(error => console.error(`Unable to get the commit info\n${error}`));
if (!committedTimeResponseMap) return;
let morning = 0; // 6 - 12
let daytime = 0; // 12 - 18
let evening = 0; // 18 - 24
let night = 0; // 0 - 6
committedTimeResponseMap.forEach(committedTimeResponse => {
committedTimeResponse?.data?.repository?.ref?.target?.history?.edges.forEach(edge => {
const committedDate = edge?.node?.committedDate;
const timeString = new Date(committedDate).toLocaleTimeString('en-US', { hour12: false, timeZone: process.env.TIMEZONE });
const hour = +(timeString.split(':')[0]);
/**
* voting and counting
*/
if (hour >= 6 && hour < 12) morning++;
if (hour >= 12 && hour < 18) daytime++;
if (hour >= 18 && hour < 24) evening++;
if (hour >= 0 && hour < 6) night++;
});
});
/**
* Next, generate diagram
*/
const sum = morning + daytime + evening + night;
if (!sum) return;
const oneDay = [
{ label: '馃尀 Morning', commits: morning },
{ label: '馃寙 Daytime', commits: daytime },
{ label: '馃寖 Evening', commits: evening },
{ label: '馃寵 Night', commits: night },
];
const lines = oneDay.reduce((prev, cur) => {
const percent = cur.commits / sum * 100;
const line = [
`${cur.label}`.padEnd(10),
`${cur.commits.toString().padStart(5)} commits`.padEnd(14),
generateBarChart(percent, 21),
String(percent.toFixed(1)).padStart(5) + '%',
];
return [...prev, line.join(' ')];
}, []);
/**
* Finally, write into gist
*/
const octokit = new Octokit({ auth: `token ${process.env.GH_TOKEN}` });
const gist = await octokit.gists.get({
gist_id: process.env.GIST_ID
}).catch(error => console.error(`Unable to update gist\n${error}`));
if (!gist) return;
const filename = Object.keys(gist.data.files)[0];
await octokit.gists.update({
gist_id: process.env.GIST_ID,
files: {
[filename]: {
// eslint-disable-next-line quotes
filename: (morning + daytime) > (evening + night) ? "I'm an early 馃悿" : "I'm a night 馃",
content: lines.join('\n'),
},
},
});
})();