Appharbor provides a service hook which let’s you get informed when a build is finished sending both succeeded and failed builds.

We wanted to use this to get informed in our Campfire / HipChat room so that our distributed team can be informed about builds status without having to go to AppHarbor web site.

The idea was to extend our GitHub Hubot hosted on Heroku.

From AppHarbor support here is the information we need about “Developing a service hook

We will send a POST request content-type “application/json” and the following body:

    {

“application”: {
“name”: “Foo”
},
“build”: {
“commit”: {
“id”: “77d991fe61187d205f329ddf9387d118a09fadcd”,
“message”: “Implement foo”
},
“status”: “succeeded”
}
}

We need to build a Hubot script; which are CoffeeScript, to have an HTTP endpoint listening to this Post payload. Then it needs to read the payload and format it to be able to send it in readable message to the Campfire / HipChat room.

With the release 2.1.3 of Hubot there is a new easy way to have an HTTP Listener:

HTTP Listener

Hubot has a HTTP listener which listens on the port specified by the PORT environment variable.

You can specify routes to listen on in your scripts by using the router property on robot.

module.exports = (robot) ->
robot.router.get “/hubot/version”, (req, res) ->
res.end robot.version

There are functions for GET, POST, PUT and DELETE, which all take a route and callback function that accepts a request and a response.

We can use this easily in our Hubot script which is called appharbor-listener.coffee.

module.exports = (robot) ->
  robot.router.post “/hubot/appharbor”, (req, res) ->
    robot.logger.info “Message received for appharbor”

Now that we are able to listen to POST payload on the url …/hubot/appharbor we need to send a message to the Campfire / HipChat room, which is a bit different from the other scripts. The http listener scripts doesn’t get msg which is normally used to send the response from our bot to the room. Here we have to do it differently and use **robot.send **which I found on the post ‘Hubot HTTP Daemon Support

user = robot.userForId ‘broadcast’
user.room = ‘Your Room Id’
user.type = ‘groupchat’

message = “AppHarbor build ‘#{buildStatus}’ for application: ‘#{builtApplicationName}’”

robot.logger.info “User: ‘#{user.room}’,’#{user.type}’”
robot.logger.info “Message: ‘#{message}’”

robot.send user, “#{message}”

Currently this is working only with the Campfire adapter, the HipChat one is crashing as described here.

Here is the whole script

And finally here is the result of posting a sample payload using fiddler

github hubot appharbor integration