Skip to content

Commit 9a6a677

Browse files
authored
Merge pull request #81 from japneetlambdatest/MLE-6963
added generate report command
2 parents fd7f1ca + 26a6274 commit 9a6a677

File tree

4 files changed

+221
-8
lines changed

4 files changed

+221
-8
lines changed

commands/generate_reports.js

Lines changed: 173 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,173 @@
1+
const request = require("request");
2+
const constants = require("./utils/constants.js");
3+
const process = require("process");
4+
const build_stats = require("./utils/poller/build_stats.js");
5+
const { access } = require("fs");
6+
var fs = require("fs");
7+
const StreamZip = require("node-stream-zip");
8+
9+
function download_artefact(username, access_key, env, test_id, file_path) {
10+
return new Promise(function (resolve, reject) {
11+
let response_code;
12+
if (!fs.existsSync(file_path)) {
13+
console.log("Creating paths");
14+
fs.mkdirSync(file_path, { recursive: true });
15+
}
16+
let old_path = file_path;
17+
//Create an empty file
18+
file_path += "/artefacts.zip";
19+
const stream = fs.createWriteStream(file_path);
20+
stream.end();
21+
request(
22+
constants[env].REPORT_URL + test_id,
23+
{
24+
auth: {
25+
username: username,
26+
password: access_key,
27+
},
28+
gzip: true,
29+
},
30+
(err, res, body) => {
31+
if (err) {
32+
reject(err);
33+
}
34+
response_code = res.statusCode;
35+
}
36+
).pipe(
37+
fs
38+
.createWriteStream(file_path, {
39+
overwrite: true,
40+
})
41+
.on("finish", function () {
42+
if (response_code == 200) {
43+
const zip = new StreamZip({ file: file_path });
44+
zip.on("ready", () => {
45+
zip.extract(null, old_path, (err, count) => {
46+
zip.close();
47+
resolve(
48+
err
49+
? "Extract error for " + test_id
50+
: `Extracted ${count} entries for ` + test_id
51+
);
52+
});
53+
});
54+
} else {
55+
reject("Could not download artefacts for test id " + test_id);
56+
}
57+
//delete the zip file
58+
fs.unlinkSync(file_path);
59+
})
60+
);
61+
});
62+
}
63+
64+
function generate_report(args) {
65+
return new Promise(function (resolve, reject) {
66+
var username = "";
67+
var access_key = "";
68+
69+
//Check for username
70+
if ("user" in args) {
71+
username = args.user;
72+
} else if (process.env.LT_USERNAME) {
73+
console.log("Setting Username from ENV", process.env.LT_USERNAME);
74+
username = process.env.LT_USERNAME;
75+
} else {
76+
reject("Username not provided");
77+
}
78+
79+
//Check for access key
80+
if ("access_key" in args) {
81+
access_key = args.access_key;
82+
} else if (process.env.LT_ACCESS_KEY) {
83+
console.log("Setting Access Key from ENV");
84+
access_key = process.env.LT_ACCESS_KEY;
85+
} else {
86+
reject("Access Key not provided");
87+
}
88+
89+
//Check for session id
90+
if (
91+
!("session_id" in args) ||
92+
args["session_id"] == "" ||
93+
args["session_id"] == undefined
94+
) {
95+
reject("Please provide a Session ID");
96+
}
97+
98+
//set working enviornment
99+
var env = "prod";
100+
if ("env" in args) {
101+
if (["stage", "prod", "beta"].includes(args["env"])) {
102+
env = args["env"];
103+
} else {
104+
console.log(
105+
"Environment can be stage, beta or prod, setting Env to prod"
106+
);
107+
}
108+
}
109+
110+
//paylaod required for authentication
111+
build_payload = {
112+
lambdatest_auth: {
113+
username: username,
114+
access_key: access_key,
115+
},
116+
};
117+
118+
build_stats
119+
.get_completed_build_info(build_payload, args["session_id"], env)
120+
.then(function (build_info) {
121+
if (!build_info || build_info.data == null) {
122+
reject("Session not found");
123+
return;
124+
}
125+
let directory = "./lambdatest-artefacts/" + args["session_id"];
126+
//Reject if there are no tests in sessions
127+
if (build_info["data"].length == 0) {
128+
reject("No tests in this session");
129+
}
130+
console.log("Creating directories");
131+
if (!fs.existsSync(directory)) {
132+
fs.mkdirSync(directory, { recursive: true });
133+
console.log("Directory created ", directory);
134+
}
135+
for (i = 0; i < build_info["data"].length; i++) {
136+
download_artefact(
137+
username,
138+
access_key,
139+
env,
140+
build_info["data"][i]["test_id"],
141+
directory +
142+
"/" +
143+
build_info["data"][i]["browser"] +
144+
"-" +
145+
build_info["data"][i]["version"] +
146+
"-" +
147+
build_info["data"][i]["platform"] +
148+
"/" +
149+
build_info["data"][i]["test_id"]
150+
)
151+
.then(function (resp) {
152+
//Files downloaded
153+
console.log(resp);
154+
})
155+
.catch(function (err) {
156+
console.log(err);
157+
});
158+
}
159+
resolve("Done");
160+
})
161+
.catch(function (err) {
162+
console.log("Error occured while getting the build response", err);
163+
});
164+
});
165+
}
166+
167+
module.exports = function (args) {
168+
generate_report(args)
169+
.then(function (resp) {})
170+
.catch(function (err) {
171+
console.log("ERR:", err);
172+
});
173+
};

