-
Notifications
You must be signed in to change notification settings - Fork 3
/
github.js
298 lines (266 loc) · 9.64 KB
/
github.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
288
289
290
291
292
293
294
295
296
297
298
import fs from 'node:fs';
import path from 'node:path';
import url from 'node:url';
import commonGithubService from '../../common/services/github.js';
import { logger } from '../../common/services/logger.js';
import ScalingoClient from '../../common/services/scalingo-client.js';
import { config } from '../../config.js';
import * as reviewAppRepo from '../repository/review-app-repository.js';
const repositoryToScalingoAppsReview = {
'pix-api-data': ['pix-api-data-integration'],
'pix-bot': ['pix-bot-review'],
'pix-data': ['pix-airflow-review'],
'pix-data-api-pix': ['pix-data-api-pix-integration'],
'pix-db-replication': ['pix-datawarehouse-integration'],
'pix-db-stats': ['pix-db-stats-review'],
'pix-editor': ['pix-lcms-review'],
'pix-epreuves': ['pix-epreuves-review'],
'pix-site': ['pix-site-review', 'pix-pro-review'],
'pix-tutos': ['pix-tutos-review'],
'pix-ui': ['pix-ui-review'],
pix: ['pix-api-review', 'pix-audit-logger-review', 'pix-front-review'],
pix4pix: ['pix-4pix-front-review', 'pix-4pix-api-review'],
};
const __dirname = url.fileURLToPath(new URL('.', import.meta.url));
function getMessageTemplate(repositoryName) {
const baseDir = path.join(__dirname, '..', 'templates', 'pull-request-messages');
let relativeFileName;
if (fs.existsSync(path.join(baseDir, `${repositoryName}.md`))) {
relativeFileName = `${repositoryName}.md`;
} else {
relativeFileName = 'default.md';
}
const messageTemplate = fs.readFileSync(path.join(baseDir, relativeFileName), 'utf8');
return messageTemplate;
}
function getMessage(repositoryName, pullRequestId, scalingoReviewApps, messageTemplate) {
const scalingoDashboardUrl = `https://dashboard.scalingo.com/apps/osc-fr1/${scalingoReviewApps[0]}-pr${pullRequestId}/environment`;
const shortenedRepositoryName = repositoryName.replace('pix-', '');
const webApplicationUrl = `https://${shortenedRepositoryName}-pr${pullRequestId}.review.pix.fr`;
const message = messageTemplate
.replaceAll('{{pullRequestId}}', pullRequestId)
.replaceAll('{{webApplicationUrl}}', webApplicationUrl)
.replaceAll('{{scalingoDashboardUrl}}', scalingoDashboardUrl);
return message;
}
const _addMessageToPullRequest = async ({ repositoryName, pullRequestId, scalingoReviewApps }, { githubService }) => {
const messageTemplate = getMessageTemplate(repositoryName);
const message = getMessage(repositoryName, pullRequestId, scalingoReviewApps, messageTemplate);
await githubService.commentPullRequest({
repositoryName,
pullRequestId,
comment: message,
});
};
async function _handleRA(
request,
scalingoClient = ScalingoClient,
addMessageToPullRequest = _addMessageToPullRequest,
githubService = commonGithubService,
reviewAppRepository = reviewAppRepo,
) {
const payload = request.payload;
const prId = payload.number;
const ref = payload.pull_request.head.ref;
const repository = payload.pull_request.head.repo.name;
const reviewApps = repositoryToScalingoAppsReview[repository];
const { shouldContinue, message } = _handleNoRACase(request);
if (!shouldContinue) {
return message;
}
const deployedRA = await deployPullRequest(
scalingoClient,
reviewApps,
prId,
ref,
repository,
addMessageToPullRequest,
githubService,
reviewAppRepository,
);
return `Triggered deployment of RA on app ${deployedRA.join(', ')} with pr ${prId}`;
}
async function _handleCloseRA(request, scalingoClient = ScalingoClient) {
const payload = request.payload;
const prId = payload.number;
const repository = payload.pull_request.head.repo.name;
const reviewApps = repositoryToScalingoAppsReview[repository];
if (!reviewApps) {
return `${repository} is not managed by Pix Bot.`;
}
let client;
const closedRA = [];
try {
client = await scalingoClient.getInstance('reviewApps');
} catch (error) {
throw new Error(`Scalingo auth APIError: ${error.message}`);
}
for (const appName of reviewApps) {
const reviewAppName = `${appName}-pr${prId}`;
try {
const reviewAppExists = await client.reviewAppExists(reviewAppName);
if (reviewAppExists) {
await client.deleteReviewApp(reviewAppName);
closedRA.push({ name: appName, isClosed: true, isAlreadyClosed: false });
} else {
closedRA.push({ name: appName, isClosed: false, isAlreadyClosed: true });
}
} catch (error) {
logger.error({
event: 'review-app',
stack: error.stack,
message: `Deletion of application ${reviewAppName} failed : ${error.message}`,
data: {
repository,
reviewApp: reviewAppName,
pr: prId,
},
});
}
}
const result = closedRA.map((ra) =>
ra.isAlreadyClosed ? `${ra.name}-pr${prId} (already closed)` : `${ra.name}-pr${prId}`,
);
return `Closed RA for PR ${prId} : ${result.join(', ')}.`;
}
async function deployPullRequest(
scalingoClient,
reviewApps,
prId,
ref,
repository,
addMessageToPullRequest,
githubService,
reviewAppRepository,
) {
const deployedRA = [];
let client;
try {
client = await scalingoClient.getInstance('reviewApps');
} catch (error) {
throw new Error(`Scalingo auth APIError: ${error.message}`);
}
for (const appName of reviewApps) {
const reviewAppName = `${appName}-pr${prId}`;
try {
const reviewAppExists = await client.reviewAppExists(reviewAppName);
if (reviewAppExists) {
await client.deployUsingSCM(reviewAppName, ref);
} else {
await reviewAppRepository.create({ name: reviewAppName, repository, prNumber: prId, parentApp: appName });
await client.deployReviewApp(appName, prId);
await client.disableAutoDeploy(reviewAppName);
await client.deployUsingSCM(reviewAppName, ref);
}
deployedRA.push({ name: appName, isCreated: !reviewAppExists });
} catch (error) {
logger.error({
event: 'review-app',
stack: error.stack,
message: `Deployement for application ${reviewAppName} failed : ${error.message}`,
data: {
repository,
reviewApp: reviewAppName,
pr: prId,
ref,
},
});
}
}
if (deployedRA.length === 0) {
throw new Error(`No RA deployed for repository ${repository} and pr${prId}`);
}
await githubService.addRADeploymentCheck({ repository, prNumber: prId, status: 'pending' });
if (deployedRA.some(({ isCreated }) => isCreated)) {
await addMessageToPullRequest(
{
repositoryName: repository,
scalingoReviewApps: reviewApps,
pullRequestId: prId,
},
{ githubService },
);
logger.info({
event: 'review-app',
message: `Created RA for repo ${repository} PR ${prId}`,
});
}
logger.info({
event: 'review-app',
message: `PR${prId} deployement triggered on RA for repo ${repository}`,
});
return deployedRA.map(({ name }) => name);
}
async function _pushOnDefaultBranchWebhook(request, scalingoClient = ScalingoClient) {
const branchName = request.payload.ref.split('/').slice(-1)[0];
if (request.payload.repository.default_branch != branchName) {
return `Ignoring push event on branch ${branchName} as it is not the default branch`;
}
const repositoryName = request.payload.repository.name;
if (!(repositoryName in config.scalingo.repositoryToScalingoIntegration)) {
return `Ignoring push event on repository ${repositoryName} as it is not configured`;
}
const scalingoApps = config.scalingo.repositoryToScalingoIntegration[repositoryName];
const client = await scalingoClient.getInstance('integration');
for (const applicationName of scalingoApps) {
try {
await client.deployFromArchive(applicationName, branchName, repositoryName, { withEnvSuffix: false });
} catch (error) {
throw new Error(`Error during Scalingo deployment of application ${applicationName} : ${error.message}`);
}
}
return `Deploying branch ${branchName} on integration applications : ` + scalingoApps.join(', ');
}
async function processWebhook(
request,
{
pushOnDefaultBranchWebhook = _pushOnDefaultBranchWebhook,
handleRA = _handleRA,
handleCloseRA = _handleCloseRA,
} = {},
) {
const eventName = request.headers['x-github-event'];
if (eventName === 'push') {
return pushOnDefaultBranchWebhook(request);
} else if (eventName === 'pull_request') {
if (['opened', 'reopened', 'synchronize'].includes(request.payload.action)) {
return handleRA(request);
}
if (request.payload.action === 'closed') {
return handleCloseRA(request);
}
return `Ignoring ${request.payload.action} action`;
} else {
return `Ignoring ${eventName} event`;
}
}
function _handleNoRACase(request) {
const payload = request.payload;
const repository = payload.pull_request.head.repo.name;
const reviewApps = repositoryToScalingoAppsReview[repository];
const isFork = payload.pull_request.head.repo.fork;
const labelsList = payload.pull_request.labels;
const state = payload.pull_request.state;
if (isFork) {
return { message: 'No RA for a fork', shouldContinue: false };
}
if (!reviewApps) {
return { message: 'No RA configured for this repository', shouldContinue: false };
}
if (labelsList.some((label) => label.name == 'no-review-app')) {
return { message: 'RA disabled for this PR', shouldContinue: false };
}
if (state !== 'open') {
return { message: 'No RA for closed PR', shouldContinue: false };
}
return { shouldContinue: true };
}
export {
_addMessageToPullRequest as addMessageToPullRequest,
getMessage,
getMessageTemplate,
_handleRA as handleRA,
processWebhook,
_pushOnDefaultBranchWebhook as pushOnDefaultBranchWebhook,
_handleCloseRA as handleCloseRA,
};