Skip to main content
Reference tooling 4 min read Updated

minikin-cargo-crap-main-docs-guides-github-action

GitHub Action

Since 0.8.0.

One step scores the crate, compares a pull request with its base, comments on it and gates:

# .github/workflows/crap.yml
name: CRAP

on:
  push:
    branches: [main]
  pull_request:

permissions:
  contents: read
  pull-requests: write

jobs:
  crap:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v6
      - uses: minikin/cargo-crap@v1

It works on Linux, macOS and Windows runners. It downloads a released cargo-crap binary and checks it against the release’s SHA256SUMS, so nothing is compiled but your own tests.

What it does

On a push, it runs your tests under cargo llvm-cov, scores the crate, and writes the report to the job summary. It fails only when fail-above is true and a function scores above the threshold.

On a pull request, it first scores the pull request’s base commit in the same job: it checks that commit out in place, runs the tests and records a baseline, then checks the pull request out again. The comparison is exact and needs no workflow on main, no stored artifact and no extra permission. The cost is a second test run. Then it:

  1. writes the delta report to the job summary,
  2. posts it as a comment on the pull request, or updates the one it posted before,
  3. fails the step when a function’s score rose, after commenting.

The links in the report name the pull request’s head commit, which GitHub keeps after the pull request closes. A line number can be a few lines off when the base branch changed the same file after the branch point.

A pull request from a fork has a read-only token, which cannot comment. The report is then in the job summary, the step warns once, and it still gates. Keep the Action on pull_request. It runs the pull request’s build scripts and tests in the job that holds the token, and nothing inside the Action can keep that code from the token. On pull_request a fork’s token is read-only, and a branch’s author can already write. Under pull_request_target the token can write, so checking out a fork’s code there hands that code a write token.

The base-commit run uses the pull request’s .cargo-crap.toml, so a pull request that changes the threshold or the duplicate settings is compared under one set of rules. Local edits a step made to tracked files before the Action ran are kept, and the pull request is checked out again even when the base commit fails to build.

Inputs

InputDefaultMeaning
versionthe release this tag was cut withcargo-crap release to install, or latest
binarynonea cargo-crap binary to use instead of downloading one
working-directory.where the crate is
argsnoneextra arguments for every cargo-crap run, as shell words
coverage-argsnoneextra arguments for cargo llvm-cov
lcovnonean existing LCOV file, used instead of a coverage run
baselinenonea baseline JSON file, used instead of the base-commit run
basethe pull request’s base committhe commit to compare with
commenttruepost or update the PR comment
comment-marker<!-- cargo-crap-report -->the hidden first line that finds the comment again
fail-regressiontruefail when a score rose, whenever there is something to compare with
fail-abovefalsefail when a function scores above the threshold
github-token${{ github.token }}the token that writes the comment

binary, lcov and baseline are relative to working-directory. args and coverage-args are split as the shell would split them, and a stray quote fails the step rather than dropping the arguments. The threshold and everything else come from .cargo-crap.toml, as on the command line. lcov on a pull request leaves the base commit with no coverage to score, so give baseline with it, or set fail-regression to false.

Outputs

OutputMeaning
crappyhow many functions score above the threshold
regressedon a pull request, how many functions’ scores rose
reportthe path of the markdown report

One comment per pull request

The default marker is the hidden line --format pr-comment starts with, and the one the hand-written workflow in PR comment bot looks for. Keep one of the two on a repository, or give the Action its own comment-marker. Two Action steps that comment on one pull request need a marker each.

Duplicates and triage

With [duplicates] enabled in .cargo-crap.toml, the comment lists only the duplicates the pull request adds, since the base-commit run records the base’s (see Finding duplicates). For triage, put the provider’s key in the step’s environment:

      - uses: minikin/cargo-crap@v1
        env:
          OPENAI_API_KEY: ${{ secrets.OPENAI_API_KEY }}

Only the run that writes the report sends triage requests. The other cargo-crap runs in the job clear every provider key, so max-requests is spent once per job.

Versions

@v1 moves to each 0.x release and installs that release’s cargo-crap by default. To stay on one cargo-crap while v1 moves, set version. An input that is removed or renamed starts v2.

#119 runs this Action on the README hero’s demo crate.