/
MermaidNodes.js
283 lines (257 loc) · 8.98 KB
/
MermaidNodes.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
"use strict";
const debug = require('debug')('MermaidNodes')
const mermaidConfig = require('./config/mermaid-config')
const config = require('config')
const JiraStatus = require('./JiraStatus')
let idSource = "unset"
let nameSource = "unset"
let idTarget = "unset"
let nameTarget = "unset"
/**
*cleanName: Removes all parenthesis marks from input string.
* Mermaid labels cannot contain parens
*
* @param {*} inName
* @returns modified string (no parens)
*/
function cleanName(inName) {
let cleanName = inName
cleanName = cleanName.replace(/\(/g, "").replace(/\)/g, "")
return (cleanName.replace(/\"/g, ""))
}
/**
*removeSpaces: Deletes all spaces from the specified string.
* Mermaid Class names cannot contain strings
*
* @param {*} text
* @returns modified string (no spaces)
*/
function removeSpaces(text) {
return (text.replace(/\s/g, ""))
}
const STATUSES = ['ICEBOX', 'DEFINED', 'IN PROGRESS', 'IN REVIEW', 'DONE', 'EMERGENCY', 'BLOCKED', 'DEAD']
class MermaidNodes {
/**
*Creates an instance of MermaidNodes.
* @memberof MermaidNodes
*/
constructor() {
debug("mermaidNodes created")
this.nodes = []
this.statuses = {}
STATUSES.forEach((status) => {
this.statuses[status] = []
})
}
/**
*addLink: Update the list of nodes with the specified source and target details
*
* @param {*} idSource
* @param {*} nameSource
* @param {*} idTarget
* @param {*} nameTarget
* @param {*} statusTarget: Used to track the IDs by Status
* @memberof MermaidNodes
*/
addLink(idSource, nameSource, idTarget, nameTarget, statusTarget) {
debug("node created")
this.nodes.push({ idSource: idSource, nameSource: nameSource, idTarget: idTarget, nameTarget: cleanName(nameTarget) })
this.appendStatus(idTarget, statusTarget)
}
/**
*appendStatus: Update the status array with the specified ID and Status
*
* @param {*} id
* @param {*} status
* @memberof MermaidNodes
*/
appendStatus(id, status) {
if (!this.statuses[status]) {
this.statuses[status] = []
}
this.statuses[status].push(id)
}
/**
*setRootName: Corrects or updates name of the root Defaults to 0
*
* @param {*} name
* @memberof MermaidNodes
*/
setRootIdAndName(id, name) {
debug(`setRootIdAndName(${id}, ${name}) called...`)
name = cleanName(name)
this.nodes.forEach((node) => {
if (node.idSource == 0) {
node.isSource = id
node.nameSource = name
} else if (node.idTarget == 0) {
node.isTarget = id
node.nameTarget = name
}
})
}
/**
*getContent: Create the Mermaid file content from the stored data
*
* @returns Mermaid-formatted text
* @memberof MermaidNodes
*/
getContent() {
let resp = []
resp.push("graph LR;")
resp.push("") // For readability
this.nodes.forEach((node) => {
resp.push(` ${node.idSource}(${node.nameSource})-->${node.idTarget}(${node.nameTarget});`)
})
resp.push("") // Need a blank line before the Class definitions
for (var f in mermaidConfig.formats) {
if (mermaidConfig.formats.hasOwnProperty(f)) {
resp.push(`classDef ${removeSpaces(f)} ${mermaidConfig.formats[f]}`)
}
}
resp.push("") // Need a blank line before the id/status list
for (var s in this.statuses) {
if (this.statuses[s].length > 0) {
resp.push(`class ${this.statuses[s].join(',')} ${removeSpaces(s)}`)
}
}
// Merge all the results and return it
return (resp.join("\n"))
}
static buildMermaidLinkIn(
inKey,
inSummary,
inStatus,
linkOutward,
issueLabel,
issueType,
fontawesomeIcon,
cssClass
) {
return `${inKey}("${fontawesomeIcon}${inKey} ${inSummary}"):::${cssClass} -->|${linkOutward}| ${issueLabel}`
}
static buildMermaidLinkOut(
outKey,
outSummary,
outStatus,
linkOutward,
issueLabel,
issueType,
fontawesomeIcon,
cssClass
) {
return `${issueLabel} -->|${linkOutward}| ${outKey}("${fontawesomeIcon}${outKey} ${outSummary}"):::${cssClass}`
}
static buildMermaidLinkChartDataBlock(
issueResult,
linkStyles,
clicks,
issueLabel
) {
debug(`build...DataBlock(...) called for ${issueLabel}`)
let htmlOutput = []
if (issueResult.issuelinks) {
for (let x = 0; x < issueResult.issuelinks.length; x++) {
const link = issueResult.issuelinks[x]
if (link.inwardIssue) {
linkStyles.push(mermaidConfig.links[link.type.inward])
clicks.push(link.inwardIssue.key)
debug(`inwardIssue: ${link.inwardIssue.key}`)
htmlOutput.push(
`${MermaidNodes.buildMermaidLinkIn(
link.inwardIssue.key,
link.inwardIssue.fields.summary,
link.inwardIssue.fields.status.name,
link.type.outward,
issueLabel,
link.inwardIssue.fields.issuetype.name,
JiraStatus.getFontawesomeIcon(link.inwardIssue.fields.issuetype.name),
JiraStatus.formatCssClassName(link.inwardIssue.fields.status.name)
)}\n`
)
} else if (link.outwardIssue) {
const outKey = link.outwardIssue.key
let outLink = link.type.outward
debug(`link.outwardIssue.key: ${outKey} / ${outLink}`)
clicks.push(outKey)
linkStyles.push(mermaidConfig.links[link.type.outward])
htmlOutput.push(
`${MermaidNodes.buildMermaidLinkOut(
outKey,
link.outwardIssue.fields.summary,
link.outwardIssue.fields.status.name,
outLink,
issueLabel,
link.outwardIssue.fields.issuetype.name,
JiraStatus.getFontawesomeIcon(link.outwardIssue.fields.issuetype.name),
JiraStatus.formatCssClassName(link.outwardIssue.fields.status.name)
)}\n`
)
} else {
htmlOutput.push(`unrecognized issue type`)
}
}
debug(clicks.join(`\n`))
return { html: htmlOutput.join(''), linkStyles: linkStyles, clicks: clicks }
} else {
debug(`ERR 734`)
debug(issueResult)
return false
}
}
static buildMermaidLinkChart(issueResult, urlScript = false) {
const htmlOutput = []
const links = issueResult.issuelinks
const id = issueResult.id
const title = `Links for ${id}`
debug(`writing to HTML/mermaid`)
htmlOutput.push(`<div class="mermaid">
graph LR
`)
let linkStyles = []
let clicks = []
clicks.push(issueResult.id)
const issueLabel = `${issueResult.id}["${JiraStatus.getFontawesomeIcon(
issueResult.type
)} ${issueResult.id} ${issueResult.name}"]:::${JiraStatus.formatCssClassName(
issueResult.status
)}`
const results = MermaidNodes.buildMermaidLinkChartDataBlock(
issueResult,
linkStyles,
clicks,
issueLabel,
urlScript
)
htmlOutput.push(results.html)
clicks.concat(results.clicks)
linkStyles.concat(results.linkStyles)
clicks.forEach((c) => {
if (urlScript) {
htmlOutput.push(`click ${c} "${urlScript}${c}"\n`)
} else {
htmlOutput.push(
`click ${c} "${config.get('jira.protocol')}://${
config.get('jira.host')
}/browse/${c}" _blank\n`
)
}
})
linkStyles.forEach((ls, ndx) => {
htmlOutput.push(`linkStyle ${ndx} ${ls}\n`)
})
htmlOutput.push(`
classDef Icebox fill:#ccc,color:#000;
classDef InProgress fill:#84a98c,color:#000;
classDef InReview fill:#b4d9bc,color:#000;
classDef Defined fill:#fff,color:#000;
classDef Done fill:#00f,color:#fff;
classDef Dead fill:#000,color:#fff;
classDef Emergency fill:#95190c,color:#fff;
classDef Blocked fill:#e3b505,color:#fff;
`)
htmlOutput.push(`</div><!-- end mermaid div -->`)
return htmlOutput.join('')
}
}
module.exports = MermaidNodes;