-
Notifications
You must be signed in to change notification settings - Fork 1
fix(dev-lead): handle SonarQube Quality Gate failures (issue #279) #280
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,62 @@ | ||
| #!/usr/bin/env bash | ||
| set -euo pipefail | ||
| # dev-lead-ci-relay.sh — Resolves the PR associated with a check_run and writes | ||
| # relay decision to GITHUB_OUTPUT. | ||
| # | ||
| # Env inputs: | ||
| # CHECK_RUN_PRS — JSON array from check_run.pull_requests (may be []) | ||
| # CHECK_RUN_HEAD_SHA — commit SHA from the check_run event | ||
| # REPO_FULL_NAME — "owner/repo" string | ||
| # GITHUB_OUTPUT — path to the GitHub Actions output file (or /dev/null) | ||
| # | ||
| # Writes to GITHUB_OUTPUT: | ||
| # should_relay=true|false | ||
| # pr_number=N (only when should_relay=true) | ||
|
|
||
| GITHUB_OUTPUT="${GITHUB_OUTPUT:-/dev/null}" | ||
| CHECK_RUN_PRS="${CHECK_RUN_PRS:-[]}" | ||
| CHECK_RUN_HEAD_SHA="${CHECK_RUN_HEAD_SHA:-}" | ||
| REPO_FULL_NAME="${REPO_FULL_NAME:-}" | ||
|
|
||
| pr_count=$(echo "$CHECK_RUN_PRS" | jq 'length') | ||
|
|
||
| if [ "$pr_count" -eq 0 ]; then | ||
| # GitHub App check_runs (SonarCloud, external CodeQL gates, etc.) do not | ||
| # populate check_run.pull_requests. Fall back to the commits-to-pulls API. | ||
| echo "::notice::check_run.pull_requests is empty — trying commits-to-pulls API fallback for SHA ${CHECK_RUN_HEAD_SHA}" | ||
| pulls_json=$(gh api \ | ||
| -H "Accept: application/vnd.github+json" \ | ||
| "repos/${REPO_FULL_NAME}/commits/${CHECK_RUN_HEAD_SHA}/pulls" 2>/dev/null || echo "[]") | ||
| # Filter to open PRs only — merged PRs can share the same commit SHA. | ||
| open_pulls=$(echo "$pulls_json" | jq '[.[] | select(.state=="open")]') | ||
| pr_count=$(echo "$open_pulls" | jq 'length') | ||
| if [ "$pr_count" -eq 0 ]; then | ||
| echo "::notice::No open PRs found for commit ${CHECK_RUN_HEAD_SHA} — skipping relay" | ||
| echo "should_relay=false" >> "$GITHUB_OUTPUT" | ||
| exit 0 | ||
| fi | ||
| head_repo_full=$(echo "$open_pulls" | jq -r '.[0].head.repo.full_name // empty') | ||
| if [ -n "$head_repo_full" ] && [ "$head_repo_full" != "$REPO_FULL_NAME" ]; then | ||
| echo "::notice::Commit ${CHECK_RUN_HEAD_SHA} belongs to fork (${head_repo_full}) — skipping relay" | ||
| echo "should_relay=false" >> "$GITHUB_OUTPUT" | ||
| exit 0 | ||
| fi | ||
| pr_number=$(echo "$open_pulls" | jq -r '.[0].number') | ||
| echo "should_relay=true" >> "$GITHUB_OUTPUT" | ||
| echo "pr_number=$pr_number" >> "$GITHUB_OUTPUT" | ||
| exit 0 | ||
| fi | ||
|
|
||
| # check_run.pull_requests is populated (GitHub Actions check_run). | ||
| # Fork check: pull_requests[*].head.repo only has id/url/name (not full_name), | ||
| # so compare the API URL against the expected base URL for this repo. | ||
| head_repo_url=$(echo "$CHECK_RUN_PRS" | jq -r '.[0].head.repo.url // empty') | ||
| expected_url="https://api.github.com/repos/${REPO_FULL_NAME}" | ||
| if [ -n "$head_repo_url" ] && [ "$head_repo_url" != "$expected_url" ]; then | ||
| echo "::notice::check_run PR head repo URL ($head_repo_url) does not match expected ($expected_url) — fork, skipping relay" | ||
| echo "should_relay=false" >> "$GITHUB_OUTPUT" | ||
| exit 0 | ||
| fi | ||
| pr_number=$(echo "$CHECK_RUN_PRS" | jq -r '.[0].number') | ||
| echo "should_relay=true" >> "$GITHUB_OUTPUT" | ||
| echo "pr_number=$pr_number" >> "$GITHUB_OUTPUT" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15 changes: 15 additions & 0 deletions
15
tests/dev-lead/fixtures/events/check_run_failure_sonarcloud.json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| { | ||
| "_test_expected_intent": "ci-relay", | ||
| "_test_note": "GitHub App check_run with empty pull_requests — ci-relay must use commits-to-pulls fallback", | ||
| "action": "completed", | ||
| "check_run": { | ||
| "name": "SonarCloud Code Analysis", | ||
| "conclusion": "failure", | ||
| "head_sha": "abc123def456", | ||
| "details_url": "https://sonarcloud.io", | ||
| "app": { "slug": "sonarcloud" }, | ||
| "pull_requests": [] | ||
| }, | ||
| "repository": { "full_name": "petry-projects/.github-private" }, | ||
| "sender": { "login": "sonarcloud[bot]", "type": "Bot" } | ||
| } | ||
|
don-petry marked this conversation as resolved.
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,126 @@ | ||
| #!/usr/bin/env bats | ||
| # Unit tests for scripts/dev-lead-ci-relay.sh | ||
| # Covers: non-empty pull_requests (GHA path), empty pull_requests (App fallback), | ||
| # fork rejection, open-PR filtering, and no-PR-found skip. | ||
|
|
||
| SCRIPT_DIR="$(cd "$(dirname "$BATS_TEST_FILENAME")/../../.." && pwd)" | ||
| CI_RELAY_SCRIPT="$SCRIPT_DIR/scripts/dev-lead-ci-relay.sh" | ||
| GH_STUBS_DIR="$SCRIPT_DIR/tests/dev-lead/fixtures/stubs" | ||
|
|
||
| setup() { | ||
| export GITHUB_OUTPUT | ||
| GITHUB_OUTPUT="$(mktemp)" | ||
| STUB_BIN_DIR="$(mktemp -d)" | ||
| cp "$GH_STUBS_DIR/gh" "$STUB_BIN_DIR/gh" | ||
| chmod +x "$STUB_BIN_DIR/gh" | ||
| export PATH="$STUB_BIN_DIR:$PATH" | ||
| export STUB_BIN_DIR | ||
| export REPO_FULL_NAME="petry-projects/.github-private" | ||
| export CHECK_RUN_HEAD_SHA="abc123def456" | ||
| export GH_STUB_PR_NUMBER="42" | ||
| export GH_STUB_PR_HEAD_REPO="petry-projects/.github-private" | ||
| } | ||
|
|
||
| teardown() { | ||
| rm -f "$GITHUB_OUTPUT" | ||
| rm -rf "$STUB_BIN_DIR" | ||
| } | ||
|
|
||
| _out() { grep "^${1}=" "$GITHUB_OUTPUT" | cut -d= -f2- | head -1; } | ||
|
|
||
| # ── non-empty pull_requests (GitHub Actions check_run) ──────────────────────── | ||
|
|
||
| @test "ci-relay: non-empty pull_requests, same repo → should_relay=true" { | ||
| export CHECK_RUN_PRS='[{"number":42,"head":{"repo":{"url":"https://api.github.com/repos/petry-projects/.github-private"}}}]' | ||
|
|
||
| run bash "$CI_RELAY_SCRIPT" | ||
|
|
||
| [ "$status" -eq 0 ] | ||
| [ "$(_out should_relay)" = "true" ] | ||
| [ "$(_out pr_number)" = "42" ] | ||
| } | ||
|
|
||
| @test "ci-relay: non-empty pull_requests, fork repo URL → should_relay=false" { | ||
| export CHECK_RUN_PRS='[{"number":7,"head":{"repo":{"url":"https://api.github.com/repos/forker/other-repo"}}}]' | ||
|
|
||
| run bash "$CI_RELAY_SCRIPT" | ||
|
|
||
| [ "$status" -eq 0 ] | ||
| [ "$(_out should_relay)" = "false" ] | ||
| } | ||
|
|
||
| # ── empty pull_requests (GitHub App check_run — commits-to-pulls fallback) ──── | ||
|
|
||
| @test "ci-relay: empty pull_requests, API returns open PR → should_relay=true" { | ||
| export CHECK_RUN_PRS='[]' | ||
| export GH_STUB_PR_NUMBER="99" | ||
| export GH_STUB_PR_HEAD_REPO="petry-projects/.github-private" | ||
|
|
||
| run bash "$CI_RELAY_SCRIPT" | ||
|
|
||
| [ "$status" -eq 0 ] | ||
| [ "$(_out should_relay)" = "true" ] | ||
| [ "$(_out pr_number)" = "99" ] | ||
| } | ||
|
|
||
| @test "ci-relay: empty pull_requests, API returns no PRs → should_relay=false" { | ||
| export CHECK_RUN_PRS='[]' | ||
| cat > "$STUB_BIN_DIR/gh" <<'GHEOF' | ||
| #!/usr/bin/env bash | ||
| case "$*" in | ||
| *"commits/"*"/pulls"*) echo "[]" ;; | ||
| *) echo "{}" ;; | ||
| esac | ||
| GHEOF | ||
| chmod +x "$STUB_BIN_DIR/gh" | ||
|
|
||
| run bash "$CI_RELAY_SCRIPT" | ||
|
|
||
| [ "$status" -eq 0 ] | ||
| [ "$(_out should_relay)" = "false" ] | ||
| } | ||
|
|
||
| @test "ci-relay: empty pull_requests, API returns fork PR → should_relay=false" { | ||
| export CHECK_RUN_PRS='[]' | ||
| export GH_STUB_PR_NUMBER="55" | ||
| export GH_STUB_PR_HEAD_REPO="forker/.github-private" | ||
|
|
||
| run bash "$CI_RELAY_SCRIPT" | ||
|
|
||
| [ "$status" -eq 0 ] | ||
| [ "$(_out should_relay)" = "false" ] | ||
| } | ||
|
|
||
| @test "ci-relay: empty pull_requests, API returns only merged PR → should_relay=false" { | ||
| export CHECK_RUN_PRS='[]' | ||
| cat > "$STUB_BIN_DIR/gh" <<'GHEOF' | ||
| #!/usr/bin/env bash | ||
| case "$*" in | ||
| *"commits/"*"/pulls"*) echo '[{"number":33,"state":"closed","head":{"repo":{"full_name":"petry-projects/.github-private"}}}]' ;; | ||
| *) echo "{}" ;; | ||
| esac | ||
| GHEOF | ||
| chmod +x "$STUB_BIN_DIR/gh" | ||
|
|
||
| run bash "$CI_RELAY_SCRIPT" | ||
|
|
||
| [ "$status" -eq 0 ] | ||
| [ "$(_out should_relay)" = "false" ] | ||
| } | ||
|
|
||
| @test "ci-relay: empty pull_requests, API failure → should_relay=false" { | ||
| export CHECK_RUN_PRS='[]' | ||
| cat > "$STUB_BIN_DIR/gh" <<'GHEOF' | ||
| #!/usr/bin/env bash | ||
| case "$*" in | ||
| *"commits/"*"/pulls"*) exit 1 ;; | ||
| *) echo "{}" ;; | ||
| esac | ||
| GHEOF | ||
| chmod +x "$STUB_BIN_DIR/gh" | ||
|
|
||
| run bash "$CI_RELAY_SCRIPT" | ||
|
|
||
| [ "$status" -eq 0 ] | ||
| [ "$(_out should_relay)" = "false" ] | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.