Skip to content

Commit 01de260

Browse files
committed
Limit VCS info to 64kb
1 parent 93ce05b commit 01de260

File tree

2 files changed

+73
-5
lines changed

2 files changed

+73
-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_KB = 64;
446+
447+
/* The value to be appended at the end if git metadata is larger than
448+
MAX_GIT_META_DATA_SIZE_IN_KB
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_KB,
488+
GIT_META_DATA_TRUNCATED
479489
});

bin/helpers/helper.js

Lines changed: 62 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_KB, 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 = this.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 = this.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,52 @@ exports.getSupportFiles = (bsConfig, isA11y) => {
387396
cleanupParams: Object.keys(cleanupParams).length ? cleanupParams : null
388397
};
389398
}
399+
400+
exports.checkAndTruncateVCSInfo = (gitMetaData) => {
401+
const gitMetaDataSizeInKb = this.getSizeOfJsonObjectInKb(gitMetaData);
402+
403+
if (gitMetaDataSizeInKb && gitMetaDataSizeInKb > 0 && gitMetaDataSizeInKb > MAX_GIT_META_DATA_SIZE_IN_KB) {
404+
const truncateSize = gitMetaDataSizeInKb - MAX_GIT_META_DATA_SIZE_IN_KB;
405+
const truncatedCommitMessage = this.truncateString(gitMetaData.commit_message, truncateSize);
406+
gitMetaData.commit_message = truncatedCommitMessage;
407+
exports.debug('The commit has been truncated');
408+
logger.debug('The commit has been truncated');
409+
}
410+
411+
return gitMetaData;
412+
};
413+
414+
exports.getSizeOfJsonObjectInKb = (jsonData) => {
415+
try {
416+
if (jsonData) {
417+
const buffer = Buffer.from(JSON.stringify(jsonData));
418+
419+
return Math.floor(buffer.length/1024);
420+
}
421+
} catch (error) {
422+
exports.debug(`Something went wrong while calculating size of JSON object: ${error}`, true, error);
423+
logger.debug(`Something went wrong while calculating size of JSON object: ${error}`, true, error);
424+
}
425+
426+
return -1;
427+
};
428+
429+
exports.truncateString = (field, truncateSizeInKb) => {
430+
try {
431+
const bufferSizeInBytes = Buffer.from(GIT_META_DATA_TRUNCATED).length;
432+
433+
const fieldBufferObj = Buffer.from(field);
434+
const lenOfFieldBufferObj = fieldBufferObj.length;
435+
const finalLen = Math.round(lenOfFieldBufferObj - (truncateSizeInKb * 1024) - (bufferSizeInBytes));
436+
if (finalLen > 0) {
437+
const truncatedString = fieldBufferObj.subarray(0, finalLen).toString() + GIT_META_DATA_TRUNCATED;
438+
439+
return truncatedString;
440+
}
441+
} catch (error) {
442+
exports.debug(`Error while truncating field, nothing was truncated here: ${error}`, true, error);
443+
logger.debug(`Error while truncating field, nothing was truncated here: ${error}`, true, error);
444+
}
445+
446+
return field;
447+
};

0 commit comments

Comments
 (0)