This repository has been archived by the owner on Apr 23, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapifunc.js
470 lines (406 loc) · 17.9 KB
/
apifunc.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
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
let trellomembers, trellokey, trellotoken;;
//Airtable Calls
function getBaseJson(query) {
console.log('getJsonBase');
alltests = '';
var records = [];
let requestsArray = urlArray.map((url) => {
let request = new Request(url + "&fields[0]=Experiment&fields[1]=Status&fields[2]=Trello%20Link&fields[4]=Results&fields[5]=ExperimentId&filterByFormula=AND(NOT({Status}%20=%20%27%27%20%20),%20NOT(%20{Status}%20=%20%27Completed%27,%20NOT(%20{Status}%20=%20%27Blocked%27),%20NOT(%20{Status}%20=%20%27Softcoded%27)))", {
headers: new Headers({
'Content-Type': 'text/json'
}),
method: 'GET'
});
return request;
});
//Fire off series of promises to get tests out of various airtable bases
Promise.all(requestsArray.map((request) => {
//console.log(request);
return fetch(request).then((response) => {
//console.log(response);
if(response.status === 200)
return response.json();
else
console.log('Double check your airtable fields');
}).then((data) => {
//console.log(data);
if (typeof data != 'undefined') {
data.baseid = request.url.substring(request.url.lastIndexOf("v0/") + 3, request.url.lastIndexOf("/"))
return data;
}
});
})).then((values) => {
//Merge all the testing into a single array
values.map((item) => {
if (typeof item != 'undefined') {
for (let i = item.records.length - 1; i >= 0; i--) {
item.records[i].baseid = item.baseid;
//Pull out trello card id
if (item.records[i].fields["Trello Link"]) {
var startpos = item.records[i].fields["Trello Link"].indexOf('/c/') + 3;
var endpos = item.records[i].fields["Trello Link"].indexOf('/', startpos + 1);
item.records[i].trelloid = item.records[i].fields["Trello Link"].substr(startpos, endpos - startpos);
}
records.push(item.records[i]);
}
}
});
alltests = records;
//Pull out test that we want to scan for transition to QA or Done Ready
cardstoscan = _.filter(alltests, function (c) {
return c.fields.Status === "In QA" || c.fields.Status === "Implementation" || c.fields.Status === "Live";
});
//Fire off more promises to scan each card to see what list it's on now
Promise.all(cardstoscan.map((test) => {
return ScanCard(test.trelloid).then((response) => {
return response;
}).then((data) => {
//console.log(test);
let Status = test.fields.Status;
let BaseId = test.baseid;
let RecId = test.id;
data.id = test.id;
//console.log(test.fields);
//console.log(data);
if (!data.status)
data.status = test.fields.Status;
//Check to see if card moved to a QA/Done if so update airtable
if (data.change !== "no change" && test.fields.Status !== data.status) {
//update airtable record
UpdateAtRecord('Status', data.status, BaseId, RecId);
if (parseInt(data.exid, 10) > 0)
UpdateAtRecord('ExperimentId', data.exid, BaseId, RecId);
} else if (test.fields.ExperimentId !== data.exid) {
//This is for cards that are live but didnt have a experiment id, we use the id we scanned and update
if (parseInt(data.exid, 10) > 0)
UpdateAtRecord('ExperimentId', data.exid, BaseId, RecId);
}
return data;
})
})).then((values) => {
//console.log('---');
alltests.map(function (test) {
values.map(function (utest) {
if (utest.change !== "no change") {
if (test.id === utest.id) {
if (test.fields.Status !== utest.status) {
//update main array object status
//console.log(utest);
test.fields.Status = utest.status;
test.fields.ExperimentId = utest.exid;
} else if (test.fields.ExperimentId !== utest.exid) {
if (parseInt(utest.exid, 10) > 0)
test.fields.ExperimentId = utest.exid;
}
}
}
});
});
if(query && query.length>1){
filteredtests = _.filter(alltests, function (item) {
return item.fields.Experiment.toLowerCase().indexOf(query.toLowerCase()) > -1;
});
bindTests(filteredtests);
} else {
bindTests(alltests);
}
});
});
}
function UpdateAtRecord(recordname, value, baseid, recid, callback) {
var x = new XMLHttpRequest();
x.open('PATCH', 'https://api.airtable.com/v0/' + baseid + '/Roadmap/' + recid + '?api_key=' + apikey);
x.setRequestHeader("Content-Type", "application/json;charset=UTF-8");
x.onload = function () {
//console.log(x);
json = JSON.parse(x.responseText);
if (json.error) {
alert(json.error.type);
}
return callback();
};
x.send(JSON.stringify({ fields: { [recordname]: value } }));
}
function GetAtRecord(baseid, recid, callback) {
var x = new XMLHttpRequest();
x.open('GET', 'https://api.airtable.com/v0/' + baseid + '/Roadmap/' + recid + '?api_key=' + apikey);
x.setRequestHeader("Content-Type", "application/json;charset=UTF-8");
x.onload = function () {
//console.log(x);
json = JSON.parse(x.responseText);
if (json.error) {
alert(json.error.type);
}
return callback(json);
};
x.send();
}
//Trello Calls
function ScanBoardForListId(boardid, namecontains, callback) {
var data = JSON.stringify(false);
var xhr = new XMLHttpRequest();
xhr.withCredentials = false;
xhr.addEventListener("readystatechange", function () {
if (this.readyState === this.DONE) {
let lists = JSON.parse(this.responseText);
for (var b of lists) {
if (b.name.includes(namecontains)) {
return callback(b.id, b.idBoard);
}
}
}
});
xhr.open("GET", "https://api.trello.com/1/boards/" + boardid + "/lists?key=" + trellokey.trim() + "&token=" + trellotoken.trim());
xhr.send(data);
}
function GetImplementationLists(callback) {
var data = JSON.stringify(false);
var xhr = new XMLHttpRequest();
xhr.withCredentials = false;
xhr.addEventListener("readystatechange", function () {
//this fires after trello board is read
if (this.readyState === this.DONE) {
let lists = JSON.parse(this.responseText);
let listurl = 'https://api.trello.com/1/lists/{listid}/cards?key=' + trellokey + '&token=' + trellotoken;
//Use the lists info to build api calls to get card count in each list
let requestsArray = lists.map((list) => {
//console.log(list);
let request = new Request(listurl.replace('{listid}', list.id, listurl), {
headers: new Headers({
'Content-Type': 'text/json'
}),
method: 'GET'
});
request.name = list.name;
request.id = list.id;
request.idBoard = list.idBoard;
return request;
});
Promise.all(requestsArray.map((request) => {
return fetch(request).then((response) => {
return response.json();
}).then((data) => {
let dataObj = {};
dataObj.name = request.name;
dataObj.data = data.length;
dataObj.id = request.id;
dataObj.idBoard = request.idBoard;
return dataObj;
});
})).then((values) => {
//console.log(values);
callback(values);
}).catch(console.error.bind(console));
}
});
xhr.open("GET", "https://api.trello.com/1/boards/chLoHyqs/lists?key=" + trellokey.trim() + "&token=" + trellotoken.trim());
xhr.send(data);
}
function AddMemberToCard(cardid, memberId) {
var data = JSON.stringify(false);
var xhr = new XMLHttpRequest();
xhr.withCredentials = false;
xhr.addEventListener("readystatechange", function () {
if (this.readyState === this.DONE) {
console.log(this.responseText);
}
});
xhr.open("POST", "https://api.trello.com/1/cards/" + cardid + "/idMembers?value=" + memberId + "&key=" + trellokey.trim() + "&token=" + trellotoken.trim());
xhr.send(data);
}
function MoveCard(cardid, listid, memberToAdd, username, comment, due, boardid, callback) {
// console.log(trellokey);
var duedate;
if (due) {
let mydate = new Date(due);
mydate.setHours(mydate.getHours() + 17);
duedate = mydate.toISOString();
} else {
var date = new Date();
date.setDate(date.getDate() + 3);
if (date.getDay() === 1) {
date.setDate(date.getDate() + 1);
} else if (date.getDay() === 0) {
date.setDate(date.getDate() + 2);
} else if (date.getDay() === 6) {
date.setDate(date.getDate() + 3);
}
duedate = date.getFullYear() + '-' + (date.getMonth() + 1) + '-' + date.getDate();
duedate = new Date(duedate);
duedate.setHours(17);
duedate = duedate.toISOString();
}
if (!boardid)
boardid = '54f4ad49bb26fe25381f2048';
var data = JSON.stringify(false);
var xhr = new XMLHttpRequest();
xhr.withCredentials = false;
xhr.addEventListener("readystatechange", function () {
if (this.readyState === this.DONE) {
//console.log(this);
if (memberToAdd) {
AddMemberToCard(cardid, memberToAdd);
AddTrelloComment(username, cardid, comment);
} else {
AddTrelloComment(username, cardid, comment);
}
}
});
//console.log("https://api.trello.com/1/cards/" + cardid + "?idList=" + listid + "&idBoard=" + boardid + "&due=" + duedate + "&key=" + trellokey.trim() + "&token=" + trellotoken.trim());
xhr.open("PUT", "https://api.trello.com/1/cards/" + cardid + "?idList=" + listid + "&idBoard=" + boardid + "&due=" + duedate + "&key=" + trellokey.trim() + "&token=" + trellotoken.trim());
xhr.send(data);
}
function GetTrelloMembers() {
const orgid = '51ae3b0fe6c311dd13000de0';
var data = null;
var xhr = new XMLHttpRequest();
xhr.addEventListener("readystatechange", function () {
if (this.readyState === this.DONE) {
members = JSON.parse(this.responseText);
trellomembers = members;
}
});
//console.log("https://api.trello.com/1/organizations/51ae3b0fe6c311dd13000de0/members?filter=all&fields=all&key=" + trellokey.trim() + "&token=" + trellotoken.trim());
xhr.open("GET", "https://api.trello.com/1/organizations/51ae3b0fe6c311dd13000de0/members?filter=all&fields=all&key=" + trellokey.trim() + "&token=" + trellotoken.trim());
xhr.send(data);
}
function AddTrelloComment(username, cardid, comment) {
var data = null;
var xhr = new XMLHttpRequest();
if (comment.length < 1) {
comment = "here's another one for the queue. Let me know if any questions. Thanks!";
}
xhr.addEventListener("readystatechange", function () {
if (this.readyState === this.DONE) {
//console.log(this.responseText);
}
});
if (username)
comment = encodeURIComponent("@" + username + " " + comment);
else
comment = encodeURIComponent(comment);
xhr.open("POST", "https://api.trello.com/1/cards/" + cardid + "/actions/comments?text=" + comment + "&key=" + trellokey.trim() + "&token=" + trellotoken.trim());
xhr.send(data);
}
function GetExperimentId(cardid, callback) {
var data = null;
var xhr = new XMLHttpRequest();
xhr.addEventListener("readystatechange", function () {
if (this.readyState === this.DONE) {
let desc = JSON.parse(this.responseText)._value;
let editsection = desc.indexOf('### Edit:') + 9;
let exposition = desc.indexOf('/experiments/', editsection) + 13;
let endposition = desc.indexOf('\n', exposition);
let exlength = endposition - exposition;
return callback(desc.substr(exposition, exlength));
}
});
xhr.open("GET", "https://api.trello.com/1/cards/" + cardid + "/desc?key=" + trellokey.trim() + "&token=" + trellotoken.trim());
xhr.send(data);
}
function ScanCard(cardid) {
//console.log('here');
return new Promise(function (resolve, reject) {
var data = null;
var xhr = new XMLHttpRequest();
var test = {};
xhr.addEventListener("readystatechange", function () {
if (this.status === 404 || this.status === 400) {
test.change = "no change";
//test.exid = desc.substr(exposition, exlength);
return resolve(test);
} else if (this.readyState === this.DONE) {
let card = JSON.parse(this.responseText);
//console.log(card);
let desc = card.desc;
let editsection = desc.indexOf('### Edit:') + 9;
let exposition = desc.indexOf('/experiments/', editsection) + 13;
let endposition = desc.indexOf('\n', exposition);
let exlength = endposition - exposition;
//console.log(card);
if (card.idList === "595d2dba6c4bd076d7b6c7b8" || card.idList === "5806175db43e2efadf7c1935") {
test.status = "In QA";
test.exid = desc.substr(exposition, exlength);
//test.id = card.id;
return resolve(test);
} else if (card.idList === "54f4ad58842c59cc3798bcf7") {
test.status = "Ready for PM";
test.exid = desc.substr(exposition, exlength);
//test.id = card.id;
return resolve(test);
}
else {
test.change = "ex update";
test.exid = desc.substr(exposition, exlength);
return resolve(test);
}
}
});
// Handle network errors
xhr.onerror = function () {
console.log('error');
};
xhr.open("GET", "https://api.trello.com/1/cards/" + cardid + "?key=" + trellokey.trim() + "&token=" + trellotoken.trim());
xhr.send(data);
});
}
//Opt Calls
function GetOptimizelyResults(exid, token, callback) {
var resultlink = 'https://api.optimizely.com/v2/experiments/' + exid + '/results';
var x = new XMLHttpRequest();
x.open('GET', resultlink);
x.setRequestHeader("Authorization", "Bearer " + token);
x.onload = function () {
json = JSON.parse(x.responseText);
var rtext = "";
var start = "Started on: " + json.start_time + "<br>";
if (json.reach.total_count)
var total_visitors = "Total visitors:" + json.reach.total_count + "<br>";
rtext += start + total_visitors + "<ul>";
for (var c = 0; c < json.metrics.length; c++) {
//metric name
rtext += "<li>" + json.metrics[c].name + "<br><ul>";
let o = 0;
//console.log(json.metrics[c]);
for (var prop in json.metrics[c].results) {
if (json.metrics[c].results[prop].is_baseline === false) {
var lift = Math.round((json.metrics[c].results[prop].lift.value * 100) * 10) / 10;
var ss = Math.floor(json.metrics[c].results[prop].lift.significance * 100);
if (lift > 0) lift = "+" + lift;
if (ss === 0) ss = "<1%"; else ss = ss + "%";
//variation results
rtext += "<li>" + json.metrics[c].results[prop].name + ": " + lift + "% @" + ss + " ss<br></li>";
}
o++;
}
rtext += "</li></ul>";
}
rtext += "</ul>";
return callback(rtext);
};
x.send();
}
function GetPreviewLinks(exid, token, callback) {
var resultlink = 'https://api.optimizely.com/v2/experiments/' + exid;
var x = new XMLHttpRequest();
x.open('GET', resultlink);
x.setRequestHeader("Authorization", "Bearer " + token);
x.onload = function () {
json = JSON.parse(x.responseText);
//console.log(json.variations);
//console.log('here');
let links = [];
let projid = json.project_id;
let campid = json.campaign_id;
let vars = json.variations;
vars.forEach(function (v) {
let l = {};
//l.link = json.url_targeting.edit_url + "?optimizely_token=" + token + "&optimizely_x=" + v.variation_id + "&optimizely_preview_layer_ids=" + campid + "&optimizely_snippet=" + projid + "&optimizely_preview_mode_CAMPAIGN=" + campid;
l.link = v.actions[0].share_link + "&optimizely_preview_mode_CAMPAIGN=" + campid;
l.name = v.name;
links.push(l);
});
return callback(links);
};
x.send();
}