-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
107 lines (99 loc) · 3.09 KB
/
index.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
const core = require("@actions/core");
const fs = require("fs");
const yaml = require("js-yaml");
const path = require("path");
const {
componentYamlSchemaV1D0,
endpointYamlSchemaV0D1,
componentConfigYamlSchemaV1beta1,
} = require("./schemas");
const { sourceConfigFileTypes, errCodes } = require("./enums");
function readInput() {
sourceRootDir = core.getInput("source-root-dir-path");
fileType = core.getInput("file-type");
return [sourceRootDir, fileType];
}
function readSrcConfigYaml(filePath, fileType) {
try {
let fullPath = path.join(filePath, ".choreo");
if (
fileType === sourceConfigFileTypes.COMPONENT_YAML ||
fileType === sourceConfigFileTypes.ENDPOINT_YAML ||
fileType === sourceConfigFileTypes.COMPONENT_CONFIG_YAML
) {
fullPath = path.join(fullPath, fileType);
} else {
throw new Error(`'${fileType}' is not a valid source config file type`);
}
let fileContent = fs.readFileSync(fullPath, "utf8");
return fileContent;
} catch (error) {
throw new Error(
`${errCodes.USER_ERROR} Failed to read source config file: ${error.message}`
);
}
}
function parseYaml(fileContent) {
try {
return yaml.load(fileContent);
} catch (error) {
throw new Error(
`${errCodes.USER_ERROR} Failed to parse yaml: ${error.message}`
);
}
}
function constructValidationErrorMessage(err, fileType) {
const errors = err.errors;
if (!errors || errors.length == 0) {
return (
`${errCodes.INTERNAL_ERROR} Failed to validate ${fileType}, something went wrong:` +
err
);
}
const errorMsg = `${errCodes.USER_ERROR} ${fileType} validation failed: `;
const errorList =
errors.length === 1 ? errors[0] : errors.map((e) => `\n- ${e}`).join("");
return errorMsg + errorList;
}
async function validateSourceConfigFile(sourceRootDir, fileType) {
try {
switch (fileType) {
case sourceConfigFileTypes.COMPONENT_YAML:
await componentYamlSchemaV1D0(sourceRootDir).validate(
srcConfigYamlFile,
{ abortEarly: false }
);
break;
case sourceConfigFileTypes.COMPONENT_CONFIG_YAML:
await componentConfigYamlSchemaV1beta1(sourceRootDir).validate(
srcConfigYamlFile,
{ abortEarly: false }
);
break;
case sourceConfigFileTypes.ENDPOINT_YAML:
await endpointYamlSchemaV0D1(sourceRootDir).validate(
srcConfigYamlFile,
{ abortEarly: false }
);
break;
default:
throw new Error(`'${fileType}' is not a valid source config file type`);
}
// Validate the component YAML file
} catch (err) {
throw new Error(constructValidationErrorMessage(err, fileType));
}
}
async function main() {
try {
const [sourceRootDir, fileType] = readInput();
const fileContent = readSrcConfigYaml(sourceRootDir, fileType);
srcConfigYamlFile = parseYaml(fileContent);
await validateSourceConfigFile(sourceRootDir, fileType);
} catch (error) {
console.log(error.message);
core.setFailed("Source config file validation failed");
}
}
// Exec the main function
main();