-
Notifications
You must be signed in to change notification settings - Fork 22
/
worker.js
268 lines (227 loc) · 9.67 KB
/
worker.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
const async = require('async');
const pgp = require('pg-promise')();
const dbUrl = require('url');
const exec = require('child-process-promise').exec;
const dbParams = dbUrl.parse(process.env.DATABASE_URL);
const auth = dbParams.auth.split(':');
const config = {
host: dbParams.hostname,
port: dbParams.port,
user: auth[0],
ssl: true,
password: auth[1],
database: dbParams.pathname.split('/')[1],
idleTimeoutMillis: 1000,
max: 10
};
const db = pgp(config);
function setNewStage(settings, stage) {
settings.stage = stage;
return settings;
}
function deploymentStage(settings, complete = false) {
let scratchOrgUrlSql = '';
if (complete) {
scratchOrgUrlSql = `, scratch_url = '${settings.scratchOrgUrl}'`;
}
const updateQuery = `UPDATE deployments SET stage = '${settings.stage}', complete = ${complete}${scratchOrgUrlSql} WHERE guid = '${settings.guid}'`;
db.any(updateQuery, [true]);
return settings;
}
function deploymentSteps(settings) {
const message = settings.message.replace("'", "''");
const insertQuery = `INSERT INTO deployment_steps (guid, stage, message) VALUES ('${settings.guid}', '${settings.stage}', '${message}')`;
db.any(insertQuery, [true]);
return settings;
}
function deploymentError(guid, message) {
message = message.replace(/'/g, "''");
const insertQuery = `INSERT INTO deployment_steps (guid, stage, message) VALUES ('${guid}', 'error', '${message}')`;
db.any(insertQuery, [true]);
const updateQuery = `UPDATE deployments SET stage = 'error', error_message ='${message}', complete = false WHERE guid = '${guid}'`;
db.any(updateQuery, [true]);
}
function formatMessage(settings) {
let message = '';
if (settings.stderr) {
message = `Error: ${settings.stderr}.`;
if (settings.stderr.indexOf('Flag --permsetname expects a value') > -1) {
message = 'No permset specified.';
} else {
throw new Error(`GUID: ${settings.guid} ${settings.stderr}`);
}
} else {
message = `${settings.stdout}.`;
if (settings.stage === 'clone') {
message = `Successfully cloned ${settings.githubRepo}.`;
}
if (settings.stage === 'instanceUrl') {
message = `Set instance url to ${settings.instance_url}.`;
}
if (settings.stage === 'push') {
message = `Pushed ${settings.stdout} source files.`;
}
if (settings.stage === 'permset') {
if (!settings.assignPermset) {
message = 'No permset specified.';
} else {
message = `Permset '${settings.permsetName}' assigned.`;
}
}
if (settings.stage === 'test') {
if (settings.stdout !== '') {
message = `Apex tests: ${settings.stdout}.`;
} else {
message = 'No Apex tests.';
}
}
if (settings.stage === 'dataplans') {
if (settings.dataPlans.length > 0) {
message = 'Data import successful.';
} else {
message = 'No data plan specified.';
}
}
if (settings.stage === 'url') {
settings.scratchOrgUrl = settings.stdout;
message = `Scratch org URL: ${settings.scratchOrgUrl}.`;
}
if (settings.stage === 'yaml') {
console.log('yaml', settings.yamlExists);
if (!settings.yamlExists) {
message = 'No .salesforcedx.yaml found in repository. Using defaults.';
} else {
message = 'Using .salesforcedx.yaml found in repository.';
}
}
}
settings.stderr = '';
console.log('message', settings.stage, message);
settings.message = message;
return settings;
}
function executeScript(settings, script) {
return new Promise((resolve) => {
exec(script, (error, stdout, stderr) => {
if (stderr && error) {
settings.stderr = stderr.replace(/\r?\n|\r/, '').trim();
}
settings.stdout = stdout.replace(/\r?\n|\r/, '').trim();
resolve(settings);
});
});
}
// check for anything at init
async.whilst(
() => true,
(callback) => {
const selectQuery = "SELECT guid, username, repo, settings FROM deployments WHERE stage = 'init' AND complete = false LIMIT 1";
let guid = '';
db.any(selectQuery, [true])
.then((data) => {
// throw if no data to skip the subsequent promises
if (data.length === 0) {
throw new Error('norecords');
}
const settings = JSON.parse(data[0].settings);
settings.guid = data[0].guid;
guid = settings.guid;
console.log('found job', guid);
settings.tokenName = settings.access_token.replace(/\W/g, '');
settings.startingDirectory = process.env.STARTINGDIRECTORY;
settings.directory = `${settings.tokenName}-${settings.guid}`;
settings.cloneScript = `${settings.startingDirectory}rm -rf ${settings.directory};mkdir ${settings.directory};cd ${settings.directory};git clone ${settings.githubRepo} .`;
settings.instanceUrlScript = `${settings.startingDirectory}cd ${settings.directory};export FORCE_SHOW_SPINNER=;sfdx force:config:set instanceUrl=${settings.instance_url};`;
settings.createScript = `${settings.startingDirectory}cd ${settings.directory};export FORCE_SHOW_SPINNER=;sfdx force:org:create -v '${settings.access_token}' -s -f ${settings.scratchOrgDef}`;
settings.pushScript = `${settings.startingDirectory}cd ${settings.directory};export FORCE_SHOW_SPINNER=;sfdx force:source:push --json | jq '.result.pushedSource | length'`;
settings.permSetScript = `${settings.startingDirectory}cd ${settings.directory};export FORCE_SHOW_SPINNER=;sfdx force:user:permset:assign -n ${settings.permsetName}`;
settings.dataPlanScript = `${settings.startingDirectory}cd ${settings.directory};export FORCE_SHOW_SPINNER=;`;
for (let i = 0, len = settings.dataPlans.length; i < len; i++) {
settings.dataPlanScript += `sfdx force:data:tree:import --plan ${settings.dataPlans[i]};`;
}
settings.testScript = `${settings.startingDirectory}cd ${settings.directory};export FORCE_SHOW_SPINNER=;sfdx force:apex:test:run -r human --json | jq -r .result | jq -r .summary | jq -r .outcome`;
settings.urlScript = `${settings.startingDirectory}cd ${settings.directory};export FORCE_SHOW_SPINNER=;echo $(sfdx force:org:display --json | jq -r .result.instanceUrl)"/secur/frontdoor.jsp?sid="$(sfdx force:org:display --json | jq -r .result.accessToken)`;
// add path if specified in yaml
if (settings.openPath) {
settings.urlScript += `"&retURL="${encodeURIComponent(settings.openPath)}`;
}
settings.scratchOrgUrl = '';
settings.stderr = '';
settings.stdout = '';
return settings;
})
// yaml details
.then(settings => setNewStage(settings, 'yaml'))
.then(settings => deploymentStage(settings))
.then(settings => formatMessage(settings))
.then(settings => deploymentSteps(settings))
// clone
.then(settings => setNewStage(settings, 'clone'))
.then(settings => deploymentStage(settings))
.then(settings => executeScript(settings, settings.cloneScript))
.then(settings => formatMessage(settings))
.then(settings => deploymentSteps(settings))
// instanceUrl
.then(settings => setNewStage(settings, 'instanceUrl'))
.then(settings => deploymentStage(settings))
.then(settings => executeScript(settings, settings.instanceUrlScript))
.then(settings => formatMessage(settings))
.then(settings => deploymentSteps(settings))
// create
.then(settings => setNewStage(settings, 'create'))
.then(settings => deploymentStage(settings))
.then(settings => executeScript(settings, settings.createScript))
.then(settings => formatMessage(settings))
.then(settings => deploymentSteps(settings))
// push
.then(settings => setNewStage(settings, 'push'))
.then(settings => deploymentStage(settings))
.then(settings => executeScript(settings, settings.pushScript))
.then(settings => formatMessage(settings))
.then(settings => deploymentSteps(settings))
// permset
.then(settings => setNewStage(settings, 'permset'))
.then(settings => deploymentStage(settings))
.then(settings => executeScript(settings, settings.permSetScript))
.then(settings => formatMessage(settings))
.then(settings => deploymentSteps(settings))
// dataplans
.then(settings => setNewStage(settings, 'dataplans'))
.then(settings => deploymentStage(settings))
.then(settings => executeScript(settings, settings.dataPlanScript))
.then(settings => formatMessage(settings))
.then(settings => deploymentSteps(settings))
// test
.then(settings => setNewStage(settings, 'test'))
.then(settings => deploymentStage(settings))
.then(settings => executeScript(settings, settings.testScript))
.then(settings => formatMessage(settings))
.then(settings => deploymentSteps(settings))
// url
.then(settings => setNewStage(settings, 'url'))
.then(settings => deploymentStage(settings))
.then(settings => executeScript(settings, settings.urlScript))
.then(settings => formatMessage(settings))
.then(settings => deploymentSteps(settings))
// completed
.then(settings => setNewStage(settings, 'complete'))
.then(settings => deploymentStage(settings, true))
.then((settings) => {
console.log('finished job', settings.guid);
})
.catch((error) => {
// handles cases where there are no records
if (error.message !== 'norecords') {
console.error('guid', guid);
console.error('error', error);
deploymentError(guid, error.message);
}
});
setTimeout(() => {
callback(null, true);
}, 3000);
},
(err) => {
console.error(`err: ${err}`);
}
);