-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
140 lines (110 loc) · 3.81 KB
/
Copy pathindex.js
File metadata and controls
140 lines (110 loc) · 3.81 KB
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
import 'dotenv/config'
import { Base64 } from "js-base64"
import core from '@actions/core'
import GitHub from './lib/github.js'
import BitBucket from './lib/bitbucket.js'
const GH_ACCESS_TOKEN = core.getInput('GH_ACCESS_TOKEN') || process.env.GH_ACCESS_TOKEN
const bitbucketUsername = core.getInput('BITBUCKET_USERNAME') || process.env.BITBUCKET_USERNAME
const bitbucketPassword = core.getInput('BITBUCKET_PASSWORD') || process.env.BITBUCKET_PASSWORD
if (!GH_ACCESS_TOKEN) {
core.setFailed('Auth Token not provided');
}
const makeGraph = (percentage) => {
const doneBlock = '█'
const emptyBlock = '░'
const percentageRound = (percentage.toFixed() / 4).toFixed()
let graph = ''
for (let i = 0; i < 25; i++) {
if (i < percentageRound) {
graph += doneBlock
} else {
graph += emptyBlock
}
}
return graph
}
const createList = (dataList) => {
const total = Object.values(dataList).reduce((a, b) => a + b);
let list = ''
for (const [key, value] of Object.entries(dataList)) {
const spaces = (str, num) => {
let s = ''
const ln = str.length
for (let i = 0; i < (num - ln); i++) {
s += ' '
}
return s
}
const commitTotal = `${value} commit${value === 1 ? '' : 's'}`
const percentage = ((value / total) * 100)
list += `${key}:${spaces(key, 13)}${commitTotal}${spaces(commitTotal, 15)}${makeGraph(percentage)}${spaces('', 5)}${percentage.toFixed(2)}%\n\n`
}
return list
}
const run = async () => {
let times = {
morning: 0, // 6 - 12
daytime: 0, // 12 - 18
evening: 0, // 18 - 24
night: 0 // 0 - 6
}
let weekdays = {
Monday: 0,
Tuesday: 0,
Wednesday: 0,
Thursday: 0,
Friday: 0,
Saturday: 0,
Sunday: 0
}
const gh = new GitHub(GH_ACCESS_TOKEN)
await gh.getCommitCount(times, weekdays)
if (bitbucketUsername && bitbucketPassword) {
const bb = new BitBucket({
username: bitbucketUsername,
password: bitbucketPassword,
})
try {
await bb.getCommitCount(times, weekdays)
} catch (err) {
console.log('Failed to get Bitbucket commits')
}
}
const owner = await gh.getLogin()
const weekdaysList = createList(weekdays)
const timesList = createList(times)
const key = `GITHUB STATS`
const commentBegin = `<!-- `
const commentEnd = ` -->`
const readme = await gh.getReadme()
let content = Base64.decode(readme.data.content)
const beginTokenPattern = `(${commentBegin}\\s*?${key} START\\s*?${commentEnd}.*?[\\r\\n]+)`;
const endTokenPattern = `(${commentBegin}\\s*?${key} END\\s*?${commentEnd})`;
const contentPattern = '[\\s\\S]*?';
const sourceContent = '### Weekday stats\n<pre>' + weekdaysList + '</pre>\n\n\n ### Time of day stats\n<pre>' + timesList + '</pre>'
const RX = new RegExp(`${beginTokenPattern}${contentPattern}${endTokenPattern}`, 'g');
const newContent = content.replace(RX, `$1${sourceContent}$2`)
const contentEncoded = Base64.encode(newContent);
try {
await gh.createOrUpdateFileContents({
owner: owner,
repo: owner,
path: 'README.md',
message: "chore: Added README.md programatically",
content: contentEncoded,
sha: readme.data.sha,
committer: {
name: `Octokit Bot`,
email: "kodieupton@gmail.com",
},
author: {
name: "Octokit Bot",
email: "kodieupton@gmail.com",
},
});
console.log('Readme updated')
} catch (error) {
console.error(error.message);
}
}
run();