> ## Documentation Index
> Fetch the complete documentation index at: https://docs.codeant.ai/llms.txt
> Use this file to discover all available pages before exploring further.

# GitHub Actions

> Set up GitHub Actions workflow for coverage reporting.

## CodeAnt AI Coverage Upload Action

Upload test coverage reports to CodeAnt AI for comprehensive analysis, visualization, and tracking of your code coverage metrics.

You can find this action on the [GitHub Marketplace](https://github.com/marketplace/actions/codeant-ai-coverage-upload).

### Features

* 📊 Upload coverage reports in XML format (Cobertura XML, JaCoCo XML)
* 🔍 Automatic coverage analysis and insights
* 📈 Track coverage trends over time
* 🎯 Integration with pull requests
* 🚀 Easy setup with minimal configuration

## Usage

### Basic Example

```yaml theme={null}
name: Test Coverage

on:
  push:
    branches: [main]
  pull_request:
    branches: [main]

jobs:
  coverage:
    runs-on: ubuntu-latest

    steps:
      - uses: actions/checkout@v4

      - name: Run tests with coverage
        run: |
          # Your test command that generates coverage report
          pytest --cov --cov-report=xml

      - name: Upload coverage to CodeAnt AI
        uses: CodeAnt-AI/codeant-coverage-action@v0.0.6
        with:
          access_token: ${{ secrets.CODEANT_TOKEN }}
          coverage_file: coverage.xml
```

### Advanced Example

```yaml theme={null}
- name: Upload coverage to CodeAnt AI
  uses: CodeAnt-AI/codeant-coverage-action@v0.0.6
  with:
    access_token: ${{ secrets.CODEANT_TOKEN }}
    coverage_file: coverage.xml
    api_base: https://api.codeant.ai
    platform: github
    base_url: https://github.com
    module: backend
    module_path: services/backend
```

## Inputs

| Input           | Description                                                                                          | Required | Default                  |
| --------------- | ---------------------------------------------------------------------------------------------------- | -------- | ------------------------ |
| `access_token`  | CodeAnt API token (`cdt_…`) used to authenticate the upload - see [API Tokens](/settings/api-tokens) | Yes      | -                        |
| `coverage_file` | Path to the coverage XML file (e.g., coverage.xml)                                                   | Yes      | `coverage.xml`           |
| `api_base`      | CodeAnt AI API base URL                                                                              | No       | `https://api.codeant.ai` |
| `platform`      | Git platform (github, gitlab, bitbucket)                                                             | No       | `github`                 |
| `base_url`      | Base URL of the git platform                                                                         | No       | `https://github.com`     |
| `module`        | Module name for monorepo setups                                                                      | No       | -                        |
| `module_path`   | Module path for resolving files in monorepo                                                          | No       | -                        |

## Multiple Coverage Files (Monorepo)

When a single commit produces several coverage reports - for example one per service in a monorepo - give each upload its own `module` and `module_path`. CodeAnt AI keeps the reports separate so each module is tracked, displayed, and gated independently. Without `module`, every upload writes to the same key and later steps overwrite earlier ones.

* `module` is the logical name shown in the UI (e.g. `backend`, `frontend`).
* `module_path` is the directory used to resolve source files referenced in the coverage XML (e.g. `services/backend`). If your XML's `filename` attributes are relative to the module root, set `module_path` to that root.

### Recommended: a single job with `strategy.matrix`

Instead of duplicating the upload step per module, define it once and fan it out with [`strategy.matrix`](https://docs.github.com/en/actions/using-jobs/using-a-matrix-for-your-jobs). Each matrix entry becomes its own parallel job; adding a new module is one new entry instead of an entire job stanza.

```yaml theme={null}
name: Test Coverage

on:
  push:
    branches: [main]
  pull_request:
    branches: [main]

jobs:
  coverage:
    runs-on: ubuntu-latest
    strategy:
      fail-fast: false
      matrix:
        include:
          - module: backend
            module_path: services/backend
            coverage_file: services/backend/coverage.xml
          - module: frontend
            module_path: services/frontend
            coverage_file: services/frontend/coverage/cobertura-coverage.xml
          - module: api
            module_path: services/api
            coverage_file: services/api/coverage.xml

    steps:
      - uses: actions/checkout@v4

      - name: Run tests and generate coverage
        working-directory: ${{ matrix.module_path }}
        run: |
          # Your test command that emits ${{ matrix.coverage_file }}
          pytest --cov --cov-report=xml

      - name: Upload coverage to CodeAnt AI
        uses: CodeAnt-AI/codeant-coverage-action@v0.0.6
        with:
          access_token: ${{ secrets.CODEANT_TOKEN }}
          coverage_file: ${{ matrix.coverage_file }}
          module: ${{ matrix.module }}
          module_path: ${{ matrix.module_path }}
```

GitHub spawns one job per matrix entry - `coverage (backend)`, `coverage (frontend)`, etc. - and each runs the upload independently with its own `module`, `module_path`, and `coverage_file`.

### Alternative: one step per module

If you prefer the explicit form (e.g. each module has a different test command that can't be parameterized cleanly), call the action once per module:

```yaml theme={null}
- name: Upload backend coverage
  uses: CodeAnt-AI/codeant-coverage-action@v0.0.6
  with:
    access_token: ${{ secrets.CODEANT_TOKEN }}
    coverage_file: services/backend/coverage.xml
    module: backend
    module_path: services/backend

- name: Upload frontend coverage
  uses: CodeAnt-AI/codeant-coverage-action@v0.0.6
  with:
    access_token: ${{ secrets.CODEANT_TOKEN }}
    coverage_file: services/frontend/coverage/cobertura-coverage.xml
    module: frontend
    module_path: services/frontend
```

## Supported Coverage Formats

> **Important:** Only XML format is supported for coverage reports.

* Cobertura XML (.xml)
* JaCoCo XML

## Setup

### 1. Create a CodeAnt Token

In CodeAnt AI, open the user menu (click your email at the bottom-left) and select **API Tokens**, click **Create token**, and copy the generated token (it starts with `cdt_` and is shown only once). See [API Tokens](/settings/api-tokens) for the full walkthrough. This single token authenticates the upload - you don't need a GitHub personal access token.

### 2. Generate Coverage Report

First, ensure your test suite generates a coverage report. Here are examples for common languages:

**Python (pytest)**

```bash theme={null}
pytest --cov --cov-report=xml
```

**JavaScript/TypeScript (Jest)**

```bash theme={null}
jest --coverage --coverageReporters=cobertura
```

**Java (Maven)**

```bash theme={null}
mvn test jacoco:report
```

**Go**

```bash theme={null}
go test -coverprofile=coverage.out ./...
gocov convert coverage.out | gocov-xml > coverage.xml
```

### 3. Add Secrets

Store your CodeAnt token as a repository secret:

1. Go to your repository Settings
2. Navigate to Secrets and variables → Actions
3. Click "New repository secret"
4. Name: `CODEANT_TOKEN`
5. Value: Your CodeAnt token (`cdt_…`)

### 4. Configure Workflow

Add the action to your GitHub Actions workflow as shown in the usage examples above.

## Coverage config file

You have to create a .coveragerc file in the project's root folder to include all the source files in the test coverage calculation.

Example:

```yaml theme={null}
# include every Python file under the repo root
source = .

# exclude tests, virtualenvs, build artifacts, etc.
omit =
    */tests/*
    */.venv/*
    */build/*
    */dist/*
```

When you assign source to "." , It checks for every python file in the root folder and its sub directories. You can omit some directories by placing them in the omit section of the file.

## How it works

With the above configuration:

1. coverage run -m pytest tests/ will count every .py under the workspace as “valid” lines except for those in the omitted directories.
2. Lines actually executed by your tests are marked “covered.”
3. coverage xml -o coverage.xml produces a Cobertura-style report reflecting true coverage over the entire codebase.
4. Using this coverage xml, we calculate the coverage percentage and the status check will be done on every new push to the branch.

## License

This project is licensed under the MIT License - see the LICENSE file for details.

## Support

For issues, questions, or contributions, please visit the [GitHub repository](https://github.com/CodeAnt-AI/codeant-coverage-action).

## Token permission

This pipeline authenticates with a CodeAnt API token (`cdt_…`). If the token is **scoped**, give it the **Codeant CI/CD** role - it covers scans, quality gates, and test-coverage upload - scoped to the repositories this pipeline runs on (or **All repositories**). A token missing the required permission is rejected with an HTTP `403` `token_scope_forbidden` error that names the permission it needs. See [API token permissions](/settings/api-tokens#permissions-and-scopes).
