|
| 1 | +name: PR Governance |
| 2 | + |
| 3 | +on: |
| 4 | + pull_request: |
| 5 | + types: [opened, edited, synchronize, reopened, ready_for_review] |
| 6 | + branches: [main] |
| 7 | + |
| 8 | +permissions: |
| 9 | + contents: read |
| 10 | + pull-requests: read |
| 11 | + |
| 12 | +jobs: |
| 13 | + enforce-governance: |
| 14 | + runs-on: ubuntu-latest |
| 15 | + steps: |
| 16 | + - name: Enforce Sonar quality gate and new issues policy |
| 17 | + env: |
| 18 | + PR_NUMBER: ${{ github.event.pull_request.number }} |
| 19 | + SONAR_ORGANIZATION: ${{ vars.SONAR_ORGANIZATION || github.repository_owner }} |
| 20 | + SONAR_PROJECT_KEY: ${{ vars.SONAR_PROJECT_KEY || replace(github.repository, '/', '_') }} |
| 21 | + SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }} |
| 22 | + SONAR_MAX_POLL_ITERATIONS: "30" |
| 23 | + SONAR_POLL_INTERVAL_SECONDS: "10" |
| 24 | + run: | |
| 25 | + set -euo pipefail |
| 26 | +
|
| 27 | + MAX_POLL_ITERATIONS="${SONAR_MAX_POLL_ITERATIONS:-30}" |
| 28 | + POLL_INTERVAL_SECONDS="${SONAR_POLL_INTERVAL_SECONDS:-10}" |
| 29 | + SONAR_AUTH_ARGS=() |
| 30 | + if [[ -n "${SONAR_TOKEN:-}" ]]; then |
| 31 | + SONAR_AUTH_ARGS=(-u "${SONAR_TOKEN}:") |
| 32 | + fi |
| 33 | +
|
| 34 | + quality_status="" |
| 35 | + attempt=1 |
| 36 | + while (( attempt <= MAX_POLL_ITERATIONS )); do |
| 37 | + response="$( |
| 38 | + curl -sS "${SONAR_AUTH_ARGS[@]}" \ |
| 39 | + "https://sonarcloud.io/api/project_pull_requests/list?organization=${SONAR_ORGANIZATION}&project=${SONAR_PROJECT_KEY}" |
| 40 | + )" |
| 41 | + quality_status="$(jq -r --arg pr "${PR_NUMBER}" '.pullRequests[]? | select(.key == $pr) | .status.qualityGateStatus' <<<"$response")" |
| 42 | +
|
| 43 | + if [[ -n "$quality_status" && "$quality_status" != "NONE" ]]; then |
| 44 | + break |
| 45 | + fi |
| 46 | +
|
| 47 | + sleep "${POLL_INTERVAL_SECONDS}" |
| 48 | + attempt=$((attempt + 1)) |
| 49 | + done |
| 50 | +
|
| 51 | + if [[ -z "$quality_status" || "$quality_status" == "NONE" ]]; then |
| 52 | + echo "Sonar result not ready for PR #${PR_NUMBER}." >&2 |
| 53 | + exit 1 |
| 54 | + fi |
| 55 | +
|
| 56 | + if [[ "$quality_status" != "OK" ]]; then |
| 57 | + echo "Sonar quality gate must pass. Current status: ${quality_status}" >&2 |
| 58 | + exit 1 |
| 59 | + fi |
| 60 | +
|
| 61 | + issues_response="$( |
| 62 | + curl -sS "${SONAR_AUTH_ARGS[@]}" \ |
| 63 | + "https://sonarcloud.io/api/issues/search?organization=${SONAR_ORGANIZATION}&componentKeys=${SONAR_PROJECT_KEY}&pullRequest=${PR_NUMBER}&issueStatuses=OPEN,CONFIRMED&sinceLeakPeriod=true&ps=1" |
| 64 | + )" |
| 65 | + new_issues="$(jq -r '.total // 0' <<<"$issues_response")" |
| 66 | +
|
| 67 | + if [[ "$new_issues" != "0" ]]; then |
| 68 | + echo "PR introduces ${new_issues} Sonar new issue(s). New issues must be 0." >&2 |
| 69 | + exit 1 |
| 70 | + fi |
| 71 | +
|
| 72 | + echo "Sonar checks passed: quality gate OK and 0 new issues." |
| 73 | +
|
| 74 | + - name: Enforce inline review replies and resolved threads |
| 75 | + env: |
| 76 | + PR_NUMBER: ${{ github.event.pull_request.number }} |
| 77 | + REPO_OWNER: ${{ github.repository_owner }} |
| 78 | + REPO_NAME: ${{ github.event.repository.name }} |
| 79 | + GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 80 | + run: | |
| 81 | + set -euo pipefail |
| 82 | +
|
| 83 | + query=' |
| 84 | + query($owner:String!, $repo:String!, $number:Int!) { |
| 85 | + repository(owner:$owner, name:$repo) { |
| 86 | + pullRequest(number:$number) { |
| 87 | + author { login } |
| 88 | + reviewThreads(first:100) { |
| 89 | + pageInfo { |
| 90 | + hasNextPage |
| 91 | + } |
| 92 | + nodes { |
| 93 | + id |
| 94 | + isResolved |
| 95 | + isOutdated |
| 96 | + comments(first:100) { |
| 97 | + pageInfo { |
| 98 | + hasNextPage |
| 99 | + } |
| 100 | + nodes { |
| 101 | + author { login } |
| 102 | + } |
| 103 | + } |
| 104 | + } |
| 105 | + } |
| 106 | + } |
| 107 | + } |
| 108 | + }' |
| 109 | +
|
| 110 | + response="$(gh api graphql -f query="$query" -F owner="$REPO_OWNER" -F repo="$REPO_NAME" -F number="$PR_NUMBER")" |
| 111 | + pr_author="$(jq -r '.data.repository.pullRequest.author.login' <<<"$response")" |
| 112 | +
|
| 113 | + has_more_threads="$(jq -r '.data.repository.pullRequest.reviewThreads.pageInfo.hasNextPage' <<<"$response")" |
| 114 | + has_more_comments="$(jq -r ' |
| 115 | + .data.repository.pullRequest.reviewThreads.nodes |
| 116 | + | any(.comments.pageInfo.hasNextPage == true) |
| 117 | + ' <<<"$response")" |
| 118 | +
|
| 119 | + if [[ "$has_more_threads" == "true" || "$has_more_comments" == "true" ]]; then |
| 120 | + echo "Review thread pagination limit reached; increase pagination handling before enforcing this check." >&2 |
| 121 | + exit 1 |
| 122 | + fi |
| 123 | +
|
| 124 | + unresolved_count="$(jq -r ' |
| 125 | + .data.repository.pullRequest.reviewThreads.nodes |
| 126 | + | map(select(.isOutdated | not)) |
| 127 | + | map(select(.isResolved | not)) |
| 128 | + | length |
| 129 | + ' <<<"$response")" |
| 130 | +
|
| 131 | + missing_inline_reply_count="$(jq -r --arg author "$pr_author" ' |
| 132 | + .data.repository.pullRequest.reviewThreads.nodes |
| 133 | + | map(select(.isOutdated | not)) |
| 134 | + | map(select(([.comments.nodes[]?.author.login] | index($author)) | not)) |
| 135 | + | length |
| 136 | + ' <<<"$response")" |
| 137 | +
|
| 138 | + if [[ "$missing_inline_reply_count" != "0" ]]; then |
| 139 | + echo "Each active review thread must include an inline reply from PR author (${pr_author})." >&2 |
| 140 | + exit 1 |
| 141 | + fi |
| 142 | +
|
| 143 | + if [[ "$unresolved_count" != "0" ]]; then |
| 144 | + echo "All active review threads must be resolved before merge." >&2 |
| 145 | + exit 1 |
| 146 | + fi |
| 147 | +
|
| 148 | + echo "Review thread checks passed." |
0 commit comments