-
Notifications
You must be signed in to change notification settings - Fork 168
/
Copy pathjira.js
2142 lines (1865 loc) · 61.3 KB
/
jira.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// # JavaScript JIRA API for node.js #
//
// [](https://travis-ci.org/steves/node-jira)
//
// A node.js module, which provides an object oriented wrapper for the JIRA REST API.
//
// This library is built to support version `2.0.alpha1` of the JIRA REST API.
// This library is also tested with version `2` of the JIRA REST API
// It has been noted that with Jira OnDemand, `2.0.alpha1` does not work, devs
// should revert to `2`. If this changes, please notify us.
//
// JIRA REST API documentation can be found [here](http://docs.atlassian.com/jira/REST/latest/)
//
// ## Installation ##
//
// Install with the node package manager [npm](http://npmjs.org):
//
// $ npm install jira
//
// or
//
// Install via git clone:
//
// $ git clone git://github.com/steves/node-jira.git
// $ cd node-jira
// $ npm install
//
// ## Example ##
//
// Find the status of an issue.
//
// JiraApi = require('jira').JiraApi;
//
// var jira = new JiraApi('https', config.host, config.port, config.user, config.password, '2.0.alpha1');
// jira.findIssue(issueNumber, function(error, issue) {
// console.log('Status: ' + issue.fields.status.name);
// });
//
// Currently there is no explicit login call necessary as each API call uses Basic Authentication to authenticate.
//
// ## Options ##
//
// JiraApi options:
// * `protocol<string>`: Typically 'http:' or 'https:'
// * `host<string>`: The hostname for your jira server
// * `port<int>`: The port your jira server is listening on (probably `80` or `443`)
// * `user<string>`: The username to log in with
// * `password<string>`: Keep it secret, keep it safe
// * `Jira API Version<string>`: Known to work with `2` and `2.0.alpha1`
// * `verbose<bool>`: Log some info to the console, usually for debugging
// * `strictSSL<bool>`: Set to false if you have self-signed certs or something non-trustworthy
// * `oauth`: A dictionary of `consumer_key`, `consumer_secret`, `access_token` and `access_token_secret` to be used for OAuth authentication.
// * `base`: Add base slug if your JIRA install is not at the root of the host
//
// ## Implemented APIs ##
//
// * Authentication
// * HTTP
// * OAuth
// * Projects
// * Pulling a project
// * List all projects viewable to the user
// * List Components
// * List Fields
// * List Priorities
// * Versions
// * Pulling versions
// * Adding a new version
// * Pulling unresolved issues count for a specific version
// * Rapid Views
// * Find based on project name
// * Get the latest Green Hopper sprint
// * Gets attached issues
// * Issues
// * Add a new issue
// * Update an issue
// * Transition an issue
// * Pulling an issue
// * Issue linking
// * Add an issue to a sprint
// * Get a users issues (open or all)
// * List issue types
// * Search using jql
// * Set Max Results
// * Set Start-At parameter for results
// * Add a worklog
// * Add new estimate for worklog
// * Add a comment
// * Transitions
// * List
// * Users
// * Search
//
// ## TODO ##
//
// * Refactor currently implemented APIs to be more Object Oriented
// * Refactor to make use of built-in node.js events and classes
//
// ## Changelog ##
//
//
// * _0.9.0 Add OAuth Support and New Estimates on addWorklog (thanks to
// [nagyv](https://github.com/nagyv))_
// * _0.8.2 Fix URL Format Issues (thanks to
// [eduardolundgren](https://github.com/eduardolundgren))_
// * _0.8.1 Expanding the transitions options (thanks to
// [eduardolundgren](https://github.com/eduardolundgren))_
// * _0.8.0 Ability to search users (thanks to
// [eduardolundgren](https://github.com/eduardolundgren))_
// * _0.7.2 Allows HTTP Code 204 on issue update edit (thanks to
// [eduardolundgren](https://github.com/eduardolundgren))_
// * _0.7.1 Check if body variable is undef (thanks to
// [AlexCline](https://github.com/AlexCline))_
// * _0.7.0 Adds list priorities, list fields, and project components (thanks to
// [eduardolundgren](https://github.com/eduardolundgren))_
// * _0.6.0 Comment API implemented (thanks to [StevenMcD](https://github.com/StevenMcD))_
// * _0.5.0 Last param is now for strict SSL checking, defaults to true_
// * _0.4.1 Now handing errors in the request callback (thanks [mrbrookman](https://github.com/mrbrookman))_
// * _0.4.0 Now auto-redirecting between http and https (for both GET and POST)_
// * _0.3.1 [Request](https://github.com/mikeal/request) is broken, setting max request package at 2.15.0_
// * _0.3.0 Now Gets Issues for a Rapidview/Sprint (thanks [donbonifacio](https://github.com/donbonifacio))_
// * _0.2.0 Now allowing startAt and MaxResults to be passed to searchJira,
// switching to semantic versioning._
// * _0.1.0 Using Basic Auth instead of cookies, all calls unit tested, URI
// creation refactored_
// * _0.0.6 Now linting, preparing to refactor_
// * _0.0.5 JQL search now takes a list of fields_
// * _0.0.4 Added jql search_
// * _0.0.3 Added APIs and Docco documentation_
// * _0.0.2 Initial version_
var url = require('url'),
logger = console,
OAuth = require("oauth");
var JiraApi = exports.JiraApi = function(protocol, host, port, username, password, apiVersion, verbose, strictSSL, oauth, base) {
this.protocol = protocol;
this.host = host;
this.port = port;
this.username = username;
this.password = password;
this.apiVersion = apiVersion;
this.base = base;
// Default strictSSL to true (previous behavior) but now allow it to be
// modified
if (strictSSL == null) {
strictSSL = true;
}
this.strictSSL = strictSSL;
// This is so we can fake during unit tests
this.request = require('request');
if (verbose !== true) { logger = { log: function() {} }; }
// This is the same almost every time, refactored to make changing it
// later, easier
this.makeUri = function(pathname, altBase, altApiVersion) {
var basePath = 'rest/api/';
if (altBase != null) {
basePath = altBase;
}
if (this.base) {
basePath = this.base + '/' + basePath;
}
var apiVersion = this.apiVersion;
if (altApiVersion != null) {
apiVersion = altApiVersion;
}
var uri = url.format({
protocol: this.protocol,
hostname: this.host,
port: this.port,
pathname: basePath + apiVersion + pathname
});
return decodeURIComponent(uri);
};
this.doRequest = function(options, callback) {
if(oauth && oauth.consumer_key && oauth.consumer_secret) {
options.oauth = {
consumer_key: oauth.consumer_key,
consumer_secret: oauth.consumer_secret,
token: oauth.access_token,
token_secret: oauth.access_token_secret
};
} else if(this.username && this.password) {
options.auth = {
'user': this.username,
'pass': this.password
};
}
this.request(options, callback);
};
};
(function() {
// ## Find an issue in jira ##
// ### Takes ###
//
// * issueNumber: the issueNumber to find
// * callback: for when it's done
//
// ### Returns ###
//
// * error: string of the error
// * issue: an object of the issue
//
// [Jira Doc](http://docs.atlassian.com/jira/REST/latest/#id290709)
this.findIssue = function(issueNumber, callback) {
var options = {
rejectUnauthorized: this.strictSSL,
uri: this.makeUri('/issue/' + issueNumber),
method: 'GET'
};
this.doRequest(options, function(error, response, body) {
if (error) {
callback(error, null);
return;
}
if (response.statusCode === 404) {
callback('Invalid issue number.');
return;
}
if (response.statusCode !== 200) {
callback(response.statusCode + ': Unable to connect to JIRA during findIssueStatus.');
return;
}
if (body === undefined) {
callback('Response body was undefined.');
return;
}
callback(null, JSON.parse(body));
});
};
// ## Get the unresolved issue count ##
// ### Takes ###
//
// * version: version of your product that you want issues against
// * callback: function for when it's done
//
// ### Returns ###
// * error: string with the error code
// * count: count of unresolved issues for requested version
//
// [Jira Doc](http://docs.atlassian.com/jira/REST/latest/#id288524)
this.getUnresolvedIssueCount = function(version, callback) {
var options = {
rejectUnauthorized: this.strictSSL,
uri: this.makeUri('/version/' + version + '/unresolvedIssueCount'),
method: 'GET'
};
this.doRequest(options, function(error, response, body) {
if (error) {
callback(error, null);
return;
}
if (response.statusCode === 404) {
callback('Invalid version.');
return;
}
if (response.statusCode !== 200) {
callback(response.statusCode + ': Unable to connect to JIRA during findIssueStatus.');
return;
}
body = JSON.parse(body);
callback(null, body.issuesUnresolvedCount);
});
};
// ## Get the Project by project key ##
// ### Takes ###
//
// * project: key for the project
// * callback: for when it's done
//
// ### Returns ###
// * error: string of the error
// * project: the json object representing the entire project
//
// [Jira Doc](http://docs.atlassian.com/jira/REST/latest/#id289232)
this.getProject = function(project, callback) {
var options = {
rejectUnauthorized: this.strictSSL,
uri: this.makeUri('/project/' + project),
method: 'GET'
};
this.doRequest(options, function(error, response, body) {
if (error) {
callback(error, null);
return;
}
if (response.statusCode === 404) {
callback('Invalid project.');
return;
}
if (response.statusCode !== 200) {
callback(response.statusCode + ': Unable to connect to JIRA during getProject.');
return;
}
body = JSON.parse(body);
callback(null, body);
});
};
// ## Find the Rapid View for a specified project ##
// ### Takes ###
//
// * projectName: name for the project
// * callback: for when it's done
//
// ### Returns ###
// * error: string of the error
// * rapidView: rapid view matching the projectName
/**
* Finds the Rapid View that belongs to a specified project.
*
* @param projectName
* @param callback
*/
this.findRapidView = function(projectName, callback) {
var options = {
rejectUnauthorized: this.strictSSL,
uri: this.makeUri('/rapidviews/list', 'rest/greenhopper/'),
method: 'GET',
json: true
};
this.doRequest(options, function(error, response) {
if (error) {
callback(error, null);
return;
}
if (response.statusCode === 404) {
callback('Invalid URL');
return;
}
if (response.statusCode !== 200) {
callback(response.statusCode + ': Unable to connect to JIRA during rapidView search.');
return;
}
if (response.body !== null) {
var rapidViews = response.body.views;
for (var i = 0; i < rapidViews.length; i++) {
if(rapidViews[i].name.toLowerCase() === projectName.toLowerCase()) {
callback(null, rapidViews[i]);
return;
}
}
}
});
};
// ## Get a list of Sprints belonging to a Rapid View ##
// ### Takes ###
//
// * rapidViewId: the id for the rapid view
// * callback: for when it's done
//
// ### Returns ###
//
// * error: string with the error
// * sprints: the ?array? of sprints
/**
* Returns a list of sprints belonging to a Rapid View.
*
* @param rapidViewId
* @param callback
*/
this.getLastSprintForRapidView = function(rapidViewId, callback) {
var options = {
rejectUnauthorized: this.strictSSL,
uri: this.makeUri('/sprintquery/' + rapidViewId, 'rest/greenhopper/'),
method: 'GET',
json:true
};
this.doRequest(options, function(error, response) {
if (error) {
callback(error, null);
return;
}
if (response.statusCode === 404) {
callback('Invalid URL');
return;
}
if (response.statusCode !== 200) {
callback(response.statusCode + ': Unable to connect to JIRA during sprints search.');
return;
}
if (response.body !== null) {
var sprints = response.body.sprints;
callback(null, sprints.pop());
return;
}
});
};
// ## Get the issues for a rapidView / sprint##
// ### Takes ###
//
// * rapidViewId: the id for the rapid view
// * sprintId: the id for the sprint
// * callback: for when it's done
//
// ### Returns ###
//
// * error: string with the error
// * results: the object with the issues and additional sprint information
/**
* Returns sprint and issues information
*
* @param rapidViewId
* @param sprintId
* @param callback
*/
this.getSprintIssues = function getSprintIssues(rapidViewId, sprintId, callback) {
var options = {
rejectUnauthorized: this.strictSSL,
uri: this.makeUri('/rapid/charts/sprintreport?rapidViewId=' + rapidViewId + '&sprintId=' + sprintId, 'rest/greenhopper/'),
method: 'GET',
json: true
};
this.doRequest(options, function(error, response) {
if (error) {
callback(error, null);
return;
}
if( response.statusCode === 404 ) {
callback('Invalid URL');
return;
}
if( response.statusCode !== 200 ) {
callback(response.statusCode + ': Unable to connect to JIRA during sprints search');
return;
}
if(response.body !== null) {
callback(null, response.body);
} else {
callback('No body');
}
});
};
// ## Add an issue to the project's current sprint ##
// ### Takes ###
//
// * issueId: the id of the existing issue
// * sprintId: the id of the sprint to add it to
// * callback: for when it's done
//
// ### Returns ###
//
// * error: string of the error
//
//
// **does this callback if there's success?**
/**
* Adds a given issue to a project's current sprint
*
* @param issueId
* @param sprintId
* @param callback
*/
this.addIssueToSprint = function(issueId, sprintId, callback) {
var options = {
rejectUnauthorized: this.strictSSL,
uri: this.makeUri('/sprint/' + sprintId + '/issues/add', 'rest/greenhopper/'),
method: 'PUT',
followAllRedirects: true,
json:true,
body: {
issueKeys: [issueId]
}
};
logger.log(options.uri);
this.doRequest(options, function(error, response) {
if (error) {
callback(error, null);
return;
}
if (response.statusCode === 404) {
callback('Invalid URL');
return;
}
if (response.statusCode !== 204) {
callback(response.statusCode + ': Unable to connect to JIRA to add to sprint.');
return;
}
});
};
// ## Create an issue link between two issues ##
// ### Takes ###
//
// * link: a link object
// * callback: for when it's done
//
// ### Returns ###
// * error: string if there was an issue, null if success
//
// [Jira Doc](http://docs.atlassian.com/jira/REST/latest/#id296682)
/**
* Creates an issue link between two issues. Link should follow the below format:
*
* {
* 'linkType': 'Duplicate',
* 'fromIssueKey': 'HSP-1',
* 'toIssueKey': 'MKY-1',
* 'comment': {
* 'body': 'Linked related issue!',
* 'visibility': {
* 'type': 'GROUP',
* 'value': 'jira-users'
* }
* }
* }
*
* @param link
* @param callback
*/
this.issueLink = function(link, callback) {
var options = {
rejectUnauthorized: this.strictSSL,
uri: this.makeUri('/issueLink'),
method: 'POST',
followAllRedirects: true,
json: true,
body: link
};
this.doRequest(options, function(error, response) {
if (error) {
callback(error, null);
return;
}
if (response.statusCode === 404) {
callback('Invalid project.');
return;
}
if (response.statusCode !== 201) {
callback(response.statusCode + ': Unable to connect to JIRA during issueLink.');
return;
}
callback(null);
});
};
/**
* Retrieves the remote links associated with the given issue.
*
* @param issueNumber - The internal id or key of the issue
* @param callback
*/
this.getRemoteLinks = function getRemoteLinks(issueNumber, callback) {
var options = {
rejectUnauthorized: this.strictSSL,
uri: this.makeUri('/issue/' + issueNumber + '/remotelink'),
method: 'GET',
json: true
};
this.doRequest(options, function(error, response) {
if (error) {
callback(error, null);
return;
}
if (response.statusCode === 404) {
callback('Invalid issue number.');
return;
}
if (response.statusCode !== 200) {
callback(response.statusCode + ': Unable to connect to JIRA during request.');
return;
}
callback(null, response.body);
});
};
/**
* Retrieves the remote links associated with the given issue.
*
* @param issueNumber - The internal id (not the issue key) of the issue
* @param callback
*/
this.createRemoteLink = function createRemoteLink(issueNumber, remoteLink, callback) {
var options = {
rejectUnauthorized: this.strictSSL,
uri: this.makeUri('/issue/' + issueNumber + '/remotelink'),
method: 'POST',
json: true,
body: remoteLink
};
this.doRequest(options, function(error, response) {
if (error) {
callback(error, null);
return;
}
if (response.statusCode === 404) {
callback('Cannot create remote link. Invalid issue.');
return;
}
if (response.statusCode === 400) {
callback('Cannot create remote link. ' + response.body.errors.title);
return;
}
if (response.statusCode !== 200) {
callback(response.statusCode + ': Unable to connect to JIRA during request.');
return;
}
callback(null, response.body);
});
};
// ## Get Versions for a project ##
// ### Takes ###
// * project: A project key
// * callback: for when it's done
//
// ### Returns ###
// * error: a string with the error
// * versions: array of the versions for a product
//
// [Jira Doc](http://docs.atlassian.com/jira/REST/latest/#id289653)
this.getVersions = function(project, callback) {
var options = {
rejectUnauthorized: this.strictSSL,
uri: this.makeUri('/project/' + project + '/versions'),
method: 'GET'
};
this.doRequest(options, function(error, response, body) {
if (error) {
callback(error, null);
return;
}
if (response.statusCode === 404) {
callback('Invalid project.');
return;
}
if (response.statusCode !== 200) {
callback(response.statusCode + ': Unable to connect to JIRA during getVersions.');
return;
}
body = JSON.parse(body);
callback(null, body);
});
};
// ## Create a version ##
// ### Takes ###
//
// * version: an object of the new version
// * callback: for when it's done
//
// ### Returns ###
//
// * error: error text
// * version: should be the same version you passed up
//
// [Jira Doc](http://docs.atlassian.com/jira/REST/latest/#id288232)
//
/* {
* "description": "An excellent version",
* "name": "New Version 1",
* "archived": false,
* "released": true,
* "releaseDate": "2010-07-05",
* "userReleaseDate": "5/Jul/2010",
* "project": "PXA"
* }
*/
this.createVersion = function(version, callback) {
var options = {
rejectUnauthorized: this.strictSSL,
uri: this.makeUri('/version'),
method: 'POST',
followAllRedirects: true,
json: true,
body: version
};
this.doRequest(options, function(error, response, body) {
if (error) {
callback(error, null);
return;
}
if (response.statusCode === 404) {
callback('Version does not exist or the currently authenticated user does not have permission to view it');
return;
}
if (response.statusCode === 403) {
callback('The currently authenticated user does not have permission to edit the version');
return;
}
if (response.statusCode !== 201) {
callback(response.statusCode + ': Unable to connect to JIRA during createVersion.');
return;
}
callback(null, body);
});
};
// ## Update a version ##
// ### Takes ###
//
// * version: an object of the new version
// * callback: for when it's done
//
// ### Returns ###
//
// * error: error text
// * version: should be the same version you passed up
//
// [Jira Doc](https://docs.atlassian.com/jira/REST/latest/#d2e510)
//
/* {
* "id": The ID of the version being updated. Required.
* "description": "An excellent version",
* "name": "New Version 1",
* "archived": false,
* "released": true,
* "releaseDate": "2010-07-05",
* "userReleaseDate": "5/Jul/2010",
* "project": "PXA"
* }
*/
this.updateVersion = function(version, callback) {
var options = {
rejectUnauthorized: this.strictSSL,
uri: this.makeUri('/version/'+version.id),
method: 'PUT',
followAllRedirects: true,
json: true,
body: version
};
this.doRequest(options, function(error, response, body) {
if (error) {
callback(error, null);
return;
}
if (response.statusCode === 404) {
callback('Version does not exist or the currently authenticated user does not have permission to view it');
return;
}
if (response.statusCode === 403) {
callback('The currently authenticated user does not have permission to edit the version');
return;
}
if (response.statusCode !== 200) {
callback(response.statusCode + ': Unable to connect to JIRA during updateVersion.');
return;
}
callback(null, body);
});
};
// ## Pass a search query to Jira ##
// ### Takes ###
//
// * searchString: jira query string
// * optional: object containing any of the following properties
// * startAt: optional index number (default 0)
// * maxResults: optional max results number (default 50)
// * fields: optional array of desired fields, defaults when null:
// * "summary"
// * "status"
// * "assignee"
// * "description"
// * callback: for when it's done
//
// ### Returns ###
//
// * error: string if there's an error
// * issues: array of issues for the user
//
// [Jira Doc](https://docs.atlassian.com/jira/REST/latest/#d2e4424)
this.searchJira = function(searchString, optional, callback) {
// backwards compatibility
optional = optional || {};
if (Array.isArray(optional)) {
optional = { fields: optional };
}
var options = {
rejectUnauthorized: this.strictSSL,
uri: this.makeUri('/search'),
method: 'POST',
json: true,
followAllRedirects: true,
body: {
jql: searchString,
startAt: optional.startAt || 0,
maxResults: optional.maxResults || 50,
fields: optional.fields || ["summary", "status", "assignee", "description"],
expand: optional.expand || ['schema', 'names']
}
};
this.doRequest(options, function(error, response, body) {
if (error) {
callback(error, null);
return;
}
if (response.statusCode === 400) {
callback('Problem with the JQL query');
return;
}
if (response.statusCode !== 200) {
callback(response.statusCode + ': Unable to connect to JIRA during search.');
return;
}
callback(null, body);
});
};
// ## Search user on Jira ##
// ### Takes ###
//
// username: A query string used to search username, name or e-mail address
// startAt: The index of the first user to return (0-based)
// maxResults: The maximum number of users to return (defaults to 50).
// includeActive: If true, then active users are included in the results (default true)
// includeInactive: If true, then inactive users are included in the results (default false)
// * callback: for when it's done
//
// ### Returns ###
//
// * error: string if there's an error
// * users: array of users for the user
//
// [Jira Doc](http://docs.atlassian.com/jira/REST/latest/#d2e3756)
//
this.searchUsers = function(username, startAt, maxResults, includeActive, includeInactive, callback) {
startAt = (startAt !== undefined) ? startAt : 0;
maxResults = (maxResults !== undefined) ? maxResults : 50;
includeActive = (includeActive !== undefined) ? includeActive : true;
includeInactive = (includeInactive !== undefined) ? includeInactive : false;
var options = {
rejectUnauthorized: this.strictSSL,
uri: this.makeUri(
'/user/search?username=' + username +
'&startAt=' + startAt +
'&maxResults=' + maxResults +
'&includeActive=' + includeActive +
'&includeInactive=' + includeInactive),
method: 'GET',
json: true,
followAllRedirects: true
};
this.doRequest(options, function(error, response, body) {
if (error) {
callback(error, null);
return;
}
if (response.statusCode === 400) {
callback('Unable to search');
return;
}
if (response.statusCode !== 200) {
callback(response.statusCode + ': Unable to connect to JIRA during search.');
return;
}
callback(null, body);
});
};
// ## Get all users in group on Jira ##
// ### Takes ###
//
// groupName: A query string used to search users in group
// startAt: The index of the first user to return (0-based)
// maxResults: The maximum number of users to return (defaults to 50).
//
// ### Returns ###
//
// * error: string if there's an error
// * users: array of users for the user
this.getUsersInGroup = function(groupName, startAt, maxResults, callback) {
startAt = (startAt !== undefined) ? startAt : 0;
maxResults = (maxResults !== undefined) ? maxResults : 50;
var options = {
rejectUnauthorized: this.strictSSL,
uri: this.makeUri(
'/group?groupname=' + groupName +
'&expand=users[' + startAt + ':' + maxResults + ']'),
method: 'GET',
json: true,
followAllRedirects: true
};
this.doRequest(options, function(error, response, body) {
if (error) {
callback(error, null);