forked from openshift-eng/elliott
-
Notifications
You must be signed in to change notification settings - Fork 0
117 lines (98 loc) · 4.58 KB
/
autosquash.yaml
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
name: autosquash workflow
on:
issue_comment:
types: [created]
jobs:
rebase:
name: Rebase
runs-on: ubuntu-latest
if: >-
github.event.issue.pull_request != '' &&
(
contains(github.event.comment.body, '/rebase') ||
contains(github.event.comment.body, '/autosquash')
)
steps:
- name: Checkout the latest code
uses: actions/checkout@v3
with:
token: ${{ secrets.GITHUB_TOKEN }}
fetch-depth: 0
- name: Automatic Rebase
uses: cirrus-actions/[email protected]
with:
autosquash: ${{ contains(github.event.comment.body, '/autosquash') || contains(github.event.comment.body, '/rebase-autosquash') }}
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
squashcommits:
name: squashcommits action
if: github.event.issue.pull_request != '' && contains(github.event.comment.body, '/squashcommits')
runs-on: ubuntu-latest
steps:
- name: Checkout the latest code
uses: actions/checkout@v3
with:
token: ${{ secrets.GITHUB_TOKEN }}
fetch-depth: 0
- name: squashcommits
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
PR_NUMBER=$(jq -r ".issue.number" "$GITHUB_EVENT_PATH")
COMMENT_BODY=$(jq -r '.comment.body | gsub("\r\n"; "\n")' "$GITHUB_EVENT_PATH")
# Grab the old commit message and use it if there is nothing else
# But really only handling of the message is required now, and a lot of cleanup
echo "Softfixing #$PR_NUMBER in $GITHUB_REPOSITORY"
if [[ -z "$GITHUB_TOKEN" ]]; then
echo "Set a github token"
exit 1
fi
URI="https://api.github.com"
API_HEADER="Accept: application/vnd.github.v3+json"
AUTH_HEADER="Authorization: token $GITHUB_TOKEN"
pr_response=$(curl -s -H "${AUTH_HEADER}" -H "${API_HEADER}" \
"${URI}/repos/$GITHUB_REPOSITORY/pulls/$PR_NUMBER")
COMMITS_URL=$(echo "$pr_response" | jq -r .commits_url)
commits_response=$(curl -s -H "${AUTH_HEADER}" -H "${API_HEADER}" $COMMITS_URL)
# This is limited to 250 entries, but it should be okay
N_COMMITS=$(echo $commits_response | jq -r length)
# /squashcommits ``` ... ```
COMMIT_MSG=$(jq -rRs 'match("(?<!\\S)/squashcommits\\n```\\n(.*?)\\n```(?:\\n|\\z)"; "m").captures[0].string' <<<"$COMMENT_BODY")
if [[ -z "$COMMIT_MSG" ]] && [[ "$N_COMMITS" -eq 1 ]]; then
echo "Nothing to do here, aborting..."
exit 0
fi
USER_LOGIN=$(jq -r ".comment.user.login" "$GITHUB_EVENT_PATH")
user_response=$(curl -s -H "${AUTH_HEADER}" -H "${API_HEADER}" \
"${URI}/users/${USER_LOGIN}")
USER_NAME=$(echo "$user_response" | jq -r ".name")
if [[ "$USER_NAME" == "null" ]]; then
USER_NAME=$USER_LOGIN
fi
USER_NAME="${USER_NAME} (Softfix Action)"
USER_EMAIL=$(echo "$user_response" | jq -r ".email")
if [[ "$USER_EMAIL" == "null" ]]; then
USER_EMAIL="[email protected]"
fi
HEAD_REPO=$(echo "$pr_response" | jq -r .head.repo.full_name)
HEAD_BRANCH=$(echo "$pr_response" | jq -r .head.ref)
USER_TOKEN=${USER_LOGIN}_TOKEN
COMMITTER_TOKEN=${!USER_TOKEN:-$GITHUB_TOKEN}
git config --global --add safe.directory /github/workspace
git remote set-url origin https://x-access-token:[email protected]/$GITHUB_REPOSITORY.git
git config --global user.email "$USER_EMAIL"
git config --global user.name "$USER_NAME"
git remote add fork https://x-access-token:[email protected]/$HEAD_REPO.git
git fetch fork $HEAD_BRANCH
git checkout -b $HEAD_BRANCH fork/$HEAD_BRANCH
#if [[ -z "$COMMIT_MSG" ]] && jq -eRs 'test("(?<!\\S)/softfix:squash\\b")' <<<"$COMMENT_BODY" >/dev/null; then
# /softfix:squash: GitHub's Squash and merge style
#COMMIT_MSG=$(git log --reverse --pretty=format:"* %B" "HEAD~$N_COMMITS..HEAD" | tail -c +3)
#fi
git reset --soft HEAD~$(($N_COMMITS-1))
if [[ -z "$COMMIT_MSG" ]]; then
git commit --amend --no-edit
else
git commit --amend -m "$COMMIT_MSG"
fi
git push --force-with-lease fork $HEAD_BRANCH