-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
135 lines (112 loc) · 2.98 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
const octokit = require('@octokit/rest')()
const Dashboard = require('./dashboard')
const { startServer } = require('./server')
const token = process.env.TOKEN
if (!token) {
throw new Error('Github TOKEN is required')
}
octokit.authenticate({
type: 'oauth',
token
})
const repos = require('fs')
.readFileSync(process.argv[2] || './repos.txt', 'utf8')
.split('\n')
.filter(x => x)
.map(line => {
const pattern = /([^ /\\]+)[/ ]([^ /\\]+)(.git)?$/
const match = line.match(pattern)
if (!match) {
throw new Error(`Cannot read repo: ${line}`)
}
return {
owner: match[1],
repo: match[2]
}
})
if (require.main === module) {
// delay starting work to prevent restart&crash cycle
console.log('Delayed start...')
setTimeout(() => {
doWork(repos, parseInt(process.argv[2]) || 30000)
}, 5000)
startServer()
}
async function doWork (repos, interval) {
let newRepos = repos
try {
newRepos = await buildDashboard(repos)
} catch (e) {
console.error('Unable to fetch data', e)
}
setTimeout(() => {
doWork(newRepos, interval)
}, interval)
}
async function buildDashboard (repos) {
const commitsForRepos = await collectCommits(repos)
const writer = new Dashboard()
await writer.start()
for (const repo of commitsForRepos) {
const solutions = findSolutions(repo.commits)
await writer.addRow(repo.owner, repo.repo, solutions)
}
await writer.end()
return commitsForRepos
}
function findSolutions (commits) {
commits = commits || []
return commits.reduce((solutions, commit) => {
const solution = /\[\s*#\s*(\d+(.\d+)?)\s*\]/g
let match;
while (match = solution.exec(commit.message)) {
const res = parseFloat(match[1])
if (!isNaN(res)) {
solutions[res] = commit
}
}
return solutions
}, {})
}
exports.findSolutions = findSolutions;
function extractCommitData (commit) {
return {
sha: commit.sha,
url: commit.url,
message: commit.commit.message,
name: commit.commit.author.name,
email: commit.commit.author.email
}
}
function collectCommits (repos) {
return Promise.all(repos.map(async previousData => {
const { owner, repo, commits } = previousData
try {
const lastCommit = await octokit.repos.getCommits({
owner,
repo,
per_page: 1
})
// Nothing changed
if (commits && commits[0] && lastCommit.data[0].sha === commits[0].sha) {
return previousData
}
// fetch all commits
let response = await octokit.repos.getCommits({
owner,
repo,
per_page: 100
})
let newCommits = response.data.map(extractCommitData)
while (octokit.hasNextPage(response)) {
response = await octokit.getNextPage(response)
newCommits = newCommits.concat(response.data.map(extractCommitData))
}
previousData.commits = newCommits
return previousData
} catch (e) {
console.error(e);
return previousData;
}
}))
}