Skip to content

Commit accd3be

Browse files
committed
fix coverage
1 parent 0bd742c commit accd3be

2 files changed

Lines changed: 37 additions & 16 deletions

File tree

.github/workflows/unittest.yml

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -59,21 +59,35 @@ jobs:
5959
steps:
6060
- name: Checkout
6161
uses: actions/checkout@34e114876b0b11c390a56381ad16ebd13914f8d5 # v4.3.1
62-
# Use a fetch-depth of 2 to avoid error `fatal: origin/main...HEAD: no merge base`
63-
# See https://github.com/googleapis/google-cloud-python/issues/12013
64-
# and https://github.com/actions/checkout#checkout-head.
6562
with:
66-
fetch-depth: 2
63+
# Use fetch-depth of 0 (full history) so Git can find the merge-base with the
64+
# target branch and calculate the exact list of modified packages.
65+
fetch-depth: 0
6766
persist-credentials: false
67+
# We must explicitly fetch and set the target branch reference (main, etc.)
68+
# to perform a relative diff (target_branch...HEAD). Comparing against a generic "HEAD~1"
69+
# is fragile and fails if developers merge main back into their PR, if the PR has
70+
# multiple commits, or if running inside a Merge Queue (merge_group) environment.
71+
- name: Set target branch ref
72+
id: target
73+
env:
74+
TARGET_BRANCH: ${{ github.base_ref || github.event.merge_group.base_ref || 'main' }}
75+
run: |
76+
git fetch origin $TARGET_BRANCH
77+
target_sha=$(git rev-parse FETCH_HEAD)
78+
echo "sha=${target_sha}" >> "$GITHUB_OUTPUT"
6879
- name: Setup Python
6980
uses: actions/setup-python@a26af69be951a213d495a4c3e4e4022e16d87065 # v5
7081
with:
7182
python-version: "3.10"
7283
- name: Set number of files changes in packages and preview-packages directories
7384
id: packages
85+
env:
86+
# Use TARGET_SHA...HEAD instead of HEAD~1 to accurately detect all files modified across the entire PR
87+
TARGET_SHA: ${{ steps.target.outputs.sha }}
7488
run: |
75-
git diff HEAD~1 -- packages preview-packages > /dev/null
76-
num_files_changed=$(git diff HEAD~1 -- packages preview-packages | wc -l | tr -d ' ')
89+
git diff "$TARGET_SHA"...HEAD -- packages preview-packages > /dev/null
90+
num_files_changed=$(git diff "$TARGET_SHA"...HEAD -- packages preview-packages | wc -l | tr -d ' ')
7791
echo "num_files_changed=${num_files_changed}" >> "$GITHUB_OUTPUT"
7892
- name: Install coverage
7993
if: ${{ steps.packages.outputs.num_files_changed > 0 }}
@@ -91,9 +105,12 @@ jobs:
91105
# TODO: default to 100% coverage after next gapic-generator release
92106
# https://github.com/googleapis/google-cloud-python/issues/17459
93107
DEFAULT_FAIL_UNDER: 99
108+
TARGET_SHA: ${{ steps.target.outputs.sha }}
94109
run: |
110+
# Use target_ref to find all packages modified across the entire PR branch relative to target branch
111+
target_ref="$TARGET_SHA"
95112
# Find all modified packages
96-
modified_packages=$(git diff --name-only HEAD~1 -- packages preview-packages | cut -d/ -f1,2 | sort -u)
113+
modified_packages=$(git diff --name-only ${target_ref}...HEAD -- packages preview-packages | cut -d/ -f1,2 | sort -u)
97114
98115
existing_modified_packages=()
99116
for pkg in ${modified_packages}; do
@@ -122,7 +139,7 @@ jobs:
122139
fi
123140
124141
# Find all modified packages
125-
modified_packages=$(git diff --name-only HEAD~1 -- packages preview-packages | cut -d/ -f1,2 | sort -u)
142+
modified_packages=$(git diff --name-only ${target_ref}...HEAD -- packages preview-packages | cut -d/ -f1,2 | sort -u)
126143
127144
failed_packages=()
128145
passed_packages=()

ci/run_conditional_tests.sh

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -36,14 +36,6 @@ set -eo pipefail
3636
export PROJECT_ROOT=$(realpath $(dirname "${BASH_SOURCE[0]}")/..)
3737
TARGET_BRANCH="${TARGET_BRANCH:-main}"
3838

39-
# Redirect git clones for core dependencies to the local repository.
40-
# This serves two purposes:
41-
# 1. Performance: Avoids repeated 100MB+ downloads of the monorepo for each dependency.
42-
# 2. Correctness: Ensures that changes in core packages (like google-api-core) are
43-
# tested against downstream packages in the same Pull Request.
44-
git config --global url."${PROJECT_ROOT}".insteadOf "https://github.com/googleapis/google-cloud-python"
45-
git config --global url."${PROJECT_ROOT}".insteadOf "https://github.com/googleapis/google-cloud-python.git"
46-
4739
# A script file for running the test in a sub project.
4840
test_script="${PROJECT_ROOT}/ci/run_single_test.sh"
4941

@@ -68,6 +60,18 @@ else
6860
GIT_DIFF_ARG=""
6961
fi
7062

63+
# Redirect git clones for core dependencies to the local repository.
64+
# This serves two purposes:
65+
# 1. Performance: Avoids repeated 100MB+ downloads of the monorepo for each dependency.
66+
# 2. Correctness: Ensures that changes in core packages (like google-api-core) are
67+
# tested against downstream packages in the same Pull Request.
68+
#
69+
# NOTE: This configuration MUST be set AFTER `git fetch origin` above, otherwise Git will
70+
# intercept and redirect `git fetch origin` to the local repository instead of GitHub,
71+
# breaking target branch fetching and package change detection.
72+
git config --global url."${PROJECT_ROOT}".insteadOf "https://github.com/googleapis/google-cloud-python"
73+
git config --global url."${PROJECT_ROOT}".insteadOf "https://github.com/googleapis/google-cloud-python.git"
74+
7175
# Then detect changes in the test scripts.
7276

7377
set +e

0 commit comments

Comments
 (0)