octocov

repository·main·Indexed 19 days ago

https://github.com/k1low/octocov

A toolkit for collecting and managing code metrics including code coverage, code-to-test ratio, and test execution time. It functions as a CLI tool for local development and a CI tool for GitHub Actions (k1LoW/octocov-action@v1), providing automated reporting, threshold enforcement, and SVG badge generation. It supports various datastores for report storage and comparison, including GitHub, S3, GCS, BigQuery, and local paths, and features a central mode for aggregating metrics across multiple repositories.

Tokens
8.4K
Snippets
32
Records
43
Agent score
67%

What's inside octocov

  1. Use the badge package for rendering badges

    main
    The badge package is a specialized component within the octocov ecosystem designed for rendering badges. It is used to generate visual status indicators based on coverage or other metrics.
  2. Understand the BigQuery schema for octocov datasource

    main
    The octocov datasource uses a BigQuery schema to store code metric reports. The primary data structure is the reports table, which holds the metrics sent from octocov. The schema is designed to support relational queries across different metric reports.
  3. Use custom metrics with octocov

    main

    In addition to standard metrics (Code Coverage, Code to Test Ratio, and Test Execution Time), octocov accepts custom metrics via JSON files.

    To use them, specify the path to the JSON file using an environment variable prefixed with OCTOCOV_CUSTOM_METRICS_ (e.g., OCTOCOV_CUSTOM_METRICS_PERF). If you have multiple files, use a separate environment variable for each.

    Defining acceptable conditions

    You can enforce quality gates using the acceptables array within your custom metrics JSON. This allows you to compare current values against thresholds or previous values.

    Available variables in acceptables expressions:

    • current.{metric_key}: The current metric value.
    • prev.{metric_key}: The previous metric value (retrieved from diff.datastores:).
    • diff.{metric_key}: The difference between current and previous (current.{metric_key} - prev.{metric_key}).

    If a condition fails, octocov will exit with an error message indicating which condition was not met.

    {
      "key": "performance_metrics",
      "name": "Performance Metrics",
      "metrics": [
        {
          "key": "response_time",
          "name": "Response Time",
          "value": 250.0,
          "unit": "ms"
        }
      ],
      "acceptables": [
        "current.response_time < 300",
        "current.response_time <= prev.response_time"
      ]
    }
  4. Detect pull request numbers

    main

    octocov identifies the pull request number using the following priority order:

    1. The GITHUB_PULL_REQUEST_NUMBER or OCTOCOV_GITHUB_PULL_REQUEST_NUMBER environment variables.
    2. The GITHUB_REF environment variable (e.g., refs/pull/1/merge).
    3. The branch name from GITHUB_REF (e.g., refs/heads/branch/name), which octocov then resolves using the GitHub API.

    Overriding environment variables

    You can override any environment variable used by octocov by prefixing it with OCTOCOV_. For example, setting OCTOCOV_GITHUB_REF will cause octocov to use the value of GITHUB_REF.

  5. Understand Central Mode for multi-repo coverage

    main

    Central mode allows a single repository to act as a hub for collecting coverage reports from multiple other repositories. When enabled, other functions (like standard reporting) are automatically turned off.

    Key Configuration Keys:

    • central.root: (String) Root directory or index file for collected reports. Default is ..
    • central.reports.datastores: (List) Datastores where reports are collected from. Default is local://reports.
    • central.badges.datastores: (List) Datastores where badges are generated. Default is local://badges.
    • central.push: (Boolean/Config) Configuration for pushing the index file and badges.
    • central.reReport.datastores: (List) Additional datastores to re-store collected reports.
    • central.if: (Expression) Condition for running central mode.

    Usage Patterns:

    • GitHub Artifacts: Use artifact:// in the sub-repos and perform badge generation via on.schedule in the central repo.
    • GitHub Repositories: Use github:// in sub-repos and perform badge generation via on.push in the central repo.
    • Cloud Storage (S3/GCS/BigQuery): Use the respective schemes and perform badge generation via on.schedule in the central repo.
  6. Use octocov on the Terminal

    main

    You can use octocov as a local code metrics viewer. After generating a coverage report (e.g., go test ./... -coverprofile=coverage.out), use the following commands:

    • octocov ls-files: Lists files logged in the code coverage report.
    • octocov view [FILE...]: Views the coverage report for specific files (alias: octocov cat).
    • octocov diff [REPORT_A] [REPORT_B]: Compares two reports.
    $ go test ./... -coverprofile=coverage.out
    $ octocov ls-files
    $ octocov view path/to/file.go
  7. Install octocov

    main

    Choose the installation method that matches your environment.

    Debian/Ubuntu (deb)

    export OCTOCOV_VERSION=X.X.X
    curl -o octocov.deb -L https://github.com/k1LoW/octocov/releases/download/v$OCTOCOV_VERSION/octocov_$OCTOCOV_VERSION-1_amd64.deb
    dpkg -i octocov.deb

    RedHat/CentOS (RPM)

    export OCTOCOV_VERSION=X.X.X
    yum install https://github.com/k1LoW/octocov/releases/download/v$OCTOCOV_VERSION/octocov_$OCTOCOV_VERSION-1_amd64.rpm

    Alpine (apk)

    export OCTOCOV_VERSION=X.X.X
    curl -o octocov.apk -L https://github.com/k1LoW/octocov/releases/download/v$OCTOCOV_VERSION/octocov_$OCTOCOV_VERSION-1_amd64.apk
    apk add octocov.apk

    Homebrew

    brew install k1LoW/tap/octocov

    Aqua

    aqua g -i k1LoW/octocov

    Go

    go install github.com/k1LoW/octocov@latest

    Docker

    docker pull ghcr.io/k1low/octocov:latest
  8. Get started with octocov on GitHub Actions

    main

    To use octocov in your CI pipeline, follow these steps:

    1. Generate coverage output: Run your tests with a coverage report output (e.g., for Go, use -coverprofile=coverage.out).
    2. Initialize configuration: Run octocov init to generate a .octocov.yml file in your repository.
    3. Configure Workflow: Add a step to your GitHub Actions workflow using k1LoW/octocov-action@v1.

    Note: octocov can only comment on pull requests originating from the same repository due to GitHub token permissions. However, reporting to GitHub Actions Job Summaries is always permitted.

    # .github/workflows/ci.yml
    name: Test
    
    on:
      pull_request:
    
    jobs:
      test:
        runs-on: ubuntu-latest
        steps:
          - 
            uses: actions/checkout@v3
          - 
            uses: actions/setup-go@v4
            with:
              go-version-file: go.mod
          - 
            name: Run tests with coverage report output
            run: go test ./... -coverprofile=coverage.out
          - 
            uses: k1LoW/octocov-action@v1
  9. Merge coverage reports from multiple jobs

    main

    If you use test sharding (e.g., a matrix strategy in GitHub Actions), you can aggregate the results into a single report.

    1. In the test jobs: Upload each shard's coverage file as a unique GitHub Action artifact (e.g., octocov-1, octocov-2).
    2. In the aggregation job:
      • Use actions/download-artifact with a pattern to download all shards.
      • In .octocov.yml, list the artifact names under coverage.paths.

    This allows octocov to combine coverage from multiple environments or shards into one unified metric.

    # .octocov.yml for aggregation
    coverage:
      paths:
        - "octocov-1"
        - "octocov-2"
        - "octocov-3"
  10. Automated CI features in octocov

    main

    When running in a CI environment (CI=true), octocov can perform several automated tasks based on your .octocov.yml configuration:

    • Metric Measurement: Calculates coverage, code-to-test ratio, and test execution time.
    • Badge Generation: Creates visual badges for coverage, code-to-test ratio, and test execution time.
    • Report Comparison (Diffing): Compares current metrics against the latest report stored in configured datastores to detect regressions.
    • Pull Request Integration:
      • Comments the report on the PR.
      • Inserts the report into the PR body.
      • Adds the report to the GitHub Actions job summary.
    • Data Storage: Stores reports in configured datastores (e.g., BigQuery, local files).
    • Git Automation: Pushes generated files (like badges or reports) back to the repository using local Git.
    • Acceptance Checks: Validates if the measured metrics meet the acceptable thresholds defined in the config.
  11. Generate and push coverage report badges

    main

    You can automatically generate SVG badges for your metrics and save them to a path. You can also use the push: key to automatically git push these badges.

    Supported metrics for badges:

    • coverage.badge.path
    • codeToTestRatio.badge.path
    • testExecutionTime.badge.path
    coverage:
      badge:
        path: docs/coverage.svg
    
    codeToTestRatio:
      badge:
        path: docs/ratio.svg
    
    testExecutionTime:
      badge:
        path: docs/time.svg
    
    push:
  12. Configure test execution time monitoring

    main

    Use testExecutionTime to monitor how long tests take to run.

    • testExecutionTime.steps: (List) Names of the steps to measure. If omitted, the step that generates the coverage report is used.
    • testExecutionTime.acceptable: (Expression) Condition for acceptable time (e.g., 1min or current <= 1min && diff <= 1sec).
    • testExecutionTime.badge.path: (String) Path to generate the time badge.
    • testExecutionTime.if: (Expression) Condition for measuring time.
    testExecutionTime:
      steps:
        - Run test
        - Run slow test
      acceptable: 1min
      badge:
        path: docs/time.svg