forked from vsch/flexmark-java
-
Notifications
You must be signed in to change notification settings - Fork 0
210 lines (168 loc) · 7.37 KB
/
sync-upstream.yml
File metadata and controls
210 lines (168 loc) · 7.37 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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
name: Sync upstream/master with Upstream Repository
on:
workflow_dispatch:
inputs:
dry_run:
description: 'Dry run (do not push or create PR)'
required: false
default: false
type: boolean
jobs:
sync-upstream:
runs-on: ubuntu-latest
permissions:
contents: write
pull-requests: write
steps:
- name: Checkout repository
uses: actions/checkout@v4
with:
fetch-depth: 0
ref: upstream/master
token: ${{ secrets.GITHUB_TOKEN }}
- name: Configure Git
run: |
git config user.name "github-actions[bot]"
git config user.email "github-actions[bot]@users.noreply.github.com"
- name: Add upstream remote
run: |
# Check if upstream remote exists
if git remote | grep -q '^upstream$'; then
echo "Upstream remote already exists"
git remote set-url upstream https://github.com/vsch/flexmark-java.git
else
echo "Adding upstream remote"
git remote add upstream https://github.com/vsch/flexmark-java.git
fi
# Verify remotes
echo "Current remotes:"
git remote -v
- name: Fetch upstream
run: |
echo "Fetching from upstream repository..."
git fetch upstream master
git fetch origin master
echo "Current upstream/master HEAD:"
git log --oneline -1
echo "Upstream repository master HEAD:"
git log --oneline -1 upstream/master
- name: Check for updates
id: check_updates
run: |
LOCAL_COMMIT=$(git rev-parse HEAD)
UPSTREAM_COMMIT=$(git rev-parse upstream/master)
echo "local_commit=${LOCAL_COMMIT}" >> $GITHUB_OUTPUT
echo "upstream_commit=${UPSTREAM_COMMIT}" >> $GITHUB_OUTPUT
if [ "${LOCAL_COMMIT}" = "${UPSTREAM_COMMIT}" ]; then
echo "has_updates=false" >> $GITHUB_OUTPUT
echo "✅ upstream/master is already up-to-date with upstream"
else
echo "has_updates=true" >> $GITHUB_OUTPUT
echo "📦 Updates available from upstream"
echo ""
echo "Commits to be pulled:"
git log --oneline ${LOCAL_COMMIT}..${UPSTREAM_COMMIT}
fi
- name: Pull from upstream
if: steps.check_updates.outputs.has_updates == 'true'
run: |
echo "Pulling changes from upstream/master..."
git pull upstream master --ff-only
echo ""
echo "✅ Successfully pulled from upstream"
echo ""
echo "Recent commits:"
git log --oneline -5
- name: Push to origin (upstream/master)
if: steps.check_updates.outputs.has_updates == 'true' && inputs.dry_run == false
run: |
echo "Pushing updated upstream/master to origin..."
git push origin upstream/master
echo ""
echo "✅ Successfully pushed to origin/upstream/master"
- name: Create sync branch and PR
if: steps.check_updates.outputs.has_updates == 'true' && inputs.dry_run == false
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
# Generate branch name with current date
SYNC_BRANCH="sync/upstream-$(date +%Y%m%d-%H%M%S)"
echo "Creating sync branch: ${SYNC_BRANCH}"
# Create and checkout sync branch from updated upstream/master
git checkout -b "${SYNC_BRANCH}"
# Push sync branch
git push -u origin "${SYNC_BRANCH}"
# Generate PR body with commit details
LOCAL_COMMIT="${{ steps.check_updates.outputs.local_commit }}"
UPSTREAM_COMMIT="${{ steps.check_updates.outputs.upstream_commit }}"
# Get commit list
COMMITS=$(git log --oneline ${LOCAL_COMMIT}..${UPSTREAM_COMMIT})
COMMIT_COUNT=$(echo "${COMMITS}" | wc -l | tr -d ' ')
# Get detailed commit info
COMMIT_DETAILS=$(git log --format="- %h %s (%an, %ar)" ${LOCAL_COMMIT}..${UPSTREAM_COMMIT})
# Create PR body
cat > /tmp/pr_body.md << 'EOF'
## 📦 Upstream Synchronization
This PR synchronizes changes from the upstream repository (vsch/flexmark-java) to our master branch.
### Summary
- **Commits synced**: COMMIT_COUNT_PLACEHOLDER
- **Previous commit**: `LOCAL_COMMIT_PLACEHOLDER`
- **New commit**: `UPSTREAM_COMMIT_PLACEHOLDER`
### Commits Included
COMMIT_DETAILS_PLACEHOLDER
### Verification Checklist
- [ ] Review the upstream changes
- [ ] Run tests to ensure compatibility
- [ ] Check for conflicts with internal changes
- [ ] Verify CI/CD pipeline passes
### Related Documentation
See [BRANCH.md](../BRANCH.md) for the branch strategy.
---
🤖 *This PR was automatically created by the [Sync Upstream workflow](../.github/workflows/sync-upstream.yml)*
EOF
# Replace placeholders
sed -i "s/COMMIT_COUNT_PLACEHOLDER/${COMMIT_COUNT}/" /tmp/pr_body.md
sed -i "s/LOCAL_COMMIT_PLACEHOLDER/${LOCAL_COMMIT}/" /tmp/pr_body.md
sed -i "s/UPSTREAM_COMMIT_PLACEHOLDER/${UPSTREAM_COMMIT}/" /tmp/pr_body.md
sed -i "s|COMMIT_DETAILS_PLACEHOLDER|${COMMIT_DETAILS}|" /tmp/pr_body.md
# Create PR
gh pr create \
--base master \
--head "${SYNC_BRANCH}" \
--title "🔄 Sync upstream changes ($(date +%Y-%m-%d))" \
--body-file /tmp/pr_body.md
echo ""
echo "✅ Pull request created successfully"
echo "Branch: ${SYNC_BRANCH} → master"
- name: Dry run summary
if: inputs.dry_run == true && steps.check_updates.outputs.has_updates == 'true'
run: |
echo "🔍 DRY RUN MODE - No changes pushed, no PR created"
echo ""
echo "Would have pushed the following changes:"
git log --oneline -5
echo ""
echo "Would have created PR: sync/upstream-YYYYMMDD → master"
- name: Summary
if: always()
run: |
echo "## Sync Summary" >> $GITHUB_STEP_SUMMARY
echo "" >> $GITHUB_STEP_SUMMARY
if [ "${{ steps.check_updates.outputs.has_updates }}" = "true" ]; then
echo "### 📦 Updates Found" >> $GITHUB_STEP_SUMMARY
echo "" >> $GITHUB_STEP_SUMMARY
echo "- Local commit: \`${{ steps.check_updates.outputs.local_commit }}\`" >> $GITHUB_STEP_SUMMARY
echo "- Upstream commit: \`${{ steps.check_updates.outputs.upstream_commit }}\`" >> $GITHUB_STEP_SUMMARY
echo "" >> $GITHUB_STEP_SUMMARY
if [ "${{ inputs.dry_run }}" = "true" ]; then
echo "🔍 **Dry run mode** - No changes were pushed, no PR was created" >> $GITHUB_STEP_SUMMARY
else
echo "✅ **Changes synced and PR created successfully**" >> $GITHUB_STEP_SUMMARY
echo "" >> $GITHUB_STEP_SUMMARY
echo "Check the [Pull Requests](../../pulls) tab to review and merge the changes." >> $GITHUB_STEP_SUMMARY
fi
else
echo "### ✅ Already Up-to-Date" >> $GITHUB_STEP_SUMMARY
echo "" >> $GITHUB_STEP_SUMMARY
echo "No updates available from upstream" >> $GITHUB_STEP_SUMMARY
fi