This repository has been archived by the owner on May 6, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
189 lines (161 loc) · 5.88 KB
/
index.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
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
import { readFile } from 'node:fs/promises'
import * as core from '@actions/core'
import { glob } from 'glob'
import { createValidator } from '@deconz-community/ddf-validator'
import { ZodError } from 'zod'
import { visit } from 'jsonc-parser'
import { version } from './package.json'
function handleError(error: ZodError | Error | unknown, file: string, data: string) {
if (error instanceof ZodError) {
// Build error list by path
const errors: Record<string, string[]> = {}
error.issues.forEach((issue) => {
const path = issue.path.join('/')
if (Array.isArray(errors[path]))
errors[path].push(issue.message)
else
errors[path] = [issue.message]
})
const paths = Object.keys(errors)
visit(data, {
onLiteralValue: (value: any, offset: number, length: number, startLine: number, startCharacter: number, pathSupplier) => {
const path = pathSupplier().join('/')
const index = paths.indexOf(path)
if (index > -1) {
core.error(`${errors[path].length} validation error${errors[path].length > 1 ? 's' : ''} in file ${file} at ${path}`)
errors[path].forEach((message) => {
core.error(message, {
file,
startLine,
startColumn: startCharacter,
})
})
paths.splice(index, 1)
}
},
})
if (paths.length > 0) {
paths.forEach((path) => {
core.error(`${errors[path].length} validation error${errors[path].length > 1 ? 's' : ''} in file ${file} at ${path}`)
errors[path].forEach((message) => {
core.error(message, {
file,
})
})
})
}
}
else if (error instanceof Error) {
core.error(error.message, {
file,
})
}
else {
core.error('Unknown Error')
}
}
async function run(): Promise<void> {
try {
const validator = createValidator()
core.info(`Validatig DDF using GitHub action v${version} and validator v${validator.version}.`)
// Load generic files
let genericErrorCount = 0
const genericDirectory = core.getInput('generic')
? `${core.getInput('generic')}/${core.getInput('search')}`
: `${core.getInput('directory')}/generic/${core.getInput('search')}`
const skip = !core.getBooleanInput('no-skip')
core.info(`Loading generic files from ${genericDirectory}`)
core.info(`Skipping files option = ${skip}`)
const genericFilePaths = await glob(genericDirectory)
if (genericFilePaths.length === 0)
throw new Error('No generic files found. Please check the settings.')
core.debug(`Found ${genericFilePaths.length} files.`)
// Load and sort files by schema
const genericfiles: Record<string, { path: string; raw: string; data: unknown }[]> = {
'constants1.schema.json': [],
'constants2.schema.json': [],
'resourceitem1.schema.json': [],
'subdevice1.schema.json': [],
}
for (const filePath of genericFilePaths) {
core.debug(`Loading ${filePath}.`)
try {
const data = await readFile(filePath, 'utf-8')
const decoded = JSON.parse(data)
if (skip && 'ddfvalidate' in decoded && decoded.ddfvalidate === false) {
core.info(`Skipping file ${filePath} because it has the ddfvalidate option set to false`)
continue
}
if (typeof decoded.schema === 'string' || genericfiles[decoded.schema] === undefined) {
genericfiles[decoded.schema].push({
path: filePath,
raw: data,
data: decoded,
})
}
else { core.error(`${filePath}:Unknown schema ${decoded.schema}`) }
}
catch (error) {
genericErrorCount++
if (error instanceof Error)
core.error(`${filePath}: ${error.message}`)
else
core.error(`${filePath}: Unknown Error`)
}
}
// Validating files
for (const [domain, files] of Object.entries(genericfiles)) {
core.info(`Loading ${genericfiles[domain].length} files with schema "${domain}".`)
for (const file of files) {
core.debug(`Validating ${file.path}...`)
try {
validator.loadGeneric(file.data)
core.debug(`Validating ${file.path}. OK`)
}
catch (error) {
genericErrorCount++
handleError(error, file.path, file.raw)
}
}
}
core.info(`Loaded ${genericFilePaths.length - genericErrorCount} files.`)
if (genericErrorCount > 0)
core.warning(`${genericErrorCount} files was not loaded because of errors.`)
// Validate DDF files
let ddfErrorCount = 0
const ddfDirectory = `${core.getInput('directory')}/${core.getInput('search')}`
const ignoreLog = core.getInput('ignore') ? ` (ignore: ${core.getInput('ignore')})` : ''
core.info(`Validating DDF files from ${ddfDirectory}${ignoreLog}`)
const inputFiles = await glob(ddfDirectory, {
ignore: core.getInput('ignore'),
})
if (inputFiles.length === 0)
throw new Error('No files found. Please check the settings.')
core.info(`Found ${inputFiles.length} files to valiate.`)
for (const filePath of inputFiles) {
let data = ''
try {
data = await readFile(filePath, 'utf-8')
const decoded = JSON.parse(data)
if (skip && 'ddfvalidate' in decoded && decoded.ddfvalidate === false) {
core.info(`Skipping file ${filePath} because it has the ddfvalidate option set to false`)
continue
}
validator.validate(decoded)
}
catch (error) {
ddfErrorCount++
handleError(error, filePath, data)
}
}
if ((genericErrorCount + ddfErrorCount) > 0)
core.setFailed(`Found ${genericErrorCount + ddfErrorCount} invalid files. Check logs for details.`)
else
core.info('All files passed.')
}
catch (error) {
if (error instanceof Error)
core.setFailed(error.message)
}
}
run()