|
| 1 | +name: Sync Upstream |
| 2 | + |
| 3 | +on: |
| 4 | + schedule: |
| 5 | + - cron: "0 */3 * * *" |
| 6 | + workflow_dispatch: |
| 7 | + |
| 8 | +concurrency: |
| 9 | + group: sync-upstream |
| 10 | + cancel-in-progress: false |
| 11 | + |
| 12 | +jobs: |
| 13 | + sync: |
| 14 | + runs-on: ubuntu-latest |
| 15 | + timeout-minutes: 10 |
| 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 | + |
| 26 | + - name: Add upstream remote |
| 27 | + run: git remote add upstream https://github.com/paperclipai/paperclip.git |
| 28 | + |
| 29 | + - name: Fetch upstream |
| 30 | + run: git fetch upstream main |
| 31 | + |
| 32 | + - name: Check for new commits |
| 33 | + id: check |
| 34 | + run: | |
| 35 | + behind=$(git rev-list --count HEAD..upstream/main) |
| 36 | + echo "behind=$behind" >> "$GITHUB_OUTPUT" |
| 37 | + if [ "$behind" -eq 0 ]; then |
| 38 | + echo "Already up to date with upstream." |
| 39 | + else |
| 40 | + echo "Fork is $behind commit(s) behind upstream." |
| 41 | + fi |
| 42 | +
|
| 43 | + - name: Rebase onto upstream |
| 44 | + if: steps.check.outputs.behind != '0' |
| 45 | + id: rebase |
| 46 | + run: | |
| 47 | + BRANCH="sync/upstream" |
| 48 | + git config user.name "upstream-sync-bot" |
| 49 | + git config user.email "upstream-sync-bot@users.noreply.github.com" |
| 50 | +
|
| 51 | + git checkout -B "$BRANCH" |
| 52 | +
|
| 53 | + if git rebase upstream/main; then |
| 54 | + echo "conflict=false" >> "$GITHUB_OUTPUT" |
| 55 | + else |
| 56 | + git rebase --abort |
| 57 | + echo "conflict=true" >> "$GITHUB_OUTPUT" |
| 58 | + echo "::warning::Rebase has conflicts — manual resolution required." |
| 59 | + fi |
| 60 | +
|
| 61 | + - name: Push sync branch |
| 62 | + if: steps.check.outputs.behind != '0' && steps.rebase.outputs.conflict == 'false' |
| 63 | + run: git push --force origin sync/upstream |
| 64 | + |
| 65 | + - name: Create or update pull request |
| 66 | + if: steps.check.outputs.behind != '0' |
| 67 | + id: pr |
| 68 | + env: |
| 69 | + GH_TOKEN: ${{ github.token }} |
| 70 | + run: | |
| 71 | + BRANCH="sync/upstream" |
| 72 | + behind=${{ steps.check.outputs.behind }} |
| 73 | + conflict=${{ steps.rebase.outputs.conflict }} |
| 74 | +
|
| 75 | + existing=$(gh pr list --head "$BRANCH" --json number --jq '.[0].number') |
| 76 | +
|
| 77 | + if [ "$conflict" = "true" ]; then |
| 78 | + title="sync: upstream has $behind new commit(s) (conflicts)" |
| 79 | + body="$(cat <<'EOF' |
| 80 | + ## Upstream Sync |
| 81 | +
|
| 82 | + The upstream repository [paperclipai/paperclip](https://github.com/paperclipai/paperclip) has **new commits** that could not be rebased automatically due to merge conflicts. |
| 83 | +
|
| 84 | + **Manual resolution required.** Please pull the `sync/upstream` branch locally and resolve conflicts: |
| 85 | +
|
| 86 | + ```bash |
| 87 | + git fetch origin sync/upstream |
| 88 | + git checkout sync/upstream |
| 89 | + git rebase upstream/main |
| 90 | + # resolve conflicts |
| 91 | + git push --force origin sync/upstream |
| 92 | + ``` |
| 93 | + EOF |
| 94 | + )" |
| 95 | + else |
| 96 | + title="sync: rebase $behind commit(s) from upstream" |
| 97 | + body="$(cat <<'EOF' |
| 98 | + ## Upstream Sync |
| 99 | +
|
| 100 | + Automated rebase of new commits from [paperclipai/paperclip](https://github.com/paperclipai/paperclip). No conflicts detected — safe to merge. |
| 101 | + EOF |
| 102 | + )" |
| 103 | + fi |
| 104 | +
|
| 105 | + if [ -z "$existing" ]; then |
| 106 | + if [ "$conflict" = "true" ]; then |
| 107 | + echo "Skipping PR creation — conflicts need resolution before branch can be pushed." |
| 108 | + exit 0 |
| 109 | + fi |
| 110 | + gh pr create \ |
| 111 | + --head "$BRANCH" \ |
| 112 | + --title "$title" \ |
| 113 | + --body "$body" |
| 114 | + else |
| 115 | + gh pr edit "$existing" --title "$title" --body "$body" |
| 116 | + echo "Updated existing PR #$existing." |
| 117 | + fi |
| 118 | +
|
| 119 | + - name: Auto-merge if clean |
| 120 | + if: steps.check.outputs.behind != '0' && steps.rebase.outputs.conflict == 'false' |
| 121 | + env: |
| 122 | + GH_TOKEN: ${{ github.token }} |
| 123 | + run: | |
| 124 | + pr_url="$(gh pr list --head sync/upstream --json url --jq '.[0].url')" |
| 125 | + if [ -z "$pr_url" ]; then |
| 126 | + echo "PR not found, skipping auto-merge." |
| 127 | + exit 0 |
| 128 | + fi |
| 129 | + gh pr merge --auto --rebase --delete-branch "$pr_url" |
0 commit comments