diff --git a/api/source/service/AssetService.js b/api/source/service/AssetService.js index 0a7c0ac25..89e163efc 100644 --- a/api/source/service/AssetService.js +++ b/api/source/service/AssetService.js @@ -520,8 +520,8 @@ exports.queryChecklist = async function (inProjection, inPredicates, elevate, us } if (inPredicates.revisionStr !== 'latest') { joins.splice(0, 1, 'revision rev') - const results = dbUtils.extractRevisionDetails(inPredicates.revisionStr) - const revId = `${inPredicates.benchmarkId}-${results[0]}-${results[1]}` + const {version, release} = dbUtils.parseRevisionStr(inPredicates.revisionStr) + const revId = `${inPredicates.benchmarkId}-${version}-${release}` predicates.statements.push('rev.revId = :revId') predicates.binds.revId = revId } @@ -776,8 +776,8 @@ exports.cklFromAssetStigs = async function cklFromAssetStigs (assetId, stigs, el revisionStrResolved = `V${resultGetBenchmarkId[0].version}R${resultGetBenchmarkId[0].release}` } else { - let revParse = dbUtils.extractRevisionDetails(revisionStr) - revId = `${benchmarkId}-${revParse[0]}-${revParse[1]}` + const {version, release} = dbUtils.parseRevisionStr(revisionStr) + revId = `${benchmarkId}-${version}-${release}` ;[resultGetBenchmarkId] = await connection.execute(sqlGetBenchmarkId, [revId]) } @@ -1021,8 +1021,8 @@ exports.cklbFromAssetStigs = async function cklbFromAssetStigs (assetId, stigs) revisionStrResolved = `V${resultGetBenchmarkId[0].version}R${resultGetBenchmarkId[0].release}` } else { - let revParse = dbUtils.extractRevisionDetails(revisionStr) - revId = `${benchmarkId}-${revParse[0]}-${revParse[1]}` + const {version, release} = dbUtils.parseRevisionStr(revisionStr) + revId = `${benchmarkId}-${version}-${release}` ;[resultGetBenchmarkId] = await connection.execute(sqlGetBenchmarkId, [revId]) } @@ -1181,8 +1181,8 @@ exports.xccdfFromAssetStig = async function (assetId, benchmarkId, revisionStr = revisionStrResolved = `V${result[0].version}R${result[0].release}` } else { - let revParse = dbUtils.extractRevisionDetails(revisionStr) - revId = `${benchmarkId}-${revParse[0]}-${revParse[1]}` + const {version, release} = dbUtils.parseRevisionStr(revisionStr) + revId = `${benchmarkId}-${version}-${release}` ;[result] = await connection.query(sqlGetRevision, [revId]) revisionStrResolved = revisionStr } diff --git a/api/source/service/CollectionService.js b/api/source/service/CollectionService.js index 56dcdda26..0017e2d2d 100644 --- a/api/source/service/CollectionService.js +++ b/api/source/service/CollectionService.js @@ -756,11 +756,11 @@ exports.getChecklistByCollectionStig = async function (collectionId, benchmarkId // Non-current revision if (revisionStr !== 'latest') { joins.splice(2, 1, 'left join revision rev on sa.benchmarkId=rev.benchmarkId') - const results = dbUtils.extractRevisionDetails(revisionStr) + const {version, release} = dbUtils.parseRevisionStr(revisionStr) predicates.statements.push('rev.version = :version') predicates.statements.push('rev.release = :release') - predicates.binds.version = results[0] - predicates.binds.release = results[1] + predicates.binds.version = version + predicates.binds.release = release } // Access control @@ -1670,9 +1670,7 @@ exports.writeStigPropsByCollectionStig = async function ({collectionId, benchmar let version, release if (defaultRevisionStr) { if (defaultRevisionStr !== 'latest') { - const revisionParts = dbUtils.extractRevisionDetails(defaultRevisionStr) - version = revisionParts[0] - release = revisionParts[1] + ;({version, release} = dbUtils.parseRevisionStr(defaultRevisionStr)) } } connection = await dbUtils.pool.getConnection() diff --git a/api/source/service/OperationService.js b/api/source/service/OperationService.js index 8412c7172..63cdac207 100644 --- a/api/source/service/OperationService.js +++ b/api/source/service/OperationService.js @@ -338,7 +338,7 @@ exports.replaceAppData = async function (importOpts, appData, userObject, res ) } for (const pin of c.stigs ?? []) { if (pin.revisionPinned){ - let [version, release] = dbUtils.extractRevisionDetails(pin.revisionStr) + const {version, release} = dbUtils.parseRevisionStr(pin.revisionStr) dml.collectionPins.insertBinds.push([ parseInt(c.collectionId), pin.benchmarkId, diff --git a/api/source/service/STIGService.js b/api/source/service/STIGService.js index 53940e484..9140c93a8 100644 --- a/api/source/service/STIGService.js +++ b/api/source/service/STIGService.js @@ -148,7 +148,7 @@ exports.queryGroups = async function ( inProjection, inPredicates ) { if (inPredicates.revisionStr != 'latest') { joins = ['revision r'] - let [version, release] = dbUtils.extractRevisionDetails(inPredicates.revisionStr) + const {version, release} = dbUtils.parseRevisionStr(inPredicates.revisionStr) predicates.statements.push('r.version = ?') predicates.binds.push(version) predicates.statements.push('r.release = ?') @@ -232,7 +232,7 @@ exports.queryBenchmarkRules = async function ( benchmarkId, revisionStr, inProje if (revisionStr != 'latest') { joins = ['revision rev'] - let [version, release] = dbUtils.extractRevisionDetails(revisionStr) + const {version, release} = dbUtils.parseRevisionStr(revisionStr) predicates.statements.push('rev.version = ?') predicates.binds.push(version) predicates.statements.push('rev.release = ?') @@ -907,7 +907,7 @@ exports.deleteRevisionByString = async function(benchmarkId, revisionStr, svcSta let connection; try { - let [version, release] = dbUtils.extractRevisionDetails(revisionStr) + const {version, release} = dbUtils.parseRevisionStr(revisionStr) let binds = { benchmarkId: benchmarkId, version: version, @@ -1161,7 +1161,7 @@ exports.getCcisByRevision = async function(benchmarkId, revisionStr, userObject) if (revisionStr != 'latest') { joins = ['revision r'] - let [version, release] = dbUtils.extractRevisionDetails(revisionStr) + const {version, release} = dbUtils.parseRevisionStr(revisionStr) predicates.statements.push('r.version = ?') predicates.binds.push(version) predicates.statements.push('r.release = ?') diff --git a/api/source/service/utils.js b/api/source/service/utils.js index 4bd021c41..9dd1c61d9 100644 --- a/api/source/service/utils.js +++ b/api/source/service/utils.js @@ -141,11 +141,11 @@ module.exports.initializeDatabase = async function () { } module.exports.parseRevisionStr = function (revisionStr) { - let ro = {} + const ro = {} if (revisionStr !== 'latest') { - let results = _this.extractRevisionDetails(revisionStr) - ro.version = results[0] - ro.release = results[1] + const results = /V(\d+)R(\d+(\.\d+)?)/.exec(revisionStr) + ro.version = results[1] + ro.release = results[2] ro.table = 'revision' ro.table_alias = 'r' ro.predicates = ' and r.version = ? and r.release = ? ' @@ -507,14 +507,3 @@ module.exports.updateDefaultRev = async function (connection, {collectionId, col await (connection ?? _this.pool).query(sqlInsert, binds) } - -module.exports.extractRevisionDetails = function (revisionStr) { - const regex = /V(\d+)R(\d+(\.\d+)?)/; - const [results, version, release] = regex.exec(revisionStr); - - if (!results) { - throw new Error(`Invalid revision string: ${revisionStr}`); - } - - return [version, release]; -}