|
| 1 | +# Auto-rebase open PR branches against the default branch (main by default). |
| 2 | +# |
| 3 | +# Requirements: |
| 4 | +# - Repository secret GITEA_TOKEN with API scope that can read PRs and push to branches in this fork. |
| 5 | +# - Optional repository variable DEFAULT_BRANCH to override the default branch name (defaults to "main"). |
| 6 | +# |
| 7 | +# Behavior: |
| 8 | +# - Lists open PRs in this repo. For PRs whose head branch is in this repo, |
| 9 | +# rebases the branch onto the default branch and force-pushes with lease. |
| 10 | +# - On rebase conflict, aborts and posts a comment on the PR asking for manual resolution. |
| 11 | + |
| 12 | +name: Auto Rebase PRs |
| 13 | + |
| 14 | +on: |
| 15 | + schedule: |
| 16 | + - cron: "*/30 * * * *" # every 30 minutes |
| 17 | + workflow_dispatch: {} |
| 18 | + |
| 19 | +jobs: |
| 20 | + rebase: |
| 21 | + runs-on: docker |
| 22 | + env: |
| 23 | + DEFAULT_BRANCH: ${{ vars.DEFAULT_BRANCH || 'main' }} |
| 24 | + steps: |
| 25 | + - name: Derive server and repo |
| 26 | + id: drv |
| 27 | + shell: bash |
| 28 | + run: | |
| 29 | + set -euo pipefail |
| 30 | + SERVER_URL="${GITHUB_SERVER_URL:-${GITEA_SERVER_URL:-}}" |
| 31 | + REPO_PATH="${GITHUB_REPOSITORY:-${GITEA_REPOSITORY:-}}" |
| 32 | + if [ -z "$SERVER_URL" ] || [ -z "$REPO_PATH" ]; then |
| 33 | + echo "Missing SERVER_URL or REPO_PATH from env (GITHUB_* or GITEA_*)" >&2 |
| 34 | + exit 1 |
| 35 | + fi |
| 36 | + echo "SERVER_URL=$SERVER_URL" >> "$GITHUB_OUTPUT" |
| 37 | + echo "REPO_PATH=$REPO_PATH" >> "$GITHUB_OUTPUT" |
| 38 | +
|
| 39 | + - name: Prepare repository clone |
| 40 | + shell: bash |
| 41 | + run: | |
| 42 | + set -euo pipefail |
| 43 | + git init work |
| 44 | + cd work |
| 45 | + git config user.name "gitea-actions" |
| 46 | + git config user.email "actions@localhost" |
| 47 | + ORIGIN_URL="${{ steps.drv.outputs.SERVER_URL }}/${{ steps.drv.outputs.REPO_PATH }}.git" |
| 48 | + git remote add origin "$ORIGIN_URL" |
| 49 | + git fetch --prune origin |
| 50 | + git checkout -B "$DEFAULT_BRANCH" "origin/$DEFAULT_BRANCH" || git checkout -B "$DEFAULT_BRANCH" |
| 51 | +
|
| 52 | + - name: List open PRs |
| 53 | + id: list |
| 54 | + shell: bash |
| 55 | + env: |
| 56 | + GITEA_TOKEN: ${{ secrets.GITEA_TOKEN }} |
| 57 | + run: | |
| 58 | + set -euo pipefail |
| 59 | + API_BASE="${{ steps.drv.outputs.SERVER_URL }}/api/v1" |
| 60 | + REPO="${{ steps.drv.outputs.REPO_PATH }}" |
| 61 | + AUTH=(-H "Authorization: token ${GITEA_TOKEN}") |
| 62 | + curl -sfSL "${API_BASE}/repos/${REPO}/pulls?state=open" "${AUTH[@]}" > prs.json |
| 63 | + echo "Pulled PR list:" >&2; wc -c prs.json >&2 || true |
| 64 | + # Extract: pr_number, head_branch, base_branch, head_repo_full_name |
| 65 | + python3 - << 'PY' < prs.json > pr_list.txt |
| 66 | +import sys, json |
| 67 | +data=json.load(sys.stdin) |
| 68 | +def repo_name(r): |
| 69 | + if not r: |
| 70 | + return '' |
| 71 | + # Try common fields |
| 72 | + return r.get('full_name') or ( |
| 73 | + (r.get('owner',{}).get('login','') + '/' + r.get('name','')).strip('/') |
| 74 | + ) |
| 75 | +for pr in data: |
| 76 | + num = pr.get('number') or pr.get('index') |
| 77 | + head = pr.get('head') if isinstance(pr.get('head'), str) else pr.get('head_branch') |
| 78 | + base = pr.get('base') if isinstance(pr.get('base'), str) else pr.get('base_branch') |
| 79 | + head_repo = repo_name(pr.get('head_repo') or pr.get('head_repo')) |
| 80 | + print(f"{num}\t{head or ''}\t{base or ''}\t{head_repo}") |
| 81 | +PY |
| 82 | + echo "PR list:" >&2; cat pr_list.txt >&2 || true |
| 83 | + |
| 84 | + - name: Rebase each PR branch |
| 85 | + shell: bash |
| 86 | + env: |
| 87 | + GITEA_TOKEN: ${{ secrets.GITEA_TOKEN }} |
| 88 | + run: | |
| 89 | + set -euo pipefail |
| 90 | + cd work |
| 91 | + API_BASE="${{ steps.drv.outputs.SERVER_URL }}/api/v1" |
| 92 | + REPO="${{ steps.drv.outputs.REPO_PATH }}" |
| 93 | + while IFS=$'\t' read -r PR_NUM HEAD_BRANCH BASE_BRANCH HEAD_REPO; do |
| 94 | + [ -n "$PR_NUM" ] || continue |
| 95 | + # Skip if head branch is empty |
| 96 | + if [ -z "$HEAD_BRANCH" ]; then |
| 97 | + echo "PR #$PR_NUM: no head branch reported; skipping" >&2 |
| 98 | + continue |
| 99 | + fi |
| 100 | + # Only rebase branches that live in this repo |
| 101 | + if [ -n "$HEAD_REPO" ] && [ "$HEAD_REPO" != "${REPO}" ]; then |
| 102 | + echo "PR #$PR_NUM: head repo is external ($HEAD_REPO); skipping" >&2 |
| 103 | + continue |
| 104 | + fi |
| 105 | + echo "Rebasing PR #$PR_NUM branch '$HEAD_BRANCH' onto '$DEFAULT_BRANCH'" >&2 |
| 106 | + git fetch origin "$DEFAULT_BRANCH" "$HEAD_BRANCH" || true |
| 107 | + if ! git show-ref --verify --quiet "refs/remotes/origin/$HEAD_BRANCH"; then |
| 108 | + echo "PR #$PR_NUM: origin/$HEAD_BRANCH not found; skipping" >&2 |
| 109 | + continue |
| 110 | + fi |
| 111 | + git checkout -B "$HEAD_BRANCH" "origin/$HEAD_BRANCH" |
| 112 | + set +e |
| 113 | + git rebase "origin/$DEFAULT_BRANCH" |
| 114 | + REBASE_STATUS=$? |
| 115 | + set -e |
| 116 | + if [ $REBASE_STATUS -ne 0 ]; then |
| 117 | + echo "PR #$PR_NUM: rebase conflict; aborting and commenting" >&2 |
| 118 | + git rebase --abort || true |
| 119 | + # Post a comment |
| 120 | + if [ -n "$GITEA_TOKEN" ]; then |
| 121 | + COMMENT_PAYLOAD=$(printf '{"body":"Auto-rebase failed due to conflicts. Please rebase onto %s and resolve conflicts."}' "$DEFAULT_BRANCH") |
| 122 | + curl -sfSL -X POST \ |
| 123 | + -H "Content-Type: application/json" \ |
| 124 | + -H "Authorization: token ${GITEA_TOKEN}" \ |
| 125 | + "${API_BASE}/repos/${REPO}/issues/${PR_NUM}/comments" \ |
| 126 | + -d "$COMMENT_PAYLOAD" || true |
| 127 | + # Ensure a 'needs-rebase' label exists |
| 128 | + LABELS_JSON=$(curl -sfSL -H "Authorization: token ${GITEA_TOKEN}" "${API_BASE}/repos/${REPO}/labels" || echo '[]') |
| 129 | + LABEL_EXISTS=$(echo "$LABELS_JSON" | grep -i '"name"\s*:\s*"needs-rebase"' || true) |
| 130 | + if [ -z "$LABEL_EXISTS" ]; then |
| 131 | + curl -sfSL -X POST \ |
| 132 | + -H "Content-Type: application/json" \ |
| 133 | + -H "Authorization: token ${GITEA_TOKEN}" \ |
| 134 | + "${API_BASE}/repos/${REPO}/labels" \ |
| 135 | + -d '{"name":"needs-rebase","color":"#b60205"}' || true |
| 136 | + fi |
| 137 | + # Add label to the PR |
| 138 | + curl -sfSL -X POST \ |
| 139 | + -H "Content-Type: application/json" \ |
| 140 | + -H "Authorization: token ${GITEA_TOKEN}" \ |
| 141 | + "${API_BASE}/repos/${REPO}/issues/${PR_NUM}/labels" \ |
| 142 | + -d '["needs-rebase"]' || true |
| 143 | + fi |
| 144 | + # Do not push in conflict case |
| 145 | + continue |
| 146 | + fi |
| 147 | + echo "PR #$PR_NUM: rebase succeeded; pushing with lease" >&2 |
| 148 | + git push --force-with-lease origin "$HEAD_BRANCH" |
| 149 | + done < ../pr_list.txt |
0 commit comments