Skip to content

Commit

Permalink
Fix Jenkins crumb retrieval when URL path is not on the root context.
Browse files Browse the repository at this point in the history
  • Loading branch information
cliffano committed Apr 13, 2019
1 parent d7c53cb commit aa7e964
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 3 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
### 2.2.0
*
* Fix Jenkins crumb retrieval when URL path is not on the root context

### 2.1.0
* Upgrade swaggy-jenkins to 1.0.0
Expand Down
2 changes: 1 addition & 1 deletion lib/api/jenkins.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ function computer(cb) {
* @param {Function} cb: standard cb(err, result) callback
*/
function crumb(cb) {
this.remoteAccessApi.getCrumb(cb);
this.baseApi.getCrumb(cb);
}

/**
Expand Down
17 changes: 17 additions & 0 deletions lib/jenkins.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ var jenkins = require('./api/jenkins');
var job = require('./api/job');
var Swaggy = require('swaggy-jenkins');
var text = require('bagoftext');
var _url = require('url');
var util = require('./cli/util');
var view = require('./api/view');
var fs = require('fs');
Expand All @@ -25,6 +26,13 @@ function Jenkins(url) {
}

this.url = url || process.env.JENKINS_URL || 'http://localhost:8080';

// base URL is needed for functionalities that operate against base Jenkins path
// this is needed for retrieving Jenkins crumb where the URL might contain paths to folders,
// but the the Jenkins crumb retrieval still needs to be executed against the base Jenkins path
let parsedUrl = _url.parse(this.url);
this.baseUrl = this.url.replace(parsedUrl.path, '');

var cert = process.env.JENKINS_CERT;
var ca = process.env.JENKINS_CA;
var key = process.env.JENKINS_KEY;
Expand All @@ -38,6 +46,15 @@ function Jenkins(url) {
this.remoteAccessApi = new Swaggy.RemoteAccessApi();
this.remoteAccessApi.apiClient.basePath = this.url;

// a new Swaggy.ApiClient must be created here in order
// to force baseApi to not share the same ApiClient as
// remoteAccessApi
// this is necessary because baseApi uses a URL that could be
// different to remoteAccessApi
this.baseApi = new Swaggy.BaseApi(new Swaggy.ApiClient());
this.baseApi.apiClient.basePath = this.baseUrl;


if (cert) {
var key_path = key.split(':')[0];
var passphrase = key.split(':')[1];
Expand Down
3 changes: 2 additions & 1 deletion test/api/jenkins.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ buster.testCase('api - jenkins', {
jenkins.url = 'http://localhost:8080';
jenkins.opts = { handlers: {} };
jenkins.remoteAccessApi = new Swaggy.RemoteAccessApi();
jenkins.baseApi = new Swaggy.BaseApi();

this.mockTimer = this.useFakeTimers();
this.mock({});
Expand All @@ -32,7 +33,7 @@ buster.testCase('api - jenkins', {
jenkins.computer(done);
},
'crumb - should delegate to Swaggy getCrumb': function (done) {
this.stub(jenkins.remoteAccessApi, 'getCrumb', function (cb) {
this.stub(jenkins.baseApi, 'getCrumb', function (cb) {
cb();
});
jenkins.crumb(done);
Expand Down

0 comments on commit aa7e964

Please sign in to comment.