This repository has been archived by the owner on Apr 3, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
/
util.js
250 lines (223 loc) · 7.24 KB
/
util.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
"use strict";
const request = require("request");
const async = require("async");
const key = require("./atlassian-connect").key;
const url = require("url");
const escape = require("escape-html");
// Returns the full link to the JIRA Ticket that is referenced in the request object
function getJiraTicketLink(req, ticketId) {
var issueId = req.query.issue;
var baseJiraURL = null;
if (req.headers["xdm_e"]) {
baseJiraURL = req.headers["xdm_e"];
} else {
// lets see if we have a comment.self
if (req.body.comment && req.body.comment.self) {
baseJiraURL = req.body.comment.self;
}
}
if (baseJiraURL == null) baseJiraURL = "http://yourjiraserver.com/browse/";
var parsedUrl = url.parse(baseJiraURL);
return `${parsedUrl.protocol}//${parsedUrl.host}/browse/${ticketId}`;
}
function getTenant(req, httpClient, cb) {
httpClient.get({
uri: `/rest/atlassian-connect/1/addons/${key}/properties/tenant`,
json: true,
}, (err, ires, body) => {
if (err) { return cb(err); }
req.tenant = body.value;
cb(null, req.tenant);
});
}
function getPid(req, httpClient, cb) {
if (!req.dynatraceProblem)
req.dynatraceProblem = {};
if (req.query.issue) {
httpClient.get({
uri: `/rest/api/2/issue/${req.query.issue}/properties/dynatraceProblemId`,
json: true,
}, (err, ires, body) => {
if (err) { return cb(err); }
// BACKWARD COMP CHECK. In the first version of Dynatrace -> JIRA Integration we only had the string value as PID. Now we need to wrap it into a JSON Object
if (!body.value) {
// no property set!
} else
if (typeof body.value === 'string') {
req.pid = body.value;
req.dynatraceProblem = { pid: req.pid }
setDynatraceProblem(httpClient, req, req.dynatraceProblem, null);
} else {
req.dynatraceProblem = body.value;
// always provide req.pid with the problem id
req.pid = req.dynatraceProblem.pid;
}
cb(null, req.pid);
});
} else {
cb();
}
}
/**
* This function assumes a valid dynatraceProblem object with a valid .pid value. It queries current problem status from Dynatrace
* @param {*} tenantUrl
* @param {*} tenantToken
* @param {*} dynatraceProblem
* @param {will return the updated dynatraceProblem object} cb
*/
function refreshDynatraceProblem(tenantUrl, tenantToken, dynatraceProblem, cb) {
if (dynatraceProblem && dynatraceProblem.pid) {
getProblemDetails(tenantUrl, tenantToken, dynatraceProblem.pid, (err, dres, body) => {
if (err) {
console.error(err);
cb(err, null);
return;
}
if (!body.result) {
console.error(dres);
cb(new Error("No Result from Dynatrace"), null);
return;
}
if (dres.statusCode !== 200) {
cb(new Error("Error Result from Dynatrace"), null);
return;
}
// pull all key Dynatrace Problem info and put it into JIRA Dynatrace Property Object
const problem = body.result;
if (problem.tagsOfAffectedEntities) {
dynatraceProblem.tags = problem.tagsOfAffectedEntities.map((tag) => tag.key).join(" ");
}
dynatraceProblem.problem = problem.displayName;
dynatraceProblem.impact = problem.impactLevel;
dynatraceProblem.severity = problem.severityLevel;
dynatraceProblem.hasRootCause = problem.rankedEvents[0].isRootCause.toString();
dynatraceProblem.status = problem.status;
cb(null, dynatraceProblem);
});
}
return;
}
function setDynatraceProblem(httpClient, req, dynatraceProblem, cb) {
httpClient.put({
uri: `/rest/api/2/issue/${req.query.issue}/properties/dynatraceProblemId`,
body: dynatraceProblem,
json: true
}, (err, ires, body) => {
// req.pidRequiresUpdate means we are up to date
req.pidRequiresUpdate = false;
console.log("Response from setting dynatraceProblemId property to parsed Problem Id: " + dynatraceProblem.pid);
console.log(body);
if (cb) cb(err, ires, body);
});
}
function getProblemDetails(tenant, token, pid, cb) {
request.get({
uri: `${tenant}/api/v1/problem/details/${pid}`,
headers: {
Authorization: `Api-Token ${token}`,
},
json: true,
}, cb);
}
/**
* Adds a comment to the Dynatrace Problem
* @param {string} tenant Dynatrace Tenant URL
* @param {string} token Dynatrace API Token
* @param {string} pid
* @param {string} comment
* @param {string} user
* @param {string} context
* @param {string} jiraTicketLink
* @param {function} cb
*/
function addProblemComment(tenant, token, pid, comment, user, context, jiraTicketLink, cb) {
// Lets build the comment string to first contain a link back to the JIRA Ticket
comment = `Comment sync on [JIRA - ${context}](${jiraTicketLink})\n----------------------------------------\n${comment}`;
var dtComment = {
comment,
user,
context
}
var postUrl = `${tenant}/api/v1/problem/details/${pid}/comments`
console.log("Dynatrace POST Url: " + postUrl)
request.post({
uri: postUrl,
headers: {
Authorization: `Api-Token ${token}`,
},
body: dtComment,
json: true,
}, cb);
}
function getProblemComments(tenant, token, pid, cb) {
request.get({
uri: `${tenant}/api/v1/problem/details/${pid}/comments`,
headers: {
Authorization: `Api-Token ${token}`,
},
json: true,
}, cb);
}
function getProblemDetailsWithComments(tenant, token, pid, cb) {
async.parallel([
acb => getProblemDetails(tenant, token, pid, acb),
acb => getProblemComments(tenant, token, pid, acb),
], (e, results) => {
if (e) { return cb(e); }
if (!results[0][1] || !results[1][1]) {
return cb();
}
if (results[0][0].statusCode !== 200) {
console.log("Could not find problem");
return cb();
}
const problem = results[0][1].result;
if (results[1][0].statusCode !== 200) {
console.log("Could not find comments");
problem.comments = [];
return cb(null, problem);
}
const comments = results[1][1].comments.map(c => {
c.isComment = true;
c.startTime = c.createdAtTimestamp;
c.content = escape(c.content);
c.content = c.content.replace(/(?:\r\n|\r|\n)/g, '<br />');
return c;
});;
problem.comments = comments;
cb(null, problem);
});
}
const modifiers = {
SYNTHETIC: "#monitors/webcheckdetail;webcheckId",
HOST: "#hostdetails;id",
APPLICATION: "#uemappmetrics;uemapplicationId",
MOBILE_APPLICATION: "#mobileappoverview;appId",
SERVICE: "#services/servicedetails;id",
PROCESS: "#processdetails;id",
PROCESS_GROUP_INSTANCE: "#processdetails;id",
PROCESS_GROUP: "#processgroupdetails;id",
HYPERVISOR: "#hypervisordetails;id",
SYNTHETIC_TEST: "#webcheckdetailV3;webcheckId",
DCRUM_APPLICATION: "#entity;id",
};
function eventLink(tenant, event, pid) {
const entityType = event.entityId.split("-")[0];
const modifier = modifiers[entityType];
if (!modifier) {
console.log(entityType);
return `${tenant}/#problems/problemdetails;pid=${pid}`;
}
return `${tenant}/${modifier}=${event.entityId};gtf=p_${pid};pid=${pid}`;
}
module.exports = {
eventLink,
getTenant,
getPid,
refreshDynatraceProblem,
getProblemDetails,
addProblemComment,
getJiraTicketLink,
getProblemDetailsWithComments,
setDynatraceProblem,
};