-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathhelper.js
164 lines (142 loc) · 3.75 KB
/
helper.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
const { Octokit } = require('@octokit/core');
const CONSTANTS = require('./constants');
const octokit = new Octokit({
auth: '177151cee7eacbae233e8a9d669e1d7afcd3d009',
// auth: 'a2d642bcae8d47bd1cb23dc2cf954d7809c8da8f',
// auth: 'b3b4ece495c418c21e18f3fbdf475be40be0e986',
});
exports.delay = (time) => new Promise((resolve) => setTimeout(resolve, time));
exports.getScoreFromLabels = (labels) => {
let score = 0;
for (let i = 0; i < labels.length; i++) {
const labelName = labels[i].name.toLowerCase();
if (
labelName.includes('50') ||
labelName.includes('good first issue') ||
labelName.includes('good') ||
labelName.includes('good_first_issue') ||
labelName.includes('good-first-issue')
) {
score += 50;
break;
} else if (labelName.includes('easy') || labelName.includes('100')) {
score += 100;
break;
} else if (labelName.includes('medium') || labelName.includes('200')) {
score += 200;
break;
} else if (labelName.includes('hard') || labelName.includes('500')) {
score += 500;
break;
} else {
score += 0;
}
}
return score;
};
exports.getAllPullRequestsOfRepository = async (repoOwner, repoName) => {
let listOfAllPullRequests = [];
let fetch = true;
let i = 1;
while (fetch) {
let response;
try {
response = await octokit.request('GET /repos/{owner}/{repo}/pulls', {
owner: repoOwner,
repo: repoName,
state: 'closed',
per_page: 50,
page: i,
});
} catch (err) {
console.log('Error in getAllPullRequestsOfRepository: \n', err);
fetch = false;
}
// prevent unnecessary api call on error
if (response && response.status !== 200) {
fetch = false;
}
if (
response &&
response.status === 200 &&
response.data &&
response.data.length === 0
) {
fetch = false;
}
if (response) {
listOfAllPullRequests.push(...response.data);
}
i++;
}
return listOfAllPullRequests;
};
exports.getAllIssuesOfRepository = async (repoOwner, repoName) => {
let listOfAllIssues = [];
let fetch = true;
let i = 1;
while (fetch) {
let response;
try {
response = await octokit.request('GET /repos/{owner}/{repo}/issues', {
owner: repoOwner,
repo: repoName,
state: 'all',
per_page: 50,
page: i,
});
} catch (err) {
console.log('Error in getAllIssuesOfRepository: \n', err);
fetch = false;
}
// prevent unnecessary api call on error
if (response && response.status !== 200) {
fetch = false;
}
if (
response &&
response.status === 200 &&
response.data &&
response.data.length === 0
) {
fetch = false;
}
if (response) {
listOfAllIssues.push(...response.data);
}
i++;
}
return listOfAllIssues;
};
exports.getIssuesWithCrosswocLabel = (listOfAllIssues) => {
const listOfCrosswocIssues = [];
listOfAllIssues.forEach((issue) => {
if (issue && issue.labels) {
issue.labels.forEach((label) => {
if (CONSTANTS.validCrossWocLabels.includes(label.name.toLowerCase())) {
listOfCrosswocIssues.push(issue);
}
});
}
});
return listOfCrosswocIssues;
};
exports.getFilteredPRData = (listOfAllPullRequests) =>
listOfAllPullRequests.map((pr) => ({
name: pr.title,
status: pr.state,
labels: pr.labels,
htmlUrl: pr._links.html.href,
userId: pr.user.login,
isAdmin: pr.user.site_admin,
}));
exports.addInMap = (map, id, score, prUrl) => {
if (map[id]) {
map[id].score += score;
map[id].pullrequests.push(prUrl);
} else {
map[id] = {};
map[id].score = score;
map[id].pullrequests = [prUrl];
}
};