This repository was archived by the owner on Jul 3, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 35
/
Copy pathhelpers.js
executable file
·176 lines (152 loc) · 4.46 KB
/
helpers.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
const Github = require('github-api');
const co = require('co');
const ENV = process.env;
const GITHUB_TOKEN = ENV.GITHUB_TOKEN;
const GIT_AUTHOR_NAME = ENV.GIT_AUTHOR_NAME || 'Enki Leaderboard Bot';
const GIT_AUTHOR_EMAIL = ENV.GIT_AUTHOR_EMAIL || 'luke@enki.com';
const GIT_COMMIT_MESSAGE = ENV.GIT_COMMIT_MESSAGE ||
'Updating the leaderboard';
export const LEADERBOARD_COUNT = ENV.LEADERBOARD_COUNT || 10;
export const GIT_BASE_BRANCH = ENV.GIT_BASE_BRANCH || 'master';
export const GIT_HEAD_BRANCH = ENV.GIT_HEAD_BRANCH || 'update-leaderboard';
export const ENKI_USERS = ['enkibot', 'lukem512', 'tomwmarshall', 'spypsy',
'mathieudutour', 'charlottebretonsch', 'kirillongithub', 'jordanfish',
'Loopiezlol'];
const github = new Github({
token: GITHUB_TOKEN,
auth: 'oauth',
});
export const repo = github.getRepo('enkidevs', 'commit');
export function checkGithubToken() {
return (GITHUB_TOKEN || false);
}
function excludeUser(commits, user) {
return commits.filter(c =>
((c.author || {}).login || c.commit.author.name) !== user);
}
// The data is paginated, retrieve it all, 100 at a time
// A page sized by 100 is the maximum allowed by GitHub
export function retrieveCommits(since, until, excluded = ENKI_USERS) {
/* eslint-disable camelcase */
return co(function* () {
const per_page = 100;
let commits = [];
let found = 0;
let page = 0;
do {
const pageComm = yield repo.listCommits({
since,
until,
page,
per_page,
});
commits = commits.concat(pageComm.data);
found = pageComm.data.length;
page++;
} while (found === per_page);
excluded.forEach(user => {
commits = excludeUser(commits, user);
});
return commits;
});
/* eslint-enable camelcase */
}
export function createLeaderboardMarkdown(data) {
const header = '| Rank | User | Commits |\n|------|------|---------|';
let table = header;
data.some((o, i) => {
table = table +
'\n|' + o.rank + (o.joint ? '=' : '') +
'|[' + o.user + '](https://github.com/' + o.user + ')' +
'|' + o.commits + '|';
return i >= (LEADERBOARD_COUNT - 1);
});
return table;
}
// Standard "1224" ranking
export function standardCompetitionScoring(commits) {
// Count the number of commits by each user
const counts = commits.reduce((acc, cur) => {
const user = (cur.author || {}).login || cur.commit.author.name;
if (acc[user]) {
if (acc[user].indexOf(cur.sha) === -1) {
acc[user].push(cur.sha);
}
} else {
acc[user] = [cur.sha];
}
return acc;
}, {});
// Count the number of users with a given score
const usersWithCount = {};
Object.keys(counts).forEach(user => {
const count = counts[user].length;
usersWithCount[count] = usersWithCount[count] + 1 || 1;
});
// Create a rank for each score
const sortedCounts = Object.keys(usersWithCount).sort((a, b) => b - a);
const ranks = {};
let start = 1;
sortedCounts.forEach(c => {
ranks[c] = {
start,
count: usersWithCount[c],
};
start += usersWithCount[c];
});
// Return the formatted data
const sortedUsers = Object.keys(counts).sort((a, b) =>
counts[b].length - counts[a].length);
return sortedUsers.map(user => {
const n = counts[user].length;
return {
rank: ranks[n].start,
user,
commits: n,
joint: ranks[n].count > 1,
};
});
}
export function createPR(head, base, title, body) {
return repo.createPullRequest({
head,
base,
title,
body,
});
}
export function writeFile(path, contents, head = GIT_HEAD_BRANCH, pr = false) {
return co(function* () {
const author = {
name: GIT_AUTHOR_NAME,
email: GIT_AUTHOR_EMAIL,
date: new Date().toISOString(),
};
const sameHead = (head === GIT_BASE_BRANCH);
if (!sameHead) {
const branches = yield repo.listBranches();
let found = false;
branches.data.some(branch => {
found = (branch.name === head);
return found;
});
if (!found) {
yield repo.createBranch(GIT_BASE_BRANCH, head);
}
}
yield repo.writeFile(
head, path, contents, GIT_COMMIT_MESSAGE, {
author,
committer: author,
});
if (pr && !sameHead) {
yield createPR(head, GIT_BASE_BRANCH,
GIT_COMMIT_MESSAGE, contents)
.catch(err => {
console.log(err);
console.log('Issuing PR failed, is one already open?');
});
}
return true;
});
}