Skip to content

Commit aad3e67

Browse files
authored
Merge pull request #834 from browserstack/SDK-667-limit-vcs-to-64kb-test
SDK-667 : Limit VCS info to 64kb
2 parents 93ce05b + 798bf97 commit aad3e67

File tree

2 files changed

+69
-5
lines changed

2 files changed

+69
-5
lines changed

bin/helpers/constants.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -441,6 +441,14 @@ const CYPRESS_CONFIG_FILE_NAMES = Object.keys(CYPRESS_CONFIG_FILE_MAPPING);
441441

442442
const CYPRESS_V10_AND_ABOVE_CONFIG_FILE_EXTENSIONS = ['js', 'ts', 'cjs', 'mjs']
443443

444+
// Maximum size of VCS info which is allowed
445+
const MAX_GIT_META_DATA_SIZE_IN_BYTES = 64 * 1024;
446+
447+
/* The value to be appended at the end if git metadata is larger than
448+
MAX_GIT_META_DATA_SIZE_IN_BYTES
449+
*/
450+
const GIT_META_DATA_TRUNCATED = '...[TRUNCATED]';
451+
444452
const turboScaleObj = {};
445453

446454
module.exports = Object.freeze({
@@ -475,5 +483,7 @@ module.exports = Object.freeze({
475483
CYPRESS_V10_AND_ABOVE_TYPE,
476484
CYPRESS_CONFIG_FILE_MAPPING,
477485
CYPRESS_CONFIG_FILE_NAMES,
478-
CYPRESS_V10_AND_ABOVE_CONFIG_FILE_EXTENSIONS
486+
CYPRESS_V10_AND_ABOVE_CONFIG_FILE_EXTENSIONS,
487+
MAX_GIT_META_DATA_SIZE_IN_BYTES,
488+
GIT_META_DATA_TRUNCATED
479489
});

bin/helpers/helper.js

Lines changed: 58 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ const glob = require('glob');
1818
const pGitconfig = promisify(gitconfig);
1919
const { readCypressConfigFile } = require('./readCypressConfigUtil');
2020
const CrashReporter = require('../testObservability/crashReporter');
21+
const { MAX_GIT_META_DATA_SIZE_IN_BYTES, GIT_META_DATA_TRUNCATED } = require('./constants')
2122

2223
exports.debug = (text, shouldReport = false, throwable = null) => {
2324
if (process.env.BROWSERSTACK_OBSERVABILITY_DEBUG === "true" || process.env.BROWSERSTACK_OBSERVABILITY_DEBUG === "1") {
@@ -119,7 +120,7 @@ exports.getGitMetaData = () => {
119120

120121
const { remote } = await pGitconfig(info.commonGitDir);
121122
const remotes = Object.keys(remote).map(remoteName => ({name: remoteName, url: remote[remoteName]['url']}));
122-
resolve({
123+
let gitMetaData = {
123124
"name": "git",
124125
"sha": info["sha"],
125126
"short_sha": info["abbreviatedSha"],
@@ -136,7 +137,11 @@ exports.getGitMetaData = () => {
136137
"last_tag": info["lastTag"],
137138
"commits_since_last_tag": info["commitsSinceLastTag"],
138139
"remotes": remotes
139-
});
140+
};
141+
142+
gitMetaData = exports.checkAndTruncateVCSInfo(gitMetaData);
143+
144+
resolve(gitMetaData);
140145
} catch(e) {
141146
exports.debug(`Exception in populating Git Metadata with error : ${e}`, true, e);
142147
logger.debug(`Exception in populating Git Metadata with error : ${e}`, true, e);
@@ -146,7 +151,7 @@ exports.getGitMetaData = () => {
146151
} else {
147152
const { remote } = await pGitconfig(info.commonGitDir);
148153
const remotes = Object.keys(remote).map(remoteName => ({name: remoteName, url: remote[remoteName]['url']}));
149-
resolve({
154+
let gitMetaData = {
150155
"name": "git",
151156
"sha": info["sha"],
152157
"short_sha": info["abbreviatedSha"],
@@ -163,7 +168,11 @@ exports.getGitMetaData = () => {
163168
"last_tag": info["lastTag"],
164169
"commits_since_last_tag": info["commitsSinceLastTag"],
165170
"remotes": remotes
166-
});
171+
};
172+
173+
gitMetaData = exports.checkAndTruncateVCSInfo(gitMetaData);
174+
175+
resolve(gitMetaData);
167176
}
168177
} catch(err) {
169178
exports.debug(`Exception in populating Git metadata with error : ${err}`, true, err);
@@ -387,3 +396,48 @@ exports.getSupportFiles = (bsConfig, isA11y) => {
387396
cleanupParams: Object.keys(cleanupParams).length ? cleanupParams : null
388397
};
389398
}
399+
400+
exports.checkAndTruncateVCSInfo = (gitMetaData) => {
401+
const gitMetaDataSizeInBytes = exports.getSizeOfJsonObjectInBytes(gitMetaData);
402+
403+
if (gitMetaDataSizeInBytes && gitMetaDataSizeInBytes > MAX_GIT_META_DATA_SIZE_IN_BYTES) {
404+
const truncateSize = gitMetaDataSizeInBytes - MAX_GIT_META_DATA_SIZE_IN_BYTES;
405+
gitMetaData.commit_message = exports.truncateString(gitMetaData.commit_message, truncateSize);
406+
logger.info(`The commit has been truncated. Size of commit after truncation is ${ exports.getSizeOfJsonObjectInBytes(gitMetaData) / 1024} KB`);
407+
}
408+
409+
return gitMetaData;
410+
};
411+
412+
exports.getSizeOfJsonObjectInBytes = (jsonData) => {
413+
try {
414+
if (jsonData && jsonData instanceof Object) {
415+
const buffer = Buffer.from(JSON.stringify(jsonData));
416+
417+
return buffer.length;
418+
}
419+
} catch (error) {
420+
logger.debug(`Something went wrong while calculating size of JSON object: ${error}`);
421+
}
422+
423+
return -1;
424+
};
425+
426+
exports.truncateString = (field, truncateSizeInBytes) => {
427+
try {
428+
const bufferSizeInBytes = Buffer.from(GIT_META_DATA_TRUNCATED).length;
429+
430+
const fieldBufferObj = Buffer.from(field);
431+
const lenOfFieldBufferObj = fieldBufferObj.length;
432+
const finalLen = Math.ceil(lenOfFieldBufferObj - truncateSizeInBytes - bufferSizeInBytes);
433+
if (finalLen > 0) {
434+
const truncatedString = fieldBufferObj.subarray(0, finalLen).toString() + GIT_META_DATA_TRUNCATED;
435+
436+
return truncatedString;
437+
}
438+
} catch (error) {
439+
logger.debug(`Error while truncating field, nothing was truncated here: ${error}`);
440+
}
441+
442+
return field;
443+
};

0 commit comments

Comments
 (0)