forked from microsoft/opensource-management-portal
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdata.js
More file actions
792 lines (750 loc) · 26 KB
/
Copy pathdata.js
File metadata and controls
792 lines (750 loc) · 26 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
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
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
//
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
//
'use strict';
/*eslint no-console: ["error", { allow: ["warn"] }] */
// This file is very sad. :(
// This is the original data interface for this portal. It uses Azure
// table storage and its Node.js SDK.
const _ = require('lodash');
const azure = require('azure-storage');
const async = require('async');
const uuid = require('node-uuid');
const os = require('os');
var staticHostname = os.hostname().toString();
function DataClient(options, callback) {
if (options.config === undefined) {
return callback(new Error('Configuration must be provided to the data client.'));
}
var storageAccountName = options.config.github.links.table.account;
var storageAccountKey = options.config.github.links.table.key;
var prefix = options.config.github.links.table.prefix;
try {
if (!storageAccountName || !storageAccountKey) {
throw new Error('Storage account information is not configured.');
}
this.table = azure.createTableService(storageAccountName, storageAccountKey);
} catch (storageAccountError) {
return callback(storageAccountError);
}
this.entGen = azure.TableUtilities.entityGenerator;
if (prefix === undefined) {
prefix = '';
}
this.options = {
partitionKey: prefix + 'pk',
linksTableName: prefix + 'links',
pendingApprovalsTableName: prefix + 'pending',
errorsTableName: prefix + 'errors',
settingsTableName: `${prefix}settings`,
encryption: options.config.github.links.table.encryption,
};
if (this.options.encryption === true) {
const encryptColumns = new Set(['githubToken', 'githubTokenIncreasedScope']);
const encryptionOptions = {
keyEncryptionKeyId: options.config.github.links.table.encryptionKeyId,
keyResolver: options.keyEncryptionKeyResolver,
encryptedPropertyNames: encryptColumns,
binaryProperties: 'buffer',
tableDehydrator: reduceEntity,
tableRehydrator: this.createEntity.bind(this),
};
const tableClient = this.table;
this.table = require('./lib/tableEncryption')(tableClient, encryptionOptions);
}
var dc = this;
var tableNames = [
dc.options.linksTableName,
dc.options.pendingApprovalsTableName,
dc.options.errorsTableName,
dc.options.settingsTableName,
];
async.each(tableNames, function (tableName, callback) {
dc.table.createTableIfNotExists(tableName, callback);
}, function (error) {
if (callback) return callback(error, dc);
});
}
var reduceEntity = function reduceEntity(instance) {
if (instance === undefined || instance === null) {
return instance;
}
for (var column in instance) {
if (instance[column] && instance[column]._ !== undefined) {
instance[column] = instance[column]._;
}
}
return instance;
};
DataClient.prototype.reduceEntity = reduceEntity;
DataClient.prototype.requestToUserInformation = function rtui(req) {
var info = {
ghid: undefined,
ghu: undefined,
aad: undefined,
};
if (req && req.user && req.user.github && req.user.github.id) {
info.ghid = req.user.github.id;
if (info.ghid.toString) {
info.ghid = info.ghid.toString();
}
if (req.user.github.username) {
info.ghu = req.user.github.username;
}
}
if (req && req.user && req.user.azure && req.user.azure.username) {
info.aad = req.user.azure.username;
}
return info;
};
DataClient.prototype.insertErrorLogEntry = function insertErrorEntry(version, req, err, meta, callback) {
// generic configuration, should move out at some point...
var storeUnknownUserErrors = false;
var storeRequestInformation = true;
var cbNoErrors = function (callback) {
if (callback) {
callback();
}
};
var dc = this;
var entity;
// (PartitionKey, RowKey): (ghid || 0, new uuid)
// (ghu, ghid, aad): user information
// (t, cid): (time when method called, correlation ID)
// (e, json, meta): (error message, JSON serialized err, JSON metadata)
// (url, host, ...): various host and request informational fields
try {
var info = dc.requestToUserInformation(req);
// We may encounter users without a session. In these cases, we could log with -1 ID for pkey (OR use correlation ID for the pkey... hmm.)
if (info.ghid === undefined) {
if (!storeUnknownUserErrors) {
return cbNoErrors(callback);
}
info.ghid = -1;
}
info.v = version;
if (req.headers && req.headers.referer) {
info.referer = req.headers.referer;
}
var partitionKey = info.ghid;
var uniqueErrorId = uuid.v4();
entity = dc.createEntity(partitionKey, uniqueErrorId, info);
var errorMessage = 'The error object was undefined.';
var errorJson;
var errorStack;
var errorStatus = '200';
if (err) {
// If err.meta is set, use that for the metadata up-level, and remove from err object.
if (err.meta && !meta) {
meta = err.meta;
delete err.meta;
}
errorStack = err.stack;
if (err.status) {
errorStatus = err.status;
// delete err.status; // ? may not want to do this...
}
if (err.message) {
errorMessage = err.message;
} else {
if (err.toString) {
errorMessage = err.toString();
} else {
errorMessage = 'The provided error instance is not a string and has no toString method.';
}
}
try {
errorJson = JSON.stringify(err);
} catch (je) {
// Ignore any serialization errors or circular reference problems, the rest will still be logged in this case.
}
}
var metaJson;
if (meta) {
try {
metaJson = JSON.stringify(meta);
} catch (je) {
// Ignore.
}
}
var errorEntity = {
t: new Date().getTime(),
cid: (req && req.correlationId ? req.correlationId : undefined),
e: errorMessage,
stack: errorStack,
json: errorJson,
meta: metaJson,
status: errorStatus,
'new': true
};
dc.mergeIntoEntity(entity, errorEntity);
if (storeRequestInformation) {
var sri = {
url: req.scrubbedUrl || req.originalUrl || req.url,
ua: req.headers['user-agent'],
host: staticHostname
};
dc.mergeIntoEntity(entity, sri);
}
} catch (ex) {
// Retry policy could be nice, OR log this separately if possible.
return cbNoErrors(callback);
}
if (entity) {
dc.table.insertEntity(dc.options.errorsTableName, entity, function (/* ignoredError */) {
cbNoErrors(callback);
});
} else {
cbNoErrors(callback);
}
};
DataClient.prototype.updateError = function (partitionKey, rowKey, mergeEntity, callback) {
var dc = this;
var entity = dc.createEntity(partitionKey, rowKey, mergeEntity);
console.warn('This method does not work with encryption at this time.');
dc.table.mergeEntity(dc.options.errorsTableName, entity, callback);
};
DataClient.prototype.removeError = function (partitionKey, rowKey, callback) {
var dc = this;
dc.table.deleteEntity(dc.options.errorsTableName, dc.createEntity(partitionKey, rowKey), callback);
};
DataClient.prototype.getActiveErrors = function (correlationId, callback) {
var dc = this;
// Correlation ID is optional
if (typeof (correlationId) === 'function') {
callback = correlationId;
correlationId = undefined;
}
var done = false;
var continuationToken = null;
var entries = [];
async.whilst(
function () { return !done; },
function (asyncCallback) {
var query = new azure.TableQuery()
.where('new eq ?', true);
if (correlationId) {
query.and.apply(query, ['cid eq ?', correlationId]);
}
dc.table.queryEntities(dc.options.errorsTableName, query, continuationToken, function (error, results) {
if (error) {
done = true;
return asyncCallback(error);
}
if (results.continuationToken) {
continuationToken = results.continuationToken;
} else {
done = true;
}
if (results && results.entries && results.entries.length) {
for (var i = 0; i < results.entries.length; i++) {
entries.push(reduceEntity(results.entries[i]));
}
}
asyncCallback();
});
}, function (error) {
if (error) {
return callback(error);
}
async.sortBy(entries, function (entity, scb) {
var t;
var err = null;
try {
t = Math.round(entity.t) * -1;
}
catch (trx) {
err = trx;
}
return scb(err, t);
}, callback);
});
};
DataClient.prototype.mergeIntoEntity = function mit(entity, obj, callback) {
var dc = this;
if (obj) {
for (var key in obj) {
// Currently stripping metadata
if (key === '.metadata') {
continue;
}
if (obj[key] === undefined || obj[key] === null) {
// Skip undefined/null objects, including the key
continue;
}
if (typeof obj[key] === 'string') {
entity[key] = dc.entGen.String(obj[key]);
} else if (obj[key] === true) {
entity[key] = dc.entGen.Boolean(true);
} else if (obj[key] === false) {
entity[key] = dc.entGen.Boolean(false);
} else if (Buffer.isBuffer(obj[key])) {
entity[key] = dc.entGen.Binary(obj[key]);
} else if (obj[key] instanceof Date) {
entity[key] = dc.entGen.DateTime(obj[key]);
} else if (typeof obj[key] === 'number') {
// Opinionated entity processing: store all numbers as strings
entity[key] = dc.entGen.String(obj[key].toString());
} else {
console.warn('Consider whether a new entity merge clause is required for key ' + key + ' of type:' + typeof obj[key]);
if (obj[key].toString) {
entity[key] = dc.entGen.String(obj[key].toString());
} else {
entity[key] = dc.entGen.String(obj[key]);
}
}
}
}
if (callback) {
callback(null, entity);
} else {
return entity;
}
};
DataClient.prototype.createEntity = function ce(partitionKey, rowKey, obj, callback) {
var dc = this;
if (typeof (obj) === 'function') {
callback = obj;
obj = undefined;
}
var entity = {
PartitionKey: dc.entGen.String(partitionKey),
RowKey: dc.entGen.String(rowKey)
};
if (obj) {
dc.mergeIntoEntity(entity, obj);
}
if (callback) {
return callback(null, entity);
} else {
return entity;
}
};
// links
// -----
// CONSIDER: Replace link calls with reduced entity "association" calls, then depre. & remove these funcs.
DataClient.prototype.createLinkObjectFromRequest = function createLinkObject(req, callback) {
if (req && req.user && req.user.github && req.user.azure && req.user.github.username && req.user.github.id && req.user.azure.username && req.user.azure.oid) {
var link = {
ghu: req.user.github.username,
ghid: req.user.github.id.toString(),
aadupn: req.user.azure.username,
aadname: req.user.azure.displayName,
aadoid: req.user.azure.oid,
joined: new Date(),
};
link.ghavatar = req.user.github.avatarUrl;
if (req.user.github.accessToken) {
link.githubToken = req.user.github.accessToken;
link.githubTokenUpdated = new Date().getTime();
}
if (req.user.githubIncreasedScope && req.user.githubIncreasedScope.accessToken) {
link.githubTokenIncreasedScope = req.user.githubIncreasedScope.accessToken;
link.githubTokenIncreasedScopeUpdated = new Date().getTime();
}
return callback(null, link);
} else {
return callback(new Error('Not all fields needed for creating a link are available and authenticated. This may be a temporary problem or an implementation bug.'));
}
};
DataClient.prototype.getUserLinks = function gul(users, callback) {
var dc = this;
var query = new azure.TableQuery()
.where('PartitionKey eq ?', this.options.partitionKey);
if (!(users && users.length && users.length > 0)) {
return callback(new Error('Must include an array of GitHub user IDs, and at least one in that array.'));
}
var clauses = [];
if (users.length > 250) {
// TODO: Write better code here to use continuation tokens and utilities to resolve any number from storage.
return callback(new Error(`The application has queried for ${users.length} entities, which is too many for the current design.`));
}
for (var i = 0; i < users.length; i++) {
clauses.push('ghid eq ?string?');
}
var args = [clauses.join(' or ')].concat(users);
query.and.apply(query, args);
dc.table.queryEntities(dc.options.linksTableName,
query,
null,
function (error, results, headers) {
if (error) {
error.headers = headers;
return callback(error);
}
var entries = [];
if (results && results.entries && results.entries.length) {
for (var i = 0; i < results.entries.length; i++) {
entries.push(reduceEntity(results.entries[i]));
}
}
async.sortBy(entries, function (user, sortCallback) {
var value = user.aadupn || user.aadname || user.ghu || user.ghid;
if (value.toLowerCase) {
value = value.toLowerCase();
}
sortCallback(null, value);
}, callback);
});
};
DataClient.prototype.getUserLinkByUsername = function gulbyu(githubUsername, callback) {
this.getUserLinkByProperty('ghu', githubUsername, function (error, data) {
if (error) return callback(error);
if (data && data.length) {
if (data.length === 1) {
callback(null, data[0]);
} else {
if (data.length === 0) {
callback(null, false);
} else {
callback(new Error('Multiple entries returned. The data may be consistent. Please file a bug.'));
}
}
} else {
callback(new Error('No results.'));
}
});
};
DataClient.prototype.updateLink = function updl(userid, replaceEntity, callback) {
var dc = this;
if (userid === undefined) {
return callback(new Error('The GitHub ID is undefined.'));
}
if (typeof userid != 'string') {
userid = userid.toString();
}
var entity = dc.createEntity(dc.options.partitionKey, userid, replaceEntity);
dc.table.replaceEntity(dc.options.linksTableName, entity, callback);
};
DataClient.prototype.getUserByAadUpn = function gubauapn(employeeAlias, callback) {
this.getUserLinkByProperty('aadupn', employeeAlias.toLowerCase(), callback);
};
DataClient.prototype.getUserByAadOid = function getByOid(oid, callback) {
this.getUserLinkByProperty('aadoid', oid, callback);
};
function getUserLinkByPropertyOneAttempt(dc, propertyName, value, callback) {
'use strict';
const query = new azure.TableQuery()
.where(propertyName + ' eq ?', value);
dc.table.queryEntities(dc.options.linksTableName,
query,
null,
function (error, results) {
if (error) return callback(error);
const entries = [];
if (results && results.entries && results.entries.length) {
for (let i = 0; i < results.entries.length; i++) {
entries.push(reduceEntity(results.entries[i]));
}
}
callback(null, entries);
});
}
function getUserLinkByPropertyRetryOnEmptyResults(dc, propertyName, value, callback) {
'use strict';
let mostRecentEntries = null;
// Wrap the one-time query operation; local to this function an error is simulated
// for empty results (which are valid) to reuse the async library's retry logic.
const getAndEmptyAsError = (wrappedFunctionCallback) => {
getUserLinkByPropertyOneAttempt(dc, propertyName, value, (error, results) => {
if (!error && results && Array.isArray(results) && results.length === 0) {
error = new Error('No results were returned from the link by property query. This message should not be seen in production environments.');
error.simulated = true;
}
mostRecentEntries = results;
return wrappedFunctionCallback(error, results);
});
};
async.retry({
times: 3,
// Immediately return is an actual error
errorFilter: function (err) {
return err.simulated === true;
},
// Exponential backoff
interval: function (retryCount) {
return 50 * Math.pow(2, retryCount);
}
},
getAndEmptyAsError,
(retryError) => {
if (retryError && retryError.simulated === true) {
retryError = null;
}
return callback(retryError, retryError ? undefined : mostRecentEntries);
});
}
DataClient.prototype.getUserLinkByProperty = function gulbprop(propertyName, value, callback) {
// This is an important function that calls Azure to retrieve the link
// for a user. A query operation is used and sometimes returns an empty
// result set, even though the link exists. This robustness improvement
// is targeted for now; it will use a short exponential backoff retry
// whenever an empty result set is returned.
getUserLinkByPropertyRetryOnEmptyResults(this, propertyName, value, callback);
};
DataClient.prototype.getLink = function getLink(githubId, callback) {
var dc = this;
if (githubId === undefined) {
return callback(new Error('The GitHub ID is undefined.'));
}
if (typeof githubId != 'string') {
githubId = githubId.toString();
}
dc.table.retrieveEntity(dc.options.linksTableName, dc.options.partitionKey, githubId, function (error, result, response) {
if (error && !result) {
// This routine returns no error and a false 'link' when an entity is
// missing, but we still want to return an error for anything else,
// especially if there is encryption configured.
if (error.statusCode == 404 && error.code === 'ResourceNotFound') {
error = null;
}
return callback(error, false);
}
return callback(error, result, response);
});
};
DataClient.prototype.getAllEmployees = function getAllEmployees(options, callback) {
if (!callback && typeof(options) === 'function') {
callback = options;
options = {};
}
let columns = ['aadupn', 'ghu', 'ghid', 'PartitionKey', 'RowKey'];
if (options.includeNames) {
columns.push('aadname');
}
if (options.includeId) {
columns.push('aadoid');
}
if (options.includeServiceAccounts) {
columns.push('serviceAccount');
columns.push('serviceAccountMail');
}
if (options.all) {
columns = undefined;
}
var dc = this;
var pageSize = 500;
var employees = [];
var done = false;
var continuationToken = null;
async.whilst(
function areWeDone() { return !done; },
function grabPage(cb) {
var query = new azure.TableQuery()
.select(columns)
.top(pageSize);
dc.table.queryEntities(dc.options.linksTableName, query, continuationToken, function (error, results) {
if (error) {
done = true;
return cb(error);
}
if (results.continuationToken) {
continuationToken = results.continuationToken;
} else {
done = true;
}
if (results && results.entries && results.entries.length) {
for (var i = 0; i < results.entries.length; i++) {
employees.push(reduceEntity(results.entries[i]));
}
}
cb();
});
}, function (error) {
if (error) return callback(error);
employees.forEach(account => {
if (account.aadupn) {
account.aadupn = account.aadupn.toLowerCase();
}
});
const sorted = _.sortBy(employees, ['aadupn', 'ghu']);
callback(null, sorted);
});
};
DataClient.prototype.insertLink = function insertLink(githubId, details, callback) {
var dc = this;
if (githubId === undefined) {
return callback(new Error('The GitHub ID is undefined.'));
}
if (typeof githubId !== 'string') {
githubId = githubId.toString();
}
var entity = dc.createEntity(dc.options.partitionKey, githubId, details);
dc.table.insertEntity(dc.options.linksTableName, entity, callback);
};
DataClient.prototype.removeLink = function removeLink(githubId, callback) {
var dc = this;
if (githubId === undefined) {
return callback(new Error('The GitHub ID is undefined.'));
}
if (typeof githubId != 'string') {
githubId = githubId.toString();
}
dc.table.deleteEntity(dc.options.linksTableName, dc.createEntity(dc.options.partitionKey, githubId), callback);
};
// basic settings interface
// ------------------------
DataClient.prototype.getSetting = function (partitionKey, rowKey, callback) {
getReducedEntity(this, this.options.settingsTableName, partitionKey, rowKey, callback);
};
DataClient.prototype.setSetting = function (partitionKey, rowKey, value, callback) {
const entity = this.createEntity(partitionKey, rowKey, value);
this.table.insertEntity(this.options.settingsTableName, entity, callback);
};
DataClient.prototype.deleteSetting = function (partitionKey, rowKey, callback) {
this.table.deleteEntity(this.options.settingsTableName, this.createEntity(partitionKey, rowKey), callback);
};
// pending approvals workflow
// --------------------------
DataClient.prototype.getPendingApprovals = function getPendingApprovals(teamsIn, callback) {
var dc = this;
var teams = null;
var i;
if (typeof teamsIn === 'number') {
teams = [teamsIn.toString()];
}
else if (typeof teamsIn === 'string') {
teams = [teamsIn];
} else if (typeof teamsIn === 'function') {
callback = teamsIn;
teams = []; // Special case: empty list means all pending approvals
} else {
if (!(teamsIn && teamsIn.length)) {
throw new Error('Unknown "teams" type for getPendingApprovals. Please file a bug.');
}
// New permissions system refactoring...
if (teamsIn.length > 0 && teamsIn[0] && teamsIn[0].id) {
teams = [];
for (i = 0; i < teamsIn.length; i++) {
teams.push(teamsIn[i].id);
}
}
}
var query = new azure.TableQuery()
.where('PartitionKey eq ?', this.options.partitionKey)
.and('active eq ?', true);
if (teams.length > 0) {
var clauses = [];
for (i = 0; i < teams.length; i++) {
clauses.push('teamid eq ?string?');
}
var args = [clauses.join(' or ')].concat(teams);
query.and.apply(query, args);
}
dc.table.queryEntities(dc.options.pendingApprovalsTableName,
query,
null,
function (error, results) {
if (error) return callback(error);
var entries = [];
if (results && results.entries && results.entries.length) {
for (var i = 0; i < results.entries.length; i++) {
var r = results.entries[i];
if (r && r.active && r.active._) {
entries.push(reduceEntity(r));
}
}
}
callback(null, entries);
});
};
DataClient.prototype.insertApprovalRequest = function iar(teamid, details, callback) {
var dc = this;
if (typeof teamid != 'string') {
teamid = teamid.toString();
}
details.teamid = teamid;
dc.insertGeneralApprovalRequest('joinTeam', details, callback);
};
DataClient.prototype.insertGeneralApprovalRequest = function igar(ticketType, details, callback) {
var dc = this;
var id = uuid.v4();
var entity = dc.createEntity(dc.options.partitionKey, id, {
tickettype: ticketType
});
dc.mergeIntoEntity(entity, details);
dc.table.insertEntity(dc.options.pendingApprovalsTableName, entity, function (error, result, response) {
if (error) {
return callback(error);
}
// Pass back the generated request ID first.
callback(null, id, result, response);
});
};
function getReducedEntity(dc, tableName, partitionKey, rowKey, callback) {
dc.table.retrieveEntity(tableName, partitionKey, rowKey, function (error, ent) {
if (error) return callback(error);
callback(null, reduceEntity(ent));
});
}
DataClient.prototype.getRepositoryApproval = function (fieldName, repositoryValue, callback) {
const dc = this;
// Shortcoming: repoName is case sensitive
const query = new azure.TableQuery()
.where('PartitionKey eq ?', this.options.partitionKey)
.and('tickettype eq ?', 'repo')
.and(`${fieldName} eq ?`, repositoryValue);
dc.table.queryEntities(dc.options.pendingApprovalsTableName,
query,
null,
function (error, results) {
if (error) return callback(error);
const entries = [];
if (results && results.entries && results.entries.length) {
for (let i = 0; i < results.entries.length; i++) {
const r = results.entries[i];
entries.push(reduceEntity(r));
}
}
callback(null, entries);
});
};
DataClient.prototype.getApprovalRequest = function gar(requestId, callback) {
getReducedEntity(this, this.options.pendingApprovalsTableName, this.options.partitionKey, requestId, callback);
};
DataClient.prototype.getPendingApprovalsForUserId = function gpeaf(githubid, callback) {
var dc = this;
if (typeof githubid === 'number') {
githubid = githubid.toString();
}
var query = new azure.TableQuery()
.where('PartitionKey eq ?', this.options.partitionKey)
.and('active eq ?', true)
.and('ghid eq ?', githubid);
dc.table.queryEntities(dc.options.pendingApprovalsTableName,
query,
null,
function (error, results) {
if (error) return callback(error);
var entries = [];
if (results && results.entries && results.entries.length) {
for (var i = 0; i < results.entries.length; i++) {
var r = results.entries[i];
if (r && r.active && r.active._) {
entries.push(reduceEntity(r));
}
}
}
callback(null, entries);
});
};
DataClient.prototype.replaceApprovalRequest = function uar(requestId, mergeEntity, callback) {
var dc = this;
var entity = dc.createEntity(dc.options.partitionKey, requestId, mergeEntity);
dc.table.replaceEntity(dc.options.pendingApprovalsTableName, entity, callback);
};
DataClient.prototype.updateApprovalRequest = function updatedVersion2(requestId, mergeEntity, callback) {
// This is a less efficient implementation for now due to encryption work.
var dc = this;
dc.getApprovalRequest(requestId, (getError, currentVersion) => {
if (getError) {
return callback(getError);
}
var newObject = {};
Object.assign(newObject, currentVersion);
Object.assign(newObject, mergeEntity);
dc.replaceApprovalRequest(requestId, newObject, callback);
});
};
module.exports = DataClient;