Set up the GitHub integration for Slack for faster code reviews
The GitHub integration for Slack is an application you install from the Slack marketplace to interact with your GitHub repositories without leaving Slack.
In the context of pull requests, here are some benefits you can have from having this implementation in place:
- Quicker review: Developer has a dedicated Slack channel to view pull requests created.
- Reduce context switching: while waiting for other developers to review, the developer will start a new feature, and when he receives the previous PR, he has to stash his changes, change the branch, set up the local environment, do changes, and then switch back to another branch.
- Improve DORA metrics: fast review means faster delivery in production which can improve the Deployment Frequency, the Lead Time for Changes, and the Time to Restore Service or MTTR, which are three of the four DORA metrics.
- Reduce the cognitive load of tracking your PRs: Th quicker the review is, the fewer PRs you will have to keep track of. Imagine having three pull requests waiting for review from three different business domains.
Available GitHub events
The table below lists all the available events on a GitHub repository you can subscribe to from Slack.
GitHub event | Description |
---|---|
issues | Opened or closed issues |
pulls | New, closed, or merged pull requests |
commits | New commits on the default branch |
releases | Published releases on a repository |
deployments | Deployment status updates. |
workflows | GitHub Actions workflow run notifications. |
reviews | Pull request reviews. |
comments | New comments on issues or pull requests. |
branches | Created or deleted branches. |
discussions | Discussions created or answered. |
What we will do
In this post, we will use the GitHub application for Slack to receive pull request events happening on a GitHub repository and display messages in a dedicated Slack channel.
We will install the GitHub application in Slack, connect Slack to GitHub and authorize it to access repositories, and finally subscribe from Slack to the events we want to receive in the Slack channel.
Prerequisites
To follow this post, you will need the following elements:
- A slack organization: you can create one for free.
- A GitHub repository: I use this repository we build in the blog post below; you can fork it and use it as a project example.
Install the GitHub application in Slack
The GitHub application will receive events from GitHub, transform the event into a Slack message, and display it to Slack users in the dedicated Slack channel.
Follow the steps below to install the GitHub application in Slack:
View the list of Slack applications
In the search bar, type "github" and select the " GitHub " application.
Click the "Add" button to redirect you to the web browser.
Click the "Add to Slack" button to see the page below.
Click on the button "Allow", wait for a few seconds, and you will be redirected to Slack and see the GitHub Application installed.
Connect a GitHub account to Slack's GitHub application
Click on the button "Connect GitHub account" to link the GitHub account having the repositories you want to receive notification about. You will be redirected to the Web browser.
Click on the button "Connect GitHub account"; If you are not authenticated to GitHub, you will be required to establish, and when done, a page will be displayed with a verification code.
Copy the verification code in the clipboard, then go to the GitHub Slack application to enter the code.
A modal form appears with a text input to enter the verification code.
Paste the verification code in the text input and click on the Submit button; you will see a message from the Slack application confirming the connection to the GitHub account.
Install the Slack application on GitHub
Go to the GitHub marketplace, and search for "slack"
Click on the Slack application, and you will be redirected to a page to install the application.
Click on the "Install it for free" button to redirect you to a summary page.
Click on the "Complete order and begin installation" button, and you will be redirected to a page to select repositories you want to allow the Slack application to interact with. I will choose all my repositories but feel free to do as you wish.
Click on the button "Install" to finalize. The Slack application is installed on your GitHub account, and you can see it in your GitHub account settings.
Create a Slack channel to listen to events
Slack channel helps group conversations on a specific topic, so interested people can join the conversation. In this case, we want to create a dedicated channel to receive GitHub pull request events about the repository. The developers doing code reviews will join the channel.
Let's create a channel named "pull-requests"
A modal appears with a text input to enter the slack channel; click on the button "Next". Select the channel visibility you want, as it doesn't matter; click on the submit button to create the channel.
You can see the channel in the list of Slack channels.
Register pull request event in the Slack channel
The GitHub application installation adds the slash command /github
you must use to interact with GitHub from Slack.
To receive pull request events in the channel "pull-requests" we created previously, we send a request to GitHub to subscribe to these events for a specific repository.
The syntax to subscribe to events of a GitHub repository is the following:
/github subscribe <github-owner-name>/<github-repository-name>
The keyword <github-owner-name>/<github-repository-name>
must be replaced by the GitHub repository owner's name and the GitHub repository's name you want to subscribe to.
The GitHub repository's owner can be personal or organizational accounts. The table below shows some examples:
Repository | Owner | Subscribe command |
---|---|---|
https://github.com/tericcabrel/node-ts-starter | Personal | /github subscribe tericcabrel/node-ts-starter |
https://github.com/osscameroon/prolang-api | Organization | /github subscribe osscameroon/prolang-api |
https://github.com/tericcabrel/blog-tutorials | Personal | /github subscribe tericcabrel/blog-tutorials |
By default, the Slack channel will subscribe to these events: issues
, pulls
, commits
, releases
and deployments
.
But we only want to subscribe to pull requests events, so we will unsubscribe to the four other events we don't need using the action unsubscribe
.
Here a the commands to subscribe to pull requests of the node-ts-starter repository:
/github subscribe tericcabrel/node-ts-starter
/github unsubscribe tericcabrel/node-ts-starter issues commits releases deployments
Go to the Slack channel "pull-requests" and type the commands above; replace node-ts-starter
with your repository's name. You will have the following output.
Test the integration
Create a new branch in the repository on your local computer, make some changes, commit them, and push them to the remote branch.
Go to the GitHub repository and create a pull request; you will instantly receive a message in the "pull-requests" Slack channel.
We have all the essential information about the pull request from the Slack message. We can see the status of the CI pipeline running on GitHub Actions, for example, and also leave a comment on the pull request directly from Slack.
Let's try to close and reopen the pull request:
Finally, we can merge the pull request after all the code reviews and changes are applied and the CI pipeline statuses are green.
To be notified about comments on the pull request, you must subscribe to the comments
event: /github subscribe tericcabrel/node-ts-starter comments
.
Filter events
Let's say your company has a security team, and they want to receive in Slack only pull requests that affect the core security of the application.
When creating the pull request, we will add the label "security"; the security team will create a Slack channel and subscribe to the pull request having the label "security".
On Slack, create a channel "pull-requests-security" and run the commands below:
/github subscribe tericcabrel/node-ts-starter +label:"security"
/github unsubscribe tericcabrel/node-ts-starter issues commits releases deployments
- Create a pull request without the label "security"; you will receive an event in the channel "pull-requests" but not in the channel "pull-requests-security"
- Create a pull request with the label "security"; you will receive an event in both pull request channels.
This is how you can use the filter to only receive pull requests events you are interested in but be sure the label is added on the pull request.
Filter events using a negative assertion
It can be helpful to not receive events about some pull requests, such as those automatically created by tools such as Dependabot or Renovate, because it makes many noises in the channel, and people may miss messages about a pull request requiring their attention.
The Renovate Bot adds the label "dependencies" in the pull request; it will be easier to exclude them by saying, I don't want to receive events for the pull request containing this label.
Unfortunately, it is not doable with the current implementation of the GitHub integration for Slack.
The workaround could be adding a label (for example, "app-feature") to the pull requests you create and only subscribing to the event with this label. As stated earlier, the only concern is to find a way to not forget to add this label when creating the pull request.
Wrap up
In this post, we saw how to receive pull request events in the Slack channel and apply a filter to only receive pull requests having a specific label.
The GitHub integration for Slack can help improve the code review process, which has many benefits for the team and the company.
Check out the GitHub integration for Slack documentation to see what you can do more to enhance your development process.
Follow me on Twitter or subscribe to my newsletter to avoid missing the upcoming posts and the tips and tricks I occasionally share.