test commit rebase #10
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: autosquash workflow | |
on: | |
issue_comment: | |
types: [created] | |
jobs: | |
autosquash: | |
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 # otherwise, you will fail to push refs to dest repo | |
- 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 |