Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit cbd9e0b

Browse files
committedDec 6, 2018
Externalizing request options to make jira connection settings more
flexible steves#152
1 parent 6429788 commit cbd9e0b

File tree

2 files changed

+34
-75
lines changed

2 files changed

+34
-75
lines changed
 

‎lib/jira.js

+31-73
Original file line numberDiff line numberDiff line change
@@ -27,15 +27,30 @@
2727
//
2828
// ## Example ##
2929
//
30-
// Find the status of an issue.
30+
// Find the status of an issue. (this example provide also different types of authentication
3131
//
32-
// JiraApi = require('jira').JiraApi;
33-
//
34-
// var jira = new JiraApi('https', config.host, config.port, config.user, config.password, '2.0.alpha1');
35-
// jira.findIssue(issueNumber, function(error, issue) {
36-
// console.log('Status: ' + issue.fields.status.name);
37-
// });
32+
//const HttpProxyAgent = require("http-proxy-agent");
33+
//const agent = new HttpProxyAgent("http://proxy");
34+
//const options = {
35+
// "agent": agent, // proxy setting
36+
// rejectUnauthorized: this.strictSSL, // strict ssl option
37+
// headers: {
38+
// "Proxy-Authorization": "Bearer authentication_token", // proxy authentification
39+
// "Authorization": 'Basic ' + Buffer.from(username + ':' + password).toString('base64') // basic authentication
40+
// },
41+
// oauth: { // in the case of oauth instead of basic
42+
// consumer_key: oauth.consumer_key,
43+
// consumer_secret: oauth.consumer_secret,
44+
// token: oauth.access_token,
45+
// token_secret: oauth.access_token_secret
46+
// }
47+
//}
48+
//JiraApi = require('jira').JiraApi;
3849
//
50+
//var jira = new JiraApi('https', 'server.com', '443', '2.0.alpha1', true, 'jira', options);
51+
//jira.findIssue(issueNumber, function(error, issue) {
52+
// console.log('Status: ' + issue.fields.status.name);
53+
//});
3954
// Currently there is no explicit login call necessary as each API call uses Basic Authentication to authenticate.
4055
//
4156
// ## Options ##
@@ -44,13 +59,10 @@
4459
// * `protocol<string>`: Typically 'http:' or 'https:'
4560
// * `host<string>`: The hostname for your jira server
4661
// * `port<int>`: The port your jira server is listening on (probably `80` or `443`)
47-
// * `user<string>`: The username to log in with
48-
// * `password<string>`: Keep it secret, keep it safe
4962
// * `Jira API Version<string>`: Known to work with `2` and `2.0.alpha1`
5063
// * `verbose<bool>`: Log some info to the console, usually for debugging
51-
// * `strictSSL<bool>`: Set to false if you have self-signed certs or something non-trustworthy
52-
// * `oauth`: A dictionary of `consumer_key`, `consumer_secret`, `access_token` and `access_token_secret` to be used for OAuth authentication.
5364
// * `base`: Add base slug if your JIRA install is not at the root of the host
65+
// * `options`: request Options standard parameter (see request doc)
5466
//
5567
// ## Implemented APIs ##
5668
//
@@ -132,21 +144,14 @@ var url = require('url'),
132144
logger = console,
133145
OAuth = require("oauth");
134146

