-
Notifications
You must be signed in to change notification settings - Fork 0
/
newman.js
299 lines (274 loc) · 10.3 KB
/
newman.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
const newman = require("newman");
const pmEnv = require("./pmEnvironment.js");
const fs = require("fs");
//https://www.npmjs.com/package/debug
//Mac: DEBUG=* aep-tag-tool....
//WIN: set DEBUG=* & aep-tag-tool....
const debug = require("debug");
const debugCollections = require("debug")("collections");
const debugNewman = require("debug")("newman");
require("debug")("newman:cli");
exports.debugOptions = {
"collections": "Postman collection messages",
"newman": "Newman command messages",
"newman:cli": "Newman cli output for verbose messaging of collections"
};
let REPORTERS = ["emojitrain", "junit"];
let IO_OAUTH_COLLECTION = require("./postman/Adobe IO Token OAuth.postman_collection.json");
let IO_JWT_COLLECTION = require("./postman/Adobe IO Token.postman_collection.json");
let EXPORT_COLLECTION = require("./postman/Export Tag Property.postman_collection.json");
let IMPORT_COLLECTION = require("./postman/Import Tag Property.postman_collection.json");
let DELETE_PROPS = require("./postman/Delete Properties.postman_collection.json");
//Development commands
if (debug.enabled("collections")) {
debugCollections("Using Postman Collections");
IO_OAUTH_COLLECTION = "";
IO_JWT_COLLECTION = "https://www.getpostman.com/collections/6ad99074fc75d564ac8a";
IMPORT_COLLECTION = "https://www.getpostman.com/collections/2f3dc4c81eb464c21693";
DELETE_PROPS = "https://www.getpostman.com/collections/357a7d9bea644bfc5b46";
EXPORT_COLLECTION = "https://www.getpostman.com/collections/55520565b0f9933b5cf8";
}
//Mac: DEBUG=newman:cli aep-tag-tool....
//WIN: set DEBUG=newman:cli & aep-tag-tool....
if (debug.enabled("newman:cli")) {
REPORTERS = ["cli", "junit"];
}
let TIMESTAMP = formatDateTime();
let reportersDir = "newman/";
function exportTag(env, pid, exportDir, callback) {
authenicateAIO(env)
.then((resultEnv) => newmanRun("exportTag",
resultEnv, "",
EXPORT_COLLECTION, "",
"", [{
"key": "propID",
"value": pid
}])
)
.then(function(resultEnv){
let tagExport = {};
tagExport.propID = pid;
tagExport.propertyName = pmEnv.getEnvValue(resultEnv, "exportPropName");
tagExport.extensions = pmEnv.getEnvValue(resultEnv, "exportExtensions");
tagExport.dataElements = pmEnv.getEnvValue(resultEnv, "exportDataElements");
tagExport.ruleNames = pmEnv.getEnvValue(resultEnv, "exportRules");
tagExport.rules = {};
for (var element in tagExport.ruleNames) {
let ruleName = tagExport.ruleNames[element].attributes.name;
tagExport.rules[ruleName] = pmEnv.getEnvValue(resultEnv, "exportRuleCmps-" + element);
}
//Write to a file
var propName = tagExport.propertyName.replace(/\s+/g, "-").toLowerCase();
//TODO fix -o outputDir
if(!exportDir) exportDir = ".";
fs.writeFileSync(exportDir + "/" + propName + ".json", JSON.stringify(tagExport, null, 2));
})
.then((resultEnv) => callback(null, resultEnv))
.catch(err => callback(err, null));
}
function importTag(env, importObj, actions, globals) {
return new Promise(function(resolve, reject) {
authenicateAIO(env)
.then(function(resultEnv){ //Add propID if importing to an existing property
return new Promise(function (resolve,reject){
if(!actions) actions = getImportActions(); //TODO verify it works
if(actions[0] != "C"){
if(importObj.propID){
let env = pmEnv.setEnvValue(resultEnv, "propID", importObj.propID);
if(env) resolve(env);
else reject(new Error("Cannot update environment"));
} else {
reject(new Error("No propID specified to import into an existing property"));
}
} else resolve(resultEnv);
});
})
.then((resultEnv) => recurseImportChain(resultEnv, importObj, actions, globals))
.then(resultEnv => resolve(resultEnv))
.catch(err => reject(err));
})
.catch(err => console.error(err));
}
function recurseImportChain(environment, importItems, actions, globals){
const nextAction = actions.shift();
if(nextAction){
if(nextAction === "C"){
return createProperty(environment, importItems, globals)
.then((resultEnv) => recurseImportChain(resultEnv, importItems, actions, globals));
} else if(nextAction === "E"){
return installExtensions(environment, importItems, globals)
.then((resultEnv) => recurseImportChain(resultEnv, importItems, actions, globals));
} else if(nextAction === "D"){
return importDataElements(environment, importItems, globals)
.then((resultEnv) => recurseImportChain(resultEnv, importItems, actions, globals));
} else if(nextAction === "R"){
return importRules(environment, importItems, globals)
.then((resultEnv) => recurseImportChain(resultEnv, importItems, actions, globals));
} else if(nextAction === "L"){
return publishLibraryToDev(environment, globals)
.then((resultEnv) => recurseImportChain(resultEnv, "", actions, globals));
} else if(nextAction === "P"){
return publishLibraryToProd(environment, globals)
.then((resultEnv) => recurseImportChain(resultEnv, "", actions, globals))
.then(function (resultEnv){
let artifactURL = pmEnv.getEnvValue(resultEnv, "prodArtifactURL");
console.log("Prod Library embed code: ");
console.log("<script src='"+artifactURL+"' async></script>");
});
} else Promise.resolve(environment);
}else{
return Promise.resolve(environment);
}
}
function deleteTags(env, searchStr, callback) {
authenicateAIO(env)
.then((resultEnv) => newmanRun("deleteTags",
resultEnv, "",
DELETE_PROPS, "",
"", [{
"key": "tagNameIncludes",
"value": searchStr
}])
)
.then((resultEnv) => callback(null, resultEnv))
.catch(err => callback(err, null));
}
// Runs the Adobe IO Token collection
function authenicateAIO(environment) {
let auth_method;
for(let i = 0; i < environment.values.length; i++){
if(environment.values[i].key == "AUTH_METHOD"){
auth_method = environment.values[i].value;
i = environment.values.length;
}
}
let auth_collection;
if(auth_method == pmEnv.auth.oauth){
auth_collection = IO_OAUTH_COLLECTION;
} else if(auth_method == pmEnv.auth.jwt){
auth_collection = IO_JWT_COLLECTION;
}
return newmanRun("auth",
environment, "",
auth_collection, "",
"", "");
}
// Runs the Import Tag collection folder "Create Tag Property"
function createProperty(environment, importItems, globals) {
return newmanRun("createProp",
environment, globals,
IMPORT_COLLECTION, "Create Tag Property",
"", [{
"key": "propName",
"value": importItems.propertyName
}]);
}
// Runs the Import Tag collection folder "Add Tag Extensions"
function installExtensions(environment, importItems, globals) {
return newmanRun("installExts",
environment, globals,
IMPORT_COLLECTION, "Add Tag Extensions",
importItems.extensions, "");
}
// Runs the Import Tag collection folder "Add Tag Data Elements"
function importDataElements(environment, importItems, globals) {
return newmanRun("installDataElements",
environment, globals,
IMPORT_COLLECTION, "Add Tag Data Elements",
importItems.dataElements, "");
}
async function importRules(environment, importItems, globals){
for(const rule in importItems.rules){
await newmanRun(rule,
environment,
globals,
IMPORT_COLLECTION,
"Add Tag Rule and CMPs",
importItems.rules[rule],
[{
"key": "ruleName",
"value": rule
}]);
}
return environment;
}
// Runs the Import Tag collection folder "Publish Dev"
function publishLibraryToDev(environment, globals) {
return newmanRun("publishLibDev",
environment, globals,
IMPORT_COLLECTION, "Publish Dev",
"", "");
}
// Runs the Import Tag collection folder "Publish Prod"
function publishLibraryToProd(environment, globals) {
return newmanRun("publishLibProd",
environment, globals,
IMPORT_COLLECTION, "Publish Prod",
"", "");
}
/******* Helper Functions ******/
function newmanRun(cmdName, env, globals, collection, folder, data, envVar){
var f2 = function (k, v) { return k && v && typeof v !== "number" ? "" + v : v; };
debugNewman(JSON.stringify(data, f2, 2));
const reportName = TIMESTAMP + "-" + cmdName + "-Report";
if(folder && folder != ""){
console.log("Running: " + folder + " for: " + cmdName);
} else {
console.log("Running: " + cmdName);
}
debugNewman("ReportNameHTML: "+ reportersDir + reportName + ".[html | xml]");
// Uncomment to generate the final postman environment file
// console.log(JSON.stringify(env));
return new Promise(function(resolve, reject) {
//run newman to create new rule
newman.run({
collection: collection,
environment: env,
globals: globals,
folder: folder,
envVar: envVar,
iterationData: data,
reporters: REPORTERS,
reporter: {
"junit": { export: reportersDir + reportName + ".xml" }
}//TODO investigate saving via Win and Mac
}).on("done", function (err, summary) {
if (err) reject(err);
if (summary.run.failures == "") {
console.log("Success!");
} else {
reject("API Failures. Check the report logs in " + reportersDir);
}
resolve(summary.environment);
});
});
}
function formatDateTime() {
var d = new Date(Date.now());
var year = d.getFullYear();
var month = d.getMonth() + 1;
var date = d.getDate();
var hour = d.getHours();
var min = (d.getMinutes() < 10 ? "0" : "") + d.getMinutes();
var time = year + "-" + month + "-" + date + "-" + hour + ":" + min;
return time;
}
function getImportActions(create, extensions, dataElements, rules, libraryToDev, publishToProd){
let actions = [];
if(create || extensions || dataElements || rules || libraryToDev || publishToProd){
if(create) actions.push("C");
if(extensions) actions.push("E");
if(dataElements) actions.push("D");
if(rules) actions.push("R");
if(libraryToDev) actions.push("L");
if(publishToProd) actions.push("P");
} else { // create and import everything
actions = ["C", "E", "D", "R", "L", "P"];
}
debugNewman(actions);
return actions;
}
exports.getImportActions = getImportActions;
exports.exportTag = exportTag;
exports.importTag = importTag;
exports.deleteTags = deleteTags;