-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathindex.js
445 lines (319 loc) · 14.5 KB
/
index.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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
var _ = require('lodash');
module.exports = function(nforce, pluginName) {
if (!pluginName) pluginName = 'tooling';
// throws if the plugin already exists
var plugin = nforce.plugin(pluginName);
// returns a record based upons its ID
plugin.fn('getRecord', function(args, callback) {
var validator = validate(args, ['id', 'type']);
var opts = this._getOpts(args, callback);
if (validator.error) return callback(new Error(validator.message), null);
opts.uri = opts.oauth.instance_url + '/services/data/' + this.apiVersion
+ '/tooling/sobjects/' + args.type + '/' + args.id;
opts.method = 'GET';
return this._apiRequest(opts, opts.callback);
});
// returns 'describe' metadata at all levels for the specified object including fields, URLs, and child relationships.
plugin.fn('getDescribe', function(args, callback) {
var validator = validate(args, ['type']);
var opts = this._getOpts(args, callback);
if (validator.error) return callback(new Error(validator.message), null);
opts.uri = opts.oauth.instance_url + '/services/data/' + this.apiVersion
+ '/tooling/sobjects/' + args.type + '/describe';
opts.method = 'GET';
return this._apiRequest(opts, opts.callback);
});
// returns metadata for a specific custom field on a custom object
plugin.fn('getCustomField', function(args, callback) {
var validator = validate(args, ['id']);
var opts = this._getOpts(args, callback);
if (validator.error) return callback(new Error(validator.message), null);
opts.uri = opts.oauth.instance_url + '/services/data/' + this.apiVersion
+ '/tooling/sobjects/CustomField/' + args.id;
opts.method = 'GET';
return this._apiRequest(opts, opts.callback);
});
// Returns the high-level metadata for the specified object
plugin.fn('getObject', function(args, callback) {
var validator = validate(args, ['type']);
var opts = this._getOpts(args, callback);
if (validator.error) return callback(new Error(validator.message), null);
opts.uri = opts.oauth.instance_url + '/services/data/' + this.apiVersion
+ '/tooling/sobjects/' + args.type;
opts.method = 'GET';
return this._apiRequest(opts, opts.callback);
});
// returns the available Tooling API objects and their metadata.
plugin.fn('getObjects', function(args, callback) {
var opts = this._getOpts(args, callback);
opts.uri = opts.oauth.instance_url + '/services/data/' + this.apiVersion
+ '/tooling/sobjects/';
opts.method = 'GET';
return this._apiRequest(opts, opts.callback);
});
// creates a MetadataContainer to hold deployment objects
plugin.fn('createContainer', function(args, callback) {
var validator = validate(args, ['name']);
var opts = this._getOpts(args, callback);
if (validator.error) return callback(new Error(validator.message), null);
opts.uri = opts.oauth.instance_url + '/services/data/' + this.apiVersion
+ '/tooling/sobjects/MetadataContainer';
opts.method = 'POST';
opts.body = JSON.stringify({name: args.name});
return this._apiRequest(opts, opts.callback);
});
// returns a MetadataContainer by id
plugin.fn('getContainer', function(args, callback) {
var validator = validate(args, ['id']);
var opts = this._getOpts(args, callback);
if (validator.error) return callback(new Error(validator.message), null);
opts.uri = opts.oauth.instance_url + '/services/data/' + this.apiVersion
+ '/tooling/sobjects/MetadataContainer/' + args.id;
opts.method = 'GET';
return this._apiRequest(opts, opts.callback);
});
// returns a MetadataContainer by id
plugin.fn('addContainerArtifact', function(args, callback) {
var validator = validate(args, ['id', 'artifact']);
var opts = this._getOpts(args, callback);
var body = {};
if (validator.error) return callback(new Error(validator.message), null);
// add the container to the artifact
args.artifact.metadataContainerId = args.id;
// create the body for the request without any null properties and type
_.pairs(args.artifact).forEach(function(entry) {
if (!_.isNull(_.last(entry)) && _.first(entry) !== 'type') {
body[_.first(entry)] = _.last(entry);
}
});
opts.uri = opts.oauth.instance_url + '/services/data/' + this.apiVersion
+ '/tooling/sobjects/' + args.artifact.type;
opts.method = 'POST'
opts.body = JSON.stringify(body);
return this._apiRequest(opts, opts.callback);
});
// deletes a MetadataContainer by id
plugin.fn('deleteContainer', function(args, callback) {
var validator = validate(args, ['id']);
var opts = this._getOpts(args, callback);
if (validator.error) return callback(new Error(validator.message), null);
opts.uri = opts.oauth.instance_url + '/services/data/' + this.apiVersion
+ '/tooling/sobjects/MetadataContainer/' + args.id;
opts.method = 'DELETE';
return this._apiRequest(opts, opts.callback);
});
// deploys a MetadataContainer and all objects
plugin.fn('deployContainer', function(args, callback) {
var validator = validate(args, ['id']);
var opts = this._getOpts(args, callback);
var body = {
isCheckOnly: true,
metadataContainerId: args.id
};
if (validator.error) return callback(new Error(validator.message), null);
// look for passed argements
if (!_.isUndefined(args.isCheckOnly)) body.isCheckOnly = args.isCheckOnly;
opts.uri = opts.oauth.instance_url + '/services/data/' + this.apiVersion
+ '/tooling/sobjects/ContainerAsyncRequest';
opts.method = 'POST';
opts.body = JSON.stringify(body);
return this._apiRequest(opts, opts.callback);
});
// checks the current status of a deployment
plugin.fn('getContainerDeployStatus', function(args, callback) {
var validator = validate(args, ['id']);
var opts = this._getOpts(args, callback);
if (validator.error) return callback(new Error(validator.message), null);
opts.uri = opts.oauth.instance_url + '/services/data/' + this.apiVersion
+ '/tooling/sobjects/ContainerAsyncRequest/' + args.id;
opts.method = 'GET';
return this._apiRequest(opts, opts.callback);
});
// inserts a new record
plugin.fn('insert', function(args, callback) {
var validator = validate(args, ['type' , 'object']);
var opts = this._getOpts(args, callback);
if (validator.error) return callback(new Error(validator.message), null);
opts.uri = opts.oauth.instance_url + '/services/data/' + this.apiVersion
+ '/tooling/sobjects/' + args.type;
opts.method = 'POST';
opts.body = JSON.stringify(args.object);
return this._apiRequest(opts, opts.callback);
});
// updates an existing record
plugin.fn('update', function(args, callback) {
var validator = validate(args, ['id', 'type', 'object']);
var opts = this._getOpts(args, callback);
if (validator.error) return callback(new Error(validator.message), null);
opts.uri = opts.oauth.instance_url + '/services/data/' + this.apiVersion
+ '/tooling/sobjects/' + args.type + '/' + args.id;
opts.method = 'PATCH';
opts.body = JSON.stringify(args.object);
return this._apiRequest(opts, opts.callback);
});
// deletes a record by id
plugin.fn('delete', function(args, callback) {
var validator = validate(args, ['id', 'type']);
var opts = this._getOpts(args, callback);
if (validator.error) return callback(new Error(validator.message), null);
opts.uri = opts.oauth.instance_url + '/services/data/' + this.apiVersion
+ '/tooling/sobjects/' + args.type + '/' + args.id;
opts.method = 'DELETE';
this._apiRequest(opts, function(err, results) {
if (err) { return callback(err, null); }
if (!err) { return callback(null, {success: true}); }
});
});
plugin.fn('query', function(args, callback) {
var validator = validate(args, ['q']);
var opts = this._getOpts(args, callback);
if (validator.error) return callback(new Error(validator.message), null);
opts.uri = opts.oauth.instance_url + '/services/data/' + this.apiVersion
+ '/tooling/query?q=' + encodeURIComponent(args.q);
opts.method = 'GET';
return this._apiRequest(opts, opts.callback);
});
// executes a chunk of Apex code anonymously.
plugin.fn('executeAnonymous', function(args, callback) {
var validator = validate(args, ['code']);
var opts = this._getOpts(args, callback);
if (validator.error) return callback(new Error(validator.message), null);
opts.uri = opts.oauth.instance_url + '/services/data/' + this.apiVersion
+ '/tooling/executeAnonymous/?anonymousBody=' + encodeURIComponent(args.code);
opts.method = 'GET';
return this._apiRequest(opts, opts.callback);
});
// rreturns a raw debug log by ID.
plugin.fn('getApexLog', function(args, callback) {
var validator = validate(args, ['id']);
var opts = this._getOpts(args, callback);
if (validator.error) return callback(new Error(validator.message), null);
opts.uri = opts.oauth.instance_url + '/services/data/' + this.apiVersion
+ '/tooling/sobjects/ApexLog/' + args.id + '/Body',
opts.method = 'GET'
return this._apiRequest(opts, opts.callback);
});
// returns the apex code coverages for the entire org
plugin.fn('getApexOrgWideCoverage', function(callback) {
var q = 'select PercentCovered from ApexOrgWideCoverage';
var opts = this._getOpts(callback);
opts.uri = opts.oauth.instance_url + '/services/data/' + this.apiVersion
+ '/tooling/query?q=' + encodeURIComponent(q);
opts.method = 'GET';
this._apiRequest(opts, function(err, results) {
if (err) { return callback(err, null); }
if (!err) { return callback(null, results.records[0].PercentCovered); }
});
});
// returns the apex code coverages for a specific apex class or trigger
plugin.fn('getApexClassOrTriggerCoverage', function(args, callback) {
var validator = validate(args, ['name']);
var opts = this._getOpts(args, callback);
var q = "select Coverage, NumLinesCovered, TestMethodName, ApexClassOrTrigger.Id, ";
q += "ApexClassOrTrigger.Name, NumLinesUncovered from ApexCodeCoverage ";
q += "where ApexClassOrTrigger.Name = '"+args.name+"'";
if (validator.error) return callback(new Error(validator.message), null);
opts.uri = opts.oauth.instance_url + '/services/data/' + this.apiVersion
+ '/tooling/query?q=' + encodeURIComponent(q);
opts.method = 'GET';
this._apiRequest(opts, function(err, results) {
if (err) { return callback(err, null); }
if (!err) {
// ensure that tests have been run and codecoverage exists
if (results.size != 0) {
var record = results.records[0];
var obj = {
PercentCovered: record.NumLinesCovered / (record.NumLinesUncovered + record.NumLinesCovered)
}
return callback(null, _.extend(obj, record));
} else {
return callback(null, new Error("No test coverage data exists. Run tests for this Apex class or Trigger."));
}
}
});
});
// runs specified test synchronously
plugin.fn('runTestsAsync', function(args, callback) {
var validator = validate(args, ['ids']);
var opts = this._getOpts(args, callback);
if (validator.error) return callback(new Error(validator.message), null);
opts.uri = opts.oauth.instance_url + '/services/data/' + this.apiVersion
+ '/tooling/runTestsAsynchronous/?classids=' + args.ids,
opts.method = 'GET';
this._apiRequest(opts, function(err, results) {
if (err) { return callback(err, null); }
if (!err) { return callback(null, results); }
});
});
// runs specified test synchronously with post
plugin.fn('runTestsAsyncPost', function(args, callback) {
var validator = validate(args, ['ids']);
var opts = this._getOpts(args, callback);
if (validator.error) return callback(new Error(validator.message), null);
opts.uri = opts.oauth.instance_url + '/services/data/' + this.apiVersion
+ '/tooling/runTestsAsynchronous/',
opts.method = 'POST';
opts.body = '{"classids":"' + args.ids + '"}';
this._apiRequest(opts, function(err, results) {
if (err) { return callback(err, null); }
if (!err) { return callback(null, results); }
});
});
// checks the run tests status of a specific job
plugin.fn('getAsyncTestStatus', function(args, callback) {
var validator = validate(args, ['id']);
var opts = this._getOpts(args, callback);
var q = "select Id, Status, ApexClassId FROM ApexTestQueueItem where ParentJobId = '"+args.id+"'";
if (validator.error) return callback(new Error(validator.message), null);
opts.uri = opts.oauth.instance_url + '/services/data/' + this.apiVersion
+ '/tooling/query?q=' + encodeURIComponent(q);
opts.method = 'GET';
return this._apiRequest(opts, opts.callback);
});
// returned the results of all runTests
plugin.fn('getAsyncTestResults', function(args, callback) {
var validator = validate(args, ['ids']);
// enclose each id with a single quote for sql
var sqlIds = "'" + args.ids.join("','") + "'";
var opts = this._getOpts(args, callback);
var q = "select StackTrace, Message, AsyncApexJobId, MethodName, Outcome, ApexClass.Id, ApexClass.Name, RunTime, TestTimeStamp ";
q += "from ApexTestResult where QueueItemId IN ("+sqlIds+")";
if (validator.error) return callback(new Error(validator.message), null);
opts.uri = opts.oauth.instance_url + '/services/data/' + this.apiVersion
+ '/tooling/query?q=' + encodeURIComponent(q);
opts.method = 'GET';
return this._apiRequest(opts, opts.callback);
});
// factory to create an empty deployment artifact
plugin.fn('createDeployArtifact', function(type, fields) {
// for convenience, set up the available properties
var obj = {
type: type,
body: null,
content: null,
contentEntityId: null,
metadata: null,
metadataContainerId: null,
symbolTable: null
}
return _.extend(obj, fields);
});
// utility method to validate inputs
function validate(args, required) {
var result = {
error: false,
message: 'No errors'
}
// ensure required properties were passed in the arguments hash
if (required) {
var keys = _.keys(args);
required.forEach(function(field) {
if(!_.contains(keys, field)) {
result.error = true;
result.message = 'The following values must be passed: ' + required.join(', ');
}
})
}
return result;
}
}