├── Dockerfile ├── LICENSE ├── README.md └── entrypoint.sh /Dockerfile: -------------------------------------------------------------------------------- 1 | FROM alpine:latest 2 | 3 | LABEL repository="http://github.com/srt32/revert" 4 | LABEL homepage="http://github.com/srt32/revert" 5 | LABEL "com.github.actions.name"="Automatic Revert" 6 | LABEL "com.github.actions.description"="Automatically revert a commit on '/revert' comment" 7 | LABEL "com.github.actions.icon"="git-pull-request" 8 | LABEL "com.github.actions.color"="red" 9 | 10 | RUN apk --no-cache add jq bash curl git 11 | 12 | ADD entrypoint.sh /entrypoint.sh 13 | ENTRYPOINT ["/entrypoint.sh"] 14 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2019 Simon Taranto 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # GitHub Action to revert a commit via a comment 2 | 3 | After installation, comment `/revert ` to trigger the action. 4 | 5 | ![revert](https://user-images.githubusercontent.com/2181356/52225171-027d0100-2867-11e9-90a5-84073c790f0b.gif) 6 | 7 | 8 | ## Installation 9 | 10 | ``` 11 | name: Automatic Revert 12 | 13 | on: 14 | issue_comment: 15 | types: [created] 16 | 17 | jobs: 18 | revert-commit: 19 | 20 | runs-on: ubuntu-latest 21 | 22 | if: contains(github.event.comment.body, '/revert') 23 | 24 | steps: 25 | - name: Checkout latest code 26 | uses: actions/checkout@v2 27 | - name: Automatic Revert 28 | uses: srt32/revert@v0.0.1 29 | env: 30 | GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} 31 | ``` 32 | 33 | This Action is heavily inspired by [rebase](https://github.com/cirrus-actions/rebase). 34 | -------------------------------------------------------------------------------- /entrypoint.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | set -e 4 | 5 | # skip if no /revert 6 | echo "Checking if contains '/revert' command..." 7 | (jq -r ".comment.body" "$GITHUB_EVENT_PATH" | grep -E "/revert") || exit 78 8 | 9 | # skip if not a PR 10 | echo "Checking if a PR command..." 11 | (jq -r ".issue.pull_request.url" "$GITHUB_EVENT_PATH") || exit 78 12 | 13 | # get the SHA to revert 14 | COMMIT_TO_REVERT=$(jq -r ".comment.body" "$GITHUB_EVENT_PATH" | cut -c 9-) 15 | 16 | if [[ "$(jq -r ".action" "$GITHUB_EVENT_PATH")" != "created" ]]; then 17 | echo "This is not a new comment event!" 18 | exit 78 19 | fi 20 | 21 | PR_NUMBER=$(jq -r ".issue.number" "$GITHUB_EVENT_PATH") 22 | REPO_FULLNAME=$(jq -r ".repository.full_name" "$GITHUB_EVENT_PATH") 23 | echo "Collecting information about PR #$PR_NUMBER of $REPO_FULLNAME..." 24 | 25 | if [[ -z "$GITHUB_TOKEN" ]]; then 26 | echo "Set the GITHUB_TOKEN env variable." 27 | exit 1 28 | fi 29 | 30 | URI=https://api.github.com 31 | API_HEADER="Accept: application/vnd.github.v3+json" 32 | AUTH_HEADER="Authorization: token $GITHUB_TOKEN" 33 | 34 | pr_resp=$(curl -X GET -s -H "${AUTH_HEADER}" -H "${API_HEADER}" \ 35 | "${URI}/repos/$REPO_FULLNAME/pulls/$PR_NUMBER") 36 | 37 | HEAD_REPO=$(echo "$pr_resp" | jq -r .head.repo.full_name) 38 | HEAD_BRANCH=$(echo "$pr_resp" | jq -r .head.ref) 39 | 40 | git remote set-url origin https://x-access-token:$GITHUB_TOKEN@github.com/$REPO_FULLNAME.git 41 | git config --global user.email "revert@github.com" 42 | git config --global user.name "GitHub Revert Action" 43 | 44 | set -o xtrace 45 | 46 | git fetch origin $HEAD_BRANCH 47 | 48 | # do the revert 49 | git checkout -b $HEAD_BRANCH origin/$HEAD_BRANCH 50 | 51 | # check commit exists 52 | git cat-file -t $COMMIT_TO_REVERT 53 | git revert $COMMIT_TO_REVERT --no-edit 54 | git push origin $HEAD_BRANCH 55 | --------------------------------------------------------------------------------