Skip to content

Post Coverage Comment #49

Post Coverage Comment

Post Coverage Comment #49

# Copyright 2025 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# This workflow is independent as it requires write permissions, which GitHub blocks
# on forks for security reasons. Thus, in order to be able to post comments with
# code coverage details on PRs, we run a separate workflow in the context of the
# base repository after any PR build workflow completes, relying on files uploaded
# by the PR's build.
name: Post Coverage Comment
on:
workflow_run:
workflows: ["Build and Test"]
types:
- completed
jobs:
post-comment:
runs-on: ubuntu-latest
# This workflow runs in the context of the base repository, so it has write permissions
# even when the triggering workflow was from a fork.
# We run even if the build or thresholds failed, so long as it wasn't cancelled.
if: >
github.event.workflow_run.event == 'pull_request' &&
github.event.workflow_run.conclusion != 'cancelled'
permissions:
pull-requests: write
steps:
- name: Download coverage reports artifact
uses: actions/download-artifact@37930b1c2abaa49bbe596cd826c3c89aef350131 # ratchet:actions/download-artifact@v7
continue-on-error: true # Artifact might be missing on very early failures
with:
name: coverage-reports
github-token: ${{ secrets.GITHUB_TOKEN }}
run-id: ${{ github.event.workflow_run.id }}
- name: Check for coverage files
# We use an explicit step to check for file existence and set outputs.
# This is more robust than using hashFiles() in an 'if' expression,
# as hashFiles() is primarily intended for cache keys and lacks
# a dedicated file_exists() equivalent in GitHub Actions expressions.
id: check_files
run: |
if [ -f pr_number.txt ]; then
echo "pr_number=$(cat pr_number.txt)" >> $GITHUB_OUTPUT
echo "pr_found=true" >> $GITHUB_OUTPUT
else
echo "pr_found=false" >> $GITHUB_OUTPUT
fi
if [ -f python-code-coverage-results.md ]; then
echo "python_found=true" >> $GITHUB_OUTPUT
else
echo "python_found=false" >> $GITHUB_OUTPUT
fi
if [ -f cpp-code-coverage-results.md ]; then
echo "cpp_found=true" >> $GITHUB_OUTPUT
else
echo "cpp_found=false" >> $GITHUB_OUTPUT
fi
- name: Post Python Coverage PR Comment
if: steps.check_files.outputs.pr_found == 'true' && steps.check_files.outputs.python_found == 'true'
uses: marocchino/sticky-pull-request-comment@773744901bac0e8cbb5a0dc842800d45e9b2b405 # ratchet:marocchino/sticky-pull-request-comment@v2
with:
recreate: true
number: ${{ steps.check_files.outputs.pr_number }}
path: python-code-coverage-results.md
- name: Post C++ Coverage PR Comment
if: steps.check_files.outputs.pr_found == 'true' && steps.check_files.outputs.cpp_found == 'true'
uses: marocchino/sticky-pull-request-comment@773744901bac0e8cbb5a0dc842800d45e9b2b405 # ratchet:marocchino/sticky-pull-request-comment@v2
with:
header: cpp-coverage
recreate: true
number: ${{ steps.check_files.outputs.pr_number }}
path: cpp-code-coverage-results.md