|
| 1 | +name: Sync Upstream |
| 2 | + |
| 3 | +on: |
| 4 | + schedule: |
| 5 | + - cron: '0 6 * * *' # Daily at 6 AM UTC |
| 6 | + workflow_dispatch: |
| 7 | + |
| 8 | +jobs: |
| 9 | + sync: |
| 10 | + runs-on: ubuntu-latest |
| 11 | + permissions: |
| 12 | + contents: write |
| 13 | + pull-requests: write |
| 14 | + |
| 15 | + steps: |
| 16 | + - uses: actions/checkout@v4 |
| 17 | + with: |
| 18 | + ref: dev |
| 19 | + fetch-depth: 0 |
| 20 | + |
| 21 | + - name: Configure git |
| 22 | + run: | |
| 23 | + git config user.name "github-actions[bot]" |
| 24 | + git config user.email "github-actions[bot]@users.noreply.github.com" |
| 25 | +
|
| 26 | + - name: Add upstream and fetch |
| 27 | + run: | |
| 28 | + git remote add upstream https://github.com/ultraworkers/claw-code.git |
| 29 | + git fetch upstream main |
| 30 | +
|
| 31 | + - name: Check for new commits |
| 32 | + id: check |
| 33 | + run: | |
| 34 | + BEHIND=$(git rev-list --count dev..upstream/main) |
| 35 | + echo "behind=$BEHIND" >> $GITHUB_OUTPUT |
| 36 | + echo "$BEHIND new commits from upstream" |
| 37 | +
|
| 38 | + - name: Create sync branch and merge |
| 39 | + if: steps.check.outputs.behind != '0' |
| 40 | + id: merge |
| 41 | + run: | |
| 42 | + BRANCH="upstream-sync/$(date +%Y-%m-%d)" |
| 43 | + echo "branch=$BRANCH" >> $GITHUB_OUTPUT |
| 44 | + git checkout -b "$BRANCH" dev |
| 45 | + if git merge upstream/main --no-edit; then |
| 46 | + echo "clean=true" >> $GITHUB_OUTPUT |
| 47 | + else |
| 48 | + echo "clean=false" >> $GITHUB_OUTPUT |
| 49 | + fi |
| 50 | +
|
| 51 | + - name: List conflicts if any |
| 52 | + if: steps.check.outputs.behind != '0' && steps.merge.outputs.clean == 'false' |
| 53 | + id: conflicts |
| 54 | + run: | |
| 55 | + CONFLICT_FILES=$(git diff --name-only --diff-filter=U | sort) |
| 56 | + CONFLICT_COUNT=$(echo "$CONFLICT_FILES" | wc -l | tr -d ' ') |
| 57 | + echo "count=$CONFLICT_COUNT" >> $GITHUB_OUTPUT |
| 58 | + echo "files<<EOF" >> $GITHUB_OUTPUT |
| 59 | + echo "$CONFLICT_FILES" >> $GITHUB_OUTPUT |
| 60 | + echo "EOF" >> $GITHUB_OUTPUT |
| 61 | + echo "Conflicting files:" |
| 62 | + echo "$CONFLICT_FILES" |
| 63 | + # Abort merge so PR shows the diff clearly |
| 64 | + git merge --abort |
| 65 | + # Re-merge with ours strategy for conflicting files to create a pushable branch |
| 66 | + git merge upstream/main --no-edit -X ours || true |
| 67 | +
|
| 68 | + - name: Push sync branch |
| 69 | + if: steps.check.outputs.behind != '0' |
| 70 | + run: | |
| 71 | + git push -u origin "${{ steps.merge.outputs.branch }}" |
| 72 | +
|
| 73 | + - name: Create PR (clean merge) |
| 74 | + if: steps.check.outputs.behind != '0' && steps.merge.outputs.clean == 'true' |
| 75 | + env: |
| 76 | + GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 77 | + run: | |
| 78 | + BEHIND=${{ steps.check.outputs.behind }} |
| 79 | + gh pr create \ |
| 80 | + --title "[Upstream Sync] $BEHIND commits — clean merge" \ |
| 81 | + --body "$(cat <<'BODY' |
| 82 | + ## Upstream Sync |
| 83 | +
|
| 84 | + **$BEHIND** new commits from [claw-code](https://github.com/ultraworkers/claw-code) main. |
| 85 | +
|
| 86 | + Clean merge — no conflicts. Review and merge when ready. |
| 87 | + BODY |
| 88 | + )" \ |
| 89 | + --base dev \ |
| 90 | + --label "upstream-sync" |
| 91 | +
|
| 92 | + - name: Create PR (with conflicts) |
| 93 | + if: steps.check.outputs.behind != '0' && steps.merge.outputs.clean == 'false' |
| 94 | + env: |
| 95 | + GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 96 | + run: | |
| 97 | + BEHIND=${{ steps.check.outputs.behind }} |
| 98 | + CONFLICTS="${{ steps.conflicts.outputs.count }}" |
| 99 | + FILES="${{ steps.conflicts.outputs.files }}" |
| 100 | + gh pr create \ |
| 101 | + --title "[Upstream Sync] $BEHIND commits — $CONFLICTS conflicts" \ |
| 102 | + --body "$(cat <<BODY |
| 103 | + ## Upstream Sync |
| 104 | +
|
| 105 | + **$BEHIND** new commits from [claw-code](https://github.com/ultraworkers/claw-code) main. |
| 106 | +
|
| 107 | + ### Conflicts need manual resolution |
| 108 | +
|
| 109 | + The following files have merge conflicts (resolved with \`ours\` strategy — your version kept): |
| 110 | +
|
| 111 | + $(echo "$FILES" | sed 's/^/- `/' | sed 's/$/`/') |
| 112 | +
|
| 113 | + **To resolve properly:** |
| 114 | + \`\`\`bash |
| 115 | + git fetch origin ${{ steps.merge.outputs.branch }} |
| 116 | + git checkout ${{ steps.merge.outputs.branch }} |
| 117 | + git fetch upstream main # if not already added |
| 118 | + git merge --abort 2>/dev/null |
| 119 | + git reset --hard origin/dev |
| 120 | + git merge upstream/main |
| 121 | + # Resolve conflicts manually, then: |
| 122 | + git add -A && git commit |
| 123 | + git push --force origin ${{ steps.merge.outputs.branch }} |
| 124 | + \`\`\` |
| 125 | + BODY |
| 126 | + )" \ |
| 127 | + --base dev \ |
| 128 | + --label "upstream-sync" |
0 commit comments