-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathredmine.js
229 lines (177 loc) · 6.22 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
var cesRdmUsersUrl = "/redmine/users.json";
var cesRdmProjectsUrl = "/redmine/projects.json";
var cesRdmMembershipsUrl = "/redmine/projects/%%project%%/memberships.json";
var cesRdmIssuesUrl = "/redmine/projects/%%project%%/issues.json";
var cesRdmIssueUrl = "/redmine/issues/%%issue%%.json";
var cesRdmTimeEntriesUrl ="/redmine/projects/%%project%%/time_entries.json";
var cesRdmVersionsUrl = "/redmine/projects/%%project%%/versions.json";
function cesRdmApiCall(url, node) {
url = userProperties.getProperty('FQDN') + url;
var params = {
"method":"GET",
"headers":JSON.parse(userProperties.getProperty('AUTH_HEADER'))
};
var limit = 100;
var totalCountUrl = url;
try {
var response = UrlFetchApp.fetch(totalCountUrl, params);
}
catch(err) {
return "ERROR: " + err.message;
}
var json = response.getContentText();
var data = JSON.parse(json);
var totalCount = data.total_count;
if (totalCount > 0) {
var roundtrips = Math.ceil(totalCount / limit);
var headerArray = Object.keys(data[node][0]);
headerArray.splice(headerArray.indexOf("custom_fields"), 1);
var resultArray = [headerArray];
var responseUrl = url;
for (n=0; n<roundtrips; n++) {
responseUrl = url + "offset="+(n*limit)+"&limit="+limit;
response = UrlFetchApp.fetch(responseUrl, params);
json = response.getContentText();
data = JSON.parse(json);
for (var i in data[node]) {
var subArray = [];
for (var j in headerArray) {
if (typeof data[node][i][headerArray[j]] === "object" && data[node][i][headerArray[j]] != null) {
// TODO: handle roles in memberships
if (headerArray[j] == "issue") {
subArray.push(data[node][i][headerArray[j]]["id"]);
} else {
subArray.push(data[node][i][headerArray[j]]["name"]);
}
} else {
subArray.push(data[node][i][headerArray[j]]);
}
}
resultArray.push(subArray);
}
}
} else {
var resultArray = ["WARNING: Empty dataset returned."];
}
return resultArray;
}
function cesRdmIssueApiCall(url, node) {
url = userProperties.getProperty('FQDN') + url;
var params = {
"method":"GET",
"headers":JSON.parse(userProperties.getProperty('AUTH_HEADER'))
};
var response = UrlFetchApp.fetch(url, params);
var json = response.getContentText();
var data = JSON.parse(json);
var headerArray = Object.keys(data[node]);
headerArray.splice(headerArray.indexOf("custom_fields"), 1);
var resultArray = [headerArray];
var subArray = [];
for (var j in headerArray) {
if (typeof data[node][headerArray[j]] === "object" && data[node][headerArray[j]] != null) {
subArray.push(data[node][headerArray[j]]["name"]);
} else {
subArray.push(data[node][headerArray[j]]);
}
}
resultArray.push(subArray);
return resultArray;
}
function cesRdmCFIssueApiCall(url, custom_field) {
url = userProperties.getProperty('FQDN') + url;
var params = {
"method":"GET",
"headers":JSON.parse(userProperties.getProperty('AUTH_HEADER'))
};
var response = UrlFetchApp.fetch(url, params);
var json = response.getContentText();
var data = JSON.parse(json);
var headerArray = Object.keys(data["issue"]["custom_fields"][0]);
var resultArray = [];
var subArray = [];
for (var j in data["issue"]["custom_fields"]) {
if (data["issue"]["custom_fields"][j]["name"] == custom_field) {
subArray.push(data["issue"]["custom_fields"][j]["value"]);
}
}
resultArray.push(subArray);
return resultArray;
}
function cesRdm_Users() {
var data = cesRdmApiCall(cesRdmUsersUrl + "?", "users");
return data;
}
function cesRdm_Projects() {
var data = cesRdmApiCall(cesRdmProjectsUrl + "?", "projects");
return data;
}
function cesRdm_Memberships(project) {
if (project != null && project != "") {
var url = cesRdmMembershipsUrl.replace("%%project%%", project) + "?";
var data = cesRdmApiCall(url, "memberships");
return data;
} else {
return("Please choose a project identifier");
}
}
function cesRdm_Issues(project) {
if (project != null && project != "") {
var url = cesRdmIssuesUrl.replace("%%project%%", project) + "?";
var data = cesRdmApiCall(url, "issues");
return data;
} else {
return("Please choose a project identifier");
}
}
function cesRdm_Issue(issue) {
if (issue != null && issue != "") {
var url = cesRdmIssueUrl.replace("%%issue%%", issue) + "?";
var data = cesRdmIssueApiCall(url, "issue");
return data;
} else {
return("Please choose an issue id");
}
}
function cesRdm_CFIssue(issue, custom_field) {
if (issue != null && issue != "" && custom_field != null && custom_field != "") {
var url = cesRdmIssueUrl.replace("%%issue%%", issue) + "?";
var data = cesRdmCFIssueApiCall(url, custom_field);
return data;
} else {
return("Please choose an issue id and custom field name");
}
}
function cesRdm_TimeEntries(project, from, to) {
var date = new Date();
if (from == null || from == "") {
var firstDay = new Date(date.getFullYear(), date.getMonth(), 1);
from = [firstDay.getFullYear() + "-",
(firstDay.getMonth()>9 ? '' : '0') + firstDay.getMonth() + "-",
(firstDay.getDate()>9 ? '' : '0') + firstDay.getDate()
].join('');
}
if (to == null || to == "") {
var lastDay = new Date(date.getFullYear(), date.getMonth() + 1, 0);
to = [lastDay.getFullYear() + "-",
(lastDay.getMonth()>9 ? '' : '0') + lastDay.getMonth() + "-",
(lastDay.getDate()>9 ? '' : '0') + lastDay.getDate()
].join('');
}
if (project != null && project != "") {
var url = cesRdmTimeEntriesUrl.replace("%%project%%", project) + "?from=" + from + "&to=" + to + "&";
var data = cesRdmApiCall(url, "time_entries");
return data;
} else {
return("Please choose a project identifier");
}
}
function cesRdm_Versions(project) {
if (project != null && project != "") {
var url = cesRdmVersionsUrl.replace("%%project%%", project) + "?";
var data = cesRdmApiCall(url, "versions");
return data;
} else {
return("Please choose a project identifier");
}
}