In:
jenkinsGetCSRFCrumb = (msg) ->
url = process.env.HUBOT_JENKINS_URL
path = "#{url}/crumbIssuer/api/json"
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)
crumb = {}
req.get() (err, res, body) ->
if err
msg.send "Jenkins says getting crumb: #{err}"
else
response = ""
try
content = JSON.parse(body)
crumb = content
msg.send "Jenkins CSRF crumb response: #{JSON.stringify(content)}"
msg.send "Jenkins CSRF crumb: #{crumb.crumb}"
catch error
msg.send error
return crumb
and a calling function:
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)
crumb = jenkinsGetCSRFCrumb(msg)
req.header('Jenkins-Crumb', crumb.crumb)
msg.reply "Got crumb: #{crumb.crumb}"
msg.reply "Got crumb: #{JSON.stringify(crumb)}"
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}"
the crumb return value does not seem to be returned to the calling function producing:
Got crumb: {}
Jenkins CSRF crumb response: {"_class":"hudson.security.csrf.DefaultCrumbIssuer","crumb":"4a70e657a96579abd72ae674fba0a16873b55cb7643cf5a5b54a1181c9296ffa","crumbRequestField":"Jenkins-Crumb"}
Jenkins CSRF crumb: 4a70e657a96579abd72ae674fba0a16873b55cb7643cf5a5b54a1181c9296ffa
Why is the returned value empty?