|
| 1 | +# Examples |
| 2 | + |
| 3 | +This document details some command patterns and examples. |
| 4 | + |
| 5 | +- [Use case: Execute command using a specific repository branch](#execute-command-using-a-specific-repository-branch) |
| 6 | +- [Use case: Execute command to modify a pull request branch](#execute-command-to-modify-a-pull-request-branch) |
| 7 | + |
| 8 | +## Use case: Execute command using a specific repository branch |
| 9 | + |
| 10 | +This is pattern for a slash command where the first argument is the branch to checkout. If no argument is given it defaults to `master`. For example, the following command will cause the command workflow to checkout the `develop` branch of the repository where the command was dispatched from. After the branch has been checked out in the command workflow, scripts or actions may be executed against it. |
| 11 | + |
| 12 | +``` |
| 13 | +/do-something develop |
| 14 | +``` |
| 15 | + |
| 16 | +In the following command workflow, `REPO_ACCESS_TOKEN` is a `repo` scoped [Personal Access Token](https://help.github.com/en/github/authenticating-to-github/creating-a-personal-access-token-for-the-command-line). |
| 17 | + |
| 18 | +```yml |
| 19 | +name: do-something-command |
| 20 | +on: |
| 21 | + repository_dispatch: |
| 22 | + types: [do-something-command] |
| 23 | +jobs: |
| 24 | + doSomething: |
| 25 | + runs-on: ubuntu-latest |
| 26 | + steps: |
| 27 | + # Get the branch name |
| 28 | + - name: Get the target branch name |
| 29 | + id: vars |
| 30 | + run: | |
| 31 | + branch=${{ github.event.client_payload.slash_command.arg1 }} |
| 32 | + if [[ -z "$branch" ]]; then branch="master"; fi |
| 33 | + echo ::set-output name=branch::$branch |
| 34 | +
|
| 35 | + # Checkout the branch to test |
| 36 | + - uses: actions/checkout@v2 |
| 37 | + with: |
| 38 | + token: ${{ secrets.REPO_ACCESS_TOKEN }} |
| 39 | + repository: ${{ github.event.client_payload.github.payload.repository.full_name }} |
| 40 | + ref: ${{ steps.vars.outputs.branch }} |
| 41 | + |
| 42 | + # Execute scripts or actions |
| 43 | + - name: Do something |
| 44 | + run: echo "Do something" |
| 45 | + |
| 46 | + # Add reaction to the comment |
| 47 | + - name: Add reaction |
| 48 | + uses: peter-evans/create-or-update-comment@v1 |
| 49 | + with: |
| 50 | + token: ${{ secrets.REPO_ACCESS_TOKEN }} |
| 51 | + repository: ${{ github.event.client_payload.github.payload.repository.full_name }} |
| 52 | + comment-id: ${{ github.event.client_payload.github.payload.comment.id }} |
| 53 | + reaction-type: hooray |
| 54 | +``` |
| 55 | +
|
| 56 | +## Use case: Execute command to modify a pull request branch |
| 57 | +
|
| 58 | +This is pattern for a slash command used in pull request comments. It checks out the pull request branch and allows further script and action steps to modify it. |
| 59 | +
|
| 60 | +``` |
| 61 | +/fix-pr |
| 62 | +``` |
| 63 | + |
| 64 | +In the following command workflow, `REPO_ACCESS_TOKEN` is a `repo` scoped [Personal Access Token](https://help.github.com/en/github/authenticating-to-github/creating-a-personal-access-token-for-the-command-line). |
| 65 | + |
| 66 | +```yml |
| 67 | +name: fix-pr-command |
| 68 | +on: |
| 69 | + repository_dispatch: |
| 70 | + types: [fix-pr-command] |
| 71 | +jobs: |
| 72 | + fixPr: |
| 73 | + runs-on: ubuntu-latest |
| 74 | + steps: |
| 75 | + # Checkout the pull request branch |
| 76 | + - uses: actions/checkout@v2 |
| 77 | + with: |
| 78 | + token: ${{ secrets.REPO_ACCESS_TOKEN }} |
| 79 | + repository: ${{ github.event.client_payload.pull_request.head.repo.full_name }} |
| 80 | + ref: ${{ github.event.client_payload.pull_request.head.ref }} |
| 81 | + |
| 82 | + # Commit changes to the PR branch |
| 83 | + - name: Commit changes to the PR branch |
| 84 | + run: | |
| 85 | + # TODO Make changes to commit here |
| 86 | + # |
| 87 | + git config --global user.name 'actions-bot' |
| 88 | + git config --global user.email '58130806+actions-bot@users.noreply.github.com' |
| 89 | + git commit -am "[fix-pr-command] fixes" |
| 90 | + git push |
| 91 | +
|
| 92 | + - name: Add reaction |
| 93 | + uses: peter-evans/create-or-update-comment@v1 |
| 94 | + with: |
| 95 | + token: ${{ secrets.REPO_ACCESS_TOKEN }} |
| 96 | + repository: ${{ github.event.client_payload.github.payload.repository.full_name }} |
| 97 | + comment-id: ${{ github.event.client_payload.github.payload.comment.id }} |
| 98 | + reaction-type: hooray |
| 99 | +``` |
0 commit comments