-
Notifications
You must be signed in to change notification settings - Fork 39
/
Copy pathindex.js
178 lines (156 loc) · 6.48 KB
/
index.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
const fs = require('fs');
const path = require('path');
const request = require('request');
const http = require('http');
const https = require('https');
const logger = require("../../helpers/logger").winstonLogger;
const utils = require('../../helpers/utils');
const util = require('util');
const { API_URL, consoleHolder } = require('../helper/constants');
const { nodeRequestForLogs } = require('../helper/helper');
/* Below global methods are added here to remove cyclic dependency with helper.js, refactor later */
const httpsKeepAliveAgent = new https.Agent({
keepAlive: true,
timeout: 60000,
maxSockets: 2,
maxTotalSockets: 2
});
const debug = (text) => {
if (process.env.BROWSERSTACK_OBSERVABILITY_DEBUG === "true" || process.env.BROWSERSTACK_OBSERVABILITY_DEBUG === "1") {
logger.info(`[ OBSERVABILITY ] ${text}`);
}
}
let packages = {};
exports.requireModule = (module, internal = false) => {
let local_path = "";
if(process.env["browserStackCwd"]){
local_path = path.join(process.env["browserStackCwd"], 'node_modules', module);
} else if(internal) {
local_path = path.join(process.cwd(), 'node_modules', 'browserstack-cypress-cli', 'node_modules', module);
} else {
local_path = path.join(process.cwd(), 'node_modules', module);
}
if(!fs.existsSync(local_path)) {
let global_path;
if(['jest-runner', 'jest-runtime'].includes(module))
global_path = path.join(GLOBAL_MODULE_PATH, 'jest', 'node_modules', module);
else
global_path = path.join(GLOBAL_MODULE_PATH, module);
if(!fs.existsSync(global_path)) {
throw new Error(`${module} doesn't exist.`);
}
return require(global_path);
}
return require(local_path);
}
getPackageVersion = (package_, bsConfig = null) => {
if(packages[package_]) return packages[package_];
let packageVersion;
/* Try to find version from module path */
try {
packages[package_] = this.requireModule(`${package_}/package.json`).version;
logger.info(`Getting ${package_} package version from module path = ${packages[package_]}`);
packageVersion = packages[package_];
} catch(e) {
debug(`Unable to find package ${package_} at module path with error ${e}`);
}
/* Read package version from npm_dependencies in browserstack.json file if present */
if(utils.isUndefined(packageVersion) && bsConfig && (process.env.BROWSERSTACK_AUTOMATION == "true" || process.env.BROWSERSTACK_AUTOMATION == "1")) {
const runSettings = bsConfig.run_settings;
if (runSettings && runSettings.npm_dependencies !== undefined &&
Object.keys(runSettings.npm_dependencies).length !== 0 &&
typeof runSettings.npm_dependencies === 'object') {
if (package_ in runSettings.npm_dependencies) {
packages[package_] = runSettings.npm_dependencies[package_];
logger.info(`Getting ${package_} package version from browserstack.json = ${packages[package_]}`);
packageVersion = packages[package_];
}
}
}
/* Read package version from project's package.json if present */
const packageJSONPath = path.join(process.cwd(), 'package.json');
if(utils.isUndefined(packageVersion) && fs.existsSync(packageJSONPath)) {
const packageJSONContents = require(packageJSONPath);
if(packageJSONContents.devDependencies && !utils.isUndefined(packageJSONContents.devDependencies[package_])) packages[package_] = packageJSONContents.devDependencies[package_];
if(packageJSONContents.dependencies && !utils.isUndefined(packageJSONContents.dependencies[package_])) packages[package_] = packageJSONContents.dependencies[package_];
logger.info(`Getting ${package_} package version from package.json = ${packages[package_]}`);
packageVersion = packages[package_];
}
return packageVersion;
}
getAgentVersion = () => {
let _path = path.join(__dirname, '../../../package.json');
if(fs.existsSync(_path))
return require(_path).version;
}
class CrashReporter {
static instance;
constructor() {
}
static getInstance() {
if (!CrashReporter.instance) {
CrashReporter.instance = new CrashReporter();
}
return CrashReporter.instance;
}
setCredentialsForCrashReportUpload(credentialsStr) {
/* User credentials used for reporting crashes */
this.credentialsForCrashReportUpload = JSON.parse(credentialsStr);
}
setConfigDetails(credentialsStr, browserstackConfigFile, cypressConfigFile) {
/* User test config for build run */
this.userConfigForReporting = {
framework: 'Cypress',
browserstackConfigFile: browserstackConfigFile,
cypressConfigFile: cypressConfigFile
};
this.setCredentialsForCrashReportUpload(credentialsStr);
}
uploadCrashReport(exception, stacktrace) {
try {
if (!this.credentialsForCrashReportUpload.username || !this.credentialsForCrashReportUpload.password) {
return debug('[Crash_Report_Upload] Failed to parse user credentials while reporting crash')
}
const data = {
hashed_id: process.env.BS_TESTOPS_BUILD_HASHED_ID,
observability_version: {
frameworkName: 'Cypress',
frameworkVersion: getPackageVersion('cypress', this.userConfigForReporting.browserstackConfigFile),
sdkVersion: getAgentVersion()
},
exception: {
error: exception.toString(),
stackTrace: stacktrace
},
config: this.userConfigForReporting
}
const options = {
auth: {
...this.credentialsForCrashReportUpload
},
headers: {
'Content-Type': 'application/json',
'X-BSTACK-TESTOPS': 'true'
},
method: 'POST',
url: `${API_URL}/api/v1/analytics`,
body: data,
json: true,
agent: httpsKeepAliveAgent
};
nodeRequestForLogs(`[Crash Report] ${util.format(exception)} ${util.format(stacktrace)}`).then();
request(options, function callback(error, response, body) {
if(error) {
debug(`[Crash_Report_Upload] Failed due to ${error}`);
} else if(response.statusCode != 200) {
debug(`[Crash_Report_Upload] Failed due to ${response && response.body ? response.body : `Received response from BrowserStack Server with status : ${response.statusCode}`}`);
} else {
debug(`[Crash_Report_Upload] Success response: ${JSON.stringify({status: response.status, body: response.body})}`)
}
});
} catch(e) {
debug(`[Crash_Report_Upload] Processing failed due to ${e && e.stack}`);
}
}
}
module.exports = CrashReporter;