forked from mikepenz/action-junit-report
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathutils.ts
127 lines (114 loc) · 3.75 KB
/
utils.ts
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
import * as core from '@actions/core'
import {Transformer} from './testParser.js'
// eslint-disable-next-line import/extensions
import {SummaryTableRow} from '@actions/core/lib/summary.js'
export function retrieve(name: string, items: string[], index: number, total: number): string {
if (total > 1) {
if (items.length !== 0 && items.length !== total) {
core.warning(`${name} has a different number of items than the 'reportPaths' input. This is usually a bug.`)
}
if (items.length === 0) {
return ''
} else if (items.length === 1) {
return items[0].replace('\n', '')
} else if (items.length > index) {
return items[index].replace('\n', '')
} else {
core.error(`${name} has no valid config for position ${index}.`)
return ''
}
} else if (items.length === 1) {
return items[0].replace('\n', '')
} else {
return ''
}
}
/**
* Reads in the configuration from the JSON file
*/
export function readTransformers(raw: string | undefined): Transformer[] {
if (!raw) {
return []
}
try {
const transformers: Transformer[] = JSON.parse(raw)
for (const transformer of transformers) {
try {
transformer.regex = new RegExp(transformer.searchValue.replace('\\\\', '\\'), 'gu')
} catch (error: unknown) {
core.warning(`⚠️ Bad replacer regex: ${transformer.searchValue} (${error})`)
}
}
return transformers
} catch (error: unknown) {
core.info(`⚠️ Transformers provided, but they couldn't be parsed. Fallback to Defaults. (${error})`)
core.debug(` Provided input: ${raw}`)
return []
}
}
export function applyTransformer(transformer: Transformer, string: string): string {
const regExp = transformer.regex
if (regExp) {
return string.replace(regExp, transformer.replaceValue)
} else {
return string.replace(transformer.searchValue, transformer.replaceValue)
}
}
/**
* Function extracted from: https://github.com/actions/toolkit/blob/main/packages/core/src/summary.ts#L229
*/
export function buildTable(rows: SummaryTableRow[]): string {
const tableBody = rows
.map(row => {
const cells = row
// eslint-disable-next-line @typescript-eslint/no-explicit-any
.map((cell: any) => {
if (typeof cell === 'string') {
return wrap('td', cell)
}
const {header, data, colspan, rowspan} = cell
const tag = header ? 'th' : 'td'
const attrs = {
...(colspan && {colspan}),
...(rowspan && {rowspan})
}
return wrap(tag, data, attrs)
})
.join('')
return wrap('tr', cells)
})
.join('')
return wrap('table', tableBody)
}
/**
* Wraps content in an HTML tag, adding any HTML attributes
*
* @param {string} tag HTML tag to wrap
* @param {string | null} content content within the tag
* @param {[attribute: string]: string} attrs key-value list of HTML attributes to add
*
* @returns {string} content wrapped in HTML element
*/
function wrap(tag: string, content: string | null, attrs: {[attribute: string]: string} = {}): string {
const htmlAttrs = Object.entries(attrs)
.map(([key, value]) => ` ${key}="${value}"`)
.join('')
if (!content) {
return `<${tag}${htmlAttrs}>`
}
return `<${tag}${htmlAttrs}>${content}</${tag}>`
}
/**
* Removes a specified prefix from the beginning of a string.
*
* @param {string} str - The original string.
* @param {string} prefix - The prefix to be removed.
* @returns {string} - The string without the prefix if it was present, otherwise the original string.
*/
export function removePrefix(str: string, prefix: string): string {
if (prefix.length === 0) return str
if (str.startsWith(prefix)) {
return str.slice(prefix.length)
}
return str
}