如何从 Coffee 脚本中的 HTTP 响应访问 'Location'?
How can I access 'Location' from HTTP response in Coffee script?
我正在尝试使用 Hubot 将 Jenkins 与 Slack 集成。我在 Hubot 脚本中找到了 jenkins.coffee 脚本,它可以很好地满足我的计划。现在,我想在我 运行 从 Slack 构建它的命令后获取触发作业的内部版本号。为此,我想从 http 响应中获取 'Location'。
这是我说@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}"
我到底应该在 msg.reply
中写什么来获取位置?
TIA :)
我终于做到了 res.headers["location"]
我正在尝试使用 Hubot 将 Jenkins 与 Slack 集成。我在 Hubot 脚本中找到了 jenkins.coffee 脚本,它可以很好地满足我的计划。现在,我想在我 运行 从 Slack 构建它的命令后获取触发作业的内部版本号。为此,我想从 http 响应中获取 'Location'。
这是我说@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}"
我到底应该在 msg.reply
中写什么来获取位置?
TIA :)
我终于做到了 res.headers["location"]