forked from Ikalus1988/MisakaNet
-
Notifications
You must be signed in to change notification settings - Fork 0
76 lines (67 loc) · 2.69 KB
/
Copy pathauto-sync-prs.yml
File metadata and controls
76 lines (67 loc) · 2.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
name: Auto-Sync PR Branches
on:
push:
branches: [main]
workflow_dispatch:
permissions:
contents: write
pull-requests: write
jobs:
sync:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v7
with:
fetch-depth: 0
- name: List open PRs targeting main
id: prs
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
gh api repos/${{ github.repository }}/pulls \
--jq '[.[] | select(.base.ref == "main" and .state == "open") | {number, headRef: .head.ref, headRepo: .head.repo.full_name, mergeable: .mergeable}]' \
> open_prs.json
echo "count=$(jq length open_prs.json)" >> $GITHUB_OUTPUT
echo "Found $(jq length open_prs.json) open PR(s)"
- name: Merge main into each same-repo PR
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
COUNT=$(jq length open_prs.json)
for i in $(seq 0 $((COUNT - 1))); do
PR=$(jq ".[$i]" open_prs.json)
NUM=$(echo "$PR" | jq -r '.number')
HEAD_REF=$(echo "$PR" | jq -r '.headRef')
HEAD_REPO=$(echo "$PR" | jq -r '.headRepo')
MERGEABLE=$(echo "$PR" | jq -r '.mergeable')
echo "--- PR #$NUM ($HEAD_REF) ---"
# Skip fork PRs (cannot push to fork branches)
if [ "$HEAD_REPO" != "${{ github.repository }}" ]; then
echo " SKIP: fork PR — cannot push to $HEAD_REPO"
continue
fi
# Skip conflicting PRs
if [ "$MERGEABLE" = "CONFLICTING" ]; then
echo " SKIP: has merge conflicts"
gh pr comment "$NUM" --repo ${{ github.repository }} \
--body "⚠️ **Auto-sync skipped** — this PR has merge conflicts with main. Please resolve them manually."
continue
fi
# Attempt merge
git fetch origin "$HEAD_REF"
if git checkout "$HEAD_REF" 2>/dev/null; then
if git merge origin/main --no-edit 2>&1; then
git push origin "HEAD:$HEAD_REF"
echo " ✅ Merged main into #$NUM"
else
git merge --abort 2>/dev/null
echo " ❌ Merge failed for #$NUM (may have conflicts now)"
gh pr comment "$NUM" --repo ${{ github.repository }} \
--body "⚠️ **Auto-sync failed** — main could not be automatically merged into this branch. Please rebase manually."
fi
else
echo " ❌ Could not checkout $HEAD_REF"
fi
# Return to main for next iteration
git checkout main 2>/dev/null
done