This repository has been archived by the owner on Jul 23, 2020. It is now read-only.
/
filter.js
286 lines (265 loc) · 10.6 KB
/
filter.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
"use strict";
module.exports = FilterClient;
/**
* Used to access Jira REST endpoints in '/rest/api/2/filter'
*
* @param {JiraClient} jiraClient
* @constructor FilterClient
*/
function FilterClient(jiraClient) {
this.jiraClient = jiraClient;
/**
* Creates a new filter, and returns newly created filter. Currently sets permissions just using the users default
* sharing permissions
*
* @method createFilter
* @memberOf FilterClient#
* @param {Object} opts The request options sent to the Jira API
* @param {Array} [opts.expand] The parameters to expand.
* @param {Object} opts.filter The filter to create. See
* {@link https://docs.atlassian.com/jira/REST/latest/#d2e3347}
* @param [callback] Called when the filter has been created.
* @return {Promise} Resolved when the filter has been created.
*/
this.createFilter = function (opts, callback) {
var options = {
uri: this.jiraClient.buildURL('/filter'),
method: 'POST',
json: true,
followAllRedirects: true,
qs: {},
body: opts.filter
};
if (opts.expand) {
options.qs.expand = '';
opts.expand.forEach(function (ex) {
options.qs.expand += ex + ','
});
}
return this.jiraClient.makeRequest(options, callback);
};
/**
* Returns a filter given an id
*
* @method getFilter
* @memberOf FilterClient#
* @param {Object} opts The request options sent to the Jira API
* @param {number} opts.filterId The id of the filter to retrieve
* @param [callback] Called when the filter has been retrieved.
* @return {Promise} Resolved when the filter has been retrieved.
*/
this.getFilter = function (opts, callback) {
var options = this.buildRequestOptions(opts, '', 'GET');
return this.jiraClient.makeRequest(options, callback);
};
/**
* Returns all filters for the current user(only for cloud)
*
* @method getFilters
* @memberOf FilterClient#
* @param {Object} opts The request options sent to the Jira API
* @param [callback] Called when the filter has been retrieved.
* @return {Promise} Resolved when the filter has been retrieved.
*/
this.getFilters = function (opts, callback) {
var options = {
uri: this.jiraClient.buildURL('/filter'),
method: 'GET',
json: true,
followAllRedirects: true
};
return this.jiraClient.makeRequest(options, callback);
};
/**
* Updates an existing filter, and returns its new value.
*
* @method updateFilter
* @memberOf FilterClient#
* @param {Object} opts The request options sent to the Jira API
* @param {number} opts.filterId The id of the filter to update
* @param {Object} opts.filter The new data for the filter. See
* {@link https://docs.atlassian.com/jira/REST/latest/#d2e3401}
* @param [callback] Called when the filter has been updated.
* @return {Promise} Resolved when the filter has been updated.
*/
this.updateFilter = function (opts, callback) {
var options = this.buildRequestOptions(opts, '', 'PUT', opts.filter);
return this.jiraClient.makeRequest(options, callback);
};
/**
* Delete a filter.
*
* @method deleteFilter
* @memberOf FilterClient#
* @param {Object} opts The request options sent to the Jira API
* @param {number} opts.filterId The id of the filter to delete
* @param [callback] Called when the filter has been deleted.
* @return {Promise} Resolved when the filter has been deleted.
*/
this.deleteFilter = function (opts, callback) {
var options = this.buildRequestOptions(opts, '', 'DELETE');
return this.jiraClient.makeRequest(options, callback, 'Filter Deleted');
};
/**
* Returns the default columns for the given filter. Currently logged in user will be used as the user making such
* request.
*
* @method getFilterColumns
* @memberOf FilterClient#
* @param {Object} opts The request options sent to the Jira API
* @param {number} opts.filterId The id of the filter for which to retrieve columns.
* @param [callback] Called when the columns have been retrieved.
* @return {Promise} Resolved when the columns have been retrieved.
*/
this.getFilterColumns = function (opts, callback) {
var options = this.buildRequestOptions(opts, '/columns', 'GET');
return this.jiraClient.makeRequest(options, callback);
};
/**
* Sets the default columns for the given filter
*
* @method setFilterColumns
* @memberOf FilterClient#
* @param {Object} opts The request options sent to the Jira API
* @param {number} opts.filterId The id of the filter for which to update columns.
* @param {Array} opts.columns The names of the new columns.
* See {@link https://docs.atlassian.com/jira/REST/latest/#d2e3460}
* @param [callback] Called when the columns have been set
* @return {Promise} Resolved when the columns have been set
*/
this.setFilterColumns = function (opts, callback) {
var body = {columns: opts.columns};
var options = this.buildRequestOptions(opts, '/columns', 'PUT', body);
return this.jiraClient.makeRequest(options, callback, 'Columns Updated');
};
/**
* Resets the columns for the given filter such that the filter no longer has its own column config.
*
* @method resetFilterColumns
* @memberOf FilterClient#
* @param {Object} opts The request options sent to the Jira API
* @param {number} opts.filterId The id of the filter for which to reset columns.
* @param [callback] Called when the columns have been reset.
* @return {Promise} Resolved when the columns have been reset.
*/
this.resetFilterColumns = function (opts, callback) {
var options = this.buildRequestOptions(opts, '/columns', 'DELETE');
return this.jiraClient.makeRequest(options, callback, 'Columns Reset');
};
/**
* Adds a share permission for the given filter
*
* @method addSharePermission
* @memberOf FilterClient#
* @param {Object} opts The request options sent to the Jira API
* @param {number} opts.filterId The id of the filter for which to reset columns.
* @param {Object} opts.permission The share permission to apply
* @param [callback] Called when the columns have been reset.
* @return {Promise} Resolved when the columns have been reset.
*/
this.addSharePermission = function (opts, callback) {
var options = this.buildRequestOptions(opts, '/permission', 'POST', opts.permission);
return this.jiraClient.makeRequest(options, callback, 'Share Permission Added');
}
/**
* Returns the default share scope of the logged-in user.
*
* @method getDefaultShareScore
* @memberOf FilterClient#
* @param opts Ignored.
* @param [callback] Called when the default share scope has been retrieved.
* @return {Promise} Resolved when the default share scope has been retrieved.
*/
this.getDefaultShareScore = function (opts, callback) {
var options = {
uri: this.jiraClient.buildURL('/filter/defaultShareScope'),
method: 'GET',
json: true,
followAllRedirects: true
};
return this.jiraClient.makeRequest(options, callback);
};
/**
* Sets the default share scope of the logged-in user.
*
* @method setDefaultShareScope
* @memberOf FilterClient#
* @param {Object} opts The request options sent to jira
* @param {string} opts.scope The new default share scope. Available values are GLOBAL and PRIVATE.
* @param [callback] Called when the default share scope has been set.
* @return {Promise} Resolved when the default share scope has been set.
*/
this.setDefaultShareScope = function (opts, callback) {
var options = {
uri: this.jiraClient.buildURL('/filter/defaultShareScope'),
method: 'PUT',
json: true,
followAllRedirects: true,
body: {
scope: opts.scope
}
};
return this.jiraClient.makeRequest(options, callback);
};
/**
* Returns the favourite filters of the logged-in user.
*
* @method getFavouriteFilters
* @memberOf FilterClient#
* @param opts Ignored.
* @param [callback] Called when the list of favourites has been retrieved.
* @return {Promise} Resolved when the list of favourites has been retrieved.
*/
this.getFavoriteFilters = function (opts, callback) {
var options = {
uri: this.jiraClient.buildURL('/filter/favourite'),
method: 'GET',
json: true,
followAllRedirects: true
};
return this.jiraClient.makeRequest(options, callback);
};
/**
* Build out the request options necessary to make a particular API call.
*
* @private
* @method buildRequestOptions
* @memberOf FilterClient#
* @param {Object} opts The arguments passed to the method.
* @param {number} opts.filterId The id of the filter to use in the path.
* @param {Array} [opts.fields] The fields to include
* @param {Array} [opts.expand] The fields to expand
* @param {string} path The path of the endpoint following /filter/{id}
* @param {string} method The request method.
* @param {Object} [body] The request body, if any.
* @param {Object} [qs] The querystring, if any. opts.expand and opts.fields arrays will be automagically added.
* @returns {{uri: string, method: string, body: Object, qs: Object, followAllRedirects: boolean, json: boolean}}
*/
this.buildRequestOptions = function (opts, path, method, body, qs) {
var basePath = '/filter/' + opts.filterId;
if (!qs) qs = {};
if (!body) body = {};
if (opts.fields) {
qs.fields = '';
opts.fields.forEach(function (field) {
qs.fields += field + ','
});
qs.fields = qs.fields.slice(0, -1);
}
if (opts.expand) {
qs.expand = '';
opts.expand.forEach(function (ex) {
qs.expand += ex + ','
});
qs.expand = qs.expand.slice(0, -1);
}
return {
uri: this.jiraClient.buildURL(basePath + path),
method: method,
body: body,
qs: qs,
followAllRedirects: true,
json: true
};
};
}