Skip to content

Commit

Permalink
call existing revision string parsing function
Browse files Browse the repository at this point in the history
  • Loading branch information
csmig committed Jan 30, 2024
1 parent f1a6eeb commit 7a29b59
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 34 deletions.
16 changes: 8 additions & 8 deletions api/source/service/AssetService.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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])
}

Expand Down Expand Up @@ -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])
}

Expand Down Expand Up @@ -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
}
Expand Down
10 changes: 4 additions & 6 deletions api/source/service/CollectionService.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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()
Expand Down
2 changes: 1 addition & 1 deletion api/source/service/OperationService.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
8 changes: 4 additions & 4 deletions api/source/service/STIGService.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = ?')
Expand Down Expand Up @@ -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 = ?')
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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 = ?')
Expand Down
19 changes: 4 additions & 15 deletions api/source/service/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = ? '
Expand Down Expand Up @@ -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];
}

0 comments on commit 7a29b59

Please sign in to comment.