135-
136-
var JiraApi = exports.JiraApi = function(protocol, host, port, username, password, apiVersion, verbose, strictSSL, oauth, base) {
147+
var JiraApi = exports.JiraApi = function(protocol, host, port, apiVersion, verbose, base, requestOptions) {
137148
this.protocol = protocol;
138149
this.host = host;
139150
this.port = port;
140-
this.username = username;
141-
this.password = password;
142151
this.apiVersion = apiVersion;
143152
this.base = base;
144-
// Default strictSSL to true (previous behavior) but now allow it to be
145-
// modified
146-
if (strictSSL == null) {
147-
strictSSL = true;
148-
}
149-
this.strictSSL = strictSSL;
153+
this.requestOptions = requestOptions===undefined?{}:requestOptions;
154+
150155
// This is so we can fake during unit tests
151156
this.request = require('request');
152157
if (verbose !== true) { logger = { log: function() {} }; }
@@ -177,20 +182,11 @@ var JiraApi = exports.JiraApi = function(protocol, host, port, username, passwor
177182
};
178183

179184
this.doRequest = function(options, callback) {
180-
if(oauth && oauth.consumer_key && oauth.consumer_secret) {
181-
options.oauth = {
182-
consumer_key: oauth.consumer_key,
183-
consumer_secret: oauth.consumer_secret,
184-
token: oauth.access_token,
185-
token_secret: oauth.access_token_secret
186-
};
187-
} else if(this.username && this.password) {
188-
options.auth = {
189-
'user': this.username,
190-
'pass': this.password
191-
};
192-
}
193-
this.request(options, callback);
185+
clonedOptions = require('clone-deep')(this.requestOptions)
186+
for (var key in options) {
187+
if (options.hasOwnProperty(key)) clonedOptions[key] = options[key];
188+
}
189+
this.request(clonedOptions, callback);
194190
};
195191

196192
};
@@ -211,7 +207,6 @@ var JiraApi = exports.JiraApi = function(protocol, host, port, username, passwor
211207
this.findIssue = function(issueNumber, callback) {
212208

213209
var options = {
214-
rejectUnauthorized: this.strictSSL,
215210
uri: this.makeUri('/issue/' + issueNumber),
216211
method: 'GET'
217212
};
@@ -256,7 +251,6 @@ var JiraApi = exports.JiraApi = function(protocol, host, port, username, passwor
256251
// [Jira Doc](http://docs.atlassian.com/jira/REST/latest/#id288524)
257252
this.getUnresolvedIssueCount = function(version, callback) {
258253
var options = {
259-
rejectUnauthorized: this.strictSSL,
260254
uri: this.makeUri('/version/' + version + '/unresolvedIssueCount'),
261255
method: 'GET'
262256
};
@@ -298,7 +292,6 @@ var JiraApi = exports.JiraApi = function(protocol, host, port, username, passwor
298292
this.getProject = function(project, callback) {
299293

300294
var options = {
301-
rejectUnauthorized: this.strictSSL,
302295
uri: this.makeUri('/project/' + project),
303296
method: 'GET'
304297
};
@@ -345,7 +338,6 @@ var JiraApi = exports.JiraApi = function(protocol, host, port, username, passwor
345338
this.findRapidView = function(projectName, callback) {
346339

347340
var options = {
348-
rejectUnauthorized: this.strictSSL,
349341
uri: this.makeUri('/rapidviews/list', 'rest/greenhopper/'),
350342
method: 'GET',
351343
json: true
@@ -400,7 +392,6 @@ var JiraApi = exports.JiraApi = function(protocol, host, port, username, passwor
400392
this.getLastSprintForRapidView = function(rapidViewId, callback) {
401393

402394
var options = {
403-
rejectUnauthorized: this.strictSSL,
404395
uri: this.makeUri('/sprintquery/' + rapidViewId, 'rest/greenhopper/'),
405396
method: 'GET',
406397
json:true
@@ -453,7 +444,6 @@ var JiraApi = exports.JiraApi = function(protocol, host, port, username, passwor
453444
this.getSprintIssues = function getSprintIssues(rapidViewId, sprintId, callback) {
454445

455446
var options = {
456-
rejectUnauthorized: this.strictSSL,
457447
uri: this.makeUri('/rapid/charts/sprintreport?rapidViewId=' + rapidViewId + '&sprintId=' + sprintId, 'rest/greenhopper/'),
458448
method: 'GET',
459449
json: true
@@ -509,7 +499,6 @@ var JiraApi = exports.JiraApi = function(protocol, host, port, username, passwor
509499
this.addIssueToSprint = function(issueId, sprintId, callback) {
510500

511501
var options = {
512-
rejectUnauthorized: this.strictSSL,
513502
uri: this.makeUri('/sprint/' + sprintId + '/issues/add', 'rest/greenhopper/'),
514503
method: 'PUT',
515504
followAllRedirects: true,
@@ -573,7 +562,6 @@ var JiraApi = exports.JiraApi = function(protocol, host, port, username, passwor
573562
this.issueLink = function(link, callback) {
574563

575564
var options = {
576-
rejectUnauthorized: this.strictSSL,
577565
uri: this.makeUri('/issueLink'),
578566
method: 'POST',
579567
followAllRedirects: true,
@@ -612,7 +600,6 @@ var JiraApi = exports.JiraApi = function(protocol, host, port, username, passwor
612600
this.getRemoteLinks = function getRemoteLinks(issueNumber, callback) {
613601

614602
var options = {
615-
rejectUnauthorized: this.strictSSL,
616603
uri: this.makeUri('/issue/' + issueNumber + '/remotelink'),
617604
method: 'GET',
618605
json: true
@@ -649,7 +636,6 @@ var JiraApi = exports.JiraApi = function(protocol, host, port, username, passwor
649636
this.createRemoteLink = function createRemoteLink(issueNumber, remoteLink, callback) {
650637

651638
var options = {
652-
rejectUnauthorized: this.strictSSL,
653639
uri: this.makeUri('/issue/' + issueNumber + '/remotelink'),
654640
method: 'POST',
655641
json: true,
@@ -697,7 +683,6 @@ var JiraApi = exports.JiraApi = function(protocol, host, port, username, passwor
697683
this.getVersions = function(project, callback) {
698684

699685
var options = {
700-
rejectUnauthorized: this.strictSSL,
701686
uri: this.makeUri('/project/' + project + '/versions'),
702687
method: 'GET'
703688
};
@@ -751,7 +736,6 @@ var JiraApi = exports.JiraApi = function(protocol, host, port, username, passwor
751736
this.createVersion = function(version, callback) {
752737

753738
var options = {
754-
rejectUnauthorized: this.strictSSL,
755739
uri: this.makeUri('/version'),
756740
method: 'POST',
757741
followAllRedirects: true,
@@ -811,7 +795,6 @@ var JiraApi = exports.JiraApi = function(protocol, host, port, username, passwor
811795
*/
812796
this.updateVersion = function(version, callback) {
813797
var options = {
814-
rejectUnauthorized: this.strictSSL,
815798
uri: this.makeUri('/version/'+version.id),
816799
method: 'PUT',
817800
followAllRedirects: true,
@@ -874,7 +857,6 @@ var JiraApi = exports.JiraApi = function(protocol, host, port, username, passwor
874857
}
875858

876859
var options = {
877-
rejectUnauthorized: this.strictSSL,
878860
uri: this.makeUri('/search'),
879861
method: 'POST',
880862
json: true,
@@ -934,7 +916,6 @@ var JiraApi = exports.JiraApi = function(protocol, host, port, username, passwor
934916
includeInactive = (includeInactive !== undefined) ? includeInactive : false;
935917

936918
var options = {
937-
rejectUnauthorized: this.strictSSL,
938919
uri: this.makeUri(
939920
'/user/search?username=' + username +
940921
'&startAt=' + startAt +
@@ -985,7 +966,6 @@ var JiraApi = exports.JiraApi = function(protocol, host, port, username, passwor
985966
maxResults = (maxResults !== undefined) ? maxResults : 50;
986967

987968
var options = {
988-
rejectUnauthorized: this.strictSSL,
989969
uri: this.makeUri(
990970
'/group?groupname=' + groupName +
991971
'&expand=users[' + startAt + ':' + maxResults + ']'),
@@ -1053,7 +1033,6 @@ var JiraApi = exports.JiraApi = function(protocol, host, port, username, passwor
10531033
// [Jira Doc](http://docs.atlassian.com/jira/REST/latest/#id290028)
10541034
this.addNewIssue = function(issue, callback) {
10551035
var options = {
1056-
rejectUnauthorized: this.strictSSL,
10571036
uri: this.makeUri('/issue'),
10581037
method: 'POST',
10591038
followAllRedirects: true,
@@ -1106,7 +1085,6 @@ var JiraApi = exports.JiraApi = function(protocol, host, port, username, passwor
11061085
this.addWatcher = function (issueKey, username, callback) {
11071086

11081087
var options = {
1109-
rejectUnauthorized: this.strictSSL,
11101088
uri: this.makeUri('/issue/' + issueKey + '/watchers'),
11111089
method: 'POST',
11121090
followAllRedirects: true,
@@ -1143,7 +1121,6 @@ var JiraApi = exports.JiraApi = function(protocol, host, port, username, passwor
11431121
// [Jira Doc](http://docs.atlassian.com/jira/REST/latest/#id290791)
11441122
this.deleteIssue = function(issueNum, callback) {
11451123
var options = {
1146-
rejectUnauthorized: this.strictSSL,
11471124
uri: this.makeUri('/issue/' + issueNum),
11481125
method: 'DELETE',
11491126
followAllRedirects: true,
@@ -1181,7 +1158,6 @@ var JiraApi = exports.JiraApi = function(protocol, host, port, username, passwor
11811158
// [Jira Doc](http://docs.atlassian.com/jira/REST/latest/#id290878)
11821159
this.updateIssue = function(issueNum, issueUpdate, callback) {
11831160
var options = {
1184-
rejectUnauthorized: this.strictSSL,
11851161
uri: this.makeUri('/issue/' + issueNum),
11861162
body: issueUpdate,
11871163
method: 'PUT',
@@ -1242,7 +1218,6 @@ var JiraApi = exports.JiraApi = function(protocol, host, port, username, passwor
12421218
*/
12431219
this.listComponents = function(project, callback) {
12441220
var options = {
1245-
rejectUnauthorized: this.strictSSL,
12461221
uri: this.makeUri('/project/' + project + '/components'),
12471222
method: 'GET',
12481223
json: true
@@ -1282,7 +1257,6 @@ var JiraApi = exports.JiraApi = function(protocol, host, port, username, passwor
12821257
// [Jira Doc](http://docs.atlassian.com/jira/REST/latest/#id290028)
12831258
this.addNewComponent = function (component, callback) {
12841259
var options = {
1285-
rejectUnauthorized: this.strictSSL,
12861260
uri: this.makeUri('/component'),
12871261
method: 'POST',
12881262
followAllRedirects: true,
@@ -1325,7 +1299,6 @@ var JiraApi = exports.JiraApi = function(protocol, host, port, username, passwor
13251299
// [Jira Doc](http://docs.atlassian.com/jira/REST/latest/#id290791)
13261300
this.deleteComponent = function (componentNum, callback) {
13271301
var options = {
1328-
rejectUnauthorized: this.strictSSL,
13291302
uri: this.makeUri('/component/' + componentNum),
13301303
method: 'DELETE',
13311304
followAllRedirects: true,
@@ -1375,7 +1348,6 @@ var JiraApi = exports.JiraApi = function(protocol, host, port, username, passwor
13751348
*/
13761349
this.listFields = function(callback) {
13771350
var options = {
1378-
rejectUnauthorized: this.strictSSL,
13791351
uri: this.makeUri('/field'),
13801352
method: 'GET',
13811353
json: true
@@ -1423,7 +1395,6 @@ var JiraApi = exports.JiraApi = function(protocol, host, port, username, passwor
14231395
*/
14241396
this.listPriorities = function(callback) {
14251397
var options = {
1426-
rejectUnauthorized: this.strictSSL,
14271398
uri: this.makeUri('/priority'),
14281399
method: 'GET',
14291400
json: true
@@ -1500,7 +1471,6 @@ var JiraApi = exports.JiraApi = function(protocol, host, port, username, passwor
15001471
*/
15011472
this.listTransitions = function(issueId, callback) {
15021473
var options = {
1503-
rejectUnauthorized: this.strictSSL,
15041474
uri: this.makeUri('/issue/' + issueId + '/transitions?expand=transitions.fields'),
15051475
method: 'GET',
15061476
json: true
@@ -1541,7 +1511,6 @@ var JiraApi = exports.JiraApi = function(protocol, host, port, username, passwor
15411511
// [Jira Doc](http://docs.atlassian.com/jira/REST/latest/#id290489)
15421512
this.transitionIssue = function(issueNum, issueTransition, callback) {
15431513
var options = {
1544-
rejectUnauthorized: this.strictSSL,
15451514
uri: this.makeUri('/issue/' + issueNum + '/transitions'),
15461515
body: issueTransition,
15471516
method: 'POST',
@@ -1591,7 +1560,6 @@ var JiraApi = exports.JiraApi = function(protocol, host, port, username, passwor
15911560
*/
15921561
this.listProjects = function(callback) {
15931562
var options = {
1594-
rejectUnauthorized: this.strictSSL,
15951563
uri: this.makeUri('/project'),
15961564
method: 'GET',
15971565
json: true
@@ -1631,7 +1599,6 @@ var JiraApi = exports.JiraApi = function(protocol, host, port, username, passwor
16311599
// [Jira Doc](https://docs.atlassian.com/jira/REST/latest/#id108798)
16321600
this.addComment = function(issueId, comment, callback){
16331601
var options = {
1634-
rejectUnauthorized: this.strictSSL,
16351602
uri: this.makeUri('/issue/' + issueId + '/comment'),
16361603
body: {
16371604
"body": comment
@@ -1705,7 +1672,6 @@ var JiraApi = exports.JiraApi = function(protocol, host, port, username, passwor
17051672
newEstimate = false;
17061673
}
17071674
var options = {
1708-
rejectUnauthorized: this.strictSSL,
17091675
uri: this.makeUri('/issue/' + issueId + '/worklog' + (newEstimate ? "?adjustEstimate=new&newEstimate=" + newEstimate : "")),
17101676
body: worklog,
17111677
method: 'POST',
@@ -1754,7 +1720,6 @@ var JiraApi = exports.JiraApi = function(protocol, host, port, username, passwor
17541720
this.deleteWorklog = function(issueId, worklogId, callback) {
17551721

17561722
var options = {
1757-
rejectUnauthorized: this.strictSSL,
17581723
uri: this.makeUri('/issue/' + issueId + '/worklog/' + worklogId),
17591724
method: 'DELETE',
17601725
followAllRedirects: true,
@@ -1802,7 +1767,6 @@ var JiraApi = exports.JiraApi = function(protocol, host, port, username, passwor
18021767
*/
18031768
this.listIssueTypes = function(callback) {
18041769
var options = {
1805-
rejectUnauthorized: this.strictSSL,
18061770
uri: this.makeUri('/issuetype'),
18071771
method: 'GET',
18081772
json: true
@@ -1853,7 +1817,6 @@ var JiraApi = exports.JiraApi = function(protocol, host, port, username, passwor
18531817
*/
18541818
this.registerWebhook = function(webhook, callback) {
18551819
var options = {
1856-
rejectUnauthorized: this.strictSSL,
18571820
uri: this.makeUri('/webhook', 'rest/webhooks/', '1.0'),
18581821
method: 'POST',
18591822
json: true,
@@ -1904,7 +1867,6 @@ var JiraApi = exports.JiraApi = function(protocol, host, port, username, passwor
19041867
*/
19051868
this.listWebhooks = function(callback) {
19061869
var options = {
1907-
rejectUnauthorized: this.strictSSL,
19081870
uri: this.makeUri('/webhook', 'rest/webhooks/', '1.0'),
19091871
method: 'GET',
19101872
json: true
@@ -1940,7 +1902,6 @@ var JiraApi = exports.JiraApi = function(protocol, host, port, username, passwor
19401902
// [Jira Doc](https://developer.atlassian.com/display/JIRADEV/JIRA+Webhooks+Overview)
19411903
this.getWebhook = function(webhookID, callback) {
19421904
var options = {
1943-
rejectUnauthorized: this.strictSSL,
19441905
uri: this.makeUri('/webhook/' + webhookID, 'rest/webhooks/', '1.0'),
19451906
method: 'GET',
19461907
json: true
@@ -1976,7 +1937,6 @@ var JiraApi = exports.JiraApi = function(protocol, host, port, username, passwor
19761937
// [Jira Doc](https://developer.atlassian.com/display/JIRADEV/JIRA+Webhooks+Overview)
19771938
this.deleteWebhook = function(webhookID, callback) {
19781939
var options = {
1979-
rejectUnauthorized: this.strictSSL,
19801940
uri: this.makeUri('/webhook/' + webhookID, 'rest/webhooks/', '1.0'),
19811941
method: 'DELETE',
19821942
json: true
@@ -2025,7 +1985,6 @@ var JiraApi = exports.JiraApi = function(protocol, host, port, username, passwor
20251985
*/
20261986
this.getCurrentUser = function(callback) {
20271987
var options = {
2028-
rejectUnauthorized: this.strictSSL,
20291988
uri: this.makeUri('/session', 'rest/auth/', '1'),
20301989
method: 'GET',
20311990
json: true
@@ -2116,7 +2075,6 @@ var JiraApi = exports.JiraApi = function(protocol, host, port, username, passwor
21162075
*/
21172076
this.getBacklogForRapidView = function(rapidViewId, callback) {
21182077
var options = {
2119-
rejectUnauthorized: this.strictSSL,
21202078
uri: this.makeUri('/xboard/plan/backlog/data?rapidViewId=' + rapidViewId, 'rest/greenhopper/'),
21212079
method: 'GET',
21222080
json: true

‎package.json

+3-2
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,9 @@
2323
}
2424
],
2525
"dependencies": {
26-
"request": "<2.16.0",
27-
"oauth": "^0.9.11"
26+
"clone-deep": "^4.0.1",
27+
"oauth": "^0.9.11",
28+
"request": "<2.16.0"
2829
},
2930
"scripts": {
3031
"test": "grunt test"

0 commit comments

Comments
 (0)
Please sign in to comment.