-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgh.js
59 lines (47 loc) · 1.7 KB
/
gh.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
const axios = require('axios');
const fs = require('fs');
let client;
async function getGHRepo(slug) {
let result;
try {
result = await client.get(`/repos/${slug}`);
} catch(e) {
console.log('---');
console.error(`Couldn't get repo ${slug}`);
console.error(e && e.response && e.response.data);
console.log('---');
return;
}
return result.data;
}
async function getTopContributorsDetails(url, limit) {
const contributors = await client.get(url);
let users = [];
for (let i = 0; i < limit && i < contributors.data.length; ++i) {
const user = await client.get(contributors.data[i].url);
users.push(user.data);
}
return users;
}
async function start(token, repos) {
client = axios.create({
baseURL: `https://api.github.com`,
headers: {
'User-Agent': 'FilterReposNodeApp',
'Authorization': `token ${token}`
}
});
let headers = 'Repository Name, Contributor1 name, Contributor1 email, Contributor1 profile, Contributor2 name, Contributor2 email, Contributor2 profile, Contributor3 name, Contributor3 email, Contributor3 profile';
let csv = '';
for (let repo of repos) {
const ghRepo = await getGHRepo(repo.slug);
if (!ghRepo || ghRepo.forks === 0) continue;
let users = await getTopContributorsDetails(ghRepo.contributors_url, 3);
users = users.map(user => `${user.name}, ${user.email}, ${user.html_url}`);
csv += `\n${ghRepo.name}, ${users.join(', ')}`;
}
console.log(csv);
console.log('Writing summary CSV to ./summary.csv');
fs.writeFileSync('./summary.csv', headers + csv);
}
module.exports = start;