|
1 | | -# codacy-cli-v2 |
| 1 | +# Codacy CLI v2 |
2 | 2 |
|
3 | | -The Codacy CLI (version 2) is a command-line tool that supports analyzing code using tools like ESLint and uploading the results in SARIF format to Codacy. |
4 | | -The tool is invoked using the `codacy-cli` command, and provides two main commands: analyze and upload. |
| 3 | +Codacy CLI (version 2) is a command-line tool for running code analysis and integrating with Codacy. If your repository exists in Codacy, you can sync your configuration to ensure consistency with your organization's standards. |
5 | 4 |
|
6 | | -### Commands |
| 5 | +You can also use Codacy CLI for local code analysis without a Codacy account, leveraging the linter configuration files found in your project's root. |
7 | 6 |
|
8 | | -- **`analyze` Command**: Runs ESLint analysis on the codebase. |
9 | | - - `--output, -o`: Output file for the results. |
10 | | - - `--tool, -t`: Specifies the tool to run analysis with (e.g., ESLint). |
11 | | - - `--format`: Output format (use 'sarif' for SARIF format to terminal). |
12 | | - - `--fix`: Automatically fixes issues when possible. |
| 7 | +The CLI supports uploading analysis results (in [SARIF](https://docs.oasis-open.org/sarif/sarif/v2.1.0/sarif-v2.1.0.html) format) to Codacy as well. |
13 | 8 |
|
14 | | -- **`upload` Command With Project Token**: Uploads a SARIF file containing analysis results to Codacy. |
15 | | - - `--sarif-path, -s`: Path to the SARIF report. |
16 | | - - `--commit-uuid, -c`: Commit UUID. |
17 | | - - `--project-token, -t`: Project token for Codacy API. |
| 9 | +It is invoked using the `codacy-cli` command and provides several commands for project setup, analysis, and integration. |
18 | 10 |
|
19 | | -- **`upload` Command With API Token**: Uploads a SARIF file containing analysis results to Codacy. |
20 | | - - `--sarif-path, -s`: Path to the SARIF report. |
21 | | - - `--commit-uuid, -c`: Commit UUID. |
22 | | - - `--api-token, -a`: User level token for Codacy API. |
23 | | - - `--provider, -p`: Provider name (e.g., gh, gl, bb). |
24 | | - - `--owner, -o`: Repository owner. |
25 | | - - `--repository, -r`: Repository name. |
| 11 | +--- |
26 | 12 |
|
27 | | -### Important Concepts |
| 13 | +## Supported Platforms |
28 | 14 |
|
29 | | -- **`.codacy/codacy.yaml`**: Configuration file to specify runtimes and tools versions for the CLI. |
30 | | - ```yaml |
31 | | - runtimes: |
32 | | - |
33 | | - tools: |
34 | | - |
35 | | - |
36 | | -- **`codacy-cli install`**: Command to install the specified node and eslint versions before running analysis. |
| 15 | +- **macOS** |
| 16 | +- **Linux** |
| 17 | +- **Windows (via WSL only)** |
37 | 18 |
|
38 | | -## Download |
| 19 | +> **Note:** Native Windows is not yet supported. For Windows, use [Windows Subsystem for Linux (WSL)](https://learn.microsoft.com/en-us/windows/wsl/) and follow the Linux instructions inside your WSL terminal. |
39 | 20 |
|
40 | | -### MacOS (brew) |
| 21 | +--- |
41 | 22 |
|
42 | | -To install `codacy-cli` using Homebrew: |
| 23 | +## Installation |
| 24 | + |
| 25 | +### macOS (Homebrew) |
43 | 26 |
|
44 | 27 | ```bash |
45 | 28 | brew install codacy/codacy-cli-v2/codacy-cli-v2 |
46 | 29 | ``` |
47 | 30 |
|
48 | | -### Linux |
| 31 | +### Linux / Windows (WSL) |
49 | 32 |
|
50 | | -For Linux, we rely on the codacy-cli.sh script in the root. To download the CLI, run: |
| 33 | +Run the following in your terminal (Linux or WSL): |
51 | 34 |
|
52 | 35 | ```bash |
53 | 36 | bash <(curl -Ls https://raw.githubusercontent.com/codacy/codacy-cli-v2/main/codacy-cli.sh) |
54 | 37 | ``` |
55 | | -You can either put the downloaded script in a specific file or create an alias that will download the script and look for changes: |
| 38 | + |
| 39 | +Or create an alias for convenience: |
56 | 40 |
|
57 | 41 | ```bash |
58 | 42 | alias codacy-cli="bash <(curl -Ls https://raw.githubusercontent.com/codacy/codacy-cli-v2/main/codacy-cli.sh)" |
59 | 43 | ``` |
60 | 44 |
|
61 | | -## Installation |
| 45 | +--- |
| 46 | + |
| 47 | +## CLI Commands |
| 48 | + |
| 49 | +### `init` — Bootstrap Project Configuration |
62 | 50 |
|
63 | | -Before running the analysis, install the specified tools: |
| 51 | +Bootstraps the CLI configuration in your project's folder. This command creates a `.codacy` directory containing a `codacy.yaml` file, which specifies the runtimes and tools that will be used and installed. |
| 52 | + |
| 53 | +- If you provide Codacy repository information (API token, provider, organization, repository), the configuration will be fetched from Codacy, ensuring consistency with your organization's standards. |
| 54 | +- If no Codacy information is provided, all available tools will be included. For each tool, if a local configuration file exists in your project, it will be used; otherwise, an empty configuration file will be created for that tool. |
| 55 | + |
| 56 | +- **Local mode (local configuration files):** |
| 57 | + ```bash |
| 58 | + codacy-cli init |
| 59 | + ``` |
| 60 | +- **Remote mode (fetch configuration from Codacy):** |
| 61 | + ```bash |
| 62 | + codacy-cli init --api-token <token> --provider <gh|gl|bb> --organization <org> --repository <repo> |
| 63 | + ``` |
| 64 | + |
| 65 | +**Flags:** |
| 66 | +- `--api-token` (string): Codacy API token (optional; enables fetching remote config) |
| 67 | +- `--provider` (string): Provider (`gh`, `gl`, `bb`), required with `--api-token` |
| 68 | +- `--organization` (string): Organization name, required with `--api-token` |
| 69 | +- `--repository` (string): Repository name, required with `--api-token` |
| 70 | + |
| 71 | +### `install` — Install Runtimes and Tools |
| 72 | + |
| 73 | +Installs all runtimes and tools specified in `.codacy/codacy.yaml`: |
| 74 | +- Downloads and extracts runtimes (Node, Python, Dart, Java, etc.) |
| 75 | +- Installs tools (ESLint, Trivy, Pylint, PMD, etc.) using the correct package manager or direct download |
| 76 | +- Handles platform-specific details |
| 77 | +- Skips already installed components (e.g., Node, Python, Dart, Java, etc.) |
| 78 | +- Shows a progress bar and reports any failures |
64 | 79 |
|
65 | 80 | ```bash |
66 | 81 | codacy-cli install |
67 | 82 | ``` |
| 83 | +- Optionally specify a custom registry: |
| 84 | + ```bash |
| 85 | + codacy-cli install --registry <url> |
| 86 | + ``` |
68 | 87 |
|
69 | | -## Run Analysis |
| 88 | +### `analyze` — Run Code Analysis |
70 | 89 |
|
71 | | -To run ESLint and output the results to the terminal: |
| 90 | +Runs all configured tools, or a specific tool, on your codebase. |
72 | 91 |
|
73 | 92 | ```bash |
| 93 | +# Run all tools |
| 94 | +tcodacy-cli analyze |
| 95 | + |
| 96 | +# Run a specific tool (e.g., ESLint) |
74 | 97 | codacy-cli analyze --tool eslint |
| 98 | + |
| 99 | +# Output results in SARIF format |
| 100 | +tcodacy-cli analyze --tool eslint --format sarif |
| 101 | + |
| 102 | +# Store results as SARIF in a file |
| 103 | +codacy-cli analyze -t eslint --format sarif -o eslint.sarif |
| 104 | + |
| 105 | +# Analyze a specific file with all configured tools |
| 106 | +codacy-cli analyze path/to/file.js |
| 107 | + |
| 108 | +# Analyze a specific file with a specific tool (e.g., ESLint) |
| 109 | +codacy-cli analyze --tool eslint path/to/file.js |
75 | 110 | ``` |
76 | 111 |
|
77 | | -To output results in SARIF format to the terminal: |
| 112 | +**Flags:** |
| 113 | +- `--output, -o`: Output file for the results; if not provided, results will be printed to the console |
| 114 | +- `--tool, -t`: Tool to run analysis with (e.g., eslint) |
| 115 | +- `--format`: Output format (e.g., `sarif`) |
| 116 | +- `--fix`: Automatically fix issues when possible |
| 117 | + |
| 118 | +### `upload` — Upload SARIF Results to Codacy |
| 119 | + |
| 120 | +Uploads a SARIF file containing analysis results to Codacy. |
78 | 121 |
|
79 | 122 | ```bash |
80 | | -codacy-cli analyze --tool eslint --format sarif |
| 123 | +# With project token |
| 124 | +codacy-cli upload -s path/to/your.sarif -c <commit-uuid> -t <project-token> |
| 125 | + |
| 126 | +# With API token |
| 127 | +codacy-cli upload -s path/to/your.sarif -c <commit-uuid> -a <api-token> -p <provider> -o <owner> -r <repository> |
81 | 128 | ``` |
82 | 129 |
|
83 | | -To store the results as SARIF in a file: |
| 130 | +**Flags:** |
| 131 | +- `--sarif-path, -s`: Path to the SARIF report (required) |
| 132 | +- `--commit-uuid, -c`: Commit UUID |
| 133 | +- `--project-token, -t`: Project token for Codacy API |
| 134 | +- `--api-token, -a`: User-level token for Codacy API |
| 135 | +- `--provider, -p`: Provider name (e.g., gh, gl, bb) |
| 136 | +- `--owner, -o`: Repository owner |
| 137 | +- `--repository, -r`: Repository name |
| 138 | + |
| 139 | +### `update` — Update the CLI |
| 140 | + |
| 141 | +Fetches and installs the latest version of the CLI. |
84 | 142 |
|
85 | 143 | ```bash |
86 | | -codacy-cli analyze -t eslint --format sarif -o eslint.sarif |
| 144 | +codacy-cli update |
87 | 145 | ``` |
88 | 146 |
|
89 | | -## Upload Results |
| 147 | +### `version` — Show Version Information |
90 | 148 |
|
91 | | -To upload a SARIF file to Codacy: |
| 149 | +Displays detailed version/build information for the CLI. |
92 | 150 |
|
93 | 151 | ```bash |
94 | | -codacy-cli upload -s path/to/your.sarif -c your-commit-uuid -t your-project-token |
| 152 | +codacy-cli version |
95 | 153 | ``` |
96 | 154 |
|
97 | | -## Breaking Changes |
| 155 | +--- |
| 156 | + |
| 157 | +## Configuration |
98 | 158 |
|
99 | | -Some behaviors have changed with the new updates of the CLI. To rely on a specific version, you can add that version to your environment: |
| 159 | +- **`.codacy/codacy.yaml`**: Main configuration file specifying runtimes and tool versions. |
| 160 | +- **`.codacy/tools-configs/`**: Tool-specific configuration files (auto-generated or fetched from Codacy). |
100 | 161 |
|
| 162 | +--- |
| 163 | + |
| 164 | +## Example Usage |
| 165 | + |
| 166 | +```bash |
| 167 | +# 1. Initialize project (local or remote) |
| 168 | +codacy-cli init |
| 169 | +# or |
| 170 | +codacy-cli init --api-token <token> --provider gh --organization my-org --repository my-repo |
| 171 | + |
| 172 | +# 2. Install all required runtimes and tools |
| 173 | +codacy-cli install |
| 174 | + |
| 175 | +# 3. Run analysis (all tools or specific tool) |
| 176 | +codacy-cli analyze |
| 177 | +codacy-cli analyze --tool eslint |
| 178 | + |
| 179 | +# 4. Upload results to Codacy |
| 180 | +codacy-cli upload -s eslint.sarif -c <commit-uuid> -t <project-token> |
101 | 181 | ``` |
102 | | -export CODACY_CLI_V2_VERSION="1.0.0-main.133.3607792" |
| 182 | + |
| 183 | +--- |
| 184 | + |
| 185 | +## Troubleshooting |
| 186 | + |
| 187 | +### WSL (Windows Subsystem for Linux) |
| 188 | +- **Always use a WSL terminal** (e.g., Ubuntu on Windows) for all commands. |
| 189 | +- Ensure you have the latest version of WSL and a supported Linux distribution installed. |
| 190 | +- If you see errors related to missing Linux tools (e.g., `curl`, `tar`), install them using your WSL package manager (e.g., `sudo apt install curl tar`). |
| 191 | + |
| 192 | +### MacOS: Errors related to `docker-credential-osxkeychain` not found |
| 193 | + |
| 194 | +Install the docker credential helper: |
| 195 | + |
| 196 | +```bash |
| 197 | +brew install docker-credential-helper |
103 | 198 | ``` |
104 | 199 |
|
| 200 | +--- |
| 201 | + |
105 | 202 | ## Example Repository |
106 | 203 |
|
107 | | -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. |
| 204 | +See [eslint-test-examples](https://github.com/troubleshoot-codacy/eslint-test-examples) for a repository using this CLI in GitHub Actions. |
108 | 205 |
|
109 | | -## Troubleshooting |
| 206 | +--- |
110 | 207 |
|
111 | | -#### Errors related to `docker-credential-osxkeychain` not found when running analysis |
| 208 | +## Breaking Changes & Version Pinning |
112 | 209 |
|
113 | | -Install the docker credential helper. For example, in MacOS: |
| 210 | +Some behaviors have changed with recent updates. To rely on a specific CLI version, set the following environment variable: |
114 | 211 |
|
115 | 212 | ```bash |
116 | | -brew install docker-credential-helper |
| 213 | +export CODACY_CLI_V2_VERSION="1.0.0-main.133.3607792" |
117 | 214 | ``` |
| 215 | + |
| 216 | +Check the [releases](https://github.com/codacy/codacy-cli-v2/releases) page for all available versions. |
| 217 | + |
| 218 | +--- |
0 commit comments