Skip to content

Commit

Permalink
Gracefully handle empty rule description
Browse files Browse the repository at this point in the history
Fixes pmd#127
  • Loading branch information
adangel committed Sep 24, 2022
1 parent 76f6e25 commit 55ef601
Show file tree
Hide file tree
Showing 3 changed files with 143 additions and 1 deletion.
5 changes: 4 additions & 1 deletion lib/annotations.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
33 changes: 33 additions & 0 deletions tests/annotations.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
);
});
});
106 changes: 106 additions & 0 deletions tests/data/pmd-report-empty-full-description.sarif
Original file line number Diff line number Diff line change
@@ -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": []
}
]
}
]
}

0 comments on commit 55ef601

Please sign in to comment.