• Stars
    star
    475
  • Rank 92,465 (Top 2 %)
  • Language
    TypeScript
  • License
    MIT License
  • Created almost 5 years ago
  • Updated over 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A GitHub action that facilitates "ChatOps" by creating repository dispatch events for slash commands

Slash Command Dispatch

CI GitHub Marketplace

A GitHub action that facilitates "ChatOps" by creating dispatch events for slash commands.

How does it work?

The action runs in issue_comment event workflows and checks the first line of comments for slash commands. When a valid command is found it creates a repository dispatch event that includes a payload containing full details of the command and its context. It also supports creating workflow dispatch events with defined input parameters.

Why create dispatch events?

"ChatOps" with slash commands can work in a basic way by parsing the commands during issue_comment events and immediately processing the command. In repositories with a lot of activity, the workflow queue will get backed up very quickly trying to handle new issue_comment events and process the commands themselves.

Dispatching commands to be processed elsewhere keeps the workflow queue moving quickly. It essentially enables parallel processing of workflows.

An additional benefit of dispatching is that it allows non-sensitive workloads to be run in public repositories to save using private repository GitHub Action minutes.

Demos

See it in action with the following live demos.

Documentation

Dispatching commands

Standard configuration

The following workflow should be configured in the repository where commands will be dispatched from. This example will respond to comments containing the slash commands /deploy, /integration-test and /build-docs.

name: Slash Command Dispatch
on:
  issue_comment:
    types: [created]
jobs:
  slashCommandDispatch:
    runs-on: ubuntu-latest
    steps:
      - name: Slash Command Dispatch
        uses: peter-evans/slash-command-dispatch@v3
        with:
          token: ${{ secrets.PAT }}
          commands: |
            deploy
            integration-test
            build-docs

Note that not specifying the repository input will mean that dispatch events are created in the current repository by default. It's perfectly fine to use the current repository and not dispatch events to a separate "processor" repository.

This action also features advanced configuration that allows each command to be configured individually if necessary. Use the standard configuration shown above unless you require advanced features.

Action inputs

Input Description Default
token (required) A repo scoped Personal Access Token (PAT). Note: GITHUB_TOKEN does not work here. See token for further details.
reaction-token GITHUB_TOKEN or a repo scoped Personal Access Token (PAT). See reaction-token for further details. GITHUB_TOKEN
reactions Add reactions. 👀 = seen, 🚀 = dispatched true
commands (required) A comma or newline separated list of commands.
permission The repository permission level required by the user to dispatch commands. See permission for further details. (none, read, triage, write, maintain, admin) write
issue-type The issue type required for commands. (issue, pull-request, both) both
allow-edits Allow edited comments to trigger command dispatches. false
repository The full name of the repository to send the dispatch events. Current repository
event-type-suffix The repository dispatch event type suffix for the commands. -command
static-args A comma or newline separated list of arguments that will be dispatched with every command.
dispatch-type The dispatch type; repository or workflow. See dispatch-type for further details. repository
config JSON configuration for commands. See Advanced configuration
config-from-file JSON configuration from a file for commands. See Advanced configuration

token

This action creates repository_dispatch and workflow_dispatch events. The default GITHUB_TOKEN does not have scopes to create these events, so a repo scoped PAT is required. If you will be dispatching commands to public repositories only then you can use the more limited public_repo scope.

When using the action in a GitHub organization, the user the PAT is created on must be a member of the organization. Additionally, the PAT should be given the org:read scope.

reaction-token

If you don't specify a token for reaction-token it will use the default GITHUB_TOKEN. Reactions to comments will then be made by the @github-actions bot user. You can use a PAT if you would prefer reactions to be made by the user account associated with the PAT.

      - name: Slash Command Dispatch
        uses: peter-evans/slash-command-dispatch@v3
        with:
          token: ${{ secrets.PAT }}
          reaction-token: ${{ secrets.PAT }}
          commands: |
            deploy
            integration-test
            build-docs

permission

This input sets the repository permission level required by the user to dispatch commands. It expects one of the five repository permission levels, or none. From the least to greatest permission level they are none, read, triage, write, maintain and admin.

Setting write as the required permission level means that any user with write, maintain or admin permission level will be able to execute commands.

Note that read, triage and maintain are only applicable to organization repositories. For repositories owned by a user account there are only two permission levels, the repository owner (admin) and collaborators (write).

There is a known issue with permissions when using nested teams in a GitHub organization. See here for further details.

dispatch-type

By default, the action creates repository_dispatch events. Setting dispatch-type to workflow will instead create workflow_dispatch events. There are significant differences in the action's behaviour when using workflow dispatch. See workflow dispatch for usage details.

For the majority of use cases, the default repository dispatch will likely be the most suitable for new workflows. If you already have workflow_dispatch workflows, you can execute them with slash commands using this action.

