diff --git a/.github/actions/trivy-triage/helpers.js b/.github/actions/trivy-triage/helpers.js index 3b477dfad5..cbd99c1aac 100644 --- a/.github/actions/trivy-triage/helpers.js +++ b/.github/actions/trivy-triage/helpers.js @@ -1,6 +1,11 @@ +const patterns = { + Scanner: /### Scanner\r?\n\r?\n(.+)/, + Target: /### Target\r?\n\r?\n(.+)/, +}; + module.exports = { detectDiscussionLabels: (discussion, configDiscussionLabels) => { - res = []; + const res = []; const discussionId = discussion.id; const category = discussion.category.name; const body = discussion.body; @@ -8,15 +13,21 @@ module.exports = { console.log(`skipping discussion with category ${category} and body ${body}`); return []; } - const scannerPattern = /### Scanner\n\n(.+)/; - const scannerFound = body.match(scannerPattern); - if (scannerFound && scannerFound.length > 1) { - res.push(configDiscussionLabels[scannerFound[1]]); - } - const targetPattern = /### Target\n\n(.+)/; - const targetFound = body.match(targetPattern); - if (targetFound && targetFound.length > 1) { - res.push(configDiscussionLabels[targetFound[1]]); + + for (const key in patterns) { + const match = body.match(patterns[key]); + if (match && match.length > 1 && match[1] !== "None") { + const val = configDiscussionLabels[match[1]]; + if (val === undefined && match[1]) { + console.warn( + `Value for ${key.toLowerCase()} key "${ + match[1] + }" not found in configDiscussionLabels` + ); + } else { + res.push(val); + } + } } return res; }, diff --git a/.github/actions/trivy-triage/helpers.test.js b/.github/actions/trivy-triage/helpers.test.js index 7db708bcfd..5dd5565845 100644 --- a/.github/actions/trivy-triage/helpers.test.js +++ b/.github/actions/trivy-triage/helpers.test.js @@ -62,6 +62,17 @@ describe('trivy-triage', async function() { assert(labels.includes('ContainerImageLabel')); assert(labels.includes('VulnerabilityLabel')); }); + it('detect scanner and target labels on windows', async function() { + const discussion = { + body: 'hello hello\r\nbla bla.\r\n### Scanner\r\n\r\nVulnerability\r\n### Target\r\n\r\nContainer Image\r\nbye bye.', + category: { + name: 'Ideas' + } + }; + const labels = detectDiscussionLabels(discussion, configDiscussionLabels); + assert(labels.includes('ContainerImageLabel')); + assert(labels.includes('VulnerabilityLabel')); + }); it('not detect other labels', async function() { const discussion = { body: 'hello hello\nbla bla.\n### Scanner\n\nVulnerability\n### Target\n\nContainer Image\nbye bye.', @@ -73,6 +84,16 @@ describe('trivy-triage', async function() { assert(!labels.includes('FilesystemLabel')); assert(!labels.includes('MisconfigurationLabel')); }); + it('ignores unmatched label values from body', async function() { + const discussion = { + body: '### Target\r\n\r\nNone\r\n\r\n### Scanner\r\n\r\nMisconfiguration', + category: { + name: 'Ideas' + } + }; + const labels = detectDiscussionLabels(discussion, configDiscussionLabels); + assert.deepStrictEqual(labels, ['MisconfigurationLabel']); + }); it('process only relevant categories', async function() { const discussion = { body: 'hello world',