-
Notifications
You must be signed in to change notification settings - Fork 3
/
get-preview-login-link.js
56 lines (50 loc) · 1.74 KB
/
get-preview-login-link.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
const fetch = require('node-fetch');
exports.handler = async function(event, context) {
const apiKey = process.env.TUGBOAT_API_KEY ?? 'none';
// Make sure a preview id is provided.
const body = JSON.parse(event.body);
if (!body.previewId) {
return {
statusCode: 404,
error: 'No preview ID provided.',
}
}
// Fetch the preview logs.
async function getLogs(retry) {
return fetch(`https://api.tugboatqa.com/v3/previews/${body.previewId}/log?limit=100`, {
method: 'GET',
headers: {
'Authorization': `Bearer ${apiKey}`,
},
})
.then(response => response.json())
.then(data => {
data.reverse();
let drushIndex = data.findIndex(log => log.message && log.message.includes('drush uli'));
// If no drush uli is found, retry, then bail if no logs are found.
if (drushIndex < 0) {
if (retry) {
console.log('retry')
return new Promise(resolve => setTimeout(resolve, 2000))
.then(getLogs(false));
}
return {
statusCode: 400,
body: 'Error retrieving demo link from the Tugboat API. Please try again.',
}
}
// Return the login link.
const body = {loginLink: data[drushIndex -1].message.trim()};
return {
statusCode: 202,
body: JSON.stringify(body),
}
}).catch(error => {
return {
statusCode: 400,
body: error.message,
};
});
}
return getLogs(true);
}