Post

Slack notification for GitHub Pull Requests with status updates

Use GitHub Actions for Slack notifications of your Pull Requests including the PR status updates

Slack notification for GitHub Pull Requests with status updates

When working on code and collaborating with teammates, it can be helpful to set up Slack notifications whenever a new GitHub Pull Request is created. This is a widely recognized best practice, and many people use the slack-github-action to implement it. Less commonly used, however, are Slack reactions for Pull Request updates.

Here’s a screencast showing what the Slack notification with status updates looks like:

Slack notification for GitHub Pull Requests with status updates

In this article, I’ll walk you through setting up Slack notifications for GitHub Pull Requests with status updates using GitHub Actions.

Requirements

  • The first step is to create GitHub Action secrets MY_SLACK_BOT_TOKEN and MY_SLACK_CHANNEL_ID. You can find detailed instructions for this in the slack-github-action repository.
  • Next, create a new GitHub Action workflow file named .github/workflows/pr-slack-notification.yml with the following content:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
name: pr-slack-notification

# Based on: https://github.com/slackapi/slack-github-action/issues/269
# Description: https://ruzickap.github.io/posts/slack-notification-pull-request/

on:
  workflow_dispatch:
  pull_request:
    types:
      - opened
      - ready_for_review
      - review_requested
      - closed
  issue_comment:
    types:
      - created
  pull_request_review:
    types:
      - submitted

permissions: read-all

defaults:
  run:
    shell: bash -euxo pipefail {0}

jobs:
  debug:
    runs-on: ubuntu-latest
    steps:
      - name: Debug
        env:
          GITHUB_CONTEXT: $
        run: |
          echo "${GITHUB_CONTEXT}"

  pr-slack-notification:
    runs-on: ubuntu-latest
    name: Sends a message to Slack when a PR is opened
    if: (github.event.action == 'opened' && github.event.pull_request.draft == false) || github.event.action == 'ready_for_review'
    steps:
      - name: Post PR summary message to slack
        id: message
        uses: slackapi/slack-github-action@485a9d42d3a73031f12ec201c457e2162c45d02d # v2.0.0
        with:
          method: chat.postMessage
          token: $
          payload: |
            channel: $
            text: "💡 *$*: <$|#$ - $> (+$, -$)"

      - name: Create file with slack message timestamp
        run: |
          echo "$" > slack-message-timestamp.txt

      - name: Cache slack message timestamp
        uses: actions/cache/save@1bd1e32a3bdc45362d1e726936510720a7c30a57 # v4.2.0
        with:
          path: slack-message-timestamp.txt
          key: slack-message-timestamp-$-$

  slack-emoji-react:
    runs-on: ubuntu-latest
    name: Adds emoji reaction to slack message when a PR is closed or reviewed
    if: $
    steps:
      # gh commands needs to be executed in the repository
      - name: Checkout Code
        uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2

      # https://stackoverflow.com/questions/74640750/github-actions-not-finding-cache
      # I can not use the cache action in this job because the cache is not shared between runs
      - name: Save slack timestamp as an environment variable
        id: slack-timestamp
        env:
          GH_TOKEN: $
        run: |
          SLACK_TIMESTAMP=$(gh cache list --json key --jq '.[].key|capture("$-(?<x>.+)").x')
          echo "SLACK_TIMESTAMP=${SLACK_TIMESTAMP}" | tee -a "${GITHUB_ENV}"
          if [[ "${SLACK_TIMESTAMP}" != '' ]]; then
            echo "github_event_pull_request_html_url=true" >> "${GITHUB_OUTPUT}"
          fi

      - name: Decide which emoji to add
        if: $
        run: |
          case "$" in
            created)
              if [[ "$" == 'issue_comment' ]]; then
                echo "EMOJI=speech_balloon" >> "${GITHUB_ENV}" # 💬
              fi
              ;;
            submitted)
              case "$" in
                changes_requested)
                  echo "EMOJI=repeat" >> "${GITHUB_ENV}" # 🔁
                  ;;
                approved)
                  echo "EMOJI=ok" >> "${GITHUB_ENV}" # 🆗
                  ;;
                commented)
                  echo "EMOJI=speech_balloon" >> "${GITHUB_ENV}" # 💬
                  ;;
              esac
              ;;
            review_requested)
              echo "EMOJI=eyes" >> "${GITHUB_ENV}" # 👀
              ;;
            *)
              echo "EMOJI=false" >> "${GITHUB_ENV}"
              ;;
          esac

      - name: React to PR summary message in slack with emoji
        if: $
        uses: slackapi/slack-github-action@485a9d42d3a73031f12ec201c457e2162c45d02d # v2.0.0
        with:
          method: reactions.add
          token: $
          payload: |
            channel: $
            timestamp: "$"
            name: $

      - name: Update the original message with success
        if: $
        uses: slackapi/slack-github-action@v2.0.0
        with:
          method: chat.update
          token: $
          payload: |
            channel: $
            ts: "$"
            text: "✅ *$*: <$|#$ - $> (+$, -$)"
            attachments:
              - color: "28a745"
                fields:
                  - title: "Status"
                    short: true
                    value: "Merged ✅"

Description

The workflow file contains two jobs: pr-slack-notification and slack-emoji-react.

  • The pr-slack-notification job sends a message to Slack when a Pull Request is opened or ready for review.
  • The slack-emoji-react job adds an emoji reaction to the Slack message when a Pull Request is closed or reviewed. The slack-emoji-react job also updates the original message with a success message when the Pull Request is merged.

The slack message “emoji” updates contains the following scenarios:

  • 💬 - a new comment is added to the pull request through either a “Pull Request Comment” or a “Review Changes Comment”
  • 🔁 - the reviewer has requested changes
  • 🆗 - the reviewer has approved the Pull Request
  • 👀 - The Pull Request owner has requested the reviewer to review the Pull Request
  • ✅ - The Pull Request has been merged

The screencast showcasing some of these actions is shown above.

The GitHub Acction workflow code and it’s description can be change in the future. The latest version of the code can be found here: pr-slack-notification.yml

Enjoy … 😉

This post is licensed under CC BY 4.0 by the author.