Skip to content

Commit

Permalink
Clarify variable names in new action
Browse files Browse the repository at this point in the history
Also simplify some computations.
  • Loading branch information
aeisenberg committed Jun 15, 2022
1 parent bcb7fad commit 4918636
Showing 1 changed file with 12 additions and 21 deletions.
33 changes: 12 additions & 21 deletions .github/check-sarif/index.js
Original file line number Diff line number Diff line change
@@ -1,33 +1,24 @@
'use strict'

const core = require('@actions/core');
const core = require('@actions/core')
const fs = require('fs')

const sarif = JSON.parse(fs.readFileSync(core.getInput('sarif-file'), 'utf8'))
const rules = sarif.runs[0].tool.extensions.flatMap(ext => ext.rules || [])
const ruleIds = rules.map(rule => rule.id)

// Expected Queries
const expectedQueriesRun = getInput('queries-run')
const queriesThatShouldHaveRunButDidnt = expectedQueriesRun.reduce((acc, queryId) => {
if (!rules.some(rule => rule.id === queryId)) {
acc.push(queryId)
}
return acc
}, []);

if (queriesThatShouldHaveRunButDidnt.length > 0) {
core.setFailed(`The following queries were expected to run but did not: ${queriesThatShouldHaveRunButDidnt.join(', ')}`)
// Check that all the expected queries ran
const expectedQueriesRun = getQueryIdsInput('queries-run')
const queriesThatShouldHaveRunButDidNot = expectedQueriesRun.filter(queryId => !ruleIds.includes(queryId))

if (queriesThatShouldHaveRunButDidNot.length > 0) {
core.setFailed(`The following queries were expected to run but did not: ${queriesThatShouldHaveRunButDidNot.join(', ')}`)
}

// Unexpected Queries
const expectedQueriesNotRun = getInput('queries-not-run')
// Check that all the unexpected queries did not run
const expectedQueriesNotRun = getQueryIdsInput('queries-not-run')

const queriesThatShouldNotHaveRunButDid = expectedQueriesNotRun.reduce((acc, queryId) => {
if (rules.some(rule => rule.id === queryId)) {
acc.push(queryId)
}
return acc
}, []);
const queriesThatShouldNotHaveRunButDid = expectedQueriesNotRun.filter(queryId => ruleIds.includes(queryId))

if (queriesThatShouldNotHaveRunButDid.length > 0) {
core.setFailed(`The following queries were NOT expected to have run but did: ${queriesThatShouldNotHaveRunButDid.join(', ')}`)
Expand All @@ -44,7 +35,7 @@ core.startGroup('Full SARIF')
core.info(JSON.stringify(sarif, null, 2))
core.endGroup()

function getInput(name) {
function getQueryIdsInput(name) {
return core.getInput(name)
.split(',')
.map(q => q.trim())
Expand Down

0 comments on commit 4918636

Please sign in to comment.