Skip to content

Commit ab88328

Browse files
committed
Adding backward compatibility with old JiraApi constructor
1 parent e779a22 commit ab88328

File tree

2 files changed

+56
-6
lines changed

2 files changed

+56
-6
lines changed

lib/jira.js

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ var url = require('url'),
144144
logger = console,
145145
OAuth = require("oauth");
146146

147-
var JiraApi = exports.JiraApi = function(protocol, host, port, apiVersion, verbose, base, requestOptions) {
147+
var JiraApiWithOptions = exports.JiraApiWithOptions = function(protocol, host, port, apiVersion, verbose, base, requestOptions) {
148148
this.protocol = protocol;
149149
this.host = host;
150150
this.port = port;
@@ -187,15 +187,34 @@ var JiraApi = exports.JiraApi = function(protocol, host, port, apiVersion, verbo
187187
}
188188
return obj;
189189
}
190-
191190
this.doRequest = function(options, callback) {
192191
clonedOptions = require('lodash.clonedeep')(this.requestOptions)
193192
this.request(this.extend(options,clonedOptions), callback);
194193
};
195194

196195
};
197196

198-
(function() {
197+
var JiraApi = exports.JiraApi = function(protocol, host, port, username, password, apiVersion, verbose, strictSSL, oauth, base) {
198+
options = {
199+
rejectUnauthorized: strictSSL == null?true:strictSSL
200+
};
201+
if(oauth && oauth.consumer_key && oauth.consumer_secret) {
202+
options.oauth = {
203+
consumer_key: oauth.consumer_key,
204+
consumer_secret: oauth.consumer_secret,
205+
token: oauth.access_token,
206+
token_secret: oauth.access_token_secret
207+
};
208+
} else if(username && password) {
209+
options.auth = {
210+
'user': username,
211+
'pass': password
212+
};
213+
}
214+
JiraApiWithOptions.call(this, protocol, host, port, apiVersion, verbose, base, options)
215+
};
216+
217+
_jiraMethods=(function() {
199218
// ## Find an issue in jira ##
200219
// ### Takes ###
201220
//
@@ -2101,4 +2120,6 @@ var JiraApi = exports.JiraApi = function(protocol, host, port, apiVersion, verbo
21012120
});
21022121
};
21032122

2104-
}).call(JiraApi.prototype);
2123+
});
2124+
_jiraMethods.call(JiraApiWithOptions.prototype);
2125+
_jiraMethods.call(JiraApi.prototype);

spec/jira.spec.coffee

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,39 @@ describe "Node Jira Tests", ->
2424
options =
2525
rejectUnauthorized: true
2626
headers: 'Authorization': 'Basic ' + bufferFrom('test:test').toString('base64')
27-
@jira = new nodeJira.JiraApi 'http', 'localhost', 80, 2, false, null, options
27+
@jira = new nodeJira.JiraApiWithOptions 'http', 'localhost', 80, 2, false, null, options
2828
spyOn @jira, 'request'
2929
@cb = jasmine.createSpy 'callback'
3030

31+
it "Sets basic auth if oauth is not passed in with JiraApi old method", ->
32+
options =
33+
rejectUnauthorized: false,
34+
auth :
35+
user: 'test'
36+
pass: 'test'
37+
@jira = new nodeJira.JiraApi 'http', 'localhost', 80, 'test', 'test', 2
38+
spyOn @jira, 'request'
39+
40+
@jira.doRequest options, @cb
41+
expect(@jira.request)
42+
.toHaveBeenCalledWith(options, jasmine.any(Function))
43+
44+
it "Sets OAuth oauth for the requests if oauth is passed in with JiraApi old method", ->
45+
options =
46+
rejectUnauthorized: false,
47+
oauth :
48+
consumer_key: 'ck'
49+
consumer_secret: 'cs'
50+
access_token: 'ac'
51+
access_token_secret: 'acs'
52+
# oauth = new OAuth.OAuth(null, null, oauth.consumer_key, oauth.consumer_secret, null, null, "RSA-SHA1")
53+
@jira = new nodeJira.JiraApi 'http', 'localhost', 80, 'test', 'test', 2, false, false, options.oauth
54+
spyOn @jira, 'request'
55+
56+
@jira.doRequest options, @cb
57+
expect(@jira.request)
58+
.toHaveBeenCalledWith(options, jasmine.any(Function))
59+
3160
it "Sets basic auth if oauth is not passed in", ->
3261
options =
3362
rejectUnauthorized: false
@@ -44,7 +73,7 @@ describe "Node Jira Tests", ->
4473
access_token: 'ac'
4574
access_token_secret: 'acs'
4675
# oauth = new OAuth.OAuth(null, null, oauth.consumer_key, oauth.consumer_secret, null, null, "RSA-SHA1")
47-
@jira = new nodeJira.JiraApi 'http', 'localhost', 80, 2, false, null, options
76+
@jira = new nodeJira.JiraApiWithOptions 'http', 'localhost', 80, 2, false, null, options
4877
spyOn @jira, 'request'
4978

5079
@jira.doRequest options, @cb

0 commit comments

Comments
 (0)