|
| 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 | +}; |
0 commit comments