Skip to content

Commit aedfb38

Browse files
Fix force pushes logic
Signed-off-by: Shreeya Patel <spatel@ciq.com>
1 parent d6668f3 commit aedfb38

File tree

1 file changed

+44
-10
lines changed

1 file changed

+44
-10
lines changed

.github/workflows/kernel-build-and-test-x86_64.yml

Lines changed: 44 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -218,18 +218,39 @@ jobs:
218218
name: kselftest-logs-x86_64
219219
path: output-current
220220

221+
- name: Install GitHub CLI
222+
run: |
223+
set -euxo pipefail
224+
# Install gh CLI if not already available
225+
if ! command -v gh &> /dev/null; then
226+
sudo apt-get update
227+
sudo apt-get install -y gh
228+
fi
229+
221230
- name: Determine base branch for comparison
222231
id: base_branch
232+
env:
233+
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
223234
run: |
224235
BASE_BRANCH=""
225236
226-
# For PRs, use the base branch directly
227-
if [ -n "${{ github.base_ref }}" ]; then
237+
# First, check if an open PR already exists from this head branch
238+
echo "Checking for existing open PR from branch: ${{ github.ref_name }}"
239+
EXISTING_PR=$(gh pr list --head "${{ github.ref_name }}" --state open --json number,baseRefName --jq '.[0]' || echo "")
240+
241+
if [ -n "$EXISTING_PR" ] && [ "$EXISTING_PR" != "null" ]; then
242+
# PR exists - use its existing base branch (no merge-base detection on force push)
243+
BASE_BRANCH=$(echo "$EXISTING_PR" | jq -r '.baseRefName')
244+
PR_NUMBER=$(echo "$EXISTING_PR" | jq -r '.number')
245+
echo "Found existing PR #$PR_NUMBER, using existing base: $BASE_BRANCH"
246+
echo "Skipping merge-base detection to avoid duplicate PRs on force push"
247+
elif [ -n "${{ github.base_ref }}" ]; then
248+
# For PRs, use the base branch directly
228249
BASE_BRANCH="${{ github.base_ref }}"
229250
echo "Using PR base branch: $BASE_BRANCH"
230251
else
231-
# For direct pushes, find the common ancestor branch
232-
echo "Not a PR, finding base branch via merge-base"
252+
# No existing PR - for direct pushes, find the common ancestor branch
253+
echo "No existing PR found, finding base branch via merge-base"
233254
234255
# Get all remote branches sorted by commit date (most recent first)
235256
# This ensures we check actively developed branches first
@@ -477,7 +498,7 @@ jobs:
477498
exit 1
478499
fi
479500
480-
echo "Creating PR from ${{ github.ref_name }} to $BASE_BRANCH"
501+
echo "Creating/updating PR from ${{ github.ref_name }} to $BASE_BRANCH"
481502
482503
# Determine comparison status message
483504
COMPARISON_STATUS="${{ needs.compare-results.outputs.comparison_status }}"
@@ -509,14 +530,27 @@ jobs:
509530
"${{ github.repository }}" \
510531
> pr_body.md
511532
512-
# Check if PR already exists
513-
EXISTING_PR=$(gh pr list --head "${{ github.ref_name }}" --base "$BASE_BRANCH" --json number --jq '.[0].number' || echo "")
533+
# Check if any open PR already exists from this head branch (regardless of base)
534+
EXISTING_PR=$(gh pr list --head "${{ github.ref_name }}" --state open --json number,baseRefName --jq '.[0]' || echo "")
514535
515-
if [ -n "$EXISTING_PR" ]; then
516-
echo "PR #$EXISTING_PR already exists, updating it"
517-
gh pr edit "$EXISTING_PR" \
536+
if [ -n "$EXISTING_PR" ] && [ "$EXISTING_PR" != "null" ]; then
537+
PR_NUMBER=$(echo "$EXISTING_PR" | jq -r '.number')
538+
CURRENT_BASE=$(echo "$EXISTING_PR" | jq -r '.baseRefName')
539+
540+
echo "Found existing PR #$PR_NUMBER (current base: $CURRENT_BASE)"
541+
542+
# Update PR title and body
543+
gh pr edit "$PR_NUMBER" \
518544
--title "[$BASE_BRANCH] ${{ steps.commit_msg.outputs.commit_subject }}" \
519545
--body-file pr_body.md
546+
547+
echo "Updated PR #$PR_NUMBER"
548+
549+
# Note: We don't change the base branch even if it differs from $BASE_BRANCH
550+
# because compare-results already used the existing PR's base for comparison
551+
if [ "$CURRENT_BASE" != "$BASE_BRANCH" ]; then
552+
echo "::notice::PR base remains $CURRENT_BASE (comparison was done against this base)"
553+
fi
520554
else
521555
echo "Creating new PR from ${{ github.ref_name }} to $BASE_BRANCH"
522556
gh pr create \

0 commit comments

Comments
 (0)