Build a slack command to fetch GitHub open PRs for the team
Have you ever forgot to merge an open pull request, especially when you work across different repos? Since more and more engineers are used to communicating and collaborating on slack, let’s build a customized slack command to show all the open pull requests of the team. At the end of this article, you should be able to know:
- How to create a slack app and command
- How to build a node server and deploy it to Heroku
- How to show response in slack nicely
Create slack command
The idea of how slack command works is really simple. When you type the command in the channel, it will send out a
POST request to the request url you have configured. When the request is done, slack will simply display the response. Keep in remind that slack sends out
POST request rather than
GET request, so we need to configure the server later to handle
POST as well.
In order to create a slack command, we first need to create a slack app.
- In https://api.slack.com/apps, click
Create New Appbutton.
- Fill out the app name and slack workspace.
Add features and functionalitysection.
Create New Command
5. Fill out a proper command name, e.g.
/openpr. The request url can be a dummy one for now
https://example.com. Short description is
list all open prs". Then save. You can always come back and update it.
6. Once the command is created, we can install the app in the workspace. Go back to
Basic Information page, and click
Install your app to your workspace.
Time to try it out 🚀🚀. You can start typing
/openpr in the channel , and get response message like below:
<p>This domain is established to be used for illustrative examples in documents. You may use this
domain in examples without prior coordination or asking for permission.</p>
<p><a href="http://www.iana.org/domains/example">More information...</a></p>
Our first step is successful since we can fire a command and fetch the html from requested url!
Make NodeJs Server and deploy to Heroku
There are four main tasks for the server:
- receive request sent from slack command
- send request to GitHub to retrieve open pull requests
- transform the GitHub response so that it can be nicely displayed in slack
- return response to slack
The first and last step can be easily achieved by using a simple NodeJs server with Express which is a popular NodeJs web application framework. The second step needs us to explore GitHub APIs and figure out how to call proper endpoints for pull requests. The third step requires us to know how to display slack message nicely.
- Set up NodeJs server with Express. The source code can be found here: https://github.com/buronnie/slack-command-open-pr.
- Sign up for a Heroku user, and download the Heroku command line tool. This tutorial is quite helpful: https://devcenter.heroku.com/articles/getting-started-with-nodejs#set-up.
🚗🚗: Heroku is a cloud platform where you can deploy different types of servers for free.
3. Once you cloned the repo (https://github.com/buronnie/slack-command-open-pr), you can create a heroku app by using
4. Heroku has its own code repository similar to github. Once the app is created, we can push code to heroku repo:
git push heroku master. The code push will also trigger build process which deploys code to the node server.
5. You should be able to find out the server url when you create the app. If not, typing
heroku open will open a browser tab with the url. You will see
Cannot GET in the page because our server only handles
POST request. Instead, you can use
curl to test the server:
curl -X POST -H application/json" <node-server-url>
6. Let’s dive into the example code and see how GitHub query is done and how we format the response:
- The GitHub api for searching open pull requests is pretty straightforward. You can only search pull requests for a certain user with extra conditions:
- If we want to search for all the team members, we need to send multiple requests and use
Promise.allto wait until all the requests are complete before we send back the response to slack.
- To format the response for nice display in slack, we refer to
attachmentwhich allows rich content in the message. https://api.slack.com/docs/message-attachments
7. Let’s update the slack command with the server url in Heroku.
Now everything is ready, let’s test out the command again. We should be able to see the response message like below:
That’s all we need to build a slack command, backed by a simple node server. Enjoy!