This bot is designed to post updates to Slack when a GitHub pull request is ready to merge or a list of pull requests are under review for certain days and haven’t merged.
Architecture
The GitHub Slack Bot is built on a set of three actions (like microservices) which are invoked either by some triggers or by another action in a sequence of dependent actions. One action is invoked by regular pulll request updates received from a GitHub repository and an action sequence (consisting of two actions) is triggered periodically by an internal OpenWhisk alarm (similar to a cron job).
Each of the actions in GitHub Slack Bot are implemented in JavaScript, using the Node.js runtime provided by OpenWhisk on Bluemix. Here’s what each action does within its triggered flow.
Invoked by GitHub repository when any pull request changes
Track pull requests - An action that is subscribed to “pull_request” events on GitHub repository. GitHub repository sends pull request payload via HTTP POST which is stored in Cloudant to form a data set of pull requests. Data set in Cloudant is updated when a pull request is:
labeled “ready” or “review” or
unlabeled (“ready” or “review” label is removed) or
updated due to a new push in the branch that the pull request is tracking or
closed
Invoked twice a day by an Alarm trigger
The sequence of actions is invoked by Alarm trigger, which then starts a chain of microservices to retrieve list of delayed pull requests and compose notifications which are posted on Slack.
Find delayed pull requests - an action that is invoked every 12 hours with Cloudant data set, retrieves a list of pull requests which are ready to be merged and older than certain days.
Post to Slack - an action invoked by find delayed pull requests action. It takes a list delayed pull requests and composes message with pull requests, ID, label, and age in days to post on Slack.
Finally, here is the sample message posted on Slack showing list of delayed pull requrests from OpenWhisk repo. OpenWhisk project developers at IBM are using this bot to receive such notifications.
Bringing it all together
So to summarize, this is how all three actions are configured to post notifications on Slack:
Installation
If you would like to try this yourself, here are the detailed instructions to follow. You will need an account on IBM Bluemix.
Prerequisite
Setup OpenWhisk CLI
Follow the instructions on IBM Bluemix to setup OpenWhisk CLI. Set your API host and auth key with:
wsk property set --apihost openwhisk.ng.bluemix.net --auth <user>:<pass>
Clone this repo:
git clone git@github.com:openwhisk/openwhisk-GitHubSlackBot.git
cd openwhisk-GitHubSlackBot
Export namespace
export OPENWHISK_NAMESPACE=`wsk property get --namespace | awk '{printf("%s", $3)}'`
GitHub Slack Bot
The GitHub Slack Bot is a serverless, event-driven, bot designed and built using the OpenWhisk open source project.
This bot is designed to post updates to Slack when a GitHub pull request is ready to merge or a list of pull requests are under review for certain days and haven’t merged.
Architecture
The GitHub Slack Bot is built on a set of three actions (like microservices) which are invoked either by some triggers or by another action in a sequence of dependent actions. One action is invoked by regular pulll request updates received from a GitHub repository and an action sequence (consisting of two actions) is triggered periodically by an internal OpenWhisk alarm (similar to a cron job).
Each of the actions in GitHub Slack Bot are implemented in JavaScript, using the Node.js runtime provided by OpenWhisk on Bluemix. Here’s what each action does within its triggered flow.
Invoked by GitHub repository when any pull request changes
Track pull requests - An action that is subscribed to “pull_request” events on GitHub repository. GitHub repository sends pull request payload via HTTP POST which is stored in Cloudant to form a data set of pull requests. Data set in Cloudant is updated when a pull request is:
Invoked twice a day by an Alarm trigger
The sequence of actions is invoked by Alarm trigger, which then starts a chain of microservices to retrieve list of delayed pull requests and compose notifications which are posted on Slack.
Find delayed pull requests - an action that is invoked every 12 hours with Cloudant data set, retrieves a list of pull requests which are ready to be merged and older than certain days.
Post to Slack - an action invoked by find delayed pull requests action. It takes a list delayed pull requests and composes message with pull requests, ID, label, and age in days to post on Slack.
Finally, here is the sample message posted on Slack showing list of delayed pull requrests from OpenWhisk repo. OpenWhisk project developers at IBM are using this bot to receive such notifications.
Bringing it all together
So to summarize, this is how all three actions are configured to post notifications on Slack:
Installation
If you would like to try this yourself, here are the detailed instructions to follow. You will need an account on IBM Bluemix.
Prerequisite
Follow the instructions on IBM Bluemix to setup OpenWhisk CLI. Set your API host and auth key with:
Step 1: Create Cloudant Service
If you don’t have one already, create a new Cloudant NoSQL database by following tutorial on Creating a Cloudant instance on Bluemix or GitHub IBM Bluemix docs.
Also, create a new database if you don’t have one.
Step 2: Create Cloudant Package Binding - TrackPRsInCloudant
Get the Cloudant Service Credentials from Step 1.
This will result in a cloudant package and you can verify by running a sample action:
Step 3: Create an Action - track-pull-requests
Step 4: Create GitHub WebHook trigger - GitHubWebHookTrigger
And creates a webhook under GitHub repository, for example:
Step 5: Create a Rule - RuleToTrackPullRequests
At this point, any new pull request update is being recorded in Cloudant database, for example:
Step 6: Create an Action - find-delayed-pull-requests
If you would like to verify action creation, you can run it with:
Step 7: Add Incoming Webhook to Slack
Create a new incoming webhook by following step by step instructions from here.
Step 8: Create Slack Package Binding - PostPRToSlack
You can verify by posting a sample message to your Slack channel:
Step 9: Create an Action - post-to-slack
Step 10: Create an Action Sequence - SequenceToPostGitHubPRsToSlack
Step 11: Create Alarm Trigger - Every12Hours
Step 12: Create a Rule - RuleToPostGitHubPRsToSlack
You can fire alarm trigger to verify: