Skip to content

Commit fe8eae9

Browse files
Merge pull request #86 from LambdaTest/dev
Dev
2 parents 0b34b75 + 60a757c commit fe8eae9

File tree

6 files changed

+226
-9
lines changed

6 files changed

+226
-9
lines changed

commands/generate_reports.js

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

commands/utils/archive.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ function archive_project(lt_config) {
5353
"cypress/screenshots/**/*",
5454
"cypress/videos/**/*",
5555
"cypress/results/**/*",
56+
"lambdatest-artefacts/**/*",
5657
].concat(lt_config["run_settings"]["ignore_files"]);
5758
//If we have some env variables passed through cli or config file we will ignore the original file and create a new one using
5859
//the parameter appended in config file through code

commands/utils/constants.js

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,19 @@ 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/",
22+
},
23+
beta: {
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/",
2033
},
2134
stage: {
2235
INTEGRATION_BASE_URL: "https://stage-api.lambdatest.com/liis",
@@ -26,12 +39,7 @@ module.exports = {
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;

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "lambdatest-cypress-cli",
3-
"version": "2.1.4",
3+
"version": "2.1.5",
44
"description": "The lambdatest-cypress-cli is LambdaTest's command-line interface (CLI) aimed to help you run your Cypress tests on LambdaTest platform.",
55
"homepage": "https://github.com/LambdaTest/lambdatest-cypress-cli",
66
"author": "LambdaTest <[email protected]>",
@@ -20,6 +20,7 @@
2020
"@lambdatest/node-tunnel": "^3.0.0",
2121
"archiver": "^5.1.0",
2222
"glob": "^7.1.6",
23+
"node-stream-zip": "^1.15.0",
2324
"request": "^2.88.2",
2425
"ws": "^7.4.3",
2526
"yargs": "^16.1.0"

0 commit comments

Comments
 (0)