Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 4 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,19 +1,14 @@
# codacy-cli-v2

This is a POC for what could be a new CLI for us. The idea is to rely on the native tools and SARIF format instead of relying on Docker.

## Overview

The `codacy-cli-v2` is a command-line tool for Codacy that supports analyzing code using ESLint and uploading the results in SARIF format to Codacy. It provides two main commands: `analyze` and `upload`.
The `codacy-cli-v2` is a command-line tool for Codacy that supports analyzing code using tools like ESLint and uploading the results in SARIF format to Codacy. It provides two main commands: `analyze` and `upload`.

### Commands

- **`analyze` Command**: Runs ESLint analysis on the codebase.
- `--output, -o`: Output file for the results.
- `--tool, -t`: Specifies the tool to run analysis with (e.g., ESLint).
- `--format`: Output format (use 'sarif' for SARIF format to terminal).
- `--fix, -f`: Automatically fixes issues when possible.
- `--new-pr`: Creates a new GitHub PR with fixed issues.
- `--fix`: Automatically fixes issues when possible.

- **`upload` Command With Project Token**: Uploads a SARIF file containing analysis results to Codacy.
- `--sarif-path, -s`: Path to the SARIF report.
Expand All @@ -30,7 +25,7 @@ The `codacy-cli-v2` is a command-line tool for Codacy that supports analyzing co

### Important Concepts

- **`.codacy/codacy.yaml`**: Configuration file to specify `node` and `eslint` versions for the CLI.
- **`.codacy/codacy.yaml`**: Configuration file to specify runtimes and tools versions for the CLI.
```yaml
runtimes:
- node@22.2.0
Expand Down Expand Up @@ -87,7 +82,7 @@ codacy-cli analyze --tool eslint --format sarif
To store the results as SARIF in a file:

```bash
codacy-cli analyze -t eslint -o eslint.sarif
codacy-cli analyze -t eslint --format sarif -o eslint.sarif
```

## Upload Results
Expand All @@ -101,4 +96,3 @@ codacy-cli upload -s path/to/your.sarif -c your-commit-uuid -t your-project-toke
### Example Repository

As an example, you can check https://github.com/troubleshoot-codacy/eslint-test-examples for a repository that has an action relying on this CLI.

26 changes: 1 addition & 25 deletions cmd/analyze.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,19 @@ package cmd
import (
"codacy/cli-v2/config"
"codacy/cli-v2/tools"
"codacy/cli-v2/utils"
"encoding/json"
"fmt"
"io"
"log"
"net/http"
"os"
"os/exec"

"github.com/spf13/cobra"
)

var outputFile string
var toolToAnalyze string
var autoFix bool
var doNewPr bool
var outputFormat string
var sarifPath string
var commitUuid string
Expand Down Expand Up @@ -97,8 +94,7 @@ func init() {
analyzeCmd.Flags().StringVarP(&outputFile, "output", "o", "", "output file for the results")
analyzeCmd.Flags().StringVarP(&toolToAnalyze, "tool", "t", "", "Which tool to run analysis with")
analyzeCmd.Flags().StringVar(&outputFormat, "format", "", "Output format (use 'sarif' for SARIF format)")
analyzeCmd.Flags().BoolVarP(&autoFix, "fix", "f", false, "Apply auto fix to your issues when available")
analyzeCmd.Flags().BoolVar(&doNewPr, "new-pr", false, "Create a new PR on GitHub containing the fixed issues")
analyzeCmd.Flags().BoolVar(&autoFix, "fix", false, "Apply auto fix to your issues when available")
rootCmd.AddCommand(analyzeCmd)
}

Expand Down Expand Up @@ -208,13 +204,6 @@ var analyzeCmd = &cobra.Command{
log.Fatal("Trying to run unsupported tool: ", toolToAnalyze)
}

// can't create a new PR if there will be no changes/fixed issues
if doNewPr && !autoFix {
log.Fatal("Can't create a new PR with fixes without fixing issues. Use the '--fix' option.")
} else if doNewPr {
failIfThereArePendingChanges()
}

eslint := config.Config.Tools()["eslint"]
eslintInstallationDirectory := eslint.Info()["installDir"]
nodeRuntime := config.Config.Runtimes()["node"]
Expand All @@ -230,18 +219,5 @@ var analyzeCmd = &cobra.Command{
}

tools.RunEslint(workDirectory, eslintInstallationDirectory, nodeBinary, args, autoFix, outputFile, outputFormat)

if doNewPr {
utils.CreatePr(false)
}
},
}

func failIfThereArePendingChanges() {
cmd := exec.Command("git", "status", "--porcelain")
out, _ := cmd.Output()

if string(out) != "" {
log.Fatal("There are pending changes, cannot proceed. Commit your pending changes.")
}
}
19 changes: 0 additions & 19 deletions cmd/ping.go

This file was deleted.

52 changes: 0 additions & 52 deletions utils/prCreator.go

This file was deleted.

23 changes: 0 additions & 23 deletions utils/prCreator.sh

This file was deleted.

13 changes: 0 additions & 13 deletions utils/prCreator_test.go

This file was deleted.