-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
index.js
executable file
·92 lines (74 loc) · 2.63 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
#!/usr/bin/env node
const axios = require('axios');
const core = require('@actions/core');
async function getReposForPage(page, org) {
const {
status,
data
} = await axios.get(`https://api.github.com/orgs/${org}/repos?per_page=100&page=${page}`);
if (status !== 200) {
throw err;
}
return data;
}
async function getAllReposForOrg(org) {
let i = 1;
let allRepos = [];
while (true) {
const repos = await getReposForPage(i, org)
allRepos = allRepos.concat(repos)
if (repos.length !== 100) {
break
}
}
return allRepos;
}
async function getAllContributorsForRepo(repo_handle) {
const {
status,
data
} = await axios.get(`https://raw.githubusercontent.com/${repo_handle}/main/.all-contributorsrc`, {
validateStatus: false,
});
if (status === 404) {
return;
} else if (status !== 200) {
throw `Could not fetch all-contributors for repo ${repo_handle}`
}
return data;
}
async function main(org) {
const allRepos = await getAllReposForOrg(org);
const getAllContributorsPromises = allRepos.map(repo => getAllContributorsForRepo(repo.full_name))
Promise.all(getAllContributorsPromises).then(allContributorFiles => {
const allContributorsDict = allContributorFiles.reduce((result, contributorFile) => {
if (contributorFile != null) {
for (contributor of contributorFile.contributors) {
if (contributor.profile in result) {
for (contributionType of contributor.contributions) {
if (result[contributor.profile].contributions.indexOf(contributionType) === -1) {
result[contributor.profile].contributions.push(contributionType)
}
}
} else {
result[contributor.profile] = contributor
}
}
}
return result;
}, {});
const allContributors = Object.values(allContributorsDict);
console.log(allContributors)
if (IS_GH_ACTION) {
core.setOutput("contributors", JSON.stringify(allContributors));
}
})
}
let IS_GH_ACTION = process.argv.length < 3;
if (IS_GH_ACTION && !core.getInput('orgName')) {
console.log("[ERROR] Must pass GitHub organisation name");
console.log("Either pass to CLI or set as input to GH Action")
console.log("Example: merge-all-contributors openclimatefix");
return;
}
main(core.getInput('orgName') || process.argv[2])