       msg.send "I can't cancel without a build number"
       return
     build_num = escape(msg.match[2])
-    msg.http("https://circleci.com/api/v1/project/#{project}/#{build_num}/cancel?circle-token=#{process.env.HUBOT_CIRCLECI_TOKEN}")
+    msg.http("#{endpoint}/project/#{project}/#{build_num}/cancel?circle-token=#{process.env.HUBOT_CIRCLECI_TOKEN}")
       .headers("Accept": "application/json")
-      .post('{}') handleResponse (response) ->
-          msg.send "Canceled build #{response.build_num} for #{project}(#{response.branch})"
+      .post('{}') handleResponse msg, (response) ->
+          msg.send "Canceled build #{response.build_num} for #{project} [#{response.branch}]"
 
   robot.respond /circle clear (.*)/i, (msg) ->
     project = escape(msg.match[1])
-    msg.http("https://circleci.com/api/v1/project/#{project}/build-cache?circle-token=#{process.env.HUBOT_CIRCLECI_TOKEN}")
+    msg.http("#{endpoint}/project/#{project}/build-cache?circle-token=#{process.env.HUBOT_CIRCLECI_TOKEN}")
       .headers("Accept": "application/json")
-      .del('{}') handleResponse (response) ->
+      .del('{}') handleResponse msg, (response) ->
           msg.send "Cleared build cache for #{project}"
 
-  handleResponse = (handler) ->
+  handleResponse = (msg, handler) ->
     (err, res, body) ->
       switch res.statusCode
         when 404

#   Find the build status of aproject on CircleCI
#
# Dependencies:
#   "gitio": "1.0.1"
#
# Configuration:
#   None

url = require('url')
util = require('util')
querystring = require('querystring')

endpoint = 'https://circleci.com/api/v1'

toSha = (vcs_revision) ->
  vcs_revision.substring(0,7)

toDisplay = (status) ->
  status[0].toUpperCase() + status.slice(1)

formatBuildStatus = (build) ->
  "#{toDisplay(build.status)} in build #{build.build_num} of #{build.vcs_url} [#{build.branch}/#{toSha(build.vcs_revision)}] #{build.committer_name}: #{build.subject} - #{build.why}"

module.exports = (robot) ->
  
  robot.respond /circle me (\S*)\s*(\S*)/i, (msg) ->
    project = escape(msg.match[1])
    branch = if msg.match[2] then escape(msg.match[2]) else 'master'
    msg.http("#{endpoint}/project/#{project}/tree/#{branch}?circle-token=#{process.env.HUBOT_CIRCLECI_TOKEN}")
      .headers("Accept": "application/json")
      .get() handleResponse  msg, (response) ->
          if response.length == 0
            msg.send "Current status: #{project} [#{branch}]: unknown"
          else
            currentBuild = response[0]
            msg.send "Current status: #{formatBuildStatus(currentBuild)}"

  robot.respond /circle last (\S*)\s*(\S*)/i, (msg) ->
    project = escape(msg.match[1])
    branch = if msg.match[2] then escape(msg.match[2]) else 'master'
    msg.http("#{endpoint}/project/#{project}/tree/#{branch}?circle-token=#{process.env.HUBOT_CIRCLECI_TOKEN}")
      .headers("Accept": "application/json")
      .get() handleResponse msg, (response) ->
          if response.length == 0
            msg.send "Current status: #{project} [#{branch}]: unknown"
          else
            last = response[0]
            if last.status != 'running'
              msg.send "Current status: #{formatBuildStatus(last)}"
            else if last.previous && last.previous.status
              msg.send "Last status: #{formatBuildStatus(last)}"
            else
              msg.send "Last build status for #{project} [#{branch}]: unknown"

  robot.respond /circle retry (.*) (.*)/i, (msg) ->
    project = escape(msg.match[1])
