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