Skip to content

Commit d80e088

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

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
@@ -215,18 +215,39 @@ jobs:
215215
name: kselftest-logs-x86_64
216216
path: output-current
217217

218+
- name: Install GitHub CLI
219+
run: |
220+
set -euxo pipefail
221+
# Install gh CLI if not already available
222+
if ! command -v gh &> /dev/null; then
223+
sudo apt-get update
224+
sudo apt-get install -y gh
225+
fi
226+
218227
- name: Determine base branch for comparison
219228
id: base_branch
229+
env:
230+
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
220231
run: |
221232
BASE_BRANCH=""
222233
223-
# For PRs, use the base branch directly
224-
if [ -n "${{ github.base_ref }}" ]; then
234+
# First, check if an open PR already exists from this head branch
235+
echo "Checking for existing open PR from branch: ${{ github.ref_name }}"
236+
EXISTING_PR=$(gh pr list --head "${{ github.ref_name }}" --state open --json number,baseRefName --jq '.[0]' || echo "")
237+
238+
if [ -n "$EXISTING_PR" ] && [ "$EXISTING_PR" != "null" ]; then
239+
# PR exists - use its existing base branch (no merge-base detection on force push)
240+
BASE_BRANCH=$(echo "$EXISTING_PR" | jq -r '.baseRefName')
241+
PR_NUMBER=$(echo "$EXISTING_PR" | jq -r '.number')
242+
echo "Found existing PR #$PR_NUMBER, using existing base: $BASE_BRANCH"
243+
echo "Skipping merge-base detection to avoid duplicate PRs on force push"
244+
elif [ -n "${{ github.base_ref }}" ]; then
245+
# For PRs, use the base branch directly
225246
BASE_BRANCH="${{ github.base_ref }}"
226247
echo "Using PR base branch: $BASE_BRANCH"
227248
else
228-
# For direct pushes, find the common ancestor branch
229-
echo "Not a PR, finding base branch via merge-base"
249+
# No existing PR - for direct pushes, find the common ancestor branch
250+
echo "No existing PR found, finding base branch via merge-base"
230251
231252
# Get all remote branches sorted by commit date (most recent first)
232253
# This ensures we check actively developed branches first
@@ -474,7 +495,7 @@ jobs:
474495
exit 1
475496
fi
476497
477-
echo "Creating PR from ${{ github.ref_name }} to $BASE_BRANCH"
498+
echo "Creating/updating PR from ${{ github.ref_name }} to $BASE_BRANCH"
478499
479500
# Determine comparison status message
480501
COMPARISON_STATUS="${{ needs.compare-results.outputs.comparison_status }}"
@@ -506,14 +527,27 @@ jobs:
506527
"${{ github.repository }}" \
507528
> pr_body.md
508529
509-
# Check if PR already exists
510-
EXISTING_PR=$(gh pr list --head "${{ github.ref_name }}" --base "$BASE_BRANCH" --json number --jq '.[0].number' || echo "")
530+
# Check if any open PR already exists from this head branch (regardless of base)
531+
EXISTING_PR=$(gh pr list --head "${{ github.ref_name }}" --state open --json number,baseRefName --jq '.[0]' || echo "")
511532
512-
if [ -n "$EXISTING_PR" ]; then
513-
echo "PR #$EXISTING_PR already exists, updating it"
514-
gh pr edit "$EXISTING_PR" \
533+
if [ -n "$EXISTING_PR" ] && [ "$EXISTING_PR" != "null" ]; then
534+
PR_NUMBER=$(echo "$EXISTING_PR" | jq -r '.number')
535+
CURRENT_BASE=$(echo "$EXISTING_PR" | jq -r '.baseRefName')
536+
537+
echo "Found existing PR #$PR_NUMBER (current base: $CURRENT_BASE)"
538+
539+
# Update PR title and body
540+
gh pr edit "$PR_NUMBER" \
515541
--title "[$BASE_BRANCH] ${{ steps.commit_msg.outputs.commit_subject }}" \
516542
--body-file pr_body.md
543+
544+
echo "Updated PR #$PR_NUMBER"
545+
546+
# Note: We don't change the base branch even if it differs from $BASE_BRANCH
547+
# because compare-results already used the existing PR's base for comparison
548+
if [ "$CURRENT_BASE" != "$BASE_BRANCH" ]; then
549+
echo "::notice::PR base remains $CURRENT_BASE (comparison was done against this base)"
550+
fi
517551
else
518552
echo "Creating new PR from ${{ github.ref_name }} to $BASE_BRANCH"
519553
gh pr create \

0 commit comments

Comments
 (0)