-
-
Notifications
You must be signed in to change notification settings - Fork 778
/
Copy pathget-project-data.js
233 lines (203 loc) · 8.47 KB
/
get-project-data.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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
// @octokit/rest revised from v20.0.1 to v21.0.0 suggested
// by dependabot. *** Package v21.0.0 is now ESM ***
import fs from "fs";
import { Octokit } from "@octokit/rest";
import trueContributorsMixin from "true-github-contributors";
import _ from "lodash";
// Record the time this script started running so it can be stored later
const dateRan = new Date();
// Hard coded list of untagged repos we would like to fetch data on
// 79977929 -> https://github.com/hunterowens/workfor.la
const untaggedRepoIds = [79977929];
// Extend Octokit with new contributor endpoints and construct instance of class with API token
Object.assign(Octokit.prototype, trueContributorsMixin);
const octokit = new Octokit({ auth: process.env.token });
(async function main() {
let { oldGitHubData, dateLastRan } = getLocalData();
// Convert project array to map (JSON object) with repo id's as keys for more efficient lookup
oldGitHubData = projectListToMap(oldGitHubData);
let newGitHubData = [];
// Fetch all tagged and untagged hfla repositories
let allRepos = await getAllRepos();
// Fetch GitHub Data for each repository and add it to overall data
console.log(`Fetching data since: ${dateLastRan.toString()}`);
for(let repo of allRepos) {
let repoLanguages = await octokit.repos.listLanguages({ owner: repo.owner.login, repo: repo.name });
let commitContributors = await getCommitContributors(repo);
if(commitContributors){
let issueCommentContributors = await getCommentContributors(repo, (oldGitHubData.hasOwnProperty(repo.id)) ? dateLastRan.toISOString() : undefined);
console.log(`Comment contributors from ${repo.name}:`);
console.log(issueCommentContributors);
// If previous issue comment contributions data exists, aggregate old issue comment contributions data with previous issue comment contributions data
if(oldGitHubData.hasOwnProperty(repo.id)){
// "_aggregateContributors" is a helper method in the trueContributorsMixin that aggregates contributions from contributor objects based on a property "id"
issueCommentContributors = octokit._aggregateContributors( issueCommentContributors.concat(oldGitHubData[repo.id].issueComments.data) );
}
// Create a copy of commitContributors to use to aggregate with issueCommentContributors
let commitContributorsCopy = _.cloneDeep(commitContributors);
let projectContributors = octokit._aggregateContributors(commitContributorsCopy.concat(issueCommentContributors));
// Add data to new GitHub data array
newGitHubData.push({
id: repo.id,
name: repo.name,
languages: Object.keys(repoLanguages.data),
repoEndpoint: repo.url,
commitContributors: {
data: commitContributors
},
issueComments: {
data: issueCommentContributors
},
contributorsComplete: {
data: projectContributors
},
});
}
}
// Write updated data to github-data.json
writeData(newGitHubData);
})();
/**
* Retrieves data from github-data.json file
* @return {Object} [Contains old project data as "oldGitHubData" and the date this script last ran as "dateLastRan"]
*/
function getLocalData(){
let data = fs.readFileSync('_data/external/github-data.json', 'utf8');
data = JSON.parse(data);
if(Date.parse(data[0]) > 0){
let date = new Date(data[0]);
return { oldGitHubData: data.slice(1), dateLastRan: date }
}
throw new Error("No valid date value found for when script last ran.");
}
/**
* Creates map corresponding repo id's to project data given a list of projects
* @param {Array} projectList [List of project data objects]
* @return {Object} [Contains project id's from "projectList" as keys and corresponding "projectList" elements as values ]
*/
function projectListToMap(projectList) {
let projectMap = {};
for(let project of projectList){
projectMap[project.id] = project;
}
return projectMap;
}
/**
* Retrieves desired hfla repositories
* @return {Array} [Array of GitHub repository objects]
*/
async function getAllRepos() {
let tries = 0;
const delay = (length) => new Promise((resolve) => setTimeout(resolve, length));
while(tries < 4) {
try{
let allRepos = [];
let taggedRepos = await octokit.paginate(octokit.search.repos, {q: "topic:hack-for-la"});
allRepos = taggedRepos;
for(let i = 0; i < untaggedRepoIds.length; i++) {
let untaggedRepo = await octokit.request("GET /repositories/:id", { id: untaggedRepoIds[i] });
allRepos.push(untaggedRepo.data);
}
return allRepos;
} catch(error) {
if(tries === 3) {
throw error;
} else {
await delay(2**tries*120000);
tries++;
}
}
}
}
/**
* Fetches commit contributors for a given repo
* @param {Object} repo [Repository object from GitHub]
* @return {Array} [An array of contributors, sorted by the number of issue commits per contributor, in descending order]
*/
async function getCommitContributors(repo) {
// Construct parameters for request
let requestParams = constructContributorParams(repo);
try{
// Get commit contributors. listContributorsForOrg is a method from trueContributorsMixin that calls repos.listContributors across orgs in a repo
let commitContributors = (requestParams.hasOwnProperty("org")) ?
await octokit.listContributorsForOrg(requestParams) :
await octokit.paginate(octokit.repos.listContributors, requestParams);
formatContributorsList(commitContributors);
return commitContributors;
}catch(err){
console.error(err);
}
}
/**
* Fetches comment contributors for a given repo
* @param {Object} repo [Repository object from GitHub]
* @param {String} dateLastRan [ISO 8601 Date to fetch contributors from]
* @return {Array} [An array of contributors, sorted by the number of issue comments per contributor, in descending order]
*/
async function getCommentContributors(repo, dateLastRan) {
// Construct parameters for request
let requestParams = constructContributorParams(repo);
if(dateLastRan) requestParams.since = dateLastRan;
// Get comment contributors. listCommentContributors and listCommentContributorsForOrg are methods from trueContributorsMixin
let issueCommentContributors = (requestParams.hasOwnProperty("org")) ?
await octokit.listCommentContributorsForOrg(requestParams) :
await octokit.listCommentContributors(requestParams);
formatContributorsList(issueCommentContributors);
return issueCommentContributors;
}
/**
* Requests owner login for a given repo.
* @param {Object} repo [Repository object from GitHub]
* @return {Object} [An object containing parameters for making a contributors data request]
*/
function constructContributorParams(repo) {
let requestParams = {};
let isOrg = (repo.owner.type == "Organization" && repo.owner.login != "hackforla" && repo.owner.login != "codeforamerica");
if(isOrg) {
requestParams.org = repo.owner.login;
} else {
requestParams.owner = repo.owner.login;
requestParams.repo = repo.name;
}
return requestParams;
}
/**
* Removes unwanted properties for each contributors in a contributors lists in place
* @param {Array} contirbutorsList [List of contributor objects]
*/
function formatContributorsList(contributorsList){
for(let i = 0; i < contributorsList.length; i++){
const currentContributor = contributorsList[i];
contributorsList[i] = {
id: currentContributor.id,
github_url: currentContributor.html_url,
avatar_url: currentContributor.avatar_url,
gravatar_id: currentContributor.gravatar_id,
contributions: currentContributor.contributions
};
}
}
/**
* Writes project data to local
* @param {Array} projectData [List of project data to write]
*/
function writeData(projectData){
projectData.sort(sortById);
// Store the date this script finished running. dateRan is a global variable defined at the beginning of this script
projectData.unshift(dateRan.toString());
fs.writeFileSync('_data/external/github-data.json', JSON.stringify(projectData, null, 2));
}
/**
* Function to pass to JavaScript's sort method to sort project data by the id of the project repository
* @param {Object} a [Project data object]
* @param {Object} b [Project data object]
* @return {Integer} [0 if project id's are equal, positive if a.id > b.id, and negative if a.id < b.id]
*/
function sortById(a, b) {
if(a.id < b.id) {
return -1;
} else if(a.id > b.id) {
return 1;
}
return 0;
}