From ef54276c132bee4d58736750f490972dad51da41 Mon Sep 17 00:00:00 2001 From: KoolTheba Date: Fri, 24 Feb 2023 19:21:16 +0100 Subject: [PATCH 1/2] feat: hidden template title and subtitle if reportTags flag is enabled - Relates #31 --- dist/index.js | 10 +++++----- dist/report.ejs | 2 ++ src/action.js | 2 +- src/index.js | 4 ++-- src/utils.js | 4 ++-- templates/report.ejs | 2 ++ 6 files changed, 14 insertions(+), 10 deletions(-) diff --git a/dist/index.js b/dist/index.js index 8cf55f7..364dccb 100644 --- a/dist/index.js +++ b/dist/index.js @@ -27711,7 +27711,7 @@ const generateScope = async ({ octokit, orgs, scope, maxRequestInParallel }) => return newScope } -const generateScores = async ({ scope, database: currentDatabase, maxRequestInParallel }) => { +const generateScores = async ({ scope, database: currentDatabase, maxRequestInParallel, reportTagsEnabled }) => { // @TODO: Improve deep clone logic const database = JSON.parse(JSON.stringify(currentDatabase)) const platform = 'github.com' @@ -27770,7 +27770,7 @@ const generateScores = async ({ scope, database: currentDatabase, maxRequestInPa core.debug('All the scores are already collected') - const reportContent = await generateReportContent(scores) + const reportContent = await generateReportContent(scores, reportTagsEnabled) const issueContent = await generateIssueContent(scores) // SET OUTPUTS @@ -27834,10 +27834,10 @@ const saveScore = ({ database, platform, org, repo, score, date, commit }) => { repoRef.current = { score, date, commit } } -const generateReportContent = async (scores) => { +const generateReportContent = async (scores, reportTagsEnabled) => { core.debug('Generating report content') const template = await readFile(__nccwpck_require__.ab + "report.ejs", 'utf8') - return ejs.render(template, { scores }) + return ejs.render(template, { scores, reportTagsEnabled }) } const generateIssueContent = async (scores) => { @@ -28219,7 +28219,7 @@ async function run () { // PROCESS core.info('Generating scores...') - const { reportContent, issueContent, database: newDatabaseState } = await generateScores({ scope, database, maxRequestInParallel }) + const { reportContent, issueContent, database: newDatabaseState } = await generateScores({ scope, database, maxRequestInParallel, reportTagsEnabled }) core.info('Checking database changes...') const hasChanges = isDifferent(database, newDatabaseState) diff --git a/dist/report.ejs b/dist/report.ejs index 1f6c1a3..dc95c60 100644 --- a/dist/report.ejs +++ b/dist/report.ejs @@ -1,6 +1,8 @@ +<%_ if (!reportTagsEnabled) { -%> # OpenSSF Scorecard Report ## Summary +<%_ } -%> <%_ if (scores.length) { -%> | Repository | Commit | Score | Date | Difference | Report Link | diff --git a/src/action.js b/src/action.js index 6d674cb..b50635b 100644 --- a/src/action.js +++ b/src/action.js @@ -89,7 +89,7 @@ async function run () { // PROCESS core.info('Generating scores...') - const { reportContent, issueContent, database: newDatabaseState } = await generateScores({ scope, database, maxRequestInParallel }) + const { reportContent, issueContent, database: newDatabaseState } = await generateScores({ scope, database, maxRequestInParallel, reportTagsEnabled }) core.info('Checking database changes...') const hasChanges = isDifferent(database, newDatabaseState) diff --git a/src/index.js b/src/index.js index 20f2db0..863c646 100644 --- a/src/index.js +++ b/src/index.js @@ -98,7 +98,7 @@ const generateScope = async ({ octokit, orgs, scope, maxRequestInParallel }) => return newScope } -const generateScores = async ({ scope, database: currentDatabase, maxRequestInParallel }) => { +const generateScores = async ({ scope, database: currentDatabase, maxRequestInParallel, reportTagsEnabled }) => { // @TODO: Improve deep clone logic const database = JSON.parse(JSON.stringify(currentDatabase)) const platform = 'github.com' @@ -157,7 +157,7 @@ const generateScores = async ({ scope, database: currentDatabase, maxRequestInPa core.debug('All the scores are already collected') - const reportContent = await generateReportContent(scores) + const reportContent = await generateReportContent(scores, reportTagsEnabled) const issueContent = await generateIssueContent(scores) // SET OUTPUTS diff --git a/src/utils.js b/src/utils.js index 8031e75..e7f7c0e 100644 --- a/src/utils.js +++ b/src/utils.js @@ -42,10 +42,10 @@ const saveScore = ({ database, platform, org, repo, score, date, commit }) => { repoRef.current = { score, date, commit } } -const generateReportContent = async (scores) => { +const generateReportContent = async (scores, reportTagsEnabled) => { core.debug('Generating report content') const template = await readFile(join(process.cwd(), 'templates/report.ejs'), 'utf8') - return ejs.render(template, { scores }) + return ejs.render(template, { scores, reportTagsEnabled }) } const generateIssueContent = async (scores) => { diff --git a/templates/report.ejs b/templates/report.ejs index 1f6c1a3..dc95c60 100644 --- a/templates/report.ejs +++ b/templates/report.ejs @@ -1,6 +1,8 @@ +<%_ if (!reportTagsEnabled) { -%> # OpenSSF Scorecard Report ## Summary +<%_ } -%> <%_ if (scores.length) { -%> | Repository | Commit | Score | Date | Difference | Report Link | From 611de75ca43b9419683c5e50be218aea3114daf8 Mon Sep 17 00:00:00 2001 From: KoolTheba Date: Fri, 24 Feb 2023 19:40:33 +0100 Subject: [PATCH 2/2] test: added unit test for generateReportContent utility - Relates #10 --- __fixtures__/index.js | 15 ++++++++++- __tests__/__snapshots__/utils.test.js.snap | 31 ++++++++++++++++++++++ __tests__/utils.test.js | 23 ++++++++++++++-- 3 files changed, 66 insertions(+), 3 deletions(-) create mode 100644 __tests__/__snapshots__/utils.test.js.snap diff --git a/__fixtures__/index.js b/__fixtures__/index.js index debb255..2dca385 100644 --- a/__fixtures__/index.js +++ b/__fixtures__/index.js @@ -72,7 +72,20 @@ const scope = { } } +const scores = [ + { + org: 'fake-org', + repo: 'fake-repo', + platform: 'github.com', + commit: '846b3ddb5f75d95235e94d9eb52e920f4a067338', + score: 10, + date: '2023-02-20', + currentDiff: 5 + } +] + module.exports = { database, - scope + scope, + scores } diff --git a/__tests__/__snapshots__/utils.test.js.snap b/__tests__/__snapshots__/utils.test.js.snap new file mode 100644 index 0000000..1d82663 --- /dev/null +++ b/__tests__/__snapshots__/utils.test.js.snap @@ -0,0 +1,31 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`Utils generateReportContent Should render template with scores and title 1`] = ` +"# OpenSSF Scorecard Report + +## Summary + +| Repository | Commit | Score | Date | Difference | Report Link | +| -- | -- | -- | -- | -- | -- | +| [fake-org/fake-repo](https://github.com/fake-org/fake-repo) | [846b3dd](https://github.com/fake-org/fake-repo/commit/846b3ddb5f75d95235e94d9eb52e920f4a067338) | 10 | 2023-02-20 | 5 | [Full Report](https://deps.dev/project/github/fake-org%2Ffake-repo) | + +_Report generated by [UlisesGascon/openssf-scorecard-monitor](https://github.com/UlisesGascon/openssf-scorecard-monitor)._" +`; + +exports[`Utils generateReportContent Should render template with scores only 1`] = ` +" +| Repository | Commit | Score | Date | Difference | Report Link | +| -- | -- | -- | -- | -- | -- | +| [fake-org/fake-repo](https://github.com/fake-org/fake-repo) | [846b3dd](https://github.com/fake-org/fake-repo/commit/846b3ddb5f75d95235e94d9eb52e920f4a067338) | 10 | 2023-02-20 | 5 | [Full Report](https://deps.dev/project/github/fake-org%2Ffake-repo) | + +_Report generated by [UlisesGascon/openssf-scorecard-monitor](https://github.com/UlisesGascon/openssf-scorecard-monitor)._" +`; + +exports[`Utils generateReportContent Should render template with title only 1`] = ` +"# OpenSSF Scorecard Report + +## Summary + + +_Report generated by [UlisesGascon/openssf-scorecard-monitor](https://github.com/UlisesGascon/openssf-scorecard-monitor)._" +`; diff --git a/__tests__/utils.test.js b/__tests__/utils.test.js index bbb18b9..65c66f0 100644 --- a/__tests__/utils.test.js +++ b/__tests__/utils.test.js @@ -1,5 +1,5 @@ -const { validateDatabaseIntegrity, validateScopeIntegrity } = require('../src/utils') -const { database, scope } = require('../__fixtures__') +const { validateDatabaseIntegrity, validateScopeIntegrity, generateReportContent } = require('../src/utils') +const { database, scope, scores } = require('../__fixtures__') describe('Utils', () => { describe('validateDatabaseIntegrity', () => { @@ -19,4 +19,23 @@ describe('Utils', () => { expect(() => validateScopeIntegrity(scope.emptyScope)).not.toThrow() }) }) + + describe('generateReportContent', () => { + it('Should render template with scores and title', async () => { + const reportTagsEnabled = false + const report = await generateReportContent(scores, reportTagsEnabled) + expect(report).toMatchSnapshot() + }) + it('Should render template with scores only', async () => { + const reportTagsEnabled = true + const report = await generateReportContent(scores, reportTagsEnabled) + expect(report).toMatchSnapshot() + }) + it('Should render template with title only', async () => { + const emptyScores = [] + const reportTagsEnabled = false + const report = await generateReportContent(emptyScores, reportTagsEnabled) + expect(report).toMatchSnapshot() + }) + }) })