-
Notifications
You must be signed in to change notification settings - Fork 68
/
index.js
287 lines (267 loc) · 10.3 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
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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
// Copyright 2020 The Oppia Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS-IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
/**
* @fileoverview Entry point to the app.
*/
require('newrelic');
const scheduler = require('./lib/scheduler');
const apiForSheetsModule = require('./lib/apiForSheets');
const checkMergeConflictsModule = require('./lib/checkMergeConflicts');
const checkPullRequestLabelsModule = require('./lib/checkPullRequestLabels');
const checkPullRequestBranchModule = require('./lib/checkPullRequestBranch');
const checkPullRequestJobModule = require('./lib/checkPullRequestJob');
const checkCronJobModule = require('./lib/checkNewCronJobs');
const checkPullRequestTemplateModule = require(
'./lib/checkPullRequestTemplate'
);
const checkCriticalPullRequestModule = require(
'./lib/checkCriticalPullRequest'
);
const checkBranchPushModule = require('./lib/checkBranchPush');
const checkPullRequestReviewModule = require('./lib/checkPullRequestReview');
const ciCheckModule = require('./lib/ciChecks');
const periodicCheckModule = require('./lib/periodicChecks');
const constants = require('./constants');
const checkIssueAssigneeModule = require('./lib/checkIssueAssignee');
const whitelistedAccounts = (process.env.WHITELISTED_ACCOUNTS || '')
.toLowerCase()
.split(',');
/**
* This function checks the event type and accordingly invokes the right
* checks.
*
* @param {import('probot').Context} context
* @param {String} checkEvent
*/
const runChecks = async (context, checkEvent) => {
const repoName = context.repo().repo.toLowerCase();
const checksWhitelist = constants.getChecksWhitelist();
if (Object.prototype.hasOwnProperty.call(checksWhitelist, repoName)) {
const checks = checksWhitelist[repoName];
if (Object.prototype.hasOwnProperty.call(checks, checkEvent)) {
const checkList = checks[checkEvent];
const callable = [];
for (var i = 0; i < checkList.length; i++) {
switch (checkList[i]) {
case constants.claCheck:
callable.push(apiForSheetsModule.checkClaStatus(context));
break;
case constants.branchCheck:
callable.push(checkPullRequestBranchModule.checkBranch(context));
break;
case constants.assigneeCheck:
callable.push(checkPullRequestLabelsModule.checkAssignee(context));
break;
case constants.mergeConflictCheck:
callable.push(
checkMergeConflictsModule.checkMergeConflictsInPullRequest(
context, context.payload.pull_request
)
);
break;
case constants.allMergeConflictCheck:
callable.push(
checkMergeConflictsModule.checkMergeConflictsInAllPullRequests(
context
)
);
break;
case constants.jobCheck:
callable.push(
checkPullRequestJobModule.checkForModificationsToFiles(context));
break;
case constants.cronJobCheck:
callable.push(checkCronJobModule.checkForNewCronJob(context));
break;
case constants.modelCheck:
callable.push(
checkCriticalPullRequestModule.checkIfPRAffectsDatastoreLayer(
context
)
);
break;
case constants.issuesAssignedCheck:
callable.push(checkIssueAssigneeModule.checkAssignees(context));
break;
case constants.prLabelCheck:
callable.push(
checkPullRequestLabelsModule.checkForIssueLabel(context)
);
break;
case constants.hotfixLabelCheck:
callable.push(
checkPullRequestLabelsModule.checkHotfixLabel(context)
);
break;
case constants.datastoreLabelCheck:
callable.push(
checkPullRequestLabelsModule.checkCriticalLabel(context)
);
break;
case constants.staleBuildLabelCheck:
callable.push(
checkPullRequestLabelsModule.checkStaleBuildLabelRemoved(context)
);
break;
case constants.forcePushCheck:
callable.push(checkBranchPushModule.handleForcePush(context));
break;
case constants.prTemplateCheck:
callable.push(
checkPullRequestTemplateModule.checkTemplate(context)
);
break;
case constants.pullRequestReviewCheck:
callable.push(
checkPullRequestReviewModule.handlePullRequestReview(context)
);
break;
case constants.ciFailureCheck:
callable.push(ciCheckModule.handleFailure(context));
break;
case constants.updateWithDevelopCheck:
callable.push(
checkMergeConflictsModule.pingAllPullRequestsToMergeFromDevelop(
context
)
);
break;
case constants.periodicCheck:
callable.push(...[
periodicCheckModule.ensureAllPullRequestsAreAssigned(context),
]);
break;
case constants.respondToReviewCheck:
callable.push(
checkPullRequestReviewModule.handleResponseToReview(context)
);
break;
}
}
// Wait for all checks to resolve or reject.
await Promise.allSettled(callable);
}
}
};
/**
* This function checks if repo owner is whitelisted for Oppiabot checks.
*
* @param {import('probot').Context} context
*/
const checkWhitelistedAccounts = (context) => {
return whitelistedAccounts.includes(context.repo().owner.toLowerCase());
};
/**
* This function checks if pull request author is blacklisted for
* Oppiabot checks.
*
* @param {import('probot').Context} context
*/
const checkAuthor = (context) => {
const pullRequest = context.payload.pull_request;
const author = pullRequest.user.login;
return !constants.getBlacklistedAuthors().includes(author);
};
/**
* This is the main entry point to the Probot app
* @param {import('probot').Application} oppiabot
*/
module.exports = (oppiabot) => {
scheduler.createScheduler(oppiabot, {
delay: !process.env.DISABLE_DELAY, // delay is enabled on first run
interval: 24 * 60 * 60 * 1000, // 1 day
});
oppiabot.on('schedule.repository', async (context) => {
console.log('PERIODIC CHECKS RUNNING...');
if (checkWhitelistedAccounts(context)) {
await runChecks(context, constants.periodicCheckEvent);
}
});
oppiabot.on('issues.assigned', async (context) => {
if (checkWhitelistedAccounts(context)) {
await runChecks(context, constants.issuesAssignedEvent);
}
});
oppiabot.on('issue_comment.created', async (context) => {
if (checkWhitelistedAccounts(context)) {
// eslint-disable-next-line no-console
console.log('COMMENT CREATED ON ISSUE OR PULL REQUEST...');
await runChecks(context, constants.issueCommentCreatedEvent);
}
});
oppiabot.on('pull_request.opened', async (context) => {
// The oppiabot runs only for repositories belonging to certain
// whitelisted accounts. The whitelisted accounts are stored as an
// env variable. context.repo().owner returns the owner of the
// repository on which the bot has been installed.
// This condition checks whether the owner account is included in
// the whitelisted accounts.
if (checkWhitelistedAccounts(context) && checkAuthor(context)) {
await runChecks(context, constants.openEvent);
}
});
oppiabot.on('pull_request.reopened', async (context) => {
if (checkWhitelistedAccounts(context) && checkAuthor(context)) {
await runChecks(context, constants.reopenEvent);
}
});
oppiabot.on('pull_request.labeled', async (context) => {
if (checkWhitelistedAccounts(context) && checkAuthor(context)) {
await runChecks(context, constants.PRLabelEvent);
}
});
oppiabot.on('pull_request.unlabeled', async (context) => {
if (checkWhitelistedAccounts(context) && checkAuthor(context)) {
await runChecks(context, constants.PRUnlabelEvent);
}
});
oppiabot.on('pull_request.synchronize', async (context) => {
if (checkWhitelistedAccounts(context) && checkAuthor(context)) {
// eslint-disable-next-line no-console
console.log(' PR SYNC EVENT TRIGGERED..');
await runChecks(context, constants.synchronizeEvent);
}
});
oppiabot.on('pull_request.closed', async (context) => {
if (
checkWhitelistedAccounts(context) &&
context.payload.pull_request.merged === true &&
checkAuthor(context)
) {
// eslint-disable-next-line no-console
console.log(' A PR HAS BEEN MERGED..');
await runChecks(context, constants.closeEvent);
}
});
oppiabot.on('push', async (context) => {
if (checkWhitelistedAccounts(context)) {
// eslint-disable-next-line no-console
console.log('A BRANCH HAS BEEN PUSHED...');
await runChecks(context, constants.pushEvent);
}
});
oppiabot.on('pull_request_review.submitted', async (context) => {
if (checkWhitelistedAccounts(context)) {
console.log('A Pull Request got reviewed');
await runChecks(context, constants.pullRequestReviewEvent);
}
});
oppiabot.on('check_suite.completed', async (context) => {
if (checkWhitelistedAccounts(context)) {
// eslint-disable-next-line no-console
console.log('A CHECK SUITE HAS BEEN COMPLETED...');
await runChecks(context, constants.checkCompletedEvent);
}
});
};