httpjenkinshttp-headerscoffeescripthubot

How can I access 'Location' from HTTP response in Coffee script?


I'm trying to integrate Jenkins with Slack, using Hubot. I've found jenkins.coffee script in Hubot scripts and it works fine for what I was planning on doing. Now, I would like to get the Build number of a triggered job after I run a command to build it from Slack. In order to do that, I would like to get the 'Location' from http response.

This is the function that builds a job when I say @Hubot jenkins build <job name>

jenkinsBuild = (msg, buildWithEmptyParameters) ->
    url = process.env.HUBOT_JENKINS_URL
    job = querystring.escape msg.match[1]
    params = msg.match[3]
    command = if buildWithEmptyParameters then "buildWithParameters" else "build"
    path = if params then "#{url}/job/#{job}/buildWithParameters?#{params}" else "#{url}/job/#{job}/#{command}"

    req = msg.http(path)

    if process.env.HUBOT_JENKINS_AUTH
      auth = new Buffer(process.env.HUBOT_JENKINS_AUTH).toString('base64')
      req.headers Authorization: "Basic #{auth}"

    req.header('Content-Length', 0)
    req.post() (err, res, body) ->
        if err
          msg.reply "Jenkins says: #{err}"
        else if 200 <= res.statusCode < 400 # Or, not an error code.
          msg.reply "(#{res.statusCode}) Build started for #{job} #{url}/job/#{job}"
        else if 400 == res.statusCode
          jenkinsBuild(msg, true)
        else if 404 == res.statusCode
          msg.reply "Build not found, double check that it exists and is spelt correctly."
        else
          msg.reply "Jenkins says: Status #{res.statusCode} #{body}"

What exactly should I write in msg.reply to get the Location?

TIA :)


Solution

  • I finally did it with res.headers["location"]