Repository Dispatch (default) Workflow Dispatch
Events are created with a client_payload giving the target workflow access to a wealth of useful context properties. A client_payload cannot be sent with workflow_dispatch events. The target workflow can only make use of up to 10 pre-defined inputs, the names of which must match named arguments supplied with the slash command.
Slash commands can only execute workflows in the target repository's default branch. Slash commands can execute workflows in any branch using the ref named argument. The reference can be a branch, tag, or a commit SHA. This can be useful to test workflows in PR branches before merging.
Immediate command validation feedback is unavailable when creating the dispatch event. Immediate command validation feedback is available as an action output.

How comments are parsed for slash commands

Slash commands must be placed in the first line of the comment to be interpreted as a command.

  • The command must start with a /
  • The slash command extends to the last non-whitespace character on the first line
  • Anything after the first line is ignored and can be freely used for comments

Comment Parsing

Handling dispatched commands

The following documentation applies to the dispatch-type default, repository, which creates repository_dispatch events. For workflow dispatch documentation, see workflow dispatch.

Event types

Repository dispatch events have a type to distinguish between events. The type set by the action is a combination of the slash command and event-type-suffix. The event-type-suffix input defaults to -command.

For example, if your slash command is integration-test, the event type will be integration-test-command.

on:
  repository_dispatch:
    types: [integration-test-command]

Accessing contexts

Commands are dispatched with a payload containing a number of contexts.

slash_command context

The slash command context contains the command and any arguments that were supplied by the user. It will also contain any static arguments if configured.

To demonstrate, take the following configuration as an example.

      - uses: peter-evans/slash-command-dispatch@v3
        with:
          token: ${{ secrets.PAT }}
          commands: |
            deploy
          static-args: |
            production
            region=us-east-1

For the above example configuration, the slash command /deploy branch=main dry-run reason="new feature" will be converted to a JSON payload as follows.

    "slash_command": {
        "command": "deploy",
        "args": {
            "all": "production region=us-east-1 branch=main dry-run reason=\"new feature\"",
            "unnamed": {
                "all": "production dry-run",
                "arg1": "production",
                "arg2": "dry-run"
            },
            "named": {
                "region": "us-east-1",
                "branch": "main",
                "reason": "new feature"
            },
        }
    }

The properties in the slash_command context from the above example can be used in a workflow as follows.

      - name: Output command and arguments
        run: |
          echo ${{ github.event.client_payload.slash_command.command }}
          echo ${{ github.event.client_payload.slash_command.args.all }}
          echo ${{ github.event.client_payload.slash_command.args.unnamed.all }}
          echo ${{ github.event.client_payload.slash_command.args.unnamed.arg1 }}
          echo ${{ github.event.client_payload.slash_command.args.unnamed.arg2 }}
          echo ${{ github.event.client_payload.slash_command.args.named.region }}
          echo ${{ github.event.client_payload.slash_command.args.named.branch }}
          echo ${{ github.event.client_payload.slash_command.args.named.reason }}
          # etc.

github and pull_request contexts

The payload contains the github context of the issue_comment event at path github.event.client_payload.github. Additionally, if the comment was made in a pull request, the action calls the GitHub API to fetch the pull request detail and attach it to the payload at path github.event.client_payload.pull_request.

You can inspect the payload with the following step.

      - name: Dump the client payload context
        env:
          PAYLOAD_CONTEXT: ${{ toJson(github.event.client_payload) }}
        run: echo "$PAYLOAD_CONTEXT"

Note that the client_payload.github.payload.issue.body and client_payload.pull_request.body context properties will be truncated if they exceed 1000 characters.

Responding to the comment on command completion

Using create-or-update-comment action there are a number of ways you can respond to the comment once the command has completed.

The simplest response is to add a 🎉 reaction to the comment.

      - name: Add reaction
        uses: peter-evans/create-or-update-comment@v2
        with:
          token: ${{ secrets.PAT }}
          repository: ${{ github.event.client_payload.github.payload.repository.full_name }}
          comment-id: ${{ github.event.client_payload.github.payload.comment.id }}
          reaction-type: hooray

Another option is to reply with a new comment containing a link to the run output.

      - name: Create URL to the run output
        id: vars
        run: echo "run-url=https://github.com/$GITHUB_REPOSITORY/actions/runs/$GITHUB_RUN_ID" >> $GITHUB_OUTPUT

      - name: Create comment
        uses: peter-evans/create-or-update-comment@v2
        with:
          token: ${{ secrets.PAT }}
          repository: ${{ github.event.client_payload.github.payload.repository.full_name }}
          issue-number: ${{ github.event.client_payload.github.payload.issue.number }}
          body: |
            [Command run output][1]

            [1]: ${{ steps.vars.outputs.run-url }}

License

MIT

More Repositories

1

create-pull-request

A GitHub action to create a pull request for changes to your repository in the actions workspace
TypeScript
1,480
star
2

repository-dispatch

