|
| 1 | +const fs = require('fs'); |
| 2 | +const path = require('path'); |
| 3 | +const request = require('request'); |
| 4 | +const http = require('http'); |
| 5 | +const https = require('https'); |
| 6 | + |
| 7 | +const logger = require("../../helpers/logger").winstonLogger; |
| 8 | + |
| 9 | +const { API_URL, consoleHolder } = require('../helper/constants'); |
| 10 | + |
| 11 | +/* Below global methods are added here to remove cyclic dependency with helper.js, refactor later */ |
| 12 | +const httpKeepAliveAgent = new http.Agent({ |
| 13 | + keepAlive: true, |
| 14 | + timeout: 60000, |
| 15 | + maxSockets: 2, |
| 16 | + maxTotalSockets: 2 |
| 17 | +}); |
| 18 | + |
| 19 | +const httpsKeepAliveAgent = new https.Agent({ |
| 20 | + keepAlive: true, |
| 21 | + timeout: 60000, |
| 22 | + maxSockets: 2, |
| 23 | + maxTotalSockets: 2 |
| 24 | +}); |
| 25 | + |
| 26 | +const debug = (text) => { |
| 27 | + if (process.env.BROWSERSTACK_OBSERVABILITY_DEBUG === "true" || process.env.BROWSERSTACK_OBSERVABILITY_DEBUG === "1") { |
| 28 | + logger.info(`[ OBSERVABILITY ] ${text}`); |
| 29 | + } |
| 30 | +} |
| 31 | + |
| 32 | +let packages = {}; |
| 33 | + |
| 34 | +exports.requireModule = (module, internal = false) => { |
| 35 | + let local_path = ""; |
| 36 | + if(process.env["browserStackCwd"]){ |
| 37 | + local_path = path.join(process.env["browserStackCwd"], 'node_modules', module); |
| 38 | + } else if(internal) { |
| 39 | + local_path = path.join(process.cwd(), 'node_modules', 'browserstack-cypress-cli', 'node_modules', module); |
| 40 | + } else { |
| 41 | + local_path = path.join(process.cwd(), 'node_modules', module); |
| 42 | + } |
| 43 | + if(!fs.existsSync(local_path)) { |
| 44 | + let global_path; |
| 45 | + if(['jest-runner', 'jest-runtime'].includes(module)) |
| 46 | + global_path = path.join(GLOBAL_MODULE_PATH, 'jest', 'node_modules', module); |
| 47 | + else |
| 48 | + global_path = path.join(GLOBAL_MODULE_PATH, module); |
| 49 | + if(!fs.existsSync(global_path)) { |
| 50 | + throw new Error(`${module} doesn't exist.`); |
| 51 | + } |
| 52 | + return require(global_path); |
| 53 | + } |
| 54 | + return require(local_path); |
| 55 | +} |
| 56 | + |
| 57 | +getPackageVersion = (package_) => { |
| 58 | + if(packages[package_]) return packages[package_]; |
| 59 | + return packages[package_] = this.requireModule(`${package_}/package.json`).version; |
| 60 | +} |
| 61 | + |
| 62 | +getAgentVersion = () => { |
| 63 | + let _path = path.join(__dirname, '../../../package.json'); |
| 64 | + if(fs.existsSync(_path)) |
| 65 | + return require(_path).version; |
| 66 | +} |
| 67 | + |
| 68 | +class CrashReporter { |
| 69 | + static instance; |
| 70 | + |
| 71 | + constructor() { |
| 72 | + } |
| 73 | + |
| 74 | + static getInstance() { |
| 75 | + if (!CrashReporter.instance) { |
| 76 | + CrashReporter.instance = new CrashReporter(); |
| 77 | + } |
| 78 | + return CrashReporter.instance; |
| 79 | + } |
| 80 | + |
| 81 | + setCredentialsForCrashReportUpload(credentialsStr) { |
| 82 | + /* User credentials used for reporting crashes */ |
| 83 | + this.credentialsForCrashReportUpload = JSON.parse(credentialsStr); |
| 84 | + } |
| 85 | + |
| 86 | + setConfigDetails(credentialsStr, browserstackConfigFile, cypressConfigFile) { |
| 87 | + /* User test config for build run */ |
| 88 | + this.userConfigForReporting = { |
| 89 | + framework: 'Cypress', |
| 90 | + browserstackConfigFile: browserstackConfigFile, |
| 91 | + cypressConfigFile: cypressConfigFile |
| 92 | + }; |
| 93 | + this.setCredentialsForCrashReportUpload(credentialsStr); |
| 94 | + } |
| 95 | + |
| 96 | + uploadCrashReport(exception, stacktrace) { |
| 97 | + try { |
| 98 | + if (!this.credentialsForCrashReportUpload.username || !this.credentialsForCrashReportUpload.password) { |
| 99 | + return debug('[Crash_Report_Upload] Failed to parse user credentials while reporting crash') |
| 100 | + } |
| 101 | + |
| 102 | + const data = { |
| 103 | + hashed_id: process.env.BS_TESTOPS_BUILD_HASHED_ID, |
| 104 | + observability_version: { |
| 105 | + frameworkName: 'Cypress', |
| 106 | + frameworkVersion: getPackageVersion('cypress'), |
| 107 | + sdkVersion: getAgentVersion() |
| 108 | + }, |
| 109 | + exception: { |
| 110 | + error: exception.toString(), |
| 111 | + stackTrace: stacktrace |
| 112 | + }, |
| 113 | + config: this.userConfigForReporting |
| 114 | + } |
| 115 | + |
| 116 | + const options = { |
| 117 | + auth: { |
| 118 | + ...this.credentialsForCrashReportUpload |
| 119 | + }, |
| 120 | + headers: { |
| 121 | + 'Content-Type': 'application/json', |
| 122 | + 'X-BSTACK-TESTOPS': 'true' |
| 123 | + }, |
| 124 | + method: 'POST', |
| 125 | + url: `${API_URL}/api/v1/analytics`, |
| 126 | + body: data, |
| 127 | + json: true, |
| 128 | + agent: API_URL.includes('https') ? httpsKeepAliveAgent : httpKeepAliveAgent |
| 129 | + }; |
| 130 | + |
| 131 | + request(options, function callback(error, response, body) { |
| 132 | + if(error) { |
| 133 | + debug(`[Crash_Report_Upload] Failed due to ${error}`); |
| 134 | + } else if(response.statusCode != 200) { |
| 135 | + debug(`[Crash_Report_Upload] Failed due to ${response && response.body ? response.body : `Received response from BrowserStack Server with status : ${response.statusCode}`}`); |
| 136 | + } else { |
| 137 | + debug(`[Crash_Report_Upload] Success response: ${JSON.stringify({status: response.status, body: response.body})}`) |
| 138 | + } |
| 139 | + }); |
| 140 | + } catch(e) { |
| 141 | + debug(`[Crash_Report_Upload] Processing failed due to ${e && e.stack}`); |
| 142 | + } |
| 143 | + } |
| 144 | +} |
| 145 | + |
| 146 | +module.exports = CrashReporter; |
0 commit comments