|
| 1 | +# Copyright 2025 NVIDIA CORPORATION |
| 2 | +# SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +name: KAI Scheduler - Coverage Gate |
| 5 | + |
| 6 | +on: |
| 7 | + workflow_run: |
| 8 | + workflows: ["KAI Scheduler - Pull Request"] |
| 9 | + types: |
| 10 | + - completed |
| 11 | + pull_request: |
| 12 | + types: [labeled, unlabeled] |
| 13 | + |
| 14 | +jobs: |
| 15 | + coverage-gate: |
| 16 | + name: Coverage Gate |
| 17 | + runs-on: ubuntu-latest |
| 18 | + if: >- |
| 19 | + (github.event_name == 'workflow_run' && |
| 20 | + github.event.workflow_run.event == 'pull_request' && |
| 21 | + github.event.workflow_run.conclusion == 'success') || |
| 22 | + github.event_name == 'pull_request' |
| 23 | + permissions: |
| 24 | + actions: read |
| 25 | + contents: read |
| 26 | + issues: read |
| 27 | + pull-requests: read |
| 28 | + |
| 29 | + steps: |
| 30 | + - name: Download PR number artifact |
| 31 | + if: github.event_name == 'workflow_run' |
| 32 | + continue-on-error: true |
| 33 | + uses: actions/download-artifact@v8 |
| 34 | + with: |
| 35 | + name: pr-number-for-comment |
| 36 | + run-id: ${{ github.event.workflow_run.id }} |
| 37 | + github-token: ${{ secrets.GITHUB_TOKEN }} |
| 38 | + |
| 39 | + - name: Download coverage report artifact |
| 40 | + if: github.event_name == 'workflow_run' |
| 41 | + continue-on-error: true |
| 42 | + uses: actions/download-artifact@v8 |
| 43 | + with: |
| 44 | + name: coverage-report-for-comment |
| 45 | + run-id: ${{ github.event.workflow_run.id }} |
| 46 | + github-token: ${{ secrets.GITHUB_TOKEN }} |
| 47 | + |
| 48 | + - name: Resolve coverage gate state |
| 49 | + id: coverage_state |
| 50 | + env: |
| 51 | + EVENT_NAME: ${{ github.event_name }} |
| 52 | + GH_TOKEN: ${{ github.token }} |
| 53 | + LABEL_EVENT_PR_NUMBER: ${{ github.event.pull_request.number }} |
| 54 | + LABEL_EVENT_HAS_SKIP_LABEL: ${{ github.event_name == 'pull_request' && contains(github.event.pull_request.labels.*.name, 'skip-coverage-gate') }} |
| 55 | + REPOSITORY: ${{ github.repository }} |
| 56 | + run: | |
| 57 | + if [ "$EVENT_NAME" = "workflow_run" ]; then |
| 58 | + if [ ! -s pr_number.txt ] || [ ! -s coverage-report.txt ]; then |
| 59 | + echo "No coverage report artifact found. Skipping coverage gate." |
| 60 | + echo "has_delta=false" >> $GITHUB_OUTPUT |
| 61 | + exit 0 |
| 62 | + fi |
| 63 | +
|
| 64 | + PR_NUMBER=$(cat pr_number.txt) |
| 65 | + DELTA=$(grep -Eo 'delta -?[0-9]+([.][0-9]+)?%' coverage-report.txt | head -1 | grep -Eo -- '-?[0-9]+([.][0-9]+)?' || true) |
| 66 | +
|
| 67 | + if gh api "repos/${REPOSITORY}/issues/${PR_NUMBER}" --jq '.labels[].name' | grep -Fxq 'skip-coverage-gate'; then |
| 68 | + HAS_SKIP_LABEL=true |
| 69 | + else |
| 70 | + HAS_SKIP_LABEL=false |
| 71 | + fi |
| 72 | + else |
| 73 | + PR_NUMBER="$LABEL_EVENT_PR_NUMBER" |
| 74 | + HAS_SKIP_LABEL="$LABEL_EVENT_HAS_SKIP_LABEL" |
| 75 | + COMMENTS=$(gh api "repos/${REPOSITORY}/issues/${PR_NUMBER}/comments" --paginate) |
| 76 | + DELTA=$(echo "$COMMENTS" | jq -r ' |
| 77 | + [ |
| 78 | + .[] |
| 79 | + | select(.user.login == "github-actions[bot]") |
| 80 | + | select(.body | contains("<!-- code-coverage-report-comment -->")) |
| 81 | + ] |
| 82 | + | sort_by(.updated_at) |
| 83 | + | last |
| 84 | + | .body? // "" |
| 85 | + | (capture("delta (?<delta>-?[0-9]+([.][0-9]+)?)%")? // {}) |
| 86 | + | .delta // empty |
| 87 | + ') |
| 88 | + fi |
| 89 | +
|
| 90 | + if [ -z "$DELTA" ] || [ "$DELTA" = "null" ]; then |
| 91 | + echo "No existing coverage delta found. Coverage will be evaluated on the next code-change run." |
| 92 | + exit 0 |
| 93 | + fi |
| 94 | +
|
| 95 | + echo "delta=$DELTA" >> $GITHUB_OUTPUT |
| 96 | + echo "has_skip_label=$HAS_SKIP_LABEL" >> $GITHUB_OUTPUT |
| 97 | +
|
| 98 | + if awk -v delta="$DELTA" 'BEGIN { exit !((delta + 0) < 0) }'; then |
| 99 | + echo "delta_negative=true" >> $GITHUB_OUTPUT |
| 100 | + else |
| 101 | + echo "delta_negative=false" >> $GITHUB_OUTPUT |
| 102 | + fi |
| 103 | +
|
| 104 | + - name: Fail on coverage regression |
| 105 | + if: steps.coverage_state.outputs.delta_negative == 'true' && steps.coverage_state.outputs.has_skip_label != 'true' |
| 106 | + run: | |
| 107 | + echo "Coverage regression detected: delta=${{ steps.coverage_state.outputs.delta }}%" |
| 108 | + echo "Add the skip-coverage-gate label to bypass this gate when appropriate." |
| 109 | + exit 1 |
0 commit comments