diff --git a/lib/upload-lib.js b/lib/upload-lib.js index 7896d419e..f28f085a4 100644 --- a/lib/upload-lib.js +++ b/lib/upload-lib.js @@ -100,6 +100,7 @@ async function uploadFiles(sarifFiles) { if (matrix === "null" || matrix === "") { matrix = undefined; } + const toolNames = util.getToolNames(sarifPayload); const payload = JSON.stringify({ "commit_oid": commitOid, "ref": ref, @@ -108,7 +109,8 @@ async function uploadFiles(sarifFiles) { "workflow_run_id": workflowRunID, "checkout_uri": checkoutURI, "environment": matrix, - "started_at": startedAt + "started_at": startedAt, + "tool_names": toolNames, }); core.info('Uploading results'); const githubToken = core.getInput('token'); diff --git a/lib/util.js b/lib/util.js index 0612c1268..d12a91044 100644 --- a/lib/util.js +++ b/lib/util.js @@ -262,3 +262,21 @@ async function reportActionSucceeded(action) { await sendStatusReport(await createStatusReport(action, 'success')); } exports.reportActionSucceeded = reportActionSucceeded; +/** + * Get the array of all the tool names contained in the given sarif contents. + * + * Returns an array of unique string tool names. + */ +function getToolNames(sarifContents) { + const sarif = JSON.parse(sarifContents); + const toolNames = {}; + for (const run of sarif.runs || []) { + const tool = run.tool || {}; + const driver = tool.driver || {}; + if (typeof driver.name === "string" && driver.name.length > 0) { + toolNames[driver.name] = true; + } + } + return Object.keys(toolNames); +} +exports.getToolNames = getToolNames; diff --git a/src/testdata/tool-names.sarif b/src/testdata/tool-names.sarif new file mode 100644 index 000000000..ee6cd8cd7 --- /dev/null +++ b/src/testdata/tool-names.sarif @@ -0,0 +1,41 @@ +{ + "$schema": "https://raw.githubusercontent.com/oasis-tcs/sarif-spec/master/Schemata/sarif-schema-2.1.0.json", + "version": "2.1.0", + "runs": [ + { + "tool": { + "driver": { + "name": "CodeQL command-line toolchain" + } + } + }, + { + "tool": { + "driver": { + "name": "CodeQL command-line toolchain" + } + } + }, + { + "tool": { + "driver": { + "name": "ESLint" + } + } + }, + { + "tool": { + "driver": { + "name": "" + } + } + }, + { + "tool": { + "driver": { + "name": null + } + } + } + ] +} diff --git a/src/upload-lib.ts b/src/upload-lib.ts index 1d35b0b2a..8c6a31e4e 100644 --- a/src/upload-lib.ts +++ b/src/upload-lib.ts @@ -98,6 +98,8 @@ async function uploadFiles(sarifFiles: string[]) { matrix = undefined; } + const toolNames = util.getToolNames(sarifPayload); + const payload = JSON.stringify({ "commit_oid": commitOid, "ref": ref, @@ -106,7 +108,8 @@ async function uploadFiles(sarifFiles: string[]) { "workflow_run_id": workflowRunID, "checkout_uri": checkoutURI, "environment": matrix, - "started_at": startedAt + "started_at": startedAt, + "tool_names": toolNames, }); core.info('Uploading results'); diff --git a/src/util.ts b/src/util.ts index 7bb3ec0a5..cfdd2419c 100644 --- a/src/util.ts +++ b/src/util.ts @@ -293,3 +293,23 @@ export async function reportActionFailed(action: string, cause?: string, excepti export async function reportActionSucceeded(action: string) { await sendStatusReport(await createStatusReport(action, 'success')); } + +/** + * Get the array of all the tool names contained in the given sarif contents. + * + * Returns an array of unique string tool names. + */ +export function getToolNames(sarifContents: string): string[] { + const sarif = JSON.parse(sarifContents); + const toolNames = {}; + + for (const run of sarif.runs || []) { + const tool = run.tool || {}; + const driver = tool.driver || {}; + if (typeof driver.name === "string" && driver.name.length > 0) { + toolNames[driver.name] = true; + } + } + + return Object.keys(toolNames); +}