-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
136 lines (118 loc) · 3.31 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
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
const fs = require("fs");
const { globSync } = require("glob");
const yaml = require("js-yaml");
const core = require("@actions/core");
const path = core.getInput("path_to_files") || process.env.PATH_TO_FILES;
const validUsers = (core.getInput("api_users") || process.env.API_USERS || '')
.split(",")
.filter((u) => !!u);
const files = globSync([`${path}/*.{yml,yaml}`]);
files
.map((file) => {
try {
const content = yaml.load(fs.readFileSync(file, "utf8"));
return [file, content];
} catch (error) {
fail(`Error parsing file ${file} : ${error}`);
}
})
.forEach(([file, content]) => {
switch (content.type) {
case "topic":
parseTopic(file, content);
break;
case "post":
parsePost(file, content);
break;
case "posts":
parsePosts(file, content);
break;
default:
fail(
`File ${file} must be of type "topic" or "post", found ${content.type}`
);
}
});
function parseTopic(fileName, content) {
const { title, body, api, trigger } = content;
if (!title || !body) {
fail(`File ${fileName} with type topic must have a title and body`);
}
parseAPIUser(fileName, api);
parseTrigger(fileName, trigger);
}
function parsePost(fileName, content) {
const { topic, body, trigger, api } = content;
if (!topic || !body) {
fail(`File ${fileName} with type post must have a topic and body`);
}
if (!globSync(`${path}/${topic}.{yml,yaml}`)) {
fail(
`File ${fileName} has an invalid topic, ${topic}.yaml does not exist `
);
}
parseAPIUser(fileName, api);
parseTrigger(fileName, trigger);
}
function parsePosts(fileName, content) {
const { topic, posts, trigger, api } = content;
if (!topic) {
fail(`File ${fileName} with type posts must have a topic`);
}
posts.forEach(({ body, api: subApi }) => {
if (!body) {
fail(`File ${fileName} with type posts have a post without a body`);
}
parseAPIUser(fileName, subApi);
})
if (!globSync(`${path}/${topic}.{yml,yaml}`)) {
fail(
`File ${fileName} has an invalid topic, ${topic}.yaml does not exist `
);
}
if (api) parseAPIUser(fileName, api);
parseTrigger(fileName, trigger);
}
function parseAPIUser(fileName, api) {
if (!api || !api.user) {
fail(`File ${fileName} is missing the "api.user" entry`);
}
if (validUsers.length > 0 && !validUsers.includes(api.user)) {
fail(
`File ${fileName} has an invalid API user ${api.user}, must be one of ${validUsers}`
);
}
}
function parseTrigger(fileName, trigger) {
if (!trigger) {
return;
}
switch (trigger.type) {
case "flag":
if (!trigger.tag)
fail(
`File ${fileName} is using a flag trigger, but is missing "tag" field`
);
break;
case "timer":
if (!trigger.after)
fail(
`File ${fileName} is using a timer trigger, but is missing "after" field`
);
break;
case "score":
if (!trigger.value)
fail(
`File ${fileName} is using a score trigger, but is missing "value" field`
);
break;
default:
fail(
`File ${fileName} has an invalid trigger type ${trigger.type}, must be one of flag, timer, score`
);
}
}
function fail(message) {
core.setFailed(message);
process.exit(1);
}