Apr
30
2012

Using Github hubot and Appharbor service hook to get deployment status in Campfire/HipChat rooms

STA_0178Appharbor 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

Apr
28
2012

Running Github Hubot on a Windows machine

IMG_0063I finally managed to get Github Hubot running on my Windows 7 64 bits machine. Thanks to  Sean Copenhaver & Thomas Kahlow which pointed me to the correct direction on the ‘Run hubot on windows’ discussion.

So here is how I did it.

First of all download and install nodejs 0.6.15. This will also install npm 1.1.16 which is the node package manager.

Then start Powershell and create a new folder e.g. HubotWorking

mkdir HubotWorking

cd .\HubotWorking

npm install hubot

You should see the following output

npminstallhubot

Go on with the following

cd .\node_modules\hubot

node .\node_modules\coffee-script\bin\coffee .\bin\hubot

And the magic!

hubotonwindows

You might want also to run it through JetBrains WebStorm 4.0, so here is the configuration you will need

webstorm hubot

And here is the result in the WebStorm nodejs runner

runhubotfromwebstorm
Apr
13
2012

Sending your Git branch changes as an email attachment

IMG_1586The other day I wanted to send per email some code to a friend which doesn’t use Git. He is using Svn and I use Git Svn in front of this Svn repository. Why I do that? Don’t get me started…

So he couldn’t pull from my repo and we were kind of stuck. Really?!? For sure not, here was the goal I set as I am sure this will happen some other time: having the computer work for me. What a strange idea you would say! Yeah, the computer working for you. At the end aren’t we here to make the cool things and let the computer do the boring things?

Ok so I came up with a quick hack. I wanted to have a way to send all the new, or modified, files of my current Git branch per email as a zip attachment. Guess what it was quite easy even for a PowerShell beginner like me.

First of all I needed first to be able to determine on which Git branch I was curently. I googled and find the following

function Get-GitBranch {
    $symbolicref = git symbolic-ref HEAD
    $branch = $symbolicref.substring($symbolicref.LastIndexOf("/") +1)
    return $branch
}

Then I wanted to be able to Zip all the files, but to achieve I had to determine which were the files to Zip. this is done using the following

git diff --name-only HEAD..master

And making a Zip out of the list of files is done like this

function Zip-GitBranch([string]$zipFilename) {

    $branch = Get-GitBranch
   
    if (!$zipFilename) {
        $zipFilename = [string]::Format(".\{0}.zip", $branch)
    }

    $files = git diff --name-only HEAD..master

    foreach($file in $files) {
         & 'C:\Program Files\7-Zip\7z.exe' a $zipFilename $file
    }
   
    return $zipFilename
}

Finally I wanted to be able to send the zip as an attachment of an email using Outlook

function MailZip-GitBanch($Recipient) {
   
    if (!$Recipient) {
        Write-Host "You need to pass the email of the recipient as parameter"
        return
    }
   
    $branch = Get-GitBranch
    $zipFilename = [string]::Format(".\{0}.zip", $branch)
    $attachement = [IO.Path]::GetFullPath( $zipFilename )

    Zip-GitBranch($attachement)
       
    $ol = New-Object -comObject Outlook.Application
    $Mail = $ol.CreateItem(0)
    $Mail.Recipients.Add($Recipient)
    $Mail.Subject = "Changes for the branch: " + $branch
    $Mail.Body = "Check out the email attachement to see the changes made to the branch: " + $branch
    $Mail.Attachments.Add($attachement)
    $Mail.Send()
}

Now I can type the following command to send my changes to my friend

MailZip-GitBranch myemail@email.com

You can find the whole script on the following gist.

About Laurent

Laurent Kempé

Laurent Kempé is the editor, founder, and primary contributor of Tech Head Brothers, a French portal about Microsoft .NET technologies.

He is currently employed by Innoveo Solutions since 10/2007 as a Senior Solution Architect and certified Scrum Master.

Founder, owner and Managing Partner of Jobping, which provides a unique and efficient platform for connecting Microsoft skilled job seekers with employers using Microsoft technologies.

Laurent was awarded Most Valuable Professional (MVP) by Microsoft from April 2002 to April 2012.

JetBrains Academy Member
Certified ScrumMaster
My status

Twitter

Flickr

www.flickr.com
This is a Flickr badge showing public photos and videos from Laurent Kempé. Make your own badge here.

Month List

Page List