| # Licensed to the Apache Software Foundation (ASF) under one | |
| # or more contributor license agreements. See the NOTICE file | |
| # distributed with this work for additional information | |
| # regarding copyright ownership. The ASF licenses this file | |
| # to you under the Apache License, Version 2.0 (the | |
| # "License"); you may not use this file except in compliance | |
| # with the License. You may obtain a copy of the License at | |
| # | |
| # http://www.apache.org/licenses/LICENSE-2.0 | |
| # | |
| # Unless required by applicable law or agreed to in writing, | |
| # software distributed under the License is distributed on an | |
| # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | |
| # KIND, either express or implied. See the License for the | |
| # specific language governing permissions and limitations | |
| # under the License. | |
| name: Coverage | |
| on: | |
| pull_request: | |
| # A run on main posts the totals that later pull requests are measured against. | |
| push: | |
| branches: [main] | |
| permissions: | |
| contents: read | |
| # To post the totals as a commit status. | |
| statuses: write | |
| # A new push to a pull request cancels its previous run. Runs on main are neither cancelled nor queued behind | |
| # one another, so that every commit there gets its totals posted. | |
| concurrency: | |
| group: ${{ github.workflow }}-${{ github.event.pull_request.number || github.sha }} | |
| cancel-in-progress: true | |
| env: | |
| CARGO_TERM_COLOR: always | |
| # An exact version, where ci.yml tracks stable, because this job does not just measure: it compares its number | |
| # with the one an earlier run posted. Coverage instrumentation differs between compiler versions, so on stable | |
| # a new Rust release moves the number under code that did not change, and the gate fails for it. | |
| # | |
| # Bump it deliberately. The pull request that does is measured by one compiler against a base measured by | |
| # another, and may show a step that is the instrument moving and not the code. | |
| RUST_VERSION: "1.100.0" | |
| jobs: | |
| coverage: | |
| name: Test coverage | |
| runs-on: ubuntu-latest | |
| steps: | |
| - uses: actions/checkout@v4 | |
| - run: | | |
| rustup toolchain install "$RUST_VERSION" --profile minimal --component llvm-tools-preview | |
| rustup default "$RUST_VERSION" | |
| - run: cargo install --locked cargo-llvm-cov | |
| # What is measured is what a bare `cargo test` runs: the workspace's default members. The binaries under | |
| # checks/ and xtask are not among them, because running them is the test and they have nothing a | |
| # `cargo test` could assert. One added later stays out the same way, instead of arriving at 0% and failing | |
| # this job for a change that could not have caused the drop. | |
| # | |
| # tools/perf is a default member, so that `cargo build --release` builds `pulsar-perf`, but it is a binary | |
| # of the same kind: it is left out of the report instead. | |
| # | |
| # The generated protobuf code is left out of the report: there is far more of it than code written by hand, | |
| # most of it for commands the client does not use yet, and the total would measure little else. | |
| - name: Run the tests under cargo-llvm-cov | |
| run: > | |
| cargo llvm-cov --locked | |
| --json --output-path target/coverage.json | |
| --ignore-filename-regex '/apache-pulsar-protocol/src/proto/|/tools/perf/' | |
| - name: Summarize | |
| id: summary | |
| run: python3 .github/scripts/coverage-summary.py target/coverage.json | |
| # A request to GitHub's API fails now and then, at times after GitHub has carried it out. Posting the status | |
| # again does no harm: GitHub shows a context's newest status, and the gate reads the newest too. So the step | |
| # tries three times before it fails the job. | |
| - name: Post the totals as a commit status | |
| if: github.event_name == 'push' || github.event.pull_request.head.repo.full_name == github.repository | |
| env: | |
| GH_TOKEN: ${{ github.token }} | |
| SHA: ${{ github.event.pull_request.head.sha || github.sha }} | |
| TOTALS: ${{ steps.summary.outputs.totals }} | |
| run: | | |
| for _ in 1 2 3; do | |
| gh api "repos/$GITHUB_REPOSITORY/statuses/$SHA" \ | |
| -f state=success -f context=coverage -f description="$TOTALS" \ | |
| -f target_url="$GITHUB_SERVER_URL/$GITHUB_REPOSITORY/actions/runs/$GITHUB_RUN_ID" && exit 0 | |
| sleep 10 | |
| done | |
| exit 1 | |
| # What this branch is measured against: the totals this workflow posted as a status on the pull request's | |
| # base commit. Nothing is stored in the repository for it, so there is no baseline file to go stale and | |
| # nothing to bump by hand when coverage improves. | |
| # | |
| # A base with no status, such as the first run after this workflow lands, leaves `lines` empty and the gate | |
| # below is skipped rather than guessing. | |
| - name: Find the coverage its base recorded | |
| id: base | |
| if: github.event_name == 'pull_request' | |
| env: | |
| GH_TOKEN: ${{ github.token }} | |
| BASE: ${{ github.event.pull_request.base.sha }} | |
| run: | | |
| posted=$(gh api "repos/$GITHUB_REPOSITORY/commits/$BASE/statuses" \ | |
| --jq '[.[] | select(.context == "coverage")] | first | .description // empty' \ | |
| 2>/dev/null || true) | |
| echo "posted: ${posted:-<none>}" | |
| lines=$(printf '%s' "$posted" | sed -n 's/^lines \([0-9.]*\)%.*/\1/p') | |
| echo "lines=$lines" >> "$GITHUB_OUTPUT" | |
| # After the status above, so the number is recorded whichever way this goes: a run that fails here has | |
| # still said what the coverage was. | |
| - name: Fail if the coverage dropped | |
| if: github.event_name == 'pull_request' && steps.base.outputs.lines != '' | |
| run: > | |
| python3 .github/scripts/coverage-summary.py target/coverage.json | |
| --fail-under '${{ steps.base.outputs.lines }}' | |
| # A pull request from a fork gets a read-only token, which can't post a status. It is still gated: reading | |
| # its base's status needs no more than that. | |
| - name: Note that a fork gets no status | |
| if: github.event_name == 'pull_request' && github.event.pull_request.head.repo.full_name != github.repository | |
| run: > | |
| echo "This pull request comes from a fork, whose token can't post the totals | |
| as a status." >> "$GITHUB_STEP_SUMMARY" |