This repository was archived by the owner on Oct 5, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathapp.js
More file actions
332 lines (278 loc) · 12.8 KB
/
app.js
File metadata and controls
332 lines (278 loc) · 12.8 KB
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
(function () {
//var v1url = 'https://www11.v1host.com/V1Integrations';
//var v1username = 'admin';
//var v1password = 'admin1234';
//var v1projectID = 'Scope:1136';
//var v1sourceID = 'StorySource:1137';
//var v1AssetIDCustomField = 'custom_field_21479374';
//var v1AssetNumberCustomField = 'custom_field_21481134';
//var v1AssetStatusCustomField = 'custom_field_21487554';
//var v1ReleaseNumberCustomField = 'custom_field_21481584';
var v1url = '';
var v1username = '';
var v1password = '';
var v1projectID = '';
var v1sourceID = '';
var v1AssetIDCustomField = '';
var v1AssetNumberCustomField = '';
var v1AssetStatusCustomField = '';
var v1ReleaseNumberCustomField = '';
var v1DefectId = '';
var v1defectTag = 'v1defect';
return {
events: {
'app.activated': 'initializeApp',
'click .createDefect': 'createDefect',
'click .searchDefect': 'searchDefect',
'click .assignDefect': 'findDefect',
'click .unassignDefect': 'unassignDefect'
},
requests: {
submitDefect: function (xmlPayload) {
return {
url: v1url + '/rest-1.v1/Data/Defect?Accept=application/json',
type: 'POST',
dataType: 'application/xml',
data: xmlPayload,
headers: { 'Authorization': 'Basic ' + Base64.encode(v1username + ':' + v1password) },
proxy_v2: true
};
},
updateDefect: function (v1DefectInteger, xmlPayload) {
return {
url: v1url + '/rest-1.v1/Data/Defect/' + v1DefectInteger + '?Accept=application/json',
type: 'POST',
dataType: 'application/xml',
data: xmlPayload,
headers: { 'Authorization': 'Basic ' + Base64.encode(v1username + ':' + v1password) },
proxy_v2: true
};
},
submitLink: function (xmlPayload) {
return {
url: v1url + '/rest-1.v1/Data/Link?Accept=application/json',
type: 'POST',
dataType: 'application/xml',
data: xmlPayload,
headers: { 'Authorization': 'Basic ' + Base64.encode(v1username + ':' + v1password) },
proxy_v2: true
};
},
searchDefect: function (stringToSearch) {
return {
url: v1url + '/rest-1.v1/Data/Defect?Accept=application/json&sel=Number,Name&findin=Name,Description&find="' + stringToSearch + '"',
type: 'GET',
dataType: 'application/xml',
headers: { 'Authorization': 'Basic ' + Base64.encode(v1username + ':' + v1password) },
proxy_v2: true
};
},
getDefect: function (defectID) {
return {
url: v1url + '/rest-1.v1/Data/Defect?Accept=application/json&sel=Number,Status.Name,Owners.Name,Priority.Name,FixedInBuild,ChangeDateUTC&where=ID="' + defectID + '"',
type: 'GET',
dataType: 'application/xml',
headers: { 'Authorization': 'Basic ' + Base64.encode(v1username + ':' + v1password) },
proxy_v2: true
};
},
getDefectByNumber: function (defectNumber) {
return {
url: v1url + '/rest-1.v1/Data/Defect?Accept=application/json&sel=Number,Status.Name,Owners.Name,Priority.Name,FixedInBuild,ChangeDateUTC&where=Number="' + defectNumber + '"',
type: 'GET',
dataType: 'application/xml',
headers: { 'Authorization': 'Basic ' + Base64.encode(v1username + ':' + v1password) },
proxy_v2: true
};
}
},
//Initialize the application by setting global variables.
initializeApp: function (data) {
if (data.firstLoad) {
v1url = this.setting('v1url');
v1username = this.setting('v1username');
v1password = this.setting('v1password');
v1projectID = this.setting('v1project');
v1sourceID = this.setting('v1source');
v1AssetIDCustomField = this.setting('v1assetIDField');
v1AssetNumberCustomField = this.setting('v1assetNumberField');
v1AssetStatusCustomField = this.setting('v1assetStatusField');
v1ReleaseNumberCustomField = this.setting('v1assetNumberField');
//DEBUG ONLY:
//console.log(v1url + ":" + v1username + ":" + v1password);
//console.log(v1projectID + ":" + v1sourceID);
//console.log(v1AssetIDCustomField + ":" + v1AssetNumberCustomField + ":" + v1ReleaseNumberCustomField + ":" + v1AssetStatusCustomField);
}
this.showMain(data);
},
//Determine if current ticket is associated with a V1 defect, then display the main form in the Zendesk sidebar.
showMain: function () {
var field = this.ticket().customField(v1AssetIDCustomField);
if (field) {
var request = this.ajax('getDefect', this.ticket().customField(v1AssetIDCustomField));
request.always(this.showDefect);
}
else {
this.switchTo('mainAssign');
}
},
//Display error form in Zendesk sidebar.
showError: function () {
this.switchTo('error');
},
//Determine V1 api call success, then display V1 defect data in Zendesk sidebar.
processDefect: function (data) {
//DEBUG ONLY:
console.log(data);
if (data.status == '200') {
this.ticket().tags().add(v1defectTag);
var request = this.ajax('getDefect', v1DefectId);
request.always(this.showDefect);
}
else {
this.showSpinner(false);
this.switchTo('error');
}
},
//Display V1 defect in Zendesk sidebar.
showDefect: function (data) {
//DEBUG ONLY:
console.log(data);
if (data.status == '200') {
var defectJSON = JSON.parse(data.responseText);
this.ticket().customField(v1AssetIDCustomField, defectJSON.Assets[0].id);
this.ticket().customField(v1AssetNumberCustomField, defectJSON.Assets[0].Attributes.Number.value);
this.ticket().customField(v1AssetStatusCustomField, defectJSON.Assets[0].Attributes["Status.Name"].value);
this.showSpinner(false);
this.switchTo('defectInfo', {
defectID: defectJSON.Assets[0].id,
defectNumber: defectJSON.Assets[0].Attributes.Number.value,
defectStatus: defectJSON.Assets[0].Attributes["Status.Name"].value,
defectPriority: defectJSON.Assets[0].Attributes["Priority.Name"].value,
defectFixedInBuild: defectJSON.Assets[0].Attributes.FixedInBuild.value,
defectChangeDate: this.getFriendlyDateTime(defectJSON.Assets[0].Attributes.ChangeDateUTC.value)
});
}
else {
this.showSpinner(false);
this.switchTo('error');
}
},
//Create V1 defect from Zendesk ticket.
createDefect: function () {
//DEBUG ONLY:
this.logTicket();
var xmlPayload = '<Asset>';
xmlPayload += '<Relation name="Scope" act="set"><Asset idref="' + v1projectID + '" /></Relation>';
xmlPayload += '<Attribute name="Name" act="set">' + this.ticket().subject() + '</Attribute>';
xmlPayload += '<Attribute name="Reference" act="set">' + this.ticket().id() + '</Attribute>';
xmlPayload += '<Attribute name="Description" act="set">' + this.ticket().description() + '</Attribute>';
xmlPayload += '<Relation name="Source" act="set"><Asset idref="' + v1sourceID + '" /></Relation>';
xmlPayload += '<Attribute name="FoundBy" act="set">' + this.ticket().requester().email() + '</Attribute>';
xmlPayload += '<Attribute name="FoundInBuild" act="set">' + this.ticket().customField(v1ReleaseNumberCustomField) + '</Attribute>';
xmlPayload += '</Asset>';
console.log('xmlPayload: ' + xmlPayload);
this.showSpinner(true);
var request = this.ajax('submitDefect', xmlPayload);
request.always(this.createLink);
},
//Find V1 defect based on specified V1 asset number like "D-01234".
findDefect: function () {
this.showSpinner(true);
var request = this.ajax('getDefectByNumber', this.$("#txtSearch").val());
request.always(this.assignDefect);
},
//Associate an existing V1 defect with the current Zendesk ticket.
assignDefect: function (data) {
//DEBUG ONLY:
console.log(data);
if (data.status == '200') {
//Get the integer portion of the V1 defect ID that is needed for defect update in V1.
var defectJSON = JSON.parse(data.responseText);
var v1IdArray = defectJSON.Assets[0].id.split(":");
var v1DefectInteger = v1IdArray[1];
console.log("v1DefectInteger: " + v1DefectInteger);
var xmlPayload = '<Asset>';
xmlPayload += '<Attribute name="Reference" act="set">' + this.ticket().id() + '</Attribute>';
xmlPayload += '<Relation name="Source" act="set"><Asset idref="' + v1sourceID + '" /></Relation>';
xmlPayload += '</Asset>';
console.log('xmlPayload: ' + xmlPayload);
var request = this.ajax('updateDefect', v1DefectInteger, xmlPayload);
request.always(this.createLink);
}
else {
this.showSpinner(false);
this.switchTo('error');
}
},
//Create a v1 link asset for the given v1 defect.
createLink: function (data) {
//DEBUG ONLY:
console.log(data);
v1DefectId = this.getDefectId(data.responseText);
var ticketId = this.ticket().id();
var ticketUrl = 'https://' + this.currentAccount().subdomain() + '.zendesk.com/agent/#/tickets/' + this.ticket().id();
if (data.status == '200') {
var xmlPayload = '<Asset>';
xmlPayload += '<Attribute name="OnMenu" act="set">false</Attribute>';
xmlPayload += '<Attribute name="URL" act="set">' + ticketUrl + '</Attribute>';
xmlPayload += '<Attribute name="Name" act="set">Zendesk #' + ticketId + '</Attribute>';
xmlPayload += '<Relation name="Asset" act="set"><Asset idref="' + v1DefectId + '" /></Relation>';
xmlPayload += '</Asset>';
console.log('xmlPayload: ' + xmlPayload);
var request = this.ajax('submitLink', xmlPayload);
request.always(this.processDefect);
}
else {
this.showSpinner(false);
this.switchTo('error');
}
},
//Unassigns the defect from teh Zendesk ticket by removing field values in the Zendesk ticket.
unassignDefect: function () {
this.ticket().customField(v1AssetIDCustomField, "");
this.ticket().customField(v1AssetNumberCustomField, "");
this.ticket().customField(v1AssetStatusCustomField, "");
this.ticket().tags().remove(v1defectTag);
this.switchTo('mainAssign');
},
//TODO: Search for v1 defect based on specified search text.
searchDefect: function () {
alert('Not implemented!');
//this.showSpinner(true);
//var request = this.ajax('searchDefect', this.$("#txtSearch").val());
//request.always(this.showMain);
//this.showSpinner(false);
},
//Returns defect OID with moment value stripped off.
getDefectId: function (defect) {
var defectJSON = JSON.parse(defect);
var v1IdArray = defectJSON.id.split(":");
return v1IdArray[0] + ':' + v1IdArray[1];
},
//Converts UTCDateTime to friendly format.
getFriendlyDateTime: function (dateTimeValue) {
var newDate = new Date(dateTimeValue);
var datePart = (newDate.getMonth() + 1) + '/' + newDate.getDate() + '/' + newDate.getFullYear();
var timePart = newDate.getHours() + ':' + newDate.getMinutes();
return datePart + ' ' + timePart;
},
//Displays the Zendesk spinner.
showSpinner: function (showValue) {
if (showValue === true)
this.$(".spinner").css("visibility", "visible");
else
this.$(".spinner").css("visibility", "hidden");
},
//Logs ticket information to the browser console.
logTicket: function () {
console.log('ID: ' + this.ticket().id());
console.log('Subject: ' + this.ticket().subject());
console.log('Description: ' + this.ticket().description());
console.log('Status: ' + this.ticket().status());
console.log('Priority: ' + this.ticket().priority());
console.log('Type: ' + this.ticket().type());
console.log('Url: https://' + this.currentAccount().subdomain() + '.zendesk.com/agent/#/tickets/' + this.ticket().id());
}
};
}());