Automate custom commands on pull requests using GitHub Actions.
This GitHub Action allows you to execute ad-hoc commands on pull requests dynamically in response to comments. It provides a flexible way to trigger specific tasks based on user interactions.
gitGraph
commit "1"
commit "2"
branch develop
commit "3"
commit "4"
commit "5"
checkout main
commit "6"
commit "7"
checkout develop
commit "(rebase and squash here)"
merge main
sequenceDiagram
participant MainBranch as Main Branch
participant NewBranch as New Branch
participant User as User
participant PullRequest as Pull Request
MainBranch ->> NewBranch: Checkout new branch
activate NewBranch
User ->> NewBranch: Make changes
User ->> NewBranch: Commit changes
User ->> NewBranch: Push changes to remote
deactivate NewBranch
NewBranch ->> MainBranch: Create Pull Request (PR)
activate PullRequest
User ->> PullRequest: Add comment with /rebase, /autosquash, or /rebase-autosquash
deactivate PullRequest
note over MainBranch, PullRequest: Based on comment, perform corresponding action:
note over NewBranch, PullRequest: Rebase:
alt
PullRequest ->> NewBranch: /rebase
MainBranch ->> NewBranch: Rebased
end
note over NewBranch, PullRequest: Autosquash:
alt
PullRequest ->> NewBranch: /autosquash
NewBranch ->> NewBranch: Autosquash
end
note over NewBranch, PullRequest: Rebase and Autosquash:
alt
PullRequest ->> NewBranch: /rebase-autosquash
NewBranch ->> NewBranch: Autosquash
MainBranch ->> NewBranch: Rebased
end
- The main branch initiates the creation of a new branch.
- The user makes changes in the new branch, commits them, and pushes the changes to the remote repository.
- A pull request is created from the new branch to the main branch.
- The user adds a comment to the pull request with one of the following commands:
/rebase
,/autosquash
, or/rebase-autosquash
.
- If the comment contains
/rebase
, the main branch rebases onto the new branch. - If the comment contains
/autosquash
, autosquashing is performed in the new branch. - If the comment contains
/rebase-autosquash
, both rebase and autosquash actions are executed.
Create a GitHub Actions workflow YAML file, for example, .github/workflows/adhoc_commands.yml
:
name: PR Adhoc commands
on:
issue_comment:
types: [created]
jobs:
build:
name: PR-Adhoc-Commands
runs-on: ubuntu-latest
if: >-
github.event.issue.pull_request != '' &&
(
contains(github.event.comment.body, '/rebase') ||
contains(github.event.comment.body, '/autosquash') ||
contains(github.event.comment.body, '/rebase-autosquash')
)
steps:
- name: Checkout the latest code
uses: actions/checkout@v3
with:
token: ${{ secrets.GITHUB_TOKEN }}
fetch-depth: 0
- name: Running Adhoc commands
uses: GirishCodeAlchemy/githubaction-runtime-adhoc-pr-commands@v1
with:
autosquash: ${{ contains(github.event.comment.body, '/autosquash') || contains(github.event.comment.body, '/rebase-autosquash') }}
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
sequenceDiagram
participant User
participant GitHubEvent
participant GitHubActions
participant CheckoutAction
participant AdhocCommandsAction
User->>GitHubEvent: Create PR Comment with /rebase or /autosquash or /rebase-autosquash
GitHubEvent-->>GitHubActions: Trigger 'created' event on issue_comment
alt Check if PR Comment triggers action
GitHubActions-->>GitHubActions: Check if PR comment contains specific commands
GitHubActions-->>CheckoutAction: Checkout the latest code
CheckoutAction-->>AdhocCommandsAction: Running Adhoc commands
AdhocCommandsAction-->>GitHubActions: Adhoc commands executed successfully
GitHubActions-->>GitHubEvent: Complete Workflow
else
GitHubActions-->>GitHubEvent: Ignore event, no specific commands found
end
- The user creates a pull request comment with one of the commands:
/rebase
,/autosquash
, or/rebase-autosquash
.
- The GitHub event is triggered, specifically the 'created' event on
issue_comment
.
- GitHub Actions checks if the pull request comment contains specific commands.
- If commands are found, it proceeds to the next step; otherwise, the workflow is ignored.
- GitHub Actions checks out the latest code.
- Adhoc Commands Action is triggered to run ad-hoc commands based on the user's comment.
- Adhoc commands are executed successfully, performing actions such as rebase, autosquash, or both.
- GitHub Actions acknowledges the successful execution.
- The workflow is completed, and GitHub Actions updates the GitHub event accordingly.
- If no specific commands are found in the PR comment, GitHub Actions ignores the workflow.