This repository has been archived by the owner on Feb 28, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathcommentOnOpenedPullRequest.js
251 lines (225 loc) · 9.11 KB
/
commentOnOpenedPullRequest.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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
'use strict';
const Cesium = require('cesium');
const parseLink = require('parse-link-header');
const Promise = require('bluebird');
const requestPromise = require('request-promise');
const Settings = require('./Settings');
const Check = Cesium.Check;
const defined = Cesium.defined;
module.exports = commentOnOpenedPullRequest;
/**
* Comments on a newly opened pull request.
*
* @param {Object} body The GitHub event body.
* @param {Object} repositorySettings Headers to use for making additional GitHub requests.
* @returns {Promise} A Promise that resolves when processing is complete.
*/
function commentOnOpenedPullRequest(body, repositorySettings) {
Check.typeOf.object('body', body);
Check.typeOf.object('repositorySettings', repositorySettings);
const pullRequest = body.pull_request;
const filesUrl = `${pullRequest.url }/files`;
const commentsUrl = pullRequest.comments_url;
const userName = pullRequest.user.login;
const baseBranch = pullRequest.base.ref;
const headBranch = pullRequest.head.ref;
const headHtmlUrl = pullRequest.head.repo.html_url;
const headApiUrl = pullRequest.head.repo.url;
const repository = body.repository;
const repositoryUrl = repository.html_url;
const repositoryContributorsUrl = repository.contributors_url;
return commentOnOpenedPullRequest._implementation(filesUrl, commentsUrl, repositorySettings, userName, repositoryUrl, baseBranch, headBranch, headHtmlUrl, headApiUrl, repositoryContributorsUrl);
}
commentOnOpenedPullRequest._implementation = function (pullRequestFilesUrl, pullRequestCommentsUrl, repositorySettings, userName, repositoryUrl, baseBranch, headBranch, headHtmlUrl, headApiUrl, repositoryContributorsUrl) {
// The google sheets API will fail to initialize if any of the required settings are missing.
const claEnabled = defined(Settings.googleSheetsApi);
let askForCla = false;
let askAboutContributors = false;
let errorCla;
return repositorySettings.fetchSettings()
.then(function () {
return commentOnOpenedPullRequest._askForCla(userName);
})
.then(function (result) {
askForCla = result;
})
.catch(function(error) {
errorCla = error.toString();
})
.then(function () {
return commentOnOpenedPullRequest._askAboutContributors(userName, repositorySettings, headApiUrl, headBranch, repositoryContributorsUrl);
})
.then(function (result) {
askAboutContributors = result;
})
.then(function () {
return requestPromise.get({
url: pullRequestFilesUrl,
headers: repositorySettings.headers,
json: true
});
})
.then(function (filesJsonResponse) {
const files = filesJsonResponse.map(function (file) {
return file.filename;
});
const askAboutChanges = commentOnOpenedPullRequest._askAboutChanges(files, baseBranch);
const askAboutThirdParty = commentOnOpenedPullRequest._askAboutThirdParty(files, repositorySettings.thirdPartyFolders);
const askAboutTests = commentOnOpenedPullRequest._askAboutTests(files, repositorySettings.unitTestPath);
let contributorsUrl;
if (defined(repositorySettings.contributorsPath)) {
contributorsUrl = `${headHtmlUrl }/blob/${ headBranch }/${ repositorySettings.contributorsPath}`;
}
const message = repositorySettings.pullRequestOpenedTemplate({
userName: userName,
repository_url: repositoryUrl,
claEnabled: claEnabled,
askForCla: askForCla,
errorCla: errorCla,
askAboutChanges: askAboutChanges,
askAboutContributors: askAboutContributors,
contributorsUrl: contributorsUrl,
askAboutThirdParty: askAboutThirdParty,
thirdPartyFolders: repositorySettings.thirdPartyFolders.join(', '),
headBranch: headBranch,
askAboutTests: askAboutTests
});
return requestPromise.post({
url: pullRequestCommentsUrl,
headers: repositorySettings.headers,
body: {
body: message
},
json: true
});
});
};
commentOnOpenedPullRequest._askAboutChanges = function (files, baseBranch) {
if (baseBranch !== 'main') {
return false;
}
for (let i = 0; i < files.length; i++) {
if (/^CHANGES\.md$/.test(files[i])) {
return false;
}
}
return true;
};
function findContributorFromGitHub(userName, repositoryContributorsUrl, repositorySettingsHeaders) {
return requestPromise.get({
url: repositoryContributorsUrl,
headers: repositorySettingsHeaders,
json: true,
resolveWithFullResponse: true
})
.then(function (response) {
const contributors = response.body;
const result = contributors.find(function(contributor) {
return contributor.login === userName;
});
if (defined(result)) {
return result;
}
const linkData = parseLink(response.headers.link);
if (defined(linkData) && defined(linkData.next)) {
return findContributorFromGitHub(userName, linkData.next.url, repositorySettingsHeaders);
}
});
}
commentOnOpenedPullRequest._askAboutContributors = function (userName, repositorySettings, headApiUrl, headBranch, repositoryContributorsUrl) {
if (!defined(repositorySettings.contributorsPath) && !repositorySettings.contributorsFromGitHub) {
return Promise.resolve(false);
}
if (repositorySettings.contributorsFromGitHub) { // GitHub maintains only 500 named contributors
return findContributorFromGitHub(userName, repositoryContributorsUrl, repositorySettings.headers)
.then(function (result) {
return !defined(result);
});
}
const url = `${headApiUrl }/contents/${ repositorySettings.contributorsPath }?ref=${ headBranch}`;
return requestPromise.get({
url: url,
headers: repositorySettings.headers,
json: true
})
.then(function (response) {
const contributorsFileContent = Buffer.from(response.content, 'base64').toString();
return contributorsFileContent.indexOf(userName) === -1;
});
};
commentOnOpenedPullRequest._askAboutThirdParty = function (files, thirdPartyFolders) {
if (!defined(thirdPartyFolders) || thirdPartyFolders.length === 0) {
return false;
}
for (let i = 0; i < files.length; i++) {
for (let j = 0; j < thirdPartyFolders.length; j++) {
const folder = thirdPartyFolders[j];
if (files[i].startsWith(folder)) {
return true;
}
}
}
return false;
};
commentOnOpenedPullRequest._askAboutTests = function (files, unitTestPath) {
if (!defined(unitTestPath)) {
return false;
}
for (let i = 0; i < files.length; i++) {
const regex = new RegExp(`^${ unitTestPath}`, 'i');
if (regex.test(files[i])) {
return false;
}
}
return true;
};
commentOnOpenedPullRequest._askForCla = function (userName) {
if (!defined(Settings.googleSheetsApi)) {
return Promise.resolve(false);
}
let foundIndividualCLA = false;
let foundCorporateCLA = false;
// Check individual CLA sheet.
return Settings.googleSheetsApi.spreadsheets.values.get({
spreadsheetId: Settings.individualClaSheetID,
range: 'D2:D'
})
.then(function(response) {
const rows = response.data.values;
for (let i = 0; i < rows.length; i++) {
if(rows[i].length === 0) {
continue;
}
const rowUserName = rows[i][0].toLowerCase();
if (userName.toLowerCase() === rowUserName) {
foundIndividualCLA = true;
break;
}
}
// Now check corporate CLA sheet.
return Settings.googleSheetsApi.spreadsheets.values.get({
spreadsheetId: Settings.corporateClaSheetID,
range: 'H2:H'
});
})
.then(function(response) {
const rows = response.data.values;
for (let i = 0; i < rows.length; i++) {
if(rows[i].length === 0) {
continue;
}
let rowScheduleA = rows[i][0].toLowerCase();
// We're a little more lenient with the ScheduleA userName check, since it's an unformatted text field.
// We split the ScheduleA field by whitespace see if we can find the GitHub username in there.
rowScheduleA = rowScheduleA.replace(/\n/g, ' ');
const words = rowScheduleA.split(' ');
for (let j = 0; j < words.length; j++) {
if (userName.toLowerCase() === words[j].trim()) {
foundCorporateCLA = true;
break;
}
}
}
return !foundIndividualCLA && !foundCorporateCLA;
});
};