Skip to content

Commit 409aab0

Browse files
committed
feature: Add format flag to allow for sarif output
1 parent ab46688 commit 409aab0

File tree

3 files changed

+17
-3
lines changed

3 files changed

+17
-3
lines changed

README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ The `codacy-cli-v2` is a command-line tool for Codacy that supports analyzing co
1111
- **`analyze` Command**: Runs ESLint analysis on the codebase.
1212
- `--output, -o`: Output file for the results.
1313
- `--tool, -t`: Specifies the tool to run analysis with (e.g., ESLint).
14+
- `--format`: Output format (use 'sarif' for SARIF format to terminal).
1415
- `--fix, -f`: Automatically fixes issues when possible.
1516
- `--new-pr`: Creates a new GitHub PR with fixed issues.
1617

@@ -77,6 +78,12 @@ To run ESLint and output the results to the terminal:
7778
codacy-cli analyze --tool eslint
7879
```
7980

81+
To output results in SARIF format to the terminal:
82+
83+
```bash
84+
codacy-cli analyze --tool eslint --format sarif
85+
```
86+
8087
To store the results as SARIF in a file:
8188

8289
```bash

cmd/analyze.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ var outputFile string
1919
var toolToAnalyze string
2020
var autoFix bool
2121
var doNewPr bool
22+
var outputFormat string
2223
var sarifPath string
2324
var commitUuid string
2425
var projectToken string
@@ -95,6 +96,7 @@ type Pattern struct {
9596
func init() {
9697
analyzeCmd.Flags().StringVarP(&outputFile, "output", "o", "", "output file for the results")
9798
analyzeCmd.Flags().StringVarP(&toolToAnalyze, "tool", "t", "", "Which tool to run analysis with")
99+
analyzeCmd.Flags().StringVar(&outputFormat, "format", "", "Output format (use 'sarif' for SARIF format to terminal)")
98100
analyzeCmd.Flags().BoolVarP(&autoFix, "fix", "f", false, "Apply auto fix to your issues when available")
99101
analyzeCmd.Flags().BoolVar(&doNewPr, "new-pr", false, "Create a new PR on GitHub containing the fixed issues")
100102
rootCmd.AddCommand(analyzeCmd)
@@ -221,9 +223,11 @@ var analyzeCmd = &cobra.Command{
221223
log.Printf("Running %s...\n", toolToAnalyze)
222224
if outputFile != "" {
223225
log.Println("Output will be available at", outputFile)
226+
} else if outputFormat == "sarif" {
227+
log.Println("Output will be in SARIF format")
224228
}
225229

226-
tools.RunEslint(workDirectory, eslintInstallationDirectory, nodeBinary, args, autoFix, outputFile)
230+
tools.RunEslint(workDirectory, eslintInstallationDirectory, nodeBinary, args, autoFix, outputFile, outputFormat)
227231

228232
if doNewPr {
229233
utils.CreatePr(false)
@@ -238,4 +242,4 @@ func failIfThereArePendingChanges() {
238242
if string(out) != "" {
239243
log.Fatal("There are pending changes, cannot proceed. Commit your pending changes.")
240244
}
241-
}
245+
}

tools/eslintRunner.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import (
99
// * Run from the root of the repo we want to analyse
1010
// * NODE_PATH="<the installed eslint path>/node_modules"
1111
// * The local installed ESLint should have the @microsoft/eslint-formatter-sarif installed
12-
func RunEslint(repositoryToAnalyseDirectory string, eslintInstallationDirectory string, nodeBinary string, pathsToCheck []string, autoFix bool, outputFile string) {
12+
func RunEslint(repositoryToAnalyseDirectory string, eslintInstallationDirectory string, nodeBinary string, pathsToCheck []string, autoFix bool, outputFile string, outputFormat string) {
1313
eslintInstallationNodeModules := filepath.Join(eslintInstallationDirectory, "node_modules")
1414
eslintJsPath := filepath.Join(eslintInstallationNodeModules, ".bin", "eslint")
1515

@@ -20,6 +20,9 @@ func RunEslint(repositoryToAnalyseDirectory string, eslintInstallationDirectory
2020
if outputFile != "" {
2121
//When writing to file, we write is SARIF
2222
cmd.Args = append(cmd.Args, "-f", "@microsoft/eslint-formatter-sarif", "-o", outputFile)
23+
} else if outputFormat == "sarif" {
24+
//When outputting to terminal in SARIF format
25+
cmd.Args = append(cmd.Args, "-f", "@microsoft/eslint-formatter-sarif")
2326
}
2427
if len(pathsToCheck) > 0 {
2528
cmd.Args = append(cmd.Args, pathsToCheck...)

0 commit comments

Comments
 (0)