Skip to content

mlg87/pr-reviewer-slack-notify-action

Use this GitHub action with your project
Add this Action to an existing workflow or create a new one
View on Marketplace

Repository files navigation

PR Reviewer Slack Notify Action

This action will post a message to a channel in Slack @ing the requested reviewers when a PR is opened. This is very easy to set up, but will require you to create an app in Slack as well where you can create incoming webhooks for your desired destinations.

Setting up the Slack app

Create a Slack App with Bots and Permissions functionality. It is worth noting that you will need administrator privileges for this, so hit up your IT team in advance. Once the app is created install it to your workspace and invite the bot to the channel you would messages posted to.

You will need to make sure that the bot has the correct minimum required scopes:

  • chat:write
  • reactions:read
  • reactions:write
NOTE: previous versions of this action relied on Incoming Webhooks. In order to post to the message thread and have the bot update reactions, it is now required to provide this permissions

AWS

This no longer accepts slack-users as an input. Instead this action expects you to have a JSON file stored somewhere in S3 that has this shape:

{
  "engineers": [
    { "github_username": "myGithubHandle", "slack_id": "U123456789" }
  ]
}

This makes it much easier to manage using this action at scale where the same engineers are working in many repos. You will need to create an IAM user with permission to get this object from this particular bucket in S3.

Adding the action to your project

In your ./github/workflows directory, add a slackNotify.yml (or whatever the hell you want to call it) file. Add the following as contents:

name: PR Review Slack Notify
on:
  pull_request:
    types: [opened, ready_for_review]
  pull_request_review:
    types: [submitted]
  push:

# IMPORTANT! in order for the aws sdk to auth correctly these keys/values need to be exposed here
env:
  AWS_ACCESS_KEY_ID: ${{ secrets.YOUR_AWS_ACCESS_KEY_ID }}
  AWS_SECRET_ACCESS_KEY: ${{ secrets.YOUR_AWS_SECRET_ACCESS_KEY }}

jobs:
  notify:
    runs-on: ubuntu-latest
    name: PR Review Slack Notify
    steps:
      - name: Send slack notifications to requested reviewers
        id: pr-slack-notify
        uses: mlg87/[email protected]
        with:
          aws-region: "us-west-2"
          aws-s3-bucket: "my-bucket"
          aws-s3-object-key: "path/to/json/file/engineer-github-slack-mapping.json"
          base-branch: "staging"
          bot-token: ${{ secrets.SLACK_BOT_TOKEN }}
          channel-id: "[GET_THIS_FROM_SLACK]"
          github-token: ${{ secrets.GH_TOKEN }}
          verbose: false

Action inputs

Input Description Required Type Default
aws-region Region in which the Github <=> Slack engineer mapping JSON is stored true String n/a
aws-s3-bucket Bucket in which the Github <=> Slack engineer mapping JSON object is stored in S3 true String n/a
aws-s3-object-key Name of the Github <=> Slack engineer mapping JSON object in S3 true String n/a
base-branch Branch name that your PRs will be opened in to. true String n/a
bot-token OAuth token from Slack. Find this on your Slack App's settings page under OAuth & Permissions. true String n/a
channel-id The id of the channel you would like messages posted to. It is easiest to find this by opening your Slack workspace in the browser and grabbing it from the URL. true String n/a
github-token Personal access token generated by Github. Can be an individual user's or one generated for a bot. true String n/a
label-name-to-watch-for Optional label to watch for to notify thread of activity false String ''
verbose Optional feature to shorten the slack message. Verbose: true will post the PR body. Verbose: false will not. false String true
NOTE: It is recommended to store the webhook in GitHub Secrets

How to use

I intended to make this work for the pull_request.review_requested event. However, keying off that event will end up spamming your Slack channel you are posting to. This happens because it will include all added reviewers at the time it runs, so if you add user1 as a reviewer and then search for and add user2, it will run the action twice with arrays of requested_reviewers: [user1, user2], and since there is no way to keep track of who has already been notified, this can send n messages.

Solution: only run this action on pull_request.opened and pull_request.ready_for_review. Somewhat annoying that this requires devs to use a specific flow in order for the messages to go out, but it's the best idea I've got right now. Just make sure reviewers are assigned prior to opening the PR or converting a draft to ready.

An example of a PR lifecyle and how the bot works:

  1. User gets ready to open PR and assigns reviewers prior to opening the PR.
  2. User opens PR.
  3. Action runs, and posts a message in the provided Slack channel mentioning the requested reviewers in a message that includes a link to the PR, the PR title and the PR body (description/first comment) if it is provided.
  4. One of the reviewers requests changes to the PR, so the action adds the stop sign reaction to the original thread and mentions the PR author in a message in the thread that changes have been requested. The body (main comment associated with the review) is included in the message.
  5. PR owner pushes up code changes, so action removes reactions from the thread and notifies the requested reviewers that there is new code and they should go check it out.
  6. Someone appoves! Action updates the reactions accordingly and notifies the PR owner.
  7. The code gets merged into the base-branch, so action updates the thread and reactions.

K, bye.