Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 18 additions & 3 deletions src/yardmaster.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
# Interact with Jenkins instance remotely. Build jobs, change branches, start builders, lock jobs... The list goes on.
#
# Dependencies:
# Nope
# xml2js
# cron
#
# Configuration:
# HUBOT_JENKINS_URL - Jenkins base URL
Expand Down Expand Up @@ -38,6 +39,7 @@
#
# Author:
# @riveramj
# @lwebb - fixed problems with CSRF and post requests

{parseString} = require 'xml2js'
cronJob = require('cron').CronJob
Expand All @@ -59,6 +61,17 @@ withAuthentication = (robot, msg, callback) ->

callback(user, apiKey)

withCrumb = (robot, msg, callback) ->
withAuthentication robot, msg, (user, apiKey) ->
robot.http("#{jenkinsURL}/crumbIssuer/api/json")
.auth("#{user}", "#{apiKey}")
.get() (err, res, body) ->
if err
msg.sent "Problem getting a CSRF token #{err}"
else
json = JSON.parse(body)
callback(user, apiKey, json.crumb)

checkAuthentcation = (robot, msg) ->
yardmaster = robot.brain.get('yardmaster') || {}
yardmaster.auth ||= {}
Expand Down Expand Up @@ -128,16 +141,18 @@ get = (robot, msg, queryOptions, callback) ->
callback(res, body)

post = (robot, msg, queryOptions, postOptions, callback) ->
withAuthentication robot, msg, (user, apiKey) ->
withCrumb robot, msg, (user, apiKey, crumb) ->
robot.http("#{jenkinsURL}/#{queryOptions}")
.auth("#{user}", "#{apiKey}")
.headers('Jenkins-Crumb': crumb)
.post(postOptions) (err, res, body) ->
callback(err, res, body)

postByFullUrl = (robot, msg, url, postOptions, callback) ->
withAuthentication robot, msg, (user, apiKey) ->
withCrumb robot, msg, (user, apiKey, crumb) ->
robot.http(url)
.auth("#{user}", "#{apiKey}")
.headers('Jenkins-Crumb': crumb)
.post(postOptions) (err, res, body) ->
callback(err, res, body)

Expand Down