Skip to content

Commit

Permalink
Merge pull request #834 from browserstack/SDK-667-limit-vcs-to-64kb-test
Browse files Browse the repository at this point in the history
SDK-667 : Limit VCS info to 64kb
  • Loading branch information
pranavj1001 authored May 30, 2024
2 parents 93ce05b + 798bf97 commit aad3e67
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 5 deletions.
12 changes: 11 additions & 1 deletion bin/helpers/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -441,6 +441,14 @@ const CYPRESS_CONFIG_FILE_NAMES = Object.keys(CYPRESS_CONFIG_FILE_MAPPING);

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

// Maximum size of VCS info which is allowed
const MAX_GIT_META_DATA_SIZE_IN_BYTES = 64 * 1024;

/* The value to be appended at the end if git metadata is larger than
MAX_GIT_META_DATA_SIZE_IN_BYTES
*/
const GIT_META_DATA_TRUNCATED = '...[TRUNCATED]';

const turboScaleObj = {};

module.exports = Object.freeze({
Expand Down Expand Up @@ -475,5 +483,7 @@ module.exports = Object.freeze({
CYPRESS_V10_AND_ABOVE_TYPE,
CYPRESS_CONFIG_FILE_MAPPING,
CYPRESS_CONFIG_FILE_NAMES,
CYPRESS_V10_AND_ABOVE_CONFIG_FILE_EXTENSIONS
CYPRESS_V10_AND_ABOVE_CONFIG_FILE_EXTENSIONS,
MAX_GIT_META_DATA_SIZE_IN_BYTES,
GIT_META_DATA_TRUNCATED
});
62 changes: 58 additions & 4 deletions bin/helpers/helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ const glob = require('glob');
const pGitconfig = promisify(gitconfig);
const { readCypressConfigFile } = require('./readCypressConfigUtil');
const CrashReporter = require('../testObservability/crashReporter');
const { MAX_GIT_META_DATA_SIZE_IN_BYTES, GIT_META_DATA_TRUNCATED } = require('./constants')

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

const { remote } = await pGitconfig(info.commonGitDir);
const remotes = Object.keys(remote).map(remoteName => ({name: remoteName, url: remote[remoteName]['url']}));
resolve({
let gitMetaData = {
"name": "git",
"sha": info["sha"],
"short_sha": info["abbreviatedSha"],
Expand All @@ -136,7 +137,11 @@ exports.getGitMetaData = () => {
"last_tag": info["lastTag"],
"commits_since_last_tag": info["commitsSinceLastTag"],
"remotes": remotes
});
};

gitMetaData = exports.checkAndTruncateVCSInfo(gitMetaData);

resolve(gitMetaData);
} catch(e) {
exports.debug(`Exception in populating Git Metadata with error : ${e}`, true, e);
logger.debug(`Exception in populating Git Metadata with error : ${e}`, true, e);
Expand All @@ -146,7 +151,7 @@ exports.getGitMetaData = () => {
} else {
const { remote } = await pGitconfig(info.commonGitDir);
const remotes = Object.keys(remote).map(remoteName => ({name: remoteName, url: remote[remoteName]['url']}));
resolve({
let gitMetaData = {
"name": "git",
"sha": info["sha"],
"short_sha": info["abbreviatedSha"],
Expand All @@ -163,7 +168,11 @@ exports.getGitMetaData = () => {
"last_tag": info["lastTag"],
"commits_since_last_tag": info["commitsSinceLastTag"],
"remotes": remotes
});
};

gitMetaData = exports.checkAndTruncateVCSInfo(gitMetaData);

resolve(gitMetaData);
}
} catch(err) {
exports.debug(`Exception in populating Git metadata with error : ${err}`, true, err);
Expand Down Expand Up @@ -387,3 +396,48 @@ exports.getSupportFiles = (bsConfig, isA11y) => {
cleanupParams: Object.keys(cleanupParams).length ? cleanupParams : null
};
}

exports.checkAndTruncateVCSInfo = (gitMetaData) => {
const gitMetaDataSizeInBytes = exports.getSizeOfJsonObjectInBytes(gitMetaData);

if (gitMetaDataSizeInBytes && gitMetaDataSizeInBytes > MAX_GIT_META_DATA_SIZE_IN_BYTES) {
const truncateSize = gitMetaDataSizeInBytes - MAX_GIT_META_DATA_SIZE_IN_BYTES;
gitMetaData.commit_message = exports.truncateString(gitMetaData.commit_message, truncateSize);
logger.info(`The commit has been truncated. Size of commit after truncation is ${ exports.getSizeOfJsonObjectInBytes(gitMetaData) / 1024} KB`);
}

return gitMetaData;
};

exports.getSizeOfJsonObjectInBytes = (jsonData) => {
try {
if (jsonData && jsonData instanceof Object) {
const buffer = Buffer.from(JSON.stringify(jsonData));

return buffer.length;
}
} catch (error) {
logger.debug(`Something went wrong while calculating size of JSON object: ${error}`);
}

return -1;
};

exports.truncateString = (field, truncateSizeInBytes) => {
try {
const bufferSizeInBytes = Buffer.from(GIT_META_DATA_TRUNCATED).length;

const fieldBufferObj = Buffer.from(field);
const lenOfFieldBufferObj = fieldBufferObj.length;
const finalLen = Math.ceil(lenOfFieldBufferObj - truncateSizeInBytes - bufferSizeInBytes);
if (finalLen > 0) {
const truncatedString = fieldBufferObj.subarray(0, finalLen).toString() + GIT_META_DATA_TRUNCATED;

return truncatedString;
}
} catch (error) {
logger.debug(`Error while truncating field, nothing was truncated here: ${error}`);
}

return field;
};

0 comments on commit aad3e67

Please sign in to comment.