-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
376 lines (358 loc) Β· 13.4 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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
#!/usr/bin/env node
require("dotenv").config();
const { Octokit } = require('@octokit/rest');
const gitUtils = require('./utils');
const repoQuery = require('./query');
const {
WAKATIME_API_KEY: wakatimeApiKey,
GH_TOKEN: gitHubToken,
SHOW_TOTAL_TIME: showTime,
SHOW_PROFILE: showProfile,
SHORT_INFO: shortInfo,
SHOW_WAKASTAT: showWakaStat,
SHOW_COMMIT: showCommit,
SHOW_WEEK: showDayOfWeek,
SHOW_LANGUAGE: showLanguage,
SHOW_EDITORS: showEditors,
SHOW_OS: showOs,
SHOW_PROJECTS: showProject,
SHOW_LANGUAGE_PER_REPO: showLanguagePerRepo,
SHOW_UPDATE_DATE: showUpdateDate,
INPUT_COMMIT_MESSAGE: commitMessage,
} = process.env;
const octokit = new Octokit({
auth: gitHubToken,
});
const temaplteTag='```';
let readme = '';
let stats = '';
const date = new Date;
let today, traffic, user, contrib, userStat, contributed;
const gitProfile = {
userId: '',
userName: '',
userEmail: '',
}
const commitData = {
owner: '',
repo: '',
message: commitMessage || 'Update Readme with Waka Stats'
}
START_COMMENT = '<!--START_SECTION:waka-->'
END_COMMENT = '<!--END_SECTION:waka-->'
function generateBarChart(perc) {
const empty_block = "β";
const done_block = "β";
const mid_block = "β"
return `${(perc /4) < 4 && (perc /4) != 0 ? mid_block.repeat(1) : done_block.repeat((perc /4))}${empty_block.repeat((25 - (perc/4)))}`;
}
function makeStandardList (list){
let string = `${temaplteTag}text`;
for(const l of list){
let lname = l.name.length;
let ltext = l.text.length;
string = `${string}\n${l.name}${(' ').repeat(25 - lname)}${l.text}${(' ').repeat(20 - ltext)}${generateBarChart(l.percent)} ${Number(l.percent)}%\n`;
}
return `${string.substr(0, string.length - 1)}\n${temaplteTag}\n`;
}
function makeCommitList (list){
let string = `${temaplteTag}text`;
for(const l of list){
let lname = l.name.length;
let ltext = l.text.length;
string = `${string}\n${l.name}${(' ').repeat(13 - lname)}${l.text}${(' ').repeat(15 - ltext)}${generateBarChart(l.percent)} ${Number(l.percent)}%\n`;
}
return `${string.substr(0, string.length - 1)}\n${temaplteTag}\n`;
}
function initialize() {
return new Promise((resolve, reject) => {
gitUtils.gitApiGraphQl(gitHubToken, repoQuery.userInfoQuery).then(d => {
gitProfile.userId = d.response.viewer.id;
gitProfile.userEmail = d.response.viewer.email;
gitProfile.userName = d.response.viewer.login;
gitUtils.gitApi(`/repos/${gitProfile.userName}/${gitProfile.userName}/contents/README.md`, gitHubToken).then(d => {
commitData.sha = d.response.sha;
commitData.path = d.response.path;
commitData.repo = gitProfile.userName;
commitData.owner = gitProfile.userName;
const buff = new Buffer.from(d.response.content, 'base64');
const rdme = buff.toString('utf-8');
resolve(rdme)
});
});
});
}
function getRepos() {
return new Promise((resolve, reject) => {
gitUtils.gitApiGraphQl(gitHubToken, gitUtils.substitute(repoQuery.list_repos, ['$username', '$id'], [gitProfile.userName, gitProfile.userId]), {}, 'repos').then(d => {
resolve(d.repos.user.repositories);
});
});
}
function getStats() {
return new Promise((resolve, reject) => {
let prom = [];
let stats = '';
if (showTime == 'true') {
prom.push(gitUtils.wakatimeApi(`all_time_since_today?api_key=${wakatimeApiKey}`, 'today'));
}
if(showProfile == 'true') {
prom.push(gitUtils.gitApi(`repos/${gitProfile.userName}/${gitProfile.userName}/traffic/views?per=week`, gitHubToken, 'traffic'));
}
if(shortInfo == 'true') {
prom.push(gitUtils.gitApi(`user`, gitHubToken, 'user'));
prom.push(gitUtils.gitApi(`https://github-contributions.now.sh/api/v1/${gitProfile.userName}`, gitHubToken, 'contrib', true));
}
if (showWakaStat == 'true') {
prom.push(gitUtils.wakatimeApi(`stats/last_30_days?api_key=${wakatimeApiKey}`, 'userStat'));
}
if (showCommit == 'true') {
prom.push(gitUtils.gitApiGraphQl(gitHubToken, gitUtils.substitute(repoQuery.contributedQuery, '$username', gitProfile.userName), {}, 'contributed'));
}
Promise.all(prom).then(values => {
let statistics = '';
let contribution = '';
let traffics = '';
let userInfo = '';
let repos = [];
[today, traffic, user, contrib, userStat, contributed] = values.map((el, key) => {
return values[key][Object.keys(el)]
})
if(today) {
statistics = `![Code Time](http://img.shields.io/badge/Code_Time-${today.text.replace(/\s/g, "%20")}-blue) `
}
if(contrib){
let contr = 0;
if(contrib.years.length > 0) {
contrib.years.forEach(c => {
contr = contr + c.total;
})
}
contribution = `π ${contrib.years[0].total} Personal contributions in the last year\n\nπ‘οΈ ${contr} Total contributions when i start a github profile\n`
}
if(traffic) {
traffics = `![Profile Views](http://img.shields.io/badge/Profile_Views-${traffic.count}-red)\n\n`;
}
if(user) {
userInfo = `πΎ ${gitUtils.convertData(user.disk_usage)} Used in Github's Storage\n\n${user.hireable ? 'Not Opted to hire' : 'π« Not Opted to hire'}\n\nπ ${user.public_repos} Public repos \n\nπ ${user.total_private_repos !== null ? user.total_private_repos : 0} Private repos \n\nπ ${user.followers} Followers \n\nπ ${user.following} Following \n`;
}
stats = `${statistics}${traffics} **π€ My Personal GitHub Info** \n\n${temaplteTag}properties\n${contribution.trimLeft()}\n${userInfo}\n${temaplteTag}`;
if(contributed) {
repos = contributed.user.repositoriesContributedTo.nodes.filter(a => !a.isFork);
}
const obj = {
repos,
stats,
userStat
}
resolve(obj);
}).catch (error => {
console.log(error);
});
});
}
function generateCommitList(repos, stat) {
return new Promise((resolve, reject) => {
if(showCommit !== 'true') {
resolve();
}
let string = ``;
let sumAll = 0;
let sum_week = 0;
const weekDay = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"];
const dayPeriod = {
morning: 0,
daytime: 0,
evening: 0,
night: 0,
}
const dayOfWeek = {
Sunday: 0,
Monday: 0,
Tuesday: 0,
Wednesday: 0,
Thursday: 0,
Friday: 0,
Saturday: 0,
}
const prom =[];
for (let r of repos) {
prom.push(gitUtils.gitApiGraphQl(gitHubToken, gitUtils.substitute(repoQuery.createCommitQuery, ['$owner', '$name', '$id'], [gitProfile.userName, r.name, gitProfile.userId])));
}
Promise.all(prom).then(values => {
const no_activity = "No Activity Tracked This Week";
let target = [];
for (let c of values) {
if (c.response.repository.defaultBranchRef != null) {
target = target.concat(c.response.repository.defaultBranchRef.target.history.edges)
}
}
for (let c of target) {
const date = new Date(c.node.committedDate);
const hour = date.getHours();
const day = weekDay[date.getDay()];
dayOfWeek[day] += 1;
if (hour <= 0 && hour < 6) {
dayPeriod.night += 1;
} else if (hour <= 6 && hour < 12) {
dayPeriod.morning += 1;
} else if (hour <= 12 && hour < 18) {
dayPeriod.daytime += 1;
} else if (hour <= 18 && hour < 24) {
dayPeriod.evening += 1;
}
}
sumAll = Object.values(dayPeriod).reduce((a, b) => a + b);
sum_week = Object.values(dayOfWeek).reduce((a, b) => a + b);
const one_day = [{
name: `π Morning`,
text: `${dayPeriod.morning} commits`,
percent: ((dayPeriod.morning / sumAll) * 100).toFixed(2)
},
{
name: `π Daytime`,
text: `${dayPeriod.daytime} commits`,
percent: ((dayPeriod.daytime / sumAll) * 100).toFixed(2)
},
{
name: `π Evening`,
text: `${dayPeriod.evening} commits`,
percent: ((dayPeriod.evening / sumAll) * 100).toFixed(2)
},
{
name: `π Night`,
text: `${dayPeriod.night} commits`,
percent: ((dayPeriod.night / sumAll) * 100).toFixed(2)
}];
let dayTitle;
dayTitle = dayPeriod.morning + dayPeriod.daytime > dayPeriod.evening + dayPeriod.night ? 'I am human π¨βπ»' : 'Maybe i am a vampire π§'
string = `${string}\nπ **${dayTitle}** \n${makeCommitList(one_day)}\n`;
const day_of_week = [{
name: `Monday`,
text: `${dayOfWeek.Monday} commits`,
percent: ((dayOfWeek.Monday / sum_week) * 100).toFixed(2)
},
{
name: `Tuesday`,
text: `${dayOfWeek.Tuesday} commits`,
percent: ((dayOfWeek.Tuesday / sum_week) * 100).toFixed(2)
},
{
name: `Wednesday`,
text: `${dayOfWeek.Wednesday} commits`,
percent: ((dayOfWeek.Wednesday / sum_week) * 100).toFixed(2)
},
{
name: `Thursday`,
text: `${dayOfWeek.Thursday} commits`,
percent: ((dayOfWeek.Thursday / sum_week) * 100).toFixed(2)
},
{
name: `Friday`,
text: `${dayOfWeek.Friday} commits`,
percent: ((dayOfWeek.Friday / sum_week) * 100).toFixed(2)
},
{
name: `Saturday`,
text: `${dayOfWeek.Saturday} commits`,
percent: ((dayOfWeek.Saturday / sum_week) * 100).toFixed(2)
},
{
name: `Sunday`,
text: `${dayOfWeek.Sunday} commits`,
percent: ((dayOfWeek.Sunday / sum_week) * 100).toFixed(2)
}];
if(showDayOfWeek == 'true'){
let maxElement = {
percent : 0,
};
let dayWeekTitle;
for(let d of day_of_week) {
if(Number(d.percent) > Number(maxElement.percent)) {
maxElement = d;
}
dayWeekTitle = `**I do my best effort on** ${maxElement.name}`;
}
string = `${string}π
${dayWeekTitle}\n ${makeCommitList(day_of_week)}\n`;
}
if (showLanguage == 'true' && stat.languages) {
string = `${string}π·οΈ ***Languages*** \n${stat.languages.length > 0 ? makeStandardList(stat.languages) : no_activity}\n`;
}
if(showEditors == 'true') {
string = `${string}π§° ***Editors*** \n${stat.editors.length > 0 ? makeStandardList(stat.editors) : no_activity}\n`;
}
if(showOs == 'true') {
string = `${string}π ***Operating System*** \n${ stat.operating_systems.length > 0 ? makeStandardList(stat.operating_systems) : no_activity}\n`;
}
if(showProject == 'true') {
const projects = stat.projects;
projects.sort(function(a, b) {
return b.percent - a.percent;
});
string = `${string}π» ***Projects*** \n${ stat.projects.length > 0 ? makeStandardList(projects) : no_activity}\n`;
}
resolve(string)
});
});
}
function generateLanguagePerRepo(repos){
return new Promise((resolve, reject) => {
const lang = {};
let total = 0;
const data =[];
for(let r of repos.edges) {
if(r.node.primaryLanguage != null){
const language = r.node.primaryLanguage.name;
const color_code = r.node.primaryLanguage.color;
total += 1;
if(lang.hasOwnProperty(language) === false) {
lang[language] = {};
lang[language]['count'] = 1
} else {
lang[language]['count'] += 1
}
lang[language]['color'] = color_code;
}
}
const labels = Object.keys(lang).sort((a, b) => {return lang[b].count - lang[a].count});
const mostLanguageRepo = labels[0];
for(let label of labels){
const perc = (lang[label].count / total * 100).toFixed(2);
const extension = lang[label].count === 1 ? ' repo' : ' repos';
data.push({ name: label, text: `${String(lang[label].count)}${extension}`, percent: perc });
}
const string = `***I Mostly Code in*** ${mostLanguageRepo} \n${makeStandardList(data)}\n`;
resolve(string);
});
}
function generateNewReadme(readme, stats) {
return new Promise((resolve, reject) => {
const regex = new RegExp(`(${START_COMMENT})[^]+(${END_COMMENT})[^*]`);
const old = pat = readme.match(regex)[0];
const string = readme.replace(old, `${START_COMMENT}\n${stats}\n${END_COMMENT}\n`);
resolve(string);
});
}
(async () => {
let string = ``;
console.log(`Start on ${date.toLocaleString()}`)
readme = await initialize();
const repos = await getRepos();
stats = await getStats();
const wakaStat = await generateCommitList(stats.repos, stats.userStat);
string = `${string}${stats.stats}${wakaStat}`;
if(showLanguagePerRepo == 'true') {
string = `${string}${await generateLanguagePerRepo(repos)}\n\n`;
}
if(showUpdateDate == 'true'){
const last_update = new Date();
string = `${string}β ***Last Stats Update on***\n${last_update.toUTCString()}`;
}
const newreadme = await generateNewReadme(readme, string);
commitData.content = new Buffer.from(newreadme).toString('base64');
const result = await octokit.repos.createOrUpdateFileContents(commitData);
console.log(`Readme updated ${result.status}`);
const end_time = new Date;
console.log(`End on ${end_time.toLocaleString()}`)
console.log(`Program processed in ${Math.round((end_time.getTime() - date.getTime()) / 1000)} seconds\n`)
})();