PR Verification triggered by stonedu1011 #113
Workflow file for this run
This file contains 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 is a PR verification workflow that guards PRs against main | |
# by validating code quality and test coverage of changed files | |
name: PR | |
run-name: PR Verification triggered by ${{ github.actor }} | |
env: | |
COV_THRESHOLD: '70' | |
# Controls when the action will run. | |
on: | |
pull_request: | |
branches: [ 'main' ] | |
permissions: | |
contents: read | |
pull-requests: write | |
checks: write | |
# A workflow run is made up of one or more jobs that can run sequentially or in parallel | |
jobs: | |
main: | |
name: Verify PR | |
runs-on: ubuntu-latest | |
steps: | |
- name: "Context" | |
shell: bash | |
env: | |
GITHUB_CONTEXT: ${{ toJson(github) }} | |
run: echo "$GITHUB_CONTEXT" | |
- name: "Checkout Actions" | |
uses: actions/checkout@v4 | |
with: | |
# For security reasons, we only uses actions in "main" branch | |
ref: ${{ github.event.repository.default_branch }} | |
sparse-checkout: .github/actions | |
path: .tmp | |
- name: "Prepare" | |
uses: ./.tmp/.github/actions/prepare | |
with: | |
branch: ${{ github.head_ref }} | |
# Fetch base_ref for later use. We need to detect changed files and only consider test coverage on those files. | |
- name: "Prepare PR Verification" | |
id: prepare_pr | |
uses: ./.tmp/.github/actions/prepare_pr | |
- name: "PR Summary" | |
env: | |
PR_DIFF: ${{ steps.prepare_pr.outputs.diff }} | |
CHANGED_GO_FILES: ${{ steps.prepare_pr.outputs.changed_go_files }} | |
CHANGED_COV_GO_FILES: ${{ steps.prepare_pr.outputs.changed_cov_go_files }} | |
SKIP_COV_CHECK: ${{ steps.prepare_pr.outputs.skip_coverage_check }} | |
run: | | |
echo "Base Branch: ${{ steps.prepare_pr.outputs.base_ref }}" | |
echo "Changed Files(.go): " | |
for f in $CHANGED_GO_FILES; do | |
echo " $f" | |
done | |
echo "To-Be-Tested Files(.go): " | |
for f in $CHANGED_COV_GO_FILES; do | |
echo " $f" | |
done | |
echo "Skip Coverage Check: $SKIP_COV_CHECK" | |
echo "Diff:" | |
echo $PR_DIFF | |
- name: "Test & Code Quality" | |
uses: ./.tmp/.github/actions/verify | |
with: | |
fail_on_lint_issues: 'true' | |
- name: "Calculate PR Test Coverage" | |
id: pr_coverage | |
shell: bash | |
run: | | |
git diff ${{ steps.prepare_pr.outputs.base_ref }}... -U0 --no-color -- pkg/** test/** > dist/pr.diff | |
go install "github.com/seriousben/go-patch-cover/cmd/[email protected]" | |
echo "summary=$(go-patch-cover -o json dist/coverage.out dist/pr.diff)" >> $GITHUB_OUTPUT | |
- name: "Process PR Test Coverage" | |
id: process_pr_coverage | |
shell: bash | |
env: | |
PR_STATEMENTS: ${{ fromJSON(steps.pr_coverage.outputs.summary).patch_num_stmt }} | |
PR_COVER_COUNT: ${{ fromJSON(steps.pr_coverage.outputs.summary).patch_cover_count }} | |
PR_COVERAGE: ${{ fromJSON(steps.pr_coverage.outputs.summary).patch_coverage }} | |
run: | | |
FORMATTED_COV=$(printf "%.1f" $PR_COVERAGE) | |
echo "pr_statements=$PR_STATEMENTS" >> $GITHUB_OUTPUT | |
echo "pr_cover_count=$PR_COVER_COUNT" >> $GITHUB_OUTPUT | |
echo "pr_coverage=$FORMATTED_COV" >> $GITHUB_OUTPUT | |
echo Statements: $PR_STATEMENTS | |
echo Expected: $COV_THRESHOLD | |
echo Actual: $FORMATTED_COV | |
(( $(echo "${PR_STATEMENTS} == 0" | bc) )) || \ | |
(( $(echo "${PR_COVERAGE} >= ${{ env.COV_THRESHOLD }}" | bc) )) | |
- name: "Report PR Test Coverage" | |
if: ${{ !cancelled() }} | |
uses: mshick/add-pr-comment@v2 | |
env: | |
PR_STATEMENTS: ${{ steps.process_pr_coverage.outputs.pr_statements }} | |
PR_COVER_COUNT: ${{ steps.process_pr_coverage.outputs.pr_cover_count }} | |
PR_COVERAGE: ${{ steps.process_pr_coverage.outputs.pr_coverage }} | |
with: | |
message: | | |
| PR Coverage Summary | | | |
|:------------------------|-------:| | |
| Changed Statements | ${{ env.PR_STATEMENTS }} | | |
| Covered Statements | ${{ env.PR_COVER_COUNT }} | | |
| Test Coverage | ${{ env.PR_COVERAGE }}% | | |
PR Verification Succeeded: Coverage >= `${{ env.COV_THRESHOLD }}%` | |
message-failure: | | |
| PR Coverage Summary | | | |
|:------------------------|-------:| | |
| Changed Statements | ${{ env.PR_STATEMENTS }} | | |
| Covered Statements | ${{ env.PR_COVER_COUNT }} | | |
| Test Coverage | ${{ env.PR_COVERAGE }}% | | |
PR Verification Failed: Minimum `${{ env.COV_THRESHOLD }}%` | |