forked from ljay79/jira-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsearch.gs
242 lines (208 loc) · 6.81 KB
/
search.gs
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
/*function testSearch() {
var s = new Search('worklogDate>="2017-07-02" and worklogDate<="2017-07-11" and worklogAuthor="jrosemeier"');
s.setOrderBy('updated', 'DESC')
.setFields(['id','key','issuetype','project','status','summary']);
onSuccess = function(a,b,c) {
log('%s', '----------ON SUCCESS-----------');
log('%s %s %s', JSON.stringify(a), b, c);
log('%s', '---------------------1');
log('AMOUNT: %s !', a.length);
};
onFailure = function(a,b,c) {
log('%s', '----------ON FAILURE-----------');
log('a:%s b:%s c:%s', a, b, c);
log('%s', '---------------------1');
};
s.search()
.withSuccessHandler(onSuccess)
.withFailureHandler(onFailure)
;
}*/
/**
* @desc Class 'Search' API abstraction with pagination handling.
* Performs a JQL POST search request to JIRA Rest API.
* @param searchQuery {String} JQL Query statement
*/
function Search(searchQuery) {
var fields = ['key'],
startAt = 0, maxResults = 1000, maxPerPage = 500,
queryStr = searchQuery, orderBy = '', orderDir = 'ASC';
var response = {
'data': [],
'status': -1,
'errorMessage': ''
};
/**
* @desc Initialize anything necessary for the class object
* @return void
*/
this.init = function() {}
/**
* @desc Set the list of jira issue fields to be returned in search response for each issue.
* @param aFields {Array} Array of jira issue fields
* @return {this} Allow chaining
*/
this.setFields = function(aFields) {
if(aFields.constructor == Array) {
fields = aFields;
} else {
throw '{aFields} is not an Array.';
}
return this;
}
/**
* @desc Set result offset start for pagination of search results.
* @param iStartAt {Number} Number, default:0
* @return {this} Allow chaining
*/
this.setStartAt = function(iStartAt) {
if(iStartAt.constructor == Number) {
startAt = iStartAt;
} else {
throw '{iStartAt} is not a Number.';
}
return this;
}
/**
* @desc Set result offset limit for pagination of search results.
* @param iMaxResults {Number} Number, default:1000
* @return {this} Allow chaining
*/
this.setMaxResults = function(iMaxResults) {
if(iMaxResults.constructor == Number) {
maxResults = iMaxResults;
} else {
throw '{iMaxResults} is not a Number.';
}
return this;
}
/**
* @desc Set max results per page
* @param iMaxPerPage {Number} Integer of how many results max per page to fetch
* @return {this} Allow chaining
*/
this.setMaxPerPage = function(iMaxPerPage) {
if(iMaxPerPage.constructor == Number) {
maxPerPage = iMaxPerPage;
} else {
throw '{iMaxPerPage} is not a Number.';
}
return this;
}
/**
* @desc Set Order of results (JQL order by clause)
* @param sOrderBy {String} Jira field to order by. Example: 'updated'
* @param sDir {String} Direction of order; 'ASC' or 'DESC'
* @return {this} Allow Chaining
*/
this.setOrderBy = function(sOrderBy, sDir) {
sOrderBy = sOrderBy || '', sDir = sDir || 'ASC';
if( sOrderBy != '' ) orderBy = sOrderBy;
if( sDir != '' ) orderDir = sDir;
return this;
}
/**
* @desc Callback Success handler
* @param fn {function} Method to call on successfull request (statue=200)
* @return {this} Allow chaining
*/
this.withSuccessHandler = function(fn) {
if(response.status === 200) {
fn.call(this, response.data, response.status, response.errorMessage);
}
return this;
};
/**
* @desc Callback Failure handler
* @param fn {function} Method to call on failed request (status!==200)
* @return {this} Allow chaining
*/
this.withFailureHandler = function(fn) {
if(response.status !== 200) {
log("withFailureHandler: %s", response);
fn.call(this, response.data, response.status, response.errorMessage);
}
return this;
};
/**
* @desc Prepare JQL search query
* @return {String}
*/
var getJql = function() {
var jql = queryStr + ' ORDER BY ' + orderBy + ' ' + orderDir;
log('Search JQL: [%s]', jql);
//return encodeURIComponent(jql); //only when api call is performed as GET
return jql;
}
/**
* @desc OnSuccess handler for search request
* @param resp {Object} JSON response object from Jira
* @param httpResp {Object}
* @param status {Number}
* @return void
*/
var onSuccess = function(resp, httpResp, status) {
var _total = parseInt(resp.total || 0);
// nothing found - return class response
if( _total == 0 ) {
response = {
'data' : resp.issues || resp,
'status' : status,
'errorMessage' : resp.hasOwnProperty('warningMessages') ? resp.warningMessages : 'No results found.'
};
return;
}
// add current results and status
response.data.push.apply(response.data, resp.issues || []);
response.status = status;
// pagination / sub-requests required?
var _countTotalResults = parseInt(resp.startAt) + parseInt(resp.maxResults);
if( (_countTotalResults < _total) && (_countTotalResults < maxResults) ) {
// more data to fetch
var subSearch = new Search( queryStr );
subSearch.setOrderBy( orderBy, orderDir )
.setFields( fields )
.setMaxPerPage( maxPerPage )
.setMaxResults( maxResults )
.setStartAt( _countTotalResults );
subSearch.search().withSuccessHandler(function(data, status, msg) {
// append results to parent results
response.data.push.apply(response.data, data);
response.status = status;
}); // dont bubble up failure - 1st call was successfull so we soft-fail and response with results found so far
}
}
/**
* @desc OnFailure handler for search request
* @param resp {Object} JSON response object from Jira
* @param httpResp {Object}
* @param status {Number}
* @return void
*/
var onFailure = function(resp, httpResp, status) {
Logger.log('search:onFailure: [%s] %s', status, resp);
var msgs = resp.hasOwnProperty('errorMessages') ? resp.errorMessages : [];
msgs = msgs.concat((resp.hasOwnProperty('warningMessages') ? resp.warningMessages : []));
response.status = status;
response.errorMessage = msgs.join("\n");
}
/**
* @desc Perform Search
* @return {this} Allow chaining
*/
this.search = function() {
log("search with start:%s and maxResults:%s and field:[%s]", startAt, maxPerPage, fields);
var data = {
jql : getJql(),
fields : fields,
startAt : startAt,
maxResults : maxPerPage
};
var request = new Request();
request.call('search', data, {'method' : 'post'})
.withSuccessHandler(onSuccess)
.withFailureHandler(onFailure);
return this;
}
this.init();
}