forked from bitfinexcom/bfx-report-electron
-
Notifications
You must be signed in to change notification settings - Fork 0
/
render-markdown-template.js
169 lines (140 loc) · 3.61 KB
/
render-markdown-template.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
'use strict'
const fs = require('fs')
const path = require('path')
const truncateLog = require('./truncate-log')
const getNewGithubIssueUrl = require('./get-new-github-issue-url')
const templateByDefault = fs.readFileSync(
path.join(__dirname, 'github-issue-template.md'),
'utf8'
)
const placeholderPattern = /\$\{[a-zA-Z0-9]+\}/g
// The GitHub GET endpoint for opening a new issue
// has a restriction for maximum length of a URL: 8192 bytes
// https://github.com/cli/cli/pull/3271
// https://github.com/cli/cli/issues/1575
// https://github.com/cli/cli/blob/trunk/pkg/cmd/issue/create/create.go#L167
// https://github.com/cli/cli/blob/trunk/utils/utils.go#L84
//
// But there is a restriction is max 2081 characters on windows
// https://github.com/electron/electron/blob/main/docs/api/shell.md#shellopenexternalurl-options
// https://github.com/electron/electron/issues/12416#issuecomment-376596073
const maxIssueBytes = process.platform === 'win32'
? 2080
: 8150
const _getURLByteLength = (body, params) => {
const urlStr = getNewGithubIssueUrl({
...params,
body
})
return Buffer.byteLength(urlStr, 'utf8')
}
const _renderMarkdownTemplate = (
params = {},
template
) => {
const str = template.replace(placeholderPattern, (match) => {
const propName = match.replace('${', '').replace('}', '')
if (
!params ||
typeof params !== 'object' ||
(
!Number.isFinite(params[propName]) &&
typeof params[propName] !== 'string'
)
) {
return ''
}
return params[propName]
})
return str
}
const _getTruncatedMarkdownTemplate = (
allowedByteLengthForLogs,
orderedLogsArr,
params,
template
) => {
const logsLength = orderedLogsArr.length
const _logs = {}
let allowedByteLengthForOneLog = Math.floor(
allowedByteLengthForLogs / logsLength
)
let count = 0
for (const [propName, log] of orderedLogsArr) {
count += 1
const logByteLength = _getURLByteLength(log, params)
if (allowedByteLengthForOneLog >= logByteLength) {
_logs[propName] = log
if (allowedByteLengthForOneLog > logByteLength) {
allowedByteLengthForOneLog += Math.floor(
(allowedByteLengthForOneLog - logByteLength) / (logsLength - count)
)
}
continue
}
const truncatedLog = truncateLog(log, allowedByteLengthForOneLog)
_logs[propName] = truncatedLog
}
const md = _renderMarkdownTemplate(
{
...params,
..._logs
},
template
)
return md
}
module.exports = (
params = {},
logs = {},
template = templateByDefault
) => {
if (
!params ||
typeof params !== 'object'
) {
return template
}
const logsArr = Object.entries(logs)
const emptyLogsArr = logsArr.reduce(
(accum, [propName]) => {
accum[propName] = ''
return accum
},
{}
)
const mdWithoutLogs = _renderMarkdownTemplate(
{
...params,
...emptyLogsArr
},
template
)
const mdIssueByteLength = _getURLByteLength(
mdWithoutLogs,
params
)
if (
mdIssueByteLength >= maxIssueBytes ||
logsArr.length === 0
) {
return mdWithoutLogs
}
const orderedLogsArr = logsArr.sort((fEl, sEl) => (
Buffer.byteLength(fEl[1], 'utf8') - Buffer.byteLength(sEl[1], 'utf8')
))
let allowedByteLengthForLogs = maxIssueBytes - mdIssueByteLength
while (true) {
const md = _getTruncatedMarkdownTemplate(
allowedByteLengthForLogs,
orderedLogsArr,
params,
template
)
const byteLength = _getURLByteLength(md, params)
if (byteLength <= maxIssueBytes) {
return md
}
allowedByteLengthForLogs -= 100
}
}