commands/utils/constants.js

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,21 +17,29 @@ module.exports = {
1717
BUILD_STOP_URL: "https://beta-api.lambdatest.com/api/v1/test/stop?buildId=",
1818
SESSION_URL:
1919
"https://api.lambdatest.com/automation/api/v1/sessions?limit=200&session_id=",
20+
REPORT_URL:
21+
"https://api.lambdatest.com/automation/api/v1/cypress/report/artefacts/test/",
2022
},
2123
stage: {
24+
INTEGRATION_BASE_URL: "https://api.cypress-v3.dev.lambdatest.io/liis",
25+
BUILD_BASE_URL:
26+
"https://api.cypress-v3.dev.lambdatest.io/automation/api/v1/builds/",
27+
BUILD_STOP_URL:
28+
"https://api.cypress-v3.dev.lambdatest.io/api/v1/test/stop?buildId=",
29+
SESSION_URL:
30+
"https://api.cypress-v3.dev.lambdatest.io/automation/api/v1/sessions?limit=200&session_id=",
31+
REPORT_URL:
32+
"https://api.cypress-v3.dev.lambdatest.io/automation/api/v1/cypress/artefacts/test/",
33+
},
34+
beta: {
2235
INTEGRATION_BASE_URL: "https://stage-api.lambdatest.com/liis",
2336
BUILD_BASE_URL:
2437
"https://stage-api.lambdatest.com/automation/api/v1/builds/",
2538
BUILD_STOP_URL:
2639
"https://stage-api.lambdatest.com/api/v1/test/stop?buildId=",
2740
SESSION_URL:
2841
"https://stage-api.lambdatest.com/automation/api/v1/sessions?limit=200&session_id=",
29-
},
30-
beta: {
31-
INTEGRATION_BASE_URL: "https://beta-api.lambdatest.com/liis",
32-
BUILD_BASE_URL: "https://api.lambdatest.com/automation/api/v1/builds/",
33-
BUILD_STOP_URL: "https://beta-api.lambdatest.com/api/v1/test/stop?buildId=",
34-
SESSION_URL:
35-
"https://api.lambdatest.com/automation/api/v1/sessions?limit=200&session_id=",
42+
REPORT_URL:
43+
"https://stage-api.lambdatest.com/automation/api/v1/cypress/artefacts/test/",
3644
},
3745
};

commands/utils/validate.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,8 @@ module.exports = validate_config = function (lt_config) {
143143
) {
144144
let download_folders = lt_config["run_settings"]["downloads"].split(",");
145145
for (folder in download_folders) {
146-
if (folder[0] != ".") {
146+
console.log(download_folders[folder]);
147+
if (download_folders[folder][0] != ".") {
147148
reject("Error!! dowloads folder path is not relative ", folder);
148149
}
149150
}

index.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,4 +169,35 @@ const argv = require("yargs")
169169
require("./commands/build_stop")(argv);
170170
}
171171
)
172+
.command(
173+
"generate-report",
174+
"generate session report",
175+
function (yargs) {
176+
return yargs
177+
.option("user", {
178+
alias: "username",
179+
describe: "Lambdatest Username of User",
180+
type: "string",
181+
demandOption: true,
182+
})
183+
.option("ak", {
184+
alias: "access_key",
185+
describe: "Lambdatest Access Key of User",
186+
type: "string",
187+
})
188+
.option("sid", {
189+
alias: "session_id",
190+
describe: "Session Id",
191+
type: "string",
192+
})
193+
.option("env", {
194+
alias: "environment",
195+
describe: "testing environment",
196+
type: "string",
197+
});
198+
},
199+
function (argv) {
200+
require("./commands/generate_reports")(argv);
201+
}
202+
)
172203
.help().argv;

0 commit comments

Comments
 (0)