-
-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathretryBuild.ts
More file actions
105 lines (93 loc) · 3.53 KB
/
Copy pathretryBuild.ts
File metadata and controls
105 lines (93 loc) · 3.53 KB
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
import { admin } from '../service/firebase';
import { onRequest, Request } from 'firebase-functions/v2/https';
import { Response } from 'express-serve-static-core';
import { CiBuilds } from '../model/ciBuilds';
import { CiJobs } from '../model/ciJobs';
import { Ingeminator } from '../logic/buildQueue/ingeminator';
import { GitHub } from '../service/github';
import { Discord } from '../service/discord';
import { defineSecret } from 'firebase-functions/params';
const discordToken = defineSecret('DISCORD_TOKEN');
const githubPrivateKey = defineSecret('GITHUB_PRIVATE_KEY');
const githubClientSecret = defineSecret('GITHUB_CLIENT_SECRET');
export const retryBuild = onRequest(
{ secrets: [discordToken, githubClientSecret, githubPrivateKey] },
async (request: Request, response: Response) => {
await Discord.initSafely(discordToken.value());
try {
response.set('Content-Type', 'application/json');
// Allow pre-flight from cross origin
response.set('Access-Control-Allow-Origin', '*');
response.set('Access-Control-Allow-Methods', ['POST']);
response.set('Access-Control-Allow-Headers', ['Content-Type', 'Authorization']);
if (request.method === 'OPTIONS') {
response.status(204).send({ message: 'OK' });
return;
}
// User must be authenticated
const token = request.header('Authorization')?.replace(/^Bearer\s/, '');
console.log('token:', token);
if (!token) {
response.status(401).send({ message: 'Unauthorized' });
return;
}
// User must be an admin
const user = await admin.auth().verifyIdToken(token);
if (!user || !user.email_verified || !user.admin) {
response.status(401).send({ message: 'Unauthorized' });
return;
}
// Validate arguments
const { buildId, relatedJobId: jobId } = request.body;
if (!buildId || !jobId) {
response.status(400);
response.send({
message: 'Bad request',
description: 'Expected buildId and relatedJobId.',
});
return;
}
// Only retry existing builds
const [job, build] = await Promise.all([CiJobs.get(jobId), CiBuilds.get(buildId)]);
if (!job || !build) {
response.status(400);
response.send({
message: 'Bad request',
description: 'Expected valid buildId and relatedJobId.',
});
return;
}
// Check if build is not already running
if (build.status === 'started') {
response.status(409);
response.send({
message: 'Build was already re-scheduled',
description: request.body.buildId,
});
return;
}
// Schedule new build
const gitHubClient = await GitHub.init(githubPrivateKey.value(), githubClientSecret.value());
const scheduler = new Ingeminator(1, gitHubClient);
const scheduledSuccessfully = await scheduler.rescheduleBuild(jobId, job, buildId, build);
// Report result
if (scheduledSuccessfully) {
response.status(200);
response.send({
message: 'Build has been rescheduled',
description: request.body.buildId,
});
} else {
response.status(408);
response.send({
message: 'Request to Dockerhub timed out',
description: request.body.buildId,
});
}
} catch (error) {
console.log('error', JSON.stringify(error, Object.getOwnPropertyNames(error)));
response.status(401).send({ message: 'Unauthorized' });
}
Discord.disconnect();
},
);