Skip to content

Commit 350a149

Browse files
authored
feat: add run-one-check workflow for the new CLI (#251)
1 parent ce48f26 commit 350a149

File tree

6 files changed

+105
-7
lines changed

6 files changed

+105
-7
lines changed

__tests__/cli/__snapshots__/workflows.test.js.snap

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,48 @@ exports[`list - Non-Interactive Mode Should provide a list of available workflow
2626
"operations": null,
2727
"workflow": [Function],
2828
},
29+
{
30+
"description": "Run a specific compliance check for the stored data.",
31+
"isEnabled": true,
32+
"isRequiredAdditionalData": true,
33+
"name": "run-one-check",
34+
"operations": null,
35+
"schema": {
36+
"$schema": "http://json-schema.org/draft-07/schema#",
37+
"additionalProperties": false,
38+
"properties": {
39+
"check_name": {
40+
"description": "The name of the check to run",
41+
"type": "string",
42+
},
43+
},
44+
"required": [
45+
"check_name",
46+
],
47+
"type": "object",
48+
},
49+
"workflow": [Function],
50+
},
2951
{
3052
"description": "Upsert the OSSF Scorecard scoring by running and checking every repository in the database.",
3153
"isEnabled": false,
3254
"isRequiredAdditionalData": false,
3355
"name": "upsert-ossf-scorecard",
3456
"operations": null,
57+
"schema": {
58+
"$schema": "http://json-schema.org/draft-07/schema#",
59+
"additionalProperties": false,
60+
"properties": {
61+
"project_id": {
62+
"description": "The id of the project to run the check on",
63+
"type": "integer",
64+
},
65+
},
66+
"required": [
67+
"check_name",
68+
],
69+
"type": "object",
70+
},
3571
"workflow": [Function],
3672
},
3773
{

src/cli/workflows.js

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
const inquirer = require('inquirer').default
22
const _ = require('lodash')
33
const debug = require('debug')('cli:workflows')
4-
const { updateGithubOrgs, upsertGithubRepositories, runAllTheComplianceChecks, upsertOSSFScorecardAnalysis } = require('../workflows')
4+
const { updateGithubOrgs, upsertGithubRepositories, runAllTheComplianceChecks, runOneComplianceCheck, upsertOSSFScorecardAnalysis } = require('../workflows')
55
const { generateStaticReports } = require('../reports')
66
const { bulkImport } = require('../importers')
77
const { logger } = require('../utils')
8-
const bulkImportSchema = require('../schemas/bulkImport.json')
8+
const { executeOneCheckSchema, executeOptionalProjectSchema, bulkImportSchema } = require('../schemas')
99

1010
const commandList = [{
1111
name: 'update-github-orgs',
@@ -28,9 +28,18 @@ const commandList = [{
2828
description: 'Run all the compliance checks for the stored data.',
2929
operations: null,
3030
workflow: runAllTheComplianceChecks
31+
}, {
32+
name: 'run-one-check',
33+
isRequiredAdditionalData: true,
34+
isEnabled: true,
35+
description: 'Run a specific compliance check for the stored data.',
36+
operations: null,
37+
schema: executeOneCheckSchema,
38+
workflow: runOneComplianceCheck
3139
}, {
3240
name: 'upsert-ossf-scorecard',
3341
isRequiredAdditionalData: false,
42+
schema: executeOptionalProjectSchema,
3443
isEnabled: false,
3544
description: 'Upsert the OSSF Scorecard scoring by running and checking every repository in the database.',
3645
operations: null,
@@ -64,13 +73,14 @@ const getWorkflowsDetails = () => {
6473

6574
commandList.forEach((workflow) => {
6675
const workflowName = _.kebabCase(workflow.name)
67-
workflowsList.push({ id: workflowName, description: workflow.description, isEnabled: workflow.isEnabled, isRequiredAdditionalData: workflow.isRequiredAdditionalData, operations: workflow.operations })
76+
workflowsList.push({ id: workflowName, description: workflow.description, isEnabled: workflow.isEnabled, isRequiredAdditionalData: workflow.isRequiredAdditionalData, operations: workflow.operations, schema: JSON.stringify(workflow.schema) })
6877
workflows[workflowName] = {
6978
description: workflow.description,
7079
workflow: workflow.workflow,
7180
isEnabled: workflow.isEnabled,
7281
isRequiredAdditionalData: workflow.isRequiredAdditionalData,
73-
operations: workflow.operations
82+
operations: workflow.operations,
83+
schema: JSON.stringify(workflow.schema)
7484
}
7585
})
7686

src/schemas/execute-one-check.json

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"$schema": "http://json-schema.org/draft-07/schema#",
3+
"type": "object",
4+
"additionalProperties": false,
5+
"properties": {
6+
"check_name": {
7+
"type": "string",
8+
"description": "The name of the check to run"
9+
}
10+
},
11+
"required": ["check_name"]
12+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"$schema": "http://json-schema.org/draft-07/schema#",
3+
"type": "object",
4+
"additionalProperties": false,
5+
"properties": {
6+
"project_id": {
7+
"type": "integer",
8+
"description": "The id of the project to run the check on"
9+
}
10+
},
11+
"required": ["check_name"]
12+
}

src/schemas/index.js

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ const ossfScorecardResultSchema = require('./ossfScorecardResult.json')
77
const bulkImportSchema = require('./bulkImport.json')
88
const projectDataSchema = require('./projectData.json')
99
const indexDataSchema = require('./indexData.json')
10+
const executeOneCheckSchema = require('./execute-one-check.json')
11+
const executeOptionalProjectSchema = require('./execute-optional-project.json')
1012

1113
const ajv = new Ajv({
1214
allowUnionTypes: true // Allow union types for fields like Date/string
@@ -25,6 +27,16 @@ const validateGithubOrg = (data) => {
2527
return null
2628
}
2729

30+
const validateExecuteOneCheck = (data) => {
31+
const validate = ajv.compile(executeOneCheckSchema)
32+
const valid = validate(data)
33+
if (!valid) {
34+
const readableErrors = getReadableErrors(validate)
35+
throw new Error(`Error when validating the execute one check request: ${readableErrors}`)
36+
}
37+
return null
38+
}
39+
2840
const validateGithubListOrgRepos = (data) => {
2941
const validate = ajv.compile(githubListOrgReposSchema)
3042
const valid = validate(data)
@@ -92,5 +104,9 @@ module.exports = {
92104
validateOSSFResult,
93105
validateBulkImport,
94106
validateProjectData,
95-
validateIndexData
107+
validateIndexData,
108+
validateExecuteOneCheck,
109+
executeOneCheckSchema,
110+
executeOptionalProjectSchema,
111+
bulkImportSchema
96112
}

src/workflows/index.js

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ const debug = require('debug')('workflows')
22
const { github, ossf } = require('../providers')
33
const { initializeStore } = require('../store')
44
const { logger } = require('../utils')
5-
const { validateGithubOrg, validateGithubListOrgRepos, validateGithubRepository, validateOSSFResult } = require('../schemas')
5+
const { validateGithubOrg, validateGithubListOrgRepos, validateGithubRepository, validateOSSFResult, validateExecuteOneCheck } = require('../schemas')
66
const checks = require('../checks')
77
const { chunkArray } = require('@ulisesgascon/array-to-chunks')
88
const { ossfScorecardSettings } = require('../config').getConfig()
@@ -109,9 +109,21 @@ const upsertOSSFScorecardAnalysis = async (knex) => {
109109
logger.info('The OSSF Scorecard ran successfully')
110110
}
111111

112+
const runOneComplianceCheck = async (knex, data) => {
113+
validateExecuteOneCheck(data)
114+
const checkName = data.check_name
115+
const check = checks[checkName]
116+
if (!check) {
117+
throw new Error('Check not found')
118+
}
119+
await check(knex)
120+
logger.info(`${checkName} check completed successfully`)
121+
}
122+
112123
module.exports = {
113124
updateGithubOrgs,
114125
upsertGithubRepositories,
115126
runAllTheComplianceChecks,
116-
upsertOSSFScorecardAnalysis
127+
upsertOSSFScorecardAnalysis,
128+
runOneComplianceCheck
117129
}

0 commit comments

Comments
 (0)