A GitHub action to create a repository dispatch event
TypeScript
721
star
3

docker-compose-healthcheck

How to wait for container X before starting Y using docker-compose healthcheck
623
star
4

create-or-update-comment

A GitHub action to create or update an issue or pull request comment
TypeScript
431
star
5

swagger-github-pages

How to host Swagger API documentation with GitHub Pages
HTML
289
star
6

dockerhub-description

A GitHub action to update a Docker Hub repository description from README.md
TypeScript
258
star
7

docker-compose-actions-workflow

GitHub Actions workflow example using Docker Compose to build and test a multi-container stack
Python
248
star
8

find-comment

A GitHub action to find an issue or pull request comment
TypeScript
108
star
9

create-issue-from-file

A GitHub action to create an issue using content from a file
TypeScript
103
star
10

lightweight-architecture-decision-records

Lightweight Architecture Decision Records
89
star
11

commit-comment

A GitHub action to create a comment for a commit on GitHub
JavaScript
86
star
12

autopep8

A GitHub action for autopep8, a tool that automatically formats Python code to conform to the PEP 8 style guide.
Python
76
star
13

enable-pull-request-automerge

A GitHub action to enable auto-merge on a pull request
72
star
14

nominatim-k8s

Nominatim for Kubernetes on Google Container Engine (GKE).
Shell
60
star
15

s3-backup

A GitHub action to mirror a repository to S3 compatible object storage
Shell
56
star
16

create-or-update-project-card

A GitHub action to create or update a project card
TypeScript
54
star
17

link-checker

A GitHub action for link checking repository Markdown and HTML files
Shell
48
star
18

rebase

A GitHub action to rebase pull requests in a repository
TypeScript
38
star
19

smoke-testing

Smoke testing Docker containers with CircleCI
Kotlin
35
star
20

osrm-backend-k8s

Open Source Routing Machine (OSRM) osrm-backend for Kubernetes on Google Container Engine (GKE).
Shell
35
star
21

sendgrid-action

A GitHub Action to send email with SendGrid
Dockerfile
26
star
22

close-issue

A GitHub action to close an issue
23
star
23

locust-docker

Docker image for the Locust load testing tool and sample Kubernetes configuration files for distributed deployment.
Shell
19
star
24

vegeta-docker

Docker image for the Vegeta HTTP load testing tool
Dockerfile
18
star
25

kotlin-jib

Containerising Kotlin with Jib
Kotlin
17
star
26

kdef

Declarative resource management for Kafka
Go
17
star
27

close-pull

A GitHub action to close a pull request and optionally delete its branch.
16
star
28

osrm-backend-docker

Docker image for the Open Source Routing Machine (OSRM) osrm-backend
Dockerfile
14
star
29

kong-oauth2-consent-app

A consent application for OAuth 2.0 Authorization Code Grant flow with Kong
Go
13
star
30

rust-wasm-action

Rust-generated WebAssembly GitHub action template
Rust
12
star
31

paseto-lua

PASETO (Platform-Agnostic Security Tokens) for Lua
Lua
11
star
32

postman-pre-request

Postman Pre-request script for HMAC Authentication with Kong
JavaScript
11
star
33

mutation-testing

Mutation Testing – featuring an example using Stryker, a framework for the JavaScript ecosystem
JavaScript
10
star
34

slash-command-dispatch-processor

A command processor for slash-command-dispatch, a GitHub action that facilitates "ChatOps"
8
star
35

close-fork-pulls

A GitHub action to close pull requests from forks
TypeScript
6
star
36

duplicati-action

A GitHub action for Duplicati - Store securely encrypted backups in the cloud!
Dockerfile
5
star
37

gradle-auto-dependency-updates

How to automate Gradle dependency updates with GitHub Actions
Kotlin
5
star
38

erc20-token-wallet

A simple Ethereum blockchain ERC20 token wallet interface
JavaScript
5
star
39

patience

Go implementation of the Patience Diff algorithm
Go
4
star
40

kong-plugin-paseto

Kong plugin for PASETO (Platform-Agnostic Security Tokens)
Lua
4
star
41

python-action

A template to bootstrap the creation of a multi-platform Python GitHub action
JavaScript
4
star
42

blog

The blog of Peter Evans
CSS
3
star
43

create-pull-request-tests

Tests for create-pull-request action
3
star
44

gaps-and-islands

Gaps and islands: Merging contiguous ranges
1
star
45

soft-thresholding

Candidate selection using an iterative soft-thresholding algorithm
Python
1
star
46

create-pull-request-tests-remote

Remote repository for create-pull-request action tests
1
star
47

kong-hmac-python

Python module for HMAC Authentication with Kong
Python
1
star
48

curl-jq-docker

An alpine based Docker image with curl and jq
Dockerfile
1
star
49

kubernetes-travis-template

A template project for Kubernetes services development and integration testing
Shell
1
star