Skip to content

Commit

Permalink
Register custom rules (#56)
Browse files Browse the repository at this point in the history
Support registration of custom rules (enables #14)
  • Loading branch information
mitchspano authored May 28, 2023
1 parent 6d03b85 commit ccf9d63
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 11 deletions.
21 changes: 20 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,32 @@ To use multiple rulesets within the scan, make a top level file such as `masterR
pmdconfig: masterRuleset.xml
```
## `custom-pmd-rules`

A JSON string which defines any custom rules which need to be registered before the scan is ran. Custom rules are identified by the path to their XML/JAR file and their language.

ex:

```json
[
{ "path": "customRules/customApex.jar", "language": "apex" },
{ "path": "customRules/customXml.xml", "language": "xml" }
]
```

## `severity-threshold`

Throws an error when violations of specific severity (or more severe) are detected.

## `strictly-enforced-rules`

A JSON string which contains the rules which will be strictly enforced regardless of their priority. Enforced rules are identified by their engine, category, and rule name.
A JSON string which defines the rules which will be strictly enforced regardless of their priority. Enforced rules are identified by their engine, category, and rule name.

ex:

```json
[{ "engine": "pmd", "category": "Performance", "rule": "AvoidDebugStatements" }]
```

## `target`

Expand Down
6 changes: 4 additions & 2 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ inputs:
description: "Location of eslintrc config to customize eslint engine."
pmdconfig:
description: "Location of PMD rule reference XML file to customize rule selection."
custom-pmd-rules:
description: "JSON string which defines any custom rules which need to be registered before the scan is ran. Custom rules are identified by the path to their XML/JAR file and their language."
severity-threshold:
description: "Integer threshold value which will throw an error when violations of specific severity (or more severe) are detected."
strictly-enforced-rules:
Expand All @@ -21,10 +23,10 @@ inputs:
description: "Location of tsconfig.json file."
report-mode:
description: "Use comments or check-runs for reporting errors"
default: 'check-runs'
default: "check-runs"
delete-resolved-comments:
description: "Set `true` to delete comments once the issue has been resolved"
default: 'false'
default: "false"
runs:
using: "node16"
main: "dist/index.js"
2 changes: 1 addition & 1 deletion dist/index.js

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions src/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { ScannerViolation, ScannerViolationType } from "./sfdxCli";
export type PluginInputs = {
severityThreshold: number;
strictlyEnforcedRules: string;
customPmdRules?: string;
deleteResolvedComments: boolean;
reportMode: string | "comments" | "check-runs";
target: string;
Expand Down
37 changes: 30 additions & 7 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import { getDiffInPullRequest, GithubPullRequest } from "./git-actions";

import {
scanFiles,
registerRule,
ScannerFinding,
ScannerFlags,
ScannerViolation,
Expand All @@ -30,7 +31,7 @@ import { Reporter } from "./reporter/reporter.types";
interface ExecSyncError {
status: string;
stack: string;
output: Buffer;
output?: Buffer;
message: string;
}

Expand All @@ -52,6 +53,7 @@ function initialSetup() {
// where: 1 (high), 2 (moderate), and 3 (low)
const inputs: PluginInputs = {
reportMode: getInput("report-mode") || "check-runs",
customPmdRules: getInput("custom-pmd-rules"),
severityThreshold: parseInt(getInput("severity-threshold")) || 4,
strictlyEnforcedRules: getInput("strictly-enforced-rules"),
deleteResolvedComments: getInput("delete-resolved-comments") === "true",
Expand Down Expand Up @@ -107,7 +109,7 @@ export async function performStaticCodeAnalysisOnFilesInDiff(
message: typedErr.message,
status: typedErr.status,
stack: typedErr.stack,
output: typedErr.output.toString(),
output: typedErr.output?.toString(),
});
setFailed("Something went wrong when scanning the files.");
}
Expand Down Expand Up @@ -198,6 +200,28 @@ function getFilesToScan(
return pathsWithChangedLines;
}

/**
* @description Calls `scanner:rule:add` for every custom rule defined as input
*/
async function registerCustomPmdRules(rules: string) {
for (let rule of JSON.parse(rules) as {
[key in string]: string;
}[]) {
try {
await registerRule(rule.path, rule.language);
} catch (err) {
const typedErr = err as unknown as ExecSyncError;
console.error({
message: typedErr.message,
status: typedErr.status,
stack: typedErr.stack,
output: typedErr.output?.toString(),
});
setFailed("Something went wrong when registering custom rule.");
}
}
}

/**
* @description Main method - injection point for code execution
*/
Expand All @@ -212,18 +236,17 @@ async function main() {
pullRequest?.base?.repo?.clone_url
);

if (!inputs.target) {
console.log("Here are the lines which have changed:");
console.log({ filePathToChangedLines });
}

const filesToScan = getFilesToScan(filePathToChangedLines, inputs.target);
if (filesToScan.length === 0) {
console.log("There are no files to scan - exiting now.");
return;
}
scannerFlags.target = filesToScan.join(",");

if (inputs.customPmdRules) {
registerCustomPmdRules(inputs.customPmdRules);
}

const diffFindings = await performStaticCodeAnalysisOnFilesInDiff(
scannerFlags
);
Expand Down
8 changes: 8 additions & 0 deletions src/sfdxCli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,3 +77,11 @@ export async function scanFiles(scannerFlags: ScannerFlags) {
"--json",
]);
}

export async function registerRule(path: string, language: string) {
return cli<ScannerFinding[] | string>("scanner:rule:add", [
`--path="${path}"`,
`--language="${language}"`,
"--json",
]);
}

0 comments on commit ccf9d63

Please sign in to comment.