Skip to content

Commit 433ce99

Browse files
committed
fix searchindex.js merge failures
1 parent 93c3ad4 commit 433ce99

File tree

2 files changed

+187
-0
lines changed

2 files changed

+187
-0
lines changed

.github/workflows/auto_merge_approved_prs.yml

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,20 @@ jobs:
1515
runs-on: ubuntu-latest
1616

1717
steps:
18+
- name: Checkout repository
19+
uses: actions/checkout@v4
20+
with:
21+
fetch-depth: 0
22+
token: ${{ secrets.PAT_TOKEN }}
23+
24+
- name: Configure git
25+
run: |
26+
git config --global user.email "[email protected]"
27+
git config --global user.name "GitHub Action"
28+
29+
- name: Make conflict resolution script executable
30+
run: chmod +x ./resolve_searchindex_conflicts.sh
31+
1832
- name: Check for running workflows
1933
id: check_workflows
2034
run: |
@@ -93,6 +107,11 @@ jobs:
93107
if [ "$has_merge_comment" = true ]; then
94108
echo "Attempting to merge PR #$pr_number..."
95109
110+
# Get PR details including head branch
111+
pr_details=$(gh pr view "$pr_number" --json headRefName,baseRefName --repo "$GITHUB_REPOSITORY")
112+
head_branch=$(echo "$pr_details" | jq -r '.headRefName')
113+
base_branch=$(echo "$pr_details" | jq -r '.baseRefName')
114+
96115
# --- Polling for non-UNKNOWN mergeable status ---
97116
max_retries=10
98117
retry=0
@@ -118,6 +137,35 @@ jobs:
118137
else
119138
echo "Failed to merge PR #$pr_number: $pr_title"
120139
fi
140+
elif [ "$pr_mergeable" = "CONFLICTED" ]; then
141+
echo "PR #$pr_number has conflicts. Attempting to resolve searchindex.js conflicts..."
142+
143+
# Try to resolve conflicts by updating the branch
144+
export GITHUB_REPOSITORY="$GITHUB_REPOSITORY"
145+
export GH_TOKEN="${{ secrets.PAT_TOKEN }}"
146+
if ./resolve_searchindex_conflicts.sh "$pr_number" "$head_branch" "$base_branch"; then
147+
echo "Successfully resolved searchindex.js conflicts for PR #$pr_number"
148+
149+
# Wait for GitHub to update the mergeable status after conflict resolution
150+
echo "Waiting for GitHub to process the conflict resolution..."
151+
sleep 15
152+
153+
# Check if it's now mergeable
154+
pr_mergeable_after=$(gh pr view "$pr_number" --json mergeable --jq '.mergeable' --repo "$GITHUB_REPOSITORY")
155+
if [ "$pr_mergeable_after" = "MERGEABLE" ]; then
156+
if gh pr merge "$pr_number" --merge --delete-branch --repo "$GITHUB_REPOSITORY"; then
157+
echo "Successfully merged PR #$pr_number after resolving conflicts: $pr_title"
158+
current_count=$(cat /tmp/merged_count)
159+
echo $((current_count + 1)) > /tmp/merged_count
160+
else
161+
echo "Failed to merge PR #$pr_number after conflict resolution: $pr_title"
162+
fi
163+
else
164+
echo "PR #$pr_number still not mergeable after conflict resolution (status: $pr_mergeable_after)"
165+
fi
166+
else
167+
echo "Failed to resolve conflicts for PR #$pr_number"
168+
fi
121169
else
122170
echo "PR #$pr_number is not mergeable (status: $pr_mergeable)"
123171
fi

