Repository navigation
feat(ci,codequality): post sticky PR quality reports via check_run workflow - #1273
Conversation
There was a problem hiding this comment.
Code Review
This pull request introduces a mechanism to post and update sticky code quality reports as PR comments from Google Cloud Build. It adds a new bash script scripts/post-codequality-pr-comment.sh to extract Base64-encoded report envelopes from Check Run logs and upsert them to GitHub, along with corresponding Go CLI flags, formatting logic, and comprehensive unit tests. Feedback on the PR highlights a potential SIGPIPE crash (exit code 141) in the bash script when using head -n 1 in a pipeline with set -o pipefail, suggesting a safer way to extract the first comment ID using bash parameter expansion.
| local existing_id | ||
| existing_id="$( | ||
| gh api --paginate "repos/${GITHUB_REPO}/issues/${pr_number}/comments" \ | ||
| --jq ".[] | select(.body | contains(\"${COMMENT_MARKER}\")) | .id" \ | ||
| | head -n 1 | ||
| )" |
There was a problem hiding this comment.
Using head -n 1 in a pipeline with set -o pipefail can cause the script to crash with a SIGPIPE error (exit code 141) if the upstream command (gh api) produces more output than head reads. This is highly likely to happen on PRs with many comments. To prevent this, fetch all matching comment IDs into a variable and extract the first ID using bash parameter expansion, which avoids the pipeline and SIGPIPE entirely.
| local existing_id | |
| existing_id="$( | |
| gh api --paginate "repos/${GITHUB_REPO}/issues/${pr_number}/comments" \ | |
| --jq ".[] | select(.body | contains(\"${COMMENT_MARKER}\")) | .id" \ | |
| | head -n 1 | |
| )" | |
| local existing_ids | |
| existing_ids="$(gh api --paginate "repos/${GITHUB_REPO}/issues/${pr_number}/comments" --jq ".[] | select(.body | contains(\"${COMMENT_MARKER}\")) | .id")" | |
| local existing_id="${existing_ids%%$'\n'*}" |
cf77ff4 to
b65e745
Compare
Summary
Adds a token-free sticky GitHub PR comment reporter for the Cloud Build
github-codequalitycheck using GitHub Actions' nativeon: check_runevent and built-insecrets.GITHUB_TOKEN(github-actions[bot]) — requiring zero Personal Access Tokens, GitHub Apps, or Secret Manager secrets.How It Works
scripts/codequality,scripts/run-codequality-ci.sh,.cloudbuild/github-codequality.yaml):--comment-envelopeis enabled (automatically enabled in Cloud Build whenBUILD_IDis present),codequalityappends a Base64-encoded Markdown summary (<!-- khi-codequality-report -->+RenderGitHubSummary+ commit SHA and Cloud Build log link, capped at 24 KB raw / 32 KB Base64) delimited byKHI_CODEQUALITY_B64_BEGINandKHI_CODEQUALITY_B64_END.INCLUDE_BUILD_LOGS_WITH_STATUSattaches this output directly to the GitHub Check Run (output.text).check_runComment Poster (.github/workflows/codequality-comment.yaml,scripts/post-codequality-pr-comment.sh):check_run: [completed]whenapp.slug == 'google-cloud-build'andstartsWith(name, 'github-codequality').scriptsonly) on the default branch (main) andsecrets.GITHUB_TOKEN(permissions: pull-requests: write, checks: read).head.shamatchescheck_run.head_sha(preventing stale out-of-order runs from overwriting newer commit reports), decodes the embedded Base64 Markdown report (or generates a fallback summary if Cloud Build failed beforecodequalityran), and creates orPATCH-updates the single sticky comment (<!-- khi-codequality-report -->).Verification
go test ./scripts/...(including hermeticpost-codequality-pr-comment.shintegration tests):PASSmake codequality-ci:PASS (0 violations)— Patch coverage100.0%(79/79), Mutation score100.0%(29/29killed)make pre-commit:PASS