Skip to content

Commit c56872d

Browse files
authored
Merge pull request #28 from codacy/add-sarif-format
feature: Add format flag to allow for SARIF output
2 parents ab46688 + 25ba290 commit c56872d

File tree

11 files changed

+32
-19
lines changed

11 files changed

+32
-19
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: 5 additions & 1 deletion
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)

cmd/ping.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@ func init() {
1010
}
1111

1212
var pingCmd = &cobra.Command{
13-
Use: "ping",
13+
Use: "ping",
1414
Short: "Replies with 'pong'.",
15-
Long: "Test command. Replies with 'pong'.",
15+
Long: "Test command. Replies with 'pong'.",
1616
Run: func(cmd *cobra.Command, args []string) {
1717
fmt.Println("pong")
1818
},

config-file/configFile.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,4 +43,4 @@ func ReadConfigFile(configPath string) error {
4343
}
4444

4545
return parseConfigFile(content)
46-
}
46+
}

config-file/configTool.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import (
66
)
77

88
type ConfigTool struct {
9-
name string
9+
name string
1010
version string
1111
}
1212

@@ -25,4 +25,4 @@ func parseConfigTool(tool string) (*ConfigTool, error) {
2525
}
2626

2727
return &ConfigTool{name: toolSplited[0], version: toolSplited[1]}, nil
28-
}
28+
}

config/config.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ type ConfigType struct {
1515
projectConfigFile string
1616

1717
runtimes map[string]*Runtime
18-
tools map[string]*Runtime
18+
tools map[string]*Runtime
1919
}
2020

2121
func (c *ConfigType) HomePath() string {
@@ -83,10 +83,10 @@ func (c *ConfigType) initCodacyDirs() {
8383
if err != nil {
8484
log.Fatal(err)
8585
}
86-
86+
8787
yamlPath := filepath.Join(c.localCodacyDirectory, "codacy.yaml")
8888
ymlPath := filepath.Join(c.localCodacyDirectory, "codacy.yml")
89-
89+
9090
if _, err := os.Stat(ymlPath); err == nil {
9191
c.projectConfigFile = ymlPath
9292
} else {

config/node-utils.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,9 @@ func genInfoNode(r *Runtime) map[string]string {
3737

3838
return map[string]string{
3939
"nodeFileName": nodeFileName,
40-
"installDir": path.Join(Config.RuntimesDirectory(), nodeFileName),
41-
"node": path.Join(Config.RuntimesDirectory(), nodeFileName, "bin", "node"),
42-
"npm": path.Join(Config.RuntimesDirectory(), nodeFileName, "bin", "npm"),
40+
"installDir": path.Join(Config.RuntimesDirectory(), nodeFileName),
41+
"node": path.Join(Config.RuntimesDirectory(), nodeFileName, "bin", "node"),
42+
"npm": path.Join(Config.RuntimesDirectory(), nodeFileName, "bin", "npm"),
4343
}
4444
}
4545

@@ -80,4 +80,3 @@ func InstallNode(r *Runtime) error {
8080

8181
return nil
8282
}
83-

config/runtime.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@ import (
55
)
66

77
type Runtime struct {
8-
name string
8+
name string
99
version string
10-
info map[string]string
10+
info map[string]string
1111
}
1212

1313
func (r *Runtime) Name() string {
@@ -42,4 +42,4 @@ func NewRuntime(name string, version string) *Runtime {
4242
}
4343
r.populateInfo()
4444
return &r
45-
}
45+
}

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...)

tools/eslintRunner_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ func TestRunEslintToFile(t *testing.T) {
2828
eslintInstallationDirectory := filepath.Join(homeDirectory, ".cache/codacy/tools/[email protected]")
2929
nodeBinary := "node"
3030

31-
RunEslint(repositoryToAnalyze, eslintInstallationDirectory, nodeBinary, nil, false, tempResultFile)
31+
RunEslint(repositoryToAnalyze, eslintInstallationDirectory, nodeBinary, nil, false, tempResultFile, "")
3232

3333
expectedSarifBytes, err := os.ReadFile(expectedSarifFile)
3434
if err != nil {

0 commit comments

Comments
 (0)