resolve_searchindex_conflicts.sh

Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
#!/bin/bash
2+
3+
# Script to resolve searchindex.js conflicts by accepting master branch version
4+
# This script is designed to handle merge conflicts that occur when PRs become
5+
# desynchronized due to the auto-generated searchindex.js file.
6+
#
7+
# The searchindex.js file is automatically generated by the build process and
8+
# frequently causes conflicts when multiple PRs are waiting to be merged.
9+
# This script automatically resolves those conflicts by accepting the master
10+
# branch version of the file.
11+
#
12+
# Usage: resolve_searchindex_conflicts.sh <pr_number> <head_branch> <base_branch>
13+
14+
set -euo pipefail
15+
16+
# Validate arguments
17+
if [ $# -ne 3 ]; then
18+
echo "Usage: $0 <pr_number> <head_branch> <base_branch>"
19+
exit 1
20+
fi
21+
22+
PR_NUMBER="$1"
23+
HEAD_BRANCH="$2"
24+
BASE_BRANCH="$3"
25+
26+
# Validate required environment variables
27+
if [ -z "${GITHUB_REPOSITORY:-}" ]; then
28+
echo "Error: GITHUB_REPOSITORY environment variable is required"
29+
exit 1
30+
fi
31+
32+
if [ -z "${GH_TOKEN:-}" ]; then
33+
echo "Error: GH_TOKEN environment variable is required"
34+
exit 1
35+
fi
36+
37+
echo "Resolving conflicts for PR #$PR_NUMBER (branch: $HEAD_BRANCH -> $BASE_BRANCH)"
38+
39+
# Get current directory for safety
40+
ORIGINAL_DIR=$(pwd)
41+
42+
# Create a temporary directory for the operation
43+
TEMP_DIR=$(mktemp -d)
44+
echo "Working in temporary directory: $TEMP_DIR"
45+
46+
cleanup() {
47+
echo "Cleaning up..."
48+
cd "$ORIGINAL_DIR"
49+
rm -rf "$TEMP_DIR"
50+
}
51+
trap cleanup EXIT
52+
53+
# Clone the repository to the temp directory
54+
echo "Cloning repository..."
55+
cd "$TEMP_DIR"
56+
gh repo clone "$GITHUB_REPOSITORY" . --branch "$HEAD_BRANCH"
57+
58+
# Configure git
59+
git config user.email "[email protected]"
60+
git config user.name "GitHub Action"
61+
62+
# Fetch all branches
63+
git fetch origin
64+
65+
# Make sure we're on the correct branch
66+
git checkout "$HEAD_BRANCH"
67+
68+
# Try to merge the base branch
69+
echo "Attempting to merge $BASE_BRANCH into $HEAD_BRANCH..."
70+
if git merge "origin/$BASE_BRANCH" --no-edit; then
71+
echo "No conflicts found, merge successful"
72+
73+
# Push the updated branch
74+
echo "Pushing merged branch..."
75+
git push origin "$HEAD_BRANCH"
76+
exit 0
77+
fi
78+
79+
# Check what files have conflicts
80+
echo "Checking for conflicts..."
81+
conflicted_files=$(git diff --name-only --diff-filter=U)
82+
echo "Conflicted files: $conflicted_files"
83+
84+
# Check if searchindex.js is the only conflict or if conflicts are only in acceptable files
85+
acceptable_conflicts=true
86+
searchindex_conflict=false
87+
88+
for file in $conflicted_files; do
89+
case "$file" in
90+
"searchindex.js")
91+
searchindex_conflict=true
92+
echo "Found searchindex.js conflict (acceptable)"
93+
;;
94+
*)
95+
echo "Found unacceptable conflict in: $file"
96+
acceptable_conflicts=false
97+
;;
98+
esac
99+
done
100+
101+
if [ "$acceptable_conflicts" = false ]; then
102+
echo "Cannot auto-resolve: conflicts found in files other than searchindex.js"
103+
git merge --abort
104+
exit 1
105+
fi
106+
107+
if [ "$searchindex_conflict" = false ]; then
108+
echo "No searchindex.js conflicts found, but merge failed for unknown reason"
109+
git merge --abort
110+
exit 1
111+
fi
112+
113+
echo "Resolving searchindex.js conflict by accepting $BASE_BRANCH version..."
114+
115+
# Accept the base branch version of searchindex.js (--theirs refers to the branch being merged in)
116+
git checkout --theirs searchindex.js
117+
git add searchindex.js
118+
119+
# Check if there are any other staged changes from the merge
120+
staged_files=$(git diff --cached --name-only || true)
121+
echo "Staged files after resolution: $staged_files"
122+
123+
# Complete the merge
124+
if git commit --no-edit; then
125+
echo "Successfully resolved merge conflicts"
126+
127+
# Push the updated branch
128+
echo "Pushing resolved branch..."
129+
if git push origin "$HEAD_BRANCH"; then
130+
echo "Successfully pushed resolved branch"
131+
exit 0
132+
else
133+
echo "Failed to push resolved branch"
134+
exit 1
135+
fi
136+
else
137+
echo "Failed to commit merge resolution"
138+
exit 1
139+
fi

0 commit comments

Comments
 (0)