-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
170 lines (135 loc) Β· 4.14 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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
#!/usr/bin/env node
'use strict';
require('dotenv').config();
const { request } = require('@octokit/request');
const { userInfoFetcher } = require('./fetch');
const differenceInDays = require('date-fns/differenceInDays')
const GIST_ID = process.env.GIST_ID;
const GITHUB_TOKEN = process.env.GH_TOKEN;
const START_AT = process.env.START_AT;
const END_AT = process.env.END_AT;
const getKoDate = (date) => {
const ret = new Date(date.getTime() + (date.getTimezoneOffset() * 60 * 1000));
// μμ λ§λ€ GH Actionμ΄ λκ³ , 3μκ°μ μ¬μ λ₯Ό λμ΄ μ€μ°¨λ₯Ό μ€μ΄λλ‘ ν¨.
return new Date(ret.getFullYear(), ret.getMonth(), ret.getDate(), 12, 0, 0, 0);
}
const getToday = () => {
const current = new Date();
return getKoDate(current);
}
const getStartAt = () => {
const startAt = new Date(START_AT);
return getKoDate(startAt);
}
const getEndAt = () => {
const endAt = new Date(END_AT);
return getKoDate(endAt);
}
const now = getToday();
const startAt = getStartAt();
const endAt = getEndAt();
const main = async () => {
if (!GIST_ID) {
throw new Error('GIST_ID is not defined');
}
if (!GITHUB_TOKEN) {
throw new Error('GH_TOKEN is not defined');
}
if (!START_AT) {
throw new Error('START_AT is not defined');
}
if (!END_AT) {
throw new Error('END_AT is not defined');
}
if (getStartAt() > getEndAt()) {
throw new Error('START_AT, END_AT is not valid');
}
try {
await updateGist();
} catch (e) {
throw new Error(`cannot update gist: ${e.message}`);
}
}
const isPreviousEnlistment = () => {
return now.getTime() < startAt.getTime();
}
const isEnlistInMilitaryToday = () => {
return now.getTime() === startAt.getTime();
}
const isDischargedFromArmyToday = () => {
return endAt.getTime() <= now.getTime();
}
const isServingInMilitary = () => {
return startAt.getTime() < now.getTime() && now.getTime() < endAt.getTime();
}
const leftDayToEnlistment = () => {
return differenceInDays(startAt, now);
}
const leftDayToDischarging = () => {
return differenceInDays(endAt, now);
}
const getUsername = async () => {
const user = await userInfoFetcher(GITHUB_TOKEN).then((res) => res.data.data.viewer);
return user.name || user.login;
}
const getPercent = () => {
if(isPreviousEnlistment()) return 0;
if(isDischargedFromArmyToday()) return 100;
return parseInt((now - startAt) / (endAt - startAt) * 100);
}
const getPercentString = () => {
const percent = getPercent();
const cnt = parseInt(percent / 10)
return `π© ${percent}% : ` + Array(cnt).fill('ββββ').join('') + Array(10 - cnt).fill('ββββ').join('')
}
const updateGist = async () => {
const username = await getUsername();
const now = getToday();
const startAt = getStartAt();
const endAt = getEndAt();
console.info('now', now);
console.info('startAt', startAt);
console.info('endAt', endAt);
let msg = ''
if(isPreviousEnlistment()) {
msg = `π₯ μ
λκΉμ§ ${leftDayToEnlistment()}μΌ λ¨μμ΄μ.`;
} else if(isEnlistInMilitaryToday()) {
msg = 'πͺ μ€λ μ
λν©λλ€.';
} else if(isDischargedFromArmyToday()) {
msg = 'π μ μμ μΆνν©λλ€!';
} else if(isServingInMilitary()) {
msg = `πͺ μ μκΉμ§ ${leftDayToDischarging()}μΌ λ¨μμ΄μ!`;
}
const gistContent = [
msg,
`π μ
λμΌ: ${START_AT}`,
`π μ μμΌ: ${END_AT}`,
getPercentString(),
].join('\n') + '\n';
console.log(gistContent);
const gist = await request('GET /gists/:gist_id', {
gist_id: GIST_ID,
headers: { authorization: `token ${GITHUB_TOKEN}` },
});
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: `${username}'s Army Stats`,
content: gistContent,
},
},
gist_id: GIST_ID,
headers: { authorization: `token ${GITHUB_TOKEN}` },
}).then(() => {
console.info(`Updated Gist ${GIST_ID} with the following content:\n${gistContent}`);
});
}
main().catch((err) => {
console.error(err.message);
process.exit(1);
});