From 55ef6010c029ef9a2fcd16e4912c6bc8c21a8b2d Mon Sep 17 00:00:00 2001 From: Andreas Dangel Date: Sat, 24 Sep 2022 22:37:07 +0200 Subject: [PATCH] Gracefully handle empty rule description Fixes #127 --- lib/annotations.js | 5 +- tests/annotations.test.js | 33 ++++++ .../pmd-report-empty-full-description.sarif | 106 ++++++++++++++++++ 3 files changed, 143 insertions(+), 1 deletion(-) create mode 100644 tests/data/pmd-report-empty-full-description.sarif diff --git a/lib/annotations.js b/lib/annotations.js index fa7890d..1e34d7c 100644 --- a/lib/annotations.js +++ b/lib/annotations.js @@ -52,7 +52,10 @@ function createAnnotation(location, title) { } function createDescription(rule) { - const lines = rule.fullDescription.text.split(/\n|\r\n/); + const lines = rule.fullDescription.text !== undefined + ? rule.fullDescription.text.split(/\n|\r\n/) + : ['']; + // remove empty first line if (lines.length > 1 && lines[0] === '') { lines.splice(0, 1); diff --git a/tests/annotations.test.js b/tests/annotations.test.js index e76c209..6661ba7 100644 --- a/tests/annotations.test.js +++ b/tests/annotations.test.js @@ -59,4 +59,37 @@ https://pmd.github.io/pmd-6.40.0/pmd_rules_apex_bestpractices.html#unusedlocalva expect(core.notice).toHaveBeenNthCalledWith(1, 'Full description for Low Prio Rule\n\n4 - low prio rule (Priority: 5, Ruleset: sample ruleset)\nhttps://pmd.github.io/latest/ruleLowPrio', { title: 'Low Prio Rule', file: '/folder/file5.txt', startLine: 8 }); expect(core.notice).toHaveBeenNthCalledWith(2, 'Full description for Low Prio Rule\n\n4 - low prio rule (Priority: 5, Ruleset: sample ruleset)\nhttps://pmd.github.io/latest/ruleLowPrio', { title: 'Low Prio Rule', file: '/folder/file6.txt', startLine: 9 }); }); + + it('can deal with empty full description (issue #127)', () => { + const report = sarif.loadReport(path.join(__dirname, 'data', 'pmd-report-empty-full-description.sarif')); + annotations.processSarifReport(report); + + expect(core.error).toHaveBeenCalledTimes(0); + expect(core.warning).toHaveBeenCalledTimes(1); + expect(core.warning).toHaveBeenNthCalledWith(1, `The first parameter of System.debug, when using the signature with two parameters, is a LoggingLevel enum. + +Having the Logging Level specified provides a cleaner log, and improves readability of it. + +DebugsShouldUseLoggingLevel (Priority: 3, Ruleset: Best Practices) +https://pmd.github.io/pmd-6.49.0/pmd_rules_apex_bestpractices.html#debugsshoulduselogginglevel`, + { + title: 'Calls to System.debug should specify a logging level.', + file: '/home/andreas/PMD/source/pmd-it/pmd-github-action-issue-127/src/Test.cls', + startLine: 3, + endLine: 3 + } + ); + expect(core.notice).toHaveBeenCalledTimes(1); + expect(core.notice).toHaveBeenNthCalledWith(1, ` + +AvoidProductionDebugLogs (Priority: 5, Ruleset: My Default) +`, + { + title: 'Avoid leaving System.debug() statments in code as they negativly influence performance.', + file: '/home/andreas/PMD/source/pmd-it/pmd-github-action-issue-127/src/Test.cls', + startLine: 3, + endLine: 3 + } + ); + }); }); diff --git a/tests/data/pmd-report-empty-full-description.sarif b/tests/data/pmd-report-empty-full-description.sarif new file mode 100644 index 0000000..e4bde8a --- /dev/null +++ b/tests/data/pmd-report-empty-full-description.sarif @@ -0,0 +1,106 @@ +{ + "$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": "PMD", + "version": "6.49.0", + "informationUri": "https://pmd.github.io/pmd/", + "rules": [ + { + "id": "AvoidProductionDebugLogs", + "shortDescription": { + "text": "Avoid leaving System.debug() statments in code as they negativly influence performance." + }, + "fullDescription": {}, + "helpUri": "", + "help": {}, + "properties": { + "ruleset": "My Default", + "priority": 5, + "tags": [ + "My Default" + ] + } + }, + { + "id": "DebugsShouldUseLoggingLevel", + "shortDescription": { + "text": "Calls to System.debug should specify a logging level." + }, + "fullDescription": { + "text": "\nThe first parameter of System.debug, when using the signature with two parameters, is a LoggingLevel enum.\n\nHaving the Logging Level specified provides a cleaner log, and improves readability of it.\n " + }, + "helpUri": "https://pmd.github.io/pmd-6.49.0/pmd_rules_apex_bestpractices.html#debugsshoulduselogginglevel", + "help": { + "text": "\nThe first parameter of System.debug, when using the signature with two parameters, is a LoggingLevel enum.\n\nHaving the Logging Level specified provides a cleaner log, and improves readability of it.\n " + }, + "properties": { + "ruleset": "Best Practices", + "priority": 3, + "tags": [ + "Best Practices" + ] + } + } + ] + } + }, + "results": [ + { + "ruleId": "AvoidProductionDebugLogs", + "ruleIndex": 0, + "message": { + "text": "Avoid leaving System.debug() statments in code as they negativly influence performance." + }, + "locations": [ + { + "physicalLocation": { + "artifactLocation": { + "uri": "/home/andreas/PMD/source/pmd-it/pmd-github-action-issue-127/src/Test.cls" + }, + "region": { + "startLine": 3, + "startColumn": 12, + "endLine": 3, + "endColumn": 16 + } + } + } + ] + }, + { + "ruleId": "DebugsShouldUseLoggingLevel", + "ruleIndex": 1, + "message": { + "text": "Calls to System.debug should specify a logging level." + }, + "locations": [ + { + "physicalLocation": { + "artifactLocation": { + "uri": "/home/andreas/PMD/source/pmd-it/pmd-github-action-issue-127/src/Test.cls" + }, + "region": { + "startLine": 3, + "startColumn": 12, + "endLine": 3, + "endColumn": 16 + } + } + } + ] + } + ], + "invocations": [ + { + "executionSuccessful": true, + "toolConfigurationNotifications": [], + "toolExecutionNotifications": [] + } + ] + } + ] +}