Skip to content

Commit

Permalink
Improve error logging (#55)
Browse files Browse the repository at this point in the history
Improves error logging when the `sfdx` command is executed.
Updates README with better instructions for all input parameters.
  • Loading branch information
mitchspano authored May 27, 2023
1 parent 47008f9 commit 6d03b85
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 10 deletions.
41 changes: 36 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ Runs sfdx-scanner on a pull request or individual commit and generates in-line c

## Inputs

| NOTE: All inputs are optional |
| ----------------------------- |

## `category`

Categor(ies) of rules to run.
Expand All @@ -24,7 +27,34 @@ Location of eslintrc config to customize eslint engine.

## `pmdconfig`

Location of PMD rule reference XML file to customize rule selection
Location of PMD rule reference XML file to customize rule selection.

### Multiple PMD Rulesets

To use multiple rulesets within the scan, make a top level file such as `masterRuleset.xml` and include the paths to the other ruleset files:

```
.
├── masterRuleset.xml
├── ruleset1.xml
└── ruleset2.xml
```

```xml
<?xml version="1.0" encoding="UTF-8"?>
<ruleset name="master"
xmlns="http://pmd.sourceforge.net/ruleset/2.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://pmd.sourceforge.net/ruleset/2.0.0 https://pmd.sourceforge.io/ruleset_2_0_0.xsd">
<description>Master Ruleset</description>
<rule ref="ruleset1.xml" />
<rule ref="ruleset2.xml" />
</ruleset>
```

```yml
pmdconfig: masterRuleset.xml
```
## `severity-threshold`

Expand All @@ -40,17 +70,18 @@ Optionally provide this to scan a whole directory instead of just the diff. If t

## `tsconfig`

Location of tsconfig.json file
Location of tsconfig.json file.

## `report-mode`

Details which way to report issues back to GitHub, can be either:

- `check-runs` - Shows findings as annotations on the PR (default)
- `comments` - Shows findings as comments

## `delete-resolved-comments`

When set to true, will delete resolved comments from a PR. Defaults to `false`. Will do nothing unless `report-mode` is set to `comments`
When set to true, will delete resolved comments from a PR. Defaults to `false`. Will do nothing unless `report-mode` is set to `comments`.

## Example usage

Expand All @@ -74,9 +105,9 @@ jobs:
sfdx plugins:install @salesforce/sfdx-scanner

- name: Run SFDX Scanner - Report findings as comments
uses: mitchspano/sfdx-scan-pull-request@v0.1.8
uses: mitchspano/sfdx-scan-pull-request@v0.1.12
with:
pmdconfig: ruleset.xml
pmdconfig: masterRuleset.xml
severity-threshold: 4
strictly-enforced-rules: '[{ "engine": "pmd", "category": "Performance", "rule": "AvoidDebugStatements" }]'
env:
Expand Down
2 changes: 1 addition & 1 deletion dist/index.js

Large diffs are not rendered by default.

24 changes: 21 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,13 @@ import { CommentsReporter } from "./reporter/comments-reporter";
import { AnnotationsReporter } from "./reporter/annoations-reporter";
import { Reporter } from "./reporter/reporter.types";

interface ExecSyncError {
status: string;
stack: string;
output: Buffer;
message: string;
}

/**
* @description Collects and verifies the inputs from the action context and metadata
* https://docs.github.com/en/actions/creating-actions/metadata-syntax-for-github-actions#inputs
Expand Down Expand Up @@ -91,9 +98,20 @@ export async function performStaticCodeAnalysisOnFilesInDiff(
console.log(
"Performing static code analysis on all of the files in the difference..."
);

const findings = await scanFiles(scannerFlags);
return typeof findings === "string" ? [] : findings;
try {
const findings = await scanFiles(scannerFlags);
return typeof findings === "string" ? [] : findings;
} 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 scanning the files.");
}
return [];
}

/**
Expand Down
1 change: 0 additions & 1 deletion src/sfdxCli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ const cli = async <T>(commandName: string, cliArgs: string[] = []) => {
let result = null as T;
try {
const cliCommand = `npx sfdx ${commandName} ${cliArgs.join(" ")}`;
console.log("Running scan...");
console.log(cliCommand);
result = (
JSON.parse(execSync(cliCommand).toString()) as SfdxCommandResult<T>
Expand Down

0 comments on commit 6d03b85

Please sign in to comment.