Integrate Stash with Slack via incoming webhooks
Online persistent chat is awesome! Ever since I joined my first IRC chat room, I was hooked. It's slash commands, people leaving/joining/collaborating, and its always running
/me likes persistence. Now fast forward to the modern web, and we have services like Campfire, HipChat, and Slack. All services based on powerful web technologies which come with all of the joyous automation and interconnectivity that we expect in a modern web app. The most ubiquitous form of this interconnection are webhooks. A webhook is an event that your robot can subscribe to and do something interesting with. It's your basic pub sub pattern, but with hooks! I mean, who whats to use a webpub, hooks are much more interesting and they also remind me of pirates...so even better.
But I digress. Slack! I'm a huge fan and have been using it with my team for a while now. Slack has all of the great features that you would expect in premium online persistent chat service, but its killer feature from my perspective are the Integrations. Not only does it listen (and talk) to dozens of officially supported apps and services out of the box (more added all the time), but it also provides a great way to build your own with minimal effort. Via the Incoming WebHooks integration, you've got a dead simple way for your apps to push into Slack.
With that said, most of my integration needs were met out of the box with Slack. But for the ones that weren't, I was able to write some middle wear code to mimic that out of the box functionality using an incoming webhook. Namely I needed my Stash source control server talking to the channels where team members cared about that type of activity. Knowing who made the commit which caused the CI build to fail is something every dev team would love their robot to tell them ASAP.
API in the middle
First, install the Stash Web Post Hooks Plugin from the add-on marketplace. It's an add-on published by Atlassian and will allow you to enable each repository to emit a POST every time a commit takes place. Once installed, you'll configure this on your repo -> Settings -> Hooks page. The payload is documented here https://confluence.atlassian.com/display/STASH/POST+service+webhook+for+Stash.
I had a couple of goals with my implementation. 1. I wanted this to look native, e.g. something Slack would implement out of the box. 2. I wanted to pick out my JIRA issue id's from the commit message and link them back to my JIRA instance.
Here's the API code:
And here is the end result in Slack:
You can configure both the logo and name of the incoming webhook to make it look like any other supported integration...very cool stuff.