-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathcreate-preview.js
95 lines (86 loc) · 2.94 KB
/
create-preview.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
const fetch = require('node-fetch');
exports.handler = async function(event, context) {
// Load but do not cache the build_steps.json file.
const path = '../../.tugboat/build_steps.json';
const config = require(path);
delete require.cache[require.resolve(path)];
const apiKey = process.env.TUGBOAT_API_KEY;
const repoId = process.env.TUGBOAT_REPO_ID;
if (!apiKey || !repoId) {
return {
statusCode: 400,
body: 'Missing TUGBOAT_API_KEY or TUGBOAT_REPO_ID.'
}
}
// Make sure a base preview was specified.
const body = JSON.parse(event.body);
if (!body.baseId) {
return {
statusCode: 400,
body: 'No base preview ID provided.',
}
}
// Set the default timezone.
const timezone = body.timezone ?? 'America/Los_Angeles';
const updateTimezone = `TIMEZONE="${timezone}"; drush config-set system.date timezone.default "$TIMEZONE" --yes`;
config.services.php.commands.build.unshift(updateTimezone);
// Set the defaault langcode.
// Temporarily set the system.site default_langcode so users are created with correct language.
// TODO: Find a different approach for this.
const langcode = body.langcode ?? 'en';
const updateLangcode = `LANGCODE=${langcode}; drush config:set language.negotiation selected_langcode "$LANGCODE" --yes; drush config:set system.site default_langcode "$LANGCODE" --yes;`;
config.services.php.commands.build.unshift(updateLangcode);
// Set expiration.
let expires = new Date();
expires.setHours(expires.getHours() + 12);
// Development settings.
if (process.env.CONTEXT === 'dev') {
body.name += ' (dev)';
expires = new Date();
expires.setHours(expires.getHours() + 1);
}
// Build payload.
const payload = {
ref: 'main',
repo: repoId,
anchor: false,
base: body.baseId,
config,
expires: expires.toJSON(),
name: body.name ?? 'Demo Farm',
}
// Create the preview.
return fetch('https://api.tugboatqa.com/v3/previews', {
method: 'POST',
headers: {
'Authorization': `Bearer ${apiKey}`,
'Content-Type': 'application/json',
},
body: JSON.stringify(payload),
})
.then(response => response.json())
.then(data => {
if (data.code == 1022) {
return {
statusCode: 400,
body: 'Demo limit has been reached. Please try again later.',
};
}
if (!data.hasOwnProperty("preview")) {
console.log(data);
return {
statusCode: 400,
body: 'Demo not created. Please try again.',
};
}
return {
statusCode: 202,
body: JSON.stringify(data),
}
}).catch(error => {
return {
statusCode: 400,
body: error.message,
};
});
}