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.
So to summarize, this is how all three actions are configured to post notifications on Slack:
If you would like to try this yourself, here are the detailed instructions to follow. You will need an account on IBM Bluemix.
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>
git clone git@github.com:openwhisk/openwhisk-GitHubSlackBot.git cd openwhisk-GitHubSlackBot
export OPENWHISK_NAMESPACE=`wsk property get --namespace | awk '{printf("%s", $3)}'`
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.
Get the Cloudant Service Credentials from Step 1.
cat cloudant.json { "username": "<username>", "password": "<password>", "host": "<host>", "port": <port>, "url": "<url>", "dbname": "<dbname>" } wsk package bind /whisk.system/cloudant TrackPRsInCloudant --param-file cloudant.json
This will result in a cloudant package and you can verify by running a sample action:
wsk action invoke TrackPRsInCloudant/list-documents --blocking
wsk action create track-pull-requests openwhisk/actions/js/track-pull-requests.js --param cloudant_package TrackPRsInCloudant
cat github.json { "username": "<username>", "repository": "<repository>", "accessToken": "<accessToken>" } wsk package bind /whisk.system/github GitHubWebHook --param-file github.json
wsk trigger create GitHubWebHookTrigger --feed GitHubWebHook/webhook --param events pull-request
wsk activation list GitHubWebHookTrigger
And creates a webhook under GitHub repository, for example:
wsk rule create RuleToTrackPullRequests /$OPENWHISK_NAMESPACE/GitHubWebHookTrigger /$OPENWHISK_NAMESPACE/track-pull-requests
At this point, any new pull request update is being recorded in Cloudant database, for example:
cat find-delayed-pull-requests.json { "cloudant_package": "TrackPRsInCloudant", "github_username": "<github_username>", "github_access_token": "<github_access_token>" } wsk action create find-delayed-pull-requests openwhisk/actions/js/find-delayed-pull-requests.js --param-file find-delayed-pull-requests.json
If you would like to verify action creation, you can run it with:
wsk action invoke find-delayed-pull-requests --blocking ... "response": { "status": "success", "statusCode": 0, "success": true, "result": { "prs": [] } }, ...
Create a new incoming webhook by following step by step instructions from here.
cat slack.json { "username": "<username>", "url": "<url>", "channel": "#<channel>" } wsk package bind /whisk.system/slack PostPRToSlack --param-file slack.json
You can verify by posting a sample message to your Slack channel:
wsk action invoke PostPRToSlack/post --blocking --result --param text "Hello World"
wsk action create post-to-slack openwhisk/actions/js/post-to-slack.js --param slack_package PostPRToSlack
wsk action create SequenceToPostGitHubPRsToSlack --sequence /$OPENWHISK_NAMESPACE/find-delayed-pull-requests,/$OPENWHISK_NAMESPACE/post-to-slack
wsk trigger create Every12Hours --feed /whisk.system/alarms/alarm --param cron "0 */12 * * *"
wsk rule create RuleToPostGitHubPRsToSlack /$OPENWHISK_NAMESPACE/Every12Hours /$OPENWHISK_NAMESPACE/SequenceToPostGitHubPRsToSlack
You can fire alarm trigger to verify:
wsk trigger fire /$OPENWHISK_NAMESPACE/Every12Hours