-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
76 lines (60 loc) · 2 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
require('dotenv').config();
const {
TOKEN,
API_URL,
REPO_OWNER,
REPO_NAME,
REPO_BRANCH
} = process.env;
const fs = require('fs-extra');
const path = require('path');
const csv = require('csv');
const { Readable, Duplex } = require('stream');
const fetch = require('./fetch');
const evaluate = require('./evaluate');
const main = async () => {
try {
const PRs = await fetch({
apiUrl: API_URL,
token: TOKEN,
repoBranch: REPO_BRANCH,
repoName: REPO_NAME,
repoOwner: REPO_OWNER
});
console.log(`%o records found.`, PRs.length);
const evaluation = await evaluate(PRs);
fs.ensureDirSync(path.join(__dirname, 'out'));
fs.writeFileSync(path.join(__dirname, 'out', 'evaluation.json'), JSON.stringify(evaluation, null, '\t'));
const arrayOfUsers = Object.keys(evaluation).map(username => ({ username, ...evaluation[username] }));
const arrayOfPRs = [];
await Promise.all(
arrayOfUsers.map(
async user => await Promise.all(
user.contributions.map(
async contrib => arrayOfPRs.push({ Contributor: user.username, Contribution: contrib.title, Date: contrib.date })
)
)
)
);
arrayOfUsers.sort((a, b) => b.count - a.count);
arrayOfPRs.sort((a, b) => Date.parse(b.Date) - Date.parse(a.Date));
for (const user of arrayOfUsers) {
console.log(`${user.username}: %o`, user.count);
}
const evaluationWriteStream = fs.createWriteStream(path.join(__dirname, 'out', 'evaluation.csv'));
const contributionsWriteStream = fs.createWriteStream(path.join(__dirname, 'out', 'contributions.csv'));
const evaluationReadStream = Readable.from(arrayOfUsers);
const contributionsReadStream = Readable.from(arrayOfPRs);
evaluationReadStream
.pipe(csv.transform(o => ({ Contributor: o.username, Score: o.count })))
.pipe(csv.stringify({ header: true }))
.pipe(evaluationWriteStream);
contributionsReadStream
.pipe(csv.stringify({ header: true }))
.pipe(contributionsWriteStream);
} catch (e) {
console.error(e);
}
};
if (require.main === module)
main();