Slack notification for GitHub pull requests
This tutorial shows how to create and test a workflow definition that subscribes to GitHub events to send Slack notification when there is a pull request.
- Prerequisites
- Step 1: Sign up to GitHub
- Step 2: Create a GitHub app
- Step 3: Setup GitHub app permissions
- Step 4: Install GitHub app
- Step 5: Retrieve client ID and client secret for GitHub app
- Step 6: Create GitHub OAuth 2.0 credentials
- Step 7: Sign up to Slack
- Step 8: Create a Slack app
- Step 9: Request scopes for Slack app
- Step 10: Installing and authorizing the Slack app
- Step 11: Create Slack OAuth 2.0 credentials
- Step 12: Create SimWorkflow workflow definition
- Step 13: Define workflow definition variables
- Step 14: Define Webhook settings for the workflow definition
- Step 15: Create a GitHub repository webhook
- Summary
Prerequisites
To perform the steps in this tutorial, you must already have the following:
- An account with GitHub.
- An account with Slack to send Slack messages.
- A SimWorkflow account that you can log in to create workflow definitions.
Step 1: Sign up to GitHub
If you're a first-time user of GitHub, your first step is to sign up for a GitHub account.
Step 2: Create a GitHub app
- Navigate to your GitHub account settings.
-
For a GitHub App owned by a personal account, click Settings.
-
For a GitHub App owned by an organization:
-
Click Your organizations.
-
To the right of the organization, click Settings.
-
-
- In the left sidebar, click Developer settings.
- In the left sidebar, click GitHub Apps.
- Click New GitHub App button.
- Enter SimWorkflow to the GitHub App name field.
- Enter https://www.simworkflow.com/ to the Homepage URL field.
- Enter https://www.simworkflow.com/credentials/callback to the Callback URL field.
- Click Create GitHub App button.
Step 3: Setup GitHub app permissions
- On the GitHub Apps page, click Edit button of SimWorkflow app.
- In the left sidebar, click Permissions & events.
- In Repository permissions section, select Access: Read-only for Pull requests permission.
- Click Save changes button.
Step 4: Install GitHub app
- On the GitHub Apps page, click Edit button of SimWorkflow app.
- Click Install App.
- Click Install next to the location where you want to install the GitHub App.
- Select All repositories or Only select repositories.
- For Only select repositories, select the repositories that you want the app to access under the Select repositories dropdown.
- Click Install button.
Step 5: Retrieve client ID and client secret for GitHub app
- On the GitHub Apps page, click Edit button of SimWorkflow app.
- In the left sidebar, click General.
- In the About section, copy the Client ID, we'll paste it later.
- In Client secrets section, click Generate a new client secret button.
- Copy the Client secret, we'll paste it later.
Step 6: Create GitHub OAuth 2.0 credentials
- Log in to the SimWorkflow.
- Navigate to Credentials.
- Click Create credentials button.
- Select OAuth 2.0 tab.
- Select GitHub for the Provider field.
- Enter GitHub to the Name field.
- Paste the Client ID to the Client ID field.
- Paste the Client secret to the Client secret field.
- Enter repo to the Scopes field.
- Click Create credentials button.
We'll use this credentials in the workflow definition when we integrate with GitHub for pull requests.
Step 7: Sign up to Slack
If you're a first-time user of Slack, your first step is to sign up for a Slack account.
Step 8: Create a Slack app
-
On the Your Apps page, select Create New App.
-
Select From scratch.
-
Enter SimWorkflow to the App Name field.
-
Select the Workspace where you'll be developing your app.
-
Select Create App.
Step 9: Request scopes for Slack app
-
Within OAuth & Permissions, scroll down to Scopes.
-
Under Bot Token Scopes, select Add an OAuth Scope.
-
To allow your app to post messages, add the
chat:write
scope.
Step 10: Installing and authorizing the Slack app
-
Return to the Basic Information section of the app management page.
-
Install your app by selecting the Install to Workspace button.
-
You'll now be sent through the Slack OAuth flow. Select Allow on the following screen.
-
After installation, navigate back to the OAuth & Permissions page.
-
Click Add New Redirect URL button under Redirect URLs.
-
Enter https://www.simworkflow.com/credentials/callback to the input field and click Add button.
-
Return to the Basic Information section of the app management page.
-
Under App Credentials, copy Client ID and Client Secret.
Step 11: Create Slack OAuth 2.0 credentials
- Log in to the SimWorkflow.
- Navigate to Credentials.
- Click Create credentials button.
- Select OAuth 2.0 tab.
- Select Slack for the Provider dropdown.
- Enter Slack chat write to the Name field.
- Paste the Client ID to the Client ID field.
- Paste the Client Secret to the Client secret field.
- Enter chat:write to the Scopes field.
- Click Create credentials button.
We'll use this credentials in the workflow definition when we integrate with Slack to send alerts.
Step 12: Create SimWorkflow workflow definition
Step 12.1: Plan the workflow definition
There are three key steps in the process:
- Workflow automatically starts when pull request events occurred on GitHub.
- We will enable Webhook for the workflow definition to subscribe to GitHub events.
- Fetch the pull request details if the pull request event action is created or reopened, otherwise completed the workflow.
- We will use HTTP Task to call GitHub API to fetch pull request details.
- Slack the pull request details to a Slack channel.
- We will use HTTP Task to call Slack API to post Slack message.
Step 12.2: Define the workflow definition
-
Log in to the SimWorkflow.
-
Navigate to Workflow definitions.
-
Click Create workflow definition button.
-
Select General tab of the workflow definition.
-
Enter Slack GitHub Pull Request to the Name field.
-
Click Start task and select Input Parameters tab.
-
Enter the following JSON object to the Input parameters field:
{ "body": "${jsonata:$eval(workflow.input.body)}" }
The system will use
jsonata
to create a JSON Object from the "body" field of the workflow input and set it to "body" field. -
Click HTTP Task to add a system task and name it Get pull request.
-
Connect the Start task to the Get pull request task.
-
Click on the route between Start task and Get pull request task.
-
Enter Created or reopened to the Name field.
-
Select Condition for Conditional routing field.
-
Enter the following JavaScript to the editor:
['created', 'reopened'].includes($.body.action)
The system will continue with this route only when the "body.action" field of the task input parameters is
created
orreopened
. -
Click End task to add a terminal task and name it End.
-
Connect the Start task to the End task.
-
Select Always true for Conditional routing field.
- The system will continue with this route to complete the workflow when the action of the pull request event is not
created
andreopened
.
- The system will continue with this route to complete the workflow when the action of the pull request event is not
-
Click Get pull request task and select Input Parameters tab.
-
Enter the following JSON object to the Input parameters field:
{ "pull_request": "${Start.input.body.pull_request}" }
The system will reference the "body.pull_request" field from the Start task input to the "pull_request" field.
-
Click Get pull request task and select Configuration tab.
-
Select GitHub (OAuth 2.0) for Credentials field.
-
Enter the following JSON object to the HTTP request field:
{ "url": "${pull_request.url}", "method": "GET", "headers": { "Accept": "application/vnd.github.text+json" } }
The HTTP request configuration will call the GitHub API to retrieve the pull request details.
-
Click HTTP Task to add a system task and name it Slack.
-
Connect the Get pull request task to the Slack task.
-
Click Slack task and select Input Parameters tab.
-
Enter the following JSON object to the Input parameters field:
{ "body": { "text": "Pull request: *${['Get pull request'].output.body.title}* ${['Get pull request'].output.body.state} with ${['Get pull request'].output.body.changed_files} changes.\n\n${Start.input.body.pull_request.html_url}", "channel": "${['swf:variables'].slackChannelId}" } }
-
Click Slack task and select Configuration tab.
-
Select Slack chat write (OAuth 2.0) for Credentials field.
-
Enter the following JSON object to the HTTP request field:
{ "url": "https://slack.com/api/chat.postMessage", "body": "${body}", "method": "POST", "headers": { "Accept": "application/json", "Content-Type": "application/json" } }
The HTTP request configuration will call the Slack API to post the alert message to the Slack channel.
-
Click Save button.
-
Toggle the Enable checkbox to enable the workflow definition.
Step 13: Define workflow definition variables
- Log in to the SimWorkflow.
- Navigate to Workflow definitions.
- Click Variables menu item from the three dots (more options) menu of the Slack GitHub Pull Request workflow definition.
Step 13.1: GitHub webhook secret
- Enter githubWebhookSecret to the Variable key field.
- Create the GitHub secret token and enter it to the Variable value field.
- Click Save variable button.
Step 13.2: Slack channel ID
- Enter slackChannelId to the Variable key field.
- Locate the Slack channel ID and enter it to the Variable value field.
- Click Save variable button.
Step 14: Define Webhook settings for the workflow definition
-
Log in to the SimWorkflow.
-
Navigate to Workflow definitions.
-
Click Webhook settings menu item from the three dots (more options) menu of the Slack GitHub Pull Request workflow definition.
-
Turn on Webhook on field.
-
Copy the Webhook endpoint URL, we'll paste it later.
-
Select a user for User to start the workflow as field.
-
Enter the following JavaScript to the Validate the payload field:
'sha256=' + Swf.hmacSha256Hex($['swf:variables'].githubWebhookSecret, $.body) === $.headers['x-hub-signature-256']
The system will use the workflow definition variable
githubWebhookSecret
to verify that a webhook delivery is from GitHub. -
Click Save webhook settings button.
Step 15: Create a GitHub repository webhook
- On GitHub.com, navigate to the main page of the repository.
- Under your repository name, click Settings. If you cannot see the "Settings" tab, select the dropdown menu, then click Settings.
- In the left sidebar, click Webhooks.
- Click Add webhook button.
- Paste the workflow definition Webhook endpoint to the Payload URL field.
- Select application/json for the Content type field.
- Paste GitHub secret token to the Secret field.
- Select Let me select individual events. for the Which events would you like to trigger this webhook? field.
- Ensure only the Pull requests checkbox is checked.
- Click Add webhook button.
Summary
You've now successfully completed all the steps necessary to define a workflow definition to send a Slack message when there is a GitHub pull request. You've learned how to integrate with third party APIs using SimWorkflow HTTP tasks and Webhooks. As a result, you are able to have an automated process to send Slack messages when there are GitHub pull requests.