Skip to content

Commit a4512d9

Browse files
committed
fix: better config check
1 parent 52a634d commit a4512d9

File tree

4 files changed

+55
-41
lines changed

4 files changed

+55
-41
lines changed

lib/index.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package-lock.json

Lines changed: 7 additions & 8 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
"dependencies": {
2929
"@actions/core": "^1.2.6",
3030
"axios": "^0.20.0",
31-
"github-label-sync": "^2.0.0",
31+
"github-label-sync": "github:EndBug/github-label-sync#typings-usable",
3232
"pretty-error": "^2.1.1",
3333
"yamljs": "^0.3.0"
3434
},

src/index.ts

Lines changed: 46 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,11 @@ const log = {
1717
core[showInReport ? 'error' : 'info']('✗ ' + str),
1818
fatal: (str: string) => core.setFailed('✗ ' + str)
1919
}
20-
let usingLocalFile: boolean
20+
let usingLocalFile!: boolean
2121
;(async () => {
2222
try {
2323
checkInputs()
2424

25-
// @ts-expect-error
2625
const labels = usingLocalFile
2726
? readConfigFile(getInput('config-file'))
2827
: await fetchRepoLabels(
@@ -50,24 +49,39 @@ let usingLocalFile: boolean
5049
}
5150
})()
5251

53-
function isProperConfig(value: any): value is LabelInfo[] {
54-
return (
55-
value instanceof Array &&
56-
value.every(
57-
(element) =>
58-
typeof element == 'object' &&
59-
element.name &&
60-
typeof element.name == 'string' &&
61-
element.description &&
62-
typeof element.description == 'string' &&
63-
(!element.aliases ||
64-
(element.aliases instanceof Array &&
65-
element.aliases.every(
66-
(alias) => alias && typeof alias == 'string'
67-
))) &&
68-
(!element.description || typeof element.description == 'string')
52+
function throwConfigError(value: LabelInfo[]) {
53+
if (!(value instanceof Array)) throw 'Parsed value should be an array'
54+
55+
value.forEach((element, index) => {
56+
if (typeof element != 'object')
57+
throw `Every entry should be an object (index: ${index})`
58+
59+
if (typeof element.name != 'string')
60+
throw `.name should be a string (received: ${typeof element.name}, index: ${index})`
61+
if (!element.name)
62+
throw `.name should not be an empty string (index: ${index})`
63+
64+
if (typeof element.color != 'string')
65+
throw `.color should be a string (received: ${typeof element.color}, index: ${index})`
66+
if (!element.color)
67+
throw `.color should not be an empty string (index: ${index})`
68+
69+
if (!['string', 'undefined'].includes(typeof element.description))
70+
throw `.description should be either a string or undefined (received: ${typeof element.description}, index: ${index})`
71+
72+
if (
73+
typeof element.aliases != 'undefined' &&
74+
!(element.aliases instanceof Array)
6975
)
70-
)
76+
throw `.aliases should be either an array or undefined (received: ${typeof element.aliases}, index: ${index})`
77+
78+
element.aliases?.forEach((alias, aliasIndex) => {
79+
if (typeof alias != 'string')
80+
throw `Every alias should be a string (received: ${typeof alias}, element index: ${index}, alias index: ${aliasIndex})`
81+
if (!alias)
82+
throw `Aliases shouldn't be empty strings (element index: ${index}, alias index: ${aliasIndex})`
83+
})
84+
})
7185
}
7286

7387
function readConfigFile(filePath: string) {
@@ -90,12 +104,12 @@ function readConfigFile(filePath: string) {
90104
// Parse YAML file
91105
log.info('Parsing YAML file...')
92106
parsed = yaml.parse(file)
93-
if (!isProperConfig(parsed))
94-
throw `Parsed YAML file is invalid. Parsed: ${JSON.stringify(
95-
parsed,
96-
null,
97-
2
98-
)}`
107+
try {
108+
throwConfigError(parsed)
109+
} catch (e) {
110+
log.error(JSON.stringify(parsed, null, 2), false)
111+
throw 'Parsed YAML file is invalid:\n' + e
112+
}
99113
} else if (fileExtension == '.json') {
100114
// Try to parse JSON file
101115
log.info('Parsing JSON file...')
@@ -104,12 +118,13 @@ function readConfigFile(filePath: string) {
104118
} catch {
105119
throw "Couldn't parse JSON config file, check for syntax errors."
106120
}
107-
if (!isProperConfig(parsed))
108-
throw `Parsed JSON file is invalid. Parsed: ${JSON.stringify(
109-
parsed,
110-
null,
111-
2
112-
)}`
121+
122+
try {
123+
throwConfigError(parsed)
124+
} catch (e) {
125+
log.error(JSON.stringify(parsed, null, 2), false)
126+
throw 'Parsed JSON file is invalid:\n' + e
127+
}
113128
} else {
114129
throw `Invalid file extension: ${fileExtension}`
115130
}

0 commit comments

Comments
 (0)