forked from gloob/redmine-google_apps_scripts
-
Notifications
You must be signed in to change notification settings - Fork 1
/
redmine.js
368 lines (254 loc) · 8.88 KB
/
redmine.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
/*
* Connector to Redmine from Google Apps Scripts platform.
*
* Copyright (c) 2011,2012 Emergya
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* Author: Alejandro Leiva <[email protected]>
*
*/
var REDMINE_URL = 'https://projects.artefactual.com';
// TODO: this should be obtained from a configuration dialog.
var API_ACCESS_KEY = 'YOUR_API_KEY';
// HTTP Class
// This prototype is defined for HTTP Basic authentication and easy
// management of HTTP Request
var HTTP = (function() {
function HTTP() {
this.default_method = "GET";
this.base_url = "";
this.authentication = false;
this.username = "";
this.password = "";
}
HTTP.prototype.Request = function(url, method, payload) {
// Support for HTTP Basic Authentication.
// if (this.authentication) {
var credentials = Utilities.base64Encode(this.username + ":" + this.password);
var headers = {
"Authorization" : credentials
};
var options = {
"headers" : headers,
"method" : method,
"validateHttpsCertificates" : false,
"muteHttpExceptions": true
};
if (typeof payload !== 'undefined') {
options['payload'] = payload;
options['contentType'] = 'application/json';
}
var content = UrlFetchApp.fetch(url, options);
return content;
};
HTTP.prototype.Get = function (url) {
return this.Request(url, "GET");
};
HTTP.prototype.Post = function (url, payload) {
return this.Request(url, "POST", payload);
};
HTTP.prototype.Put = function (url, payload) {
return this.Request(url, "PUT", payload);
};
HTTP.prototype.SetAuth = function (username, password) {
this.username = username;
this.password = password;
this.authentication = true;
};
return HTTP;
})();
var Cache = (function() {
function Cache() {
this.store = {};
}
Cache.prototype.set = function (key, value) {
this.store[key] = value;
}
Cache.prototype.get = function (key) {
return this.store[key] || undefined;
}
return Cache;
})();
// Class Translator
var Translator = (function() {
function Translator() {
}
// XML to JS Object.
Translator.prototype.xmlToJS = function (element) {
//TODO: Refactor this to add an array when necessary, not always.
var obj = {};
var element_name = element.getName().getLocalName();
var element_text = element.getText();
// element!
var text = (element_text);
var name = (element_name);
obj[name] = {};
if (text.length > 0) {
obj[name]["text"] = text;
}
var attributes = element.getAttributes();
if (attributes.length > 0) {
obj[name]["attributes"] = {};
for(i = 0; i < attributes.length; i++) {
obj[name]["attributes"][attributes[i].getName().getLocalName()] = attributes[i].getValue();
}
}
if (typeof(obj[name]["childs"]) == "undefined") {
obj[name]["childs"] = [];
}
var childs = element.getElements();
for (var i in childs) {
obj[name]["childs"].push(this.xmlToJS(childs[i]));
}
return obj;
};
Translator.prototype.searchTag = function (data, tag) {
var ret_value;
for (var i in data) {
if (data[i][tag]) {
ret_value = data[i][tag];
break;
}
}
return ret_value;
};
return Translator;
})();
var Redmine = (function() {
function Redmine(base_url, items_by_page) {
this.ITEMS_BY_PAGE = items_by_page || 100;
this.base_url = base_url || '';
this.http = new HTTP();
this.http.SetAuth(API_ACCESS_KEY);
this.translator = new Translator();
this.cache = new Cache();
// Privileged methods
this.paginate = function (url) {
var response = this.http.Get(url);
var content = response.getContentText();
var xml = Xml.parse(content, true);
var root = xml.getElement();
var entries = root.getAttribute('total_count').getValue();
var pages = Math.floor((entries / this.ITEMS_BY_PAGE) + 1);
return pages;
};
this.getDataElement = function (url, root_tag) {
// TODO: Avoid the use of root_tag and element_tag we can infer it.
if (this.cache.get(url)) {
return this.cache.get(url);
} else {
var data = [];
var xml_content = this.http.Get(url);
var xml = Xml.parse(xml_content.getContentText(), true);
var root_element = xml.getElement();
var elements_data = this.translator.xmlToJS(root_element);
var elements = elements_data[root_tag].childs;
this.cache.set(url, elements);
return elements;
}
};
this.getData = function (base_url, root_tag, element_tag) {
if (this.cache.get(base_url)) {
return this.cache.get(base_url);
} else {
var data = [];
var pages = this.paginate(base_url);
for (var i = 1; i <= pages; i++) {
if (base_url.indexOf("?") > 0)
var url = base_url + '&limit=' + this.ITEMS_BY_PAGE + '&page=' + i;
else
var url = base_url + '?limit=' + this.ITEMS_BY_PAGE + '&page=' + i;
var xml_content = this.http.Get(url);
var xml = Xml.parse(xml_content.getContentText(), true);
var root_element = xml.getElement();
var elements_data = this.translator.xmlToJS(root_element);
var elements = elements_data[root_tag].childs;
if (!elements || elements.length == 0) {
return "Something went wrong";
}
for (var j in elements) {
data.push(elements[j][element_tag].childs);
}
}
this.cache.set(base_url, data);
return data;
}
};
}
Redmine.prototype.getIssues = function (project_id) {
Logger.log("Launching getIssues(" + project_id + ")");
var url = REDMINE_URL + '/issues.xml?project_id=' + project_id + '&status_id=*';
var data = this.getData(url, 'issues', 'issue');
return data;
};
Redmine.prototype.getProjects = function () {
Logger.log("Launching getProjects()");
var url = REDMINE_URL + '/projects.xml';
var data = this.getData(url, 'projects', 'project');
return data;
};
Redmine.prototype.getProject = function (project_id) {
Logger.log("Launching getProject(" + project_id + ")");
var url = REDMINE_URL + '/projects/' + project_id + '.xml';
var data = this.getDataElement(url, 'project');
return data;
};
Redmine.prototype.getTimeEntries = function (project_id) {
Logger.log("Launching getTimeEntries(" + project_id + ")");
var url = REDMINE_URL + '/projects/' + project_id + '/time_entries.xml';
var data = this.getData(url, 'time_entries', 'time_entry');
return data;
};
Redmine.prototype.getTimeEntriesByIssue = function (issue_id, project_id) {
Logger.log("Launching getTimeEntriesByIssue(" + issue_id + ")");
/* By any reason this is returning bad values */
/*
var url = REDMINE_URL + '/issues/' + issue_id + '/time_entries.xml';
var data = this.getData(url, 'time_entries', 'time_entry');
return data;
*/
var data = [];
var time_entries = this.getTimeEntries(project_id);
for (var i = 0; i < time_entries.length; i++) {
var te_issue_id = this.translator.searchTag(time_entries[i], 'issue');
if (te_issue_id.attributes.id == issue_id)
data.push(time_entries[i]);
}
return data;
};
Redmine.prototype.getIssuesByTracker = function (project_id, tracker_id) {
Logger.log("Launching getIssuesByTracker("+project_id+","+tracker_id+")");
var url = REDMINE_URL + '/issues.xml?project_id=' + project_id + '&tracker_id='+ tracker_id + '&status_id=*';
var data = this.getData(url, 'issues', 'issue');
return data;
};
Redmine.prototype.issueUpdate = function (issue_id, start_date, due_date) {
//TODO: Create Issue class for easy handling.
this.http.SetAuth(API_ACCESS_KEY);
//TODO: Create Issue to send
var ret = this.http.Put(REDMINE_URL + '/issues/' + issue_id + '.xml');
};
Redmine.prototype.addTimeEntry = function (row) {
Logger.log("Posting time entry("+row.issueNum+","+row.date+")");
this.http.SetAuth(API_ACCESS_KEY);
var payload = {'time_entry': {
'issue_id' : row.issueNum,
'activity_id': row.activityId,
'hours' : row.hours,
'comments' : row.desc,
'spent_on' : row.date
}};
var ret = this.http.Post(REDMINE_URL + '/time_entries.json', JSON.stringify(payload));
Logger.log('Return: ' + ret);
}
return Redmine;
})();