-
Notifications
You must be signed in to change notification settings - Fork 3
/
trueContributors-mixin.js
230 lines (210 loc) · 11.5 KB
/
trueContributors-mixin.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
const trueContributors = {
/**
* Method to fetch contributors list based on number of issue comments and commits across an organization
* @param {String} parameters [Parameters to be used in GitHub API request]
* @return {Array} [Array of GitHub users with data about how many commit and comment contributions they made to an organization]
*/
async listCommitCommentContributorsForOrg(parameters) {
let desiredParams = this._createParamsFromObject(["org", "type"], parameters);
let repos = await this.paginate(this.repos.listForOrg, desiredParams);
let contributors = [];
for(let repo of repos) {
let repoContributors = await this.listCommitCommentContributors({owner: repo.owner.login, repo: repo.name, ...parameters});
contributors = contributors.concat(repoContributors);
}
return this._aggregateContributors(contributors);
},
/**
* Method to fetch commit contributors across an organization
* @param {String} parameters [Parameters to be used in GitHub API request]
* @return {Array} [Array of GitHub users with data about how many commits they made to an organization]
*/
async listCommitContributorsForOrg(parameters) {
let desiredParams = this._createParamsFromObject(["org", "type"], parameters);
let repos = await this.paginate(this.repos.listForOrg, desiredParams);
let contributors = [];
for(let repo of repos) {
let repoContributors = await this.listCommitContributors({owner: repo.owner.login, repo: repo.name, ...parameters});
contributors = contributors.concat(repoContributors);
}
return this._aggregateContributors(contributors);
},
/**
* Method to fetch commit contributors across an organization
* @param {String} parameters [Parameters to be used in GitHub API request]
* @return {Array} [Array of GitHub users with data about how many commits they made to an organization]
*/
async listContributorsForOrg(parameters) {
let desiredParams = this._createParamsFromObject(["org", "type"], parameters);
let repos = await this.paginate(this.repos.listForOrg, desiredParams);
let contributors = [];
for(let repo of repos) {
let repoContributors = []
try {
repoContributors = await this.paginate(this.repos.listContributors, { owner: repo.owner.login, repo: repo.name, ...parameters });
} catch(err) {
// If the error message is not regarding an empty repo, then propagate error
let res = await this.repos.listContributors({ owner: repo.owner.login, repo: repo.name, ...parameters });
if(res.status != 204 || res.headers.status != "204 No Content") throw err;
}
contributors = contributors.concat(repoContributors);
}
return this._aggregateContributors(contributors);
},
/**
* Method to fetch contributors list based on number of issue comments across an organization
* @param {String} parameters [Parameters to be used in GitHub API request]
* @return {Array} [Array of GitHub users with data about how many issue comments they made to an organization]
*/
async listCommentContributorsForOrg(parameters) {
let desiredParams = this._createParamsFromObject(["org", "type"], parameters);
let repos = await this.paginate(this.repos.listForOrg, desiredParams);
let contributors = [];
for(let repo of repos) {
let repoContributors = await this.listCommentContributors({ owner: repo.owner.login, repo: repo.name, ...parameters });
contributors = contributors.concat(repoContributors);
}
return this._aggregateContributors(contributors);
},
/**
* Method to fetch contributors list based on number of issue comments and commits
* @param {String} parameters [Parameters to be used in GitHub API request]
* @return {Array} [Array of GitHub users with data about how many contributions they made]
*/
async listCommitCommentContributors(parameters) {
let desiredParams = this._createParamsFromObject(["owner", "repo", "since"], parameters);
// If since is a parameter, use listCommitContributors method. If not, use octokit's faster listContributors endpoint
let contributors = [];
try {
contributors = (desiredParams.since) ?
await this.listCommitContributors(desiredParams) :
await this.paginate(this.repos.listContributors, desiredParams);
} catch(err) {
// Check to see if error through from Status Code 204 from repos.listContributors
if(desiredParams.since) throw err;
let res = await this.repos.listContributors(desiredParams);
if(res.status != 204 || res.headers.status != "204 No Content") throw err;
}
let commentContributors = await this.listCommentContributors(desiredParams);
return this._aggregateContributors(contributors.concat(commentContributors));
},
/**
* Method to fetch contributors list based on number of commits
* @param {String} parameters [Parameters to be used in GitHub API request]
* @return {Array} [Array of GitHub users with data about how many commits they made]
*/
async listCommitContributors(parameters) {
let desiredParams = this._createParamsFromObject(["owner", "repo", "sha", "path", "since", "until"], parameters);
let commits = [];
try {
commits = await this.paginate(this.repos.listCommits, desiredParams);
} catch(err) {
// If the error message is not regarding an empty repo, then propagate error
if(err.status != 409 || err.message != "Git Repository is empty.") {
throw err;
}
}
return this._aggregateContributions(commits, "author");
},
/**
* Method to fetch contributors list based on number of issue comments
* @param {String} parameters [Parameters to be used in GitHub API request]
* @return {Array} [Array of GitHub users with data about how many comments they made]
*/
async listCommentContributors(parameters) {
let desiredParams = this._createParamsFromObject(["owner", "repo", "since"], parameters);
let issueComments = await this.paginate(this.issues.listCommentsForRepo, desiredParams);
return this._aggregateContributions(issueComments, "user");
},
/**
* Helper method to aggregate GitHub contributor objects
* @param {Array} contributors [Array of GitHub contributor objects]
* @return {Array} [Array of GitHub users with data about how many contributions they made]
*/
_aggregateContributors(contributors) {
// Use JSON to create a dictionary of users and their contributions
let contributorDictionary = {};
for(let contributor of contributors) {
if(!contributor.hasOwnProperty("contributions")) throw `Error: contributor ${JSON.stringify(contributor)} has no property contributions`;
if(!contributor.hasOwnProperty("id")) throw `Error: contributor ${JSON.stringify(contributor)} has no property id`;
// If user id for this comment exists in dictionary, add a contribution to that user
if(contributor.id in contributorDictionary) {
contributorDictionary[contributor.id].contributions += contributor.contributions;
// If user id for this comment does not exist, add user to dictionary
} else {
contributorDictionary[contributor.id] = contributor
}
}
// Convert JSON dictionary to a list of users
return this._contributorDictToArr(contributorDictionary);
},
/**
* Helper method to aggregate GitHub contribution objects
* @param {Array} contributions [Array of GitHub contribution objects]
* @param {String} contributionIdentifier [Porperty name used in contribution object that represents contributor]
* @return {Array} [Array of GitHub users with data about how many contributions they made]
*/
_aggregateContributions(contributions, contributionIdentifier) {
if(!contributionIdentifier) throw "Error: no contribution identifier was given to _aggregateContributions";
// Use JSON to create a dictionary of users and their contributions
let contributorDictionary = {};
for(let contribution of contributions) {
if(!contribution.hasOwnProperty(contributionIdentifier)) throw `Error: contribution ${JSON.stringify(contribution)} has no property ${contributionIdentifier}`;
let contributor = contribution[contributionIdentifier];
// Contributions can have a null author, so we should ignore those.
if(!contributor) continue;
// If user id for this comment exists in dictionary, add a contribution to that user
if(contributor.id in contributorDictionary) {
contributorDictionary[contributor.id].contributions++;
// If user id for this comment does not exist, add user to dictionary with one contribution
} else {
contributorDictionary[contributor.id] = contributor
contributorDictionary[contributor.id].contributions = 1;
}
}
// Convert JSON dictionary to a list of users
return this._contributorDictToArr(contributorDictionary);
},
/**
* Helper method to fetch desired parameters from a given parameters object
* @param {Array} desiredParameters [Array of parameter names]
* @param {String} givenObject [Object containing parameter values]
* @return {Object} [Object with desired parameters]
*/
_createParamsFromObject(desiredParameters, givenObject) {
let parameters = {};
for(parameter of desiredParameters) {
let parameterValue = givenObject[parameter];
if(parameterValue) parameters[parameter] = parameterValue;
}
return parameters;
},
/**
* Helper method to convert a dictionary of contributors to an array of contributors
* @param {Object} dictionary [JSON contributor dictionary to be converted to array]
* @return {Array} [Array of users sorted by their contributions]
*/
_contributorDictToArr(dictionary) {
if(!dictionary) throw `Error: user dictionary is not defined`;
let array = [];
for(let item in dictionary) {
array.push(dictionary[item]);
}
return array.sort(this._sortByContributions);
},
/**
* Helper method to provide a sorting function based on a property called "contributions"
* @return {Integer} [An integer used to determine order between contribution comparison]
*/
_sortByContributions(a, b) {
if(a["contributions"] == undefined || b["contributions"] == undefined) throw `Error: tried to sort by contributions while object has no contribution property`;
if (a["contributions"] < b["contributions"]) {
return 1;
}
if (a["contributions"] > b["contributions"]) {
return -1;
}
return 0;
}
}
module.exports = trueContributors;