Repository navigation
76 lines (69 loc) · 2.84 KB
/
Copy pathrebase.yaml
File metadata and controls
76 lines (69 loc) · 2.84 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
name: Automatic Rebase
on:
issue_comment:
types: [created]
permissions:
contents: read
jobs:
rebase:
name: Rebase
runs-on: ubuntu-latest
# Added security: Only allow Repo Owners, Members, or Collaborators to trigger
if: >-
github.event.issue.pull_request != '' &&
(
github.event.comment.author_association == 'OWNER' ||
github.event.comment.author_association == 'MEMBER' ||
github.event.comment.author_association == 'COLLABORATOR'
) &&
(
startsWith(github.event.comment.body, '/rebase') ||
startsWith(github.event.comment.body, '/autosquash')
)
steps:
- name: Checkout the latest code
uses: actions/checkout@v4
with:
fetch-depth: 0 # rebase needs full history to find the merge base
persist-credentials: false
# Same steps as the now-archived cirrus-actions/rebase@1.8.
- name: Automatic Rebase
env:
GH_TOKEN: ${{ secrets.REBASE_TOKEN }}
PR_NUMBER: ${{ github.event.issue.number }}
COMMENTER: ${{ github.event.comment.user.login }}
AUTOSQUASH: ${{ contains(github.event.comment.body, '/autosquash') || contains(github.event.comment.body, '/rebase-autosquash') }}
run: |
set -euo pipefail
# GitHub computes `rebaseable` lazily, so retry while it is null.
for _ in 1 2 3 4 5 6; do
pr=$(gh api "repos/$GITHUB_REPOSITORY/pulls/$PR_NUMBER")
rebaseable=$(jq -r .rebaseable <<< "$pr")
[ "$rebaseable" != "null" ] && break
echo "The PR is not ready to rebase, retry after 10 seconds"
sleep 10
done
if [ "$rebaseable" != "true" ]; then
echo "GitHub doesn't think that the PR is rebaseable!"
exit 1
fi
base_branch=$(jq -r .base.ref <<< "$pr")
head_repo=$(jq -r .head.repo.full_name <<< "$pr")
head_branch=$(jq -r .head.ref <<< "$pr")
user=$(gh api "users/$COMMENTER")
name=$(jq -r '.name // empty' <<< "$user")
email=$(jq -r '.email // empty' <<< "$user")
git config user.name "${name:-$COMMENTER} (Rebase PR Action)"
git config user.email "${email:-$COMMENTER@users.noreply.github.com}"
# Git asks gh for credentials, and gh reads GH_TOKEN from env.
gh auth setup-git
git remote add fork "https://github.com/$head_repo.git"
git fetch origin "$base_branch"
git fetch fork "$head_branch"
git checkout -b rebase-target "fork/$head_branch"
if [ "$AUTOSQUASH" = "true" ]; then
GIT_SEQUENCE_EDITOR=: git rebase -i --autosquash "origin/$base_branch"
else
git rebase "origin/$base_branch"
fi
git push --force-with-lease fork "rebase-target:$head_branch"