Skip to content
This repository
Fetching contributors…

Cannot retrieve contributors at this time

file 55 lines (50 sloc) 2.292 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
# Description:
# Show status of 3 most recent builds.
#
# Dependencies:
# None
#
# Configuration:
# HUBOT_TEAMCITY_USERNAME = <user name>
# HUBOT_TEAMCITY_PASSWORD = <password>
# HUBOT_TEAMCITY_HOSTNAME = <host : port>
#
# Commands:
# hubot show me builds - Show status of currently running builds
#
# Author:
# assaf

module.exports = (robot) ->
  robot.respond /show (me )?builds/i, (msg) ->
    username = process.env.HUBOT_TEAMCITY_USERNAME
    password = process.env.HUBOT_TEAMCITY_PASSWORD
    hostname = process.env.HUBOT_TEAMCITY_HOSTNAME
    msg.http("http://#{hostname}/app/rest/builds")
      .query(locator: ["running:any", "count:3"].join(","))
      .headers(Authorization: "Basic #{new Buffer("#{username}:#{password}").toString("base64")}", Accept: "application/json")
      .get() (err, res, body) ->
        if err
          msg.send "Team city says: #{err}"
          return
        # Sort by build number.
        builds = JSON.parse(body).build.sort((a, b)-> parseInt(b.number) - parseInt(a.number))

        displayBuild = (msg, build) ->
          msg.http("http://#{hostname}#{build.href}")
            .headers(Authorization: "Basic #{new Buffer("#{username}:#{password}").toString("base64")}", Accept: "application/json")
            .get() (err, res, body) ->
              if err
                msg.send "Team city says: #{err}"
                return

              project = JSON.parse(body)

              if build.running
                started = Date.parse(build.startDate.replace(/(\d{4})(\d{2})(\d{2})T(\d{2})(\d{2})(\d{2})([+\-]\d{4})/, "$1-$2-$3T$4:$5:$6$7"))
                elapsed = (Date.now() - started) / 1000
                seconds = "" + Math.floor(elapsed % 60)
                seconds = "0#{seconds}" if seconds.length < 2
                msg.send "#{project.buildType.projectName} - #{build.number}, #{build.percentageComplete}% complete, #{Math.floor(elapsed / 60)}:#{seconds} minutes"
              else if build.status is "SUCCESS"
                msg.send "#{project.buildType.projectName} - #{build.number} is full of win"
              else if build.status is "FAILURE"
                msg.send "#{project.buildType.projectName} - #{build.number} is #fail"

        for build in builds
          displayBuild(msg, build)
Something went wrong with that request. Please try again.