@@ -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=()
0 commit comments