forked from seanmcilroy29/github-issue-parser
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
181 lines (152 loc) · 4.29 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
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
// @ts-check
const fs = require("fs");
const yaml = require("js-yaml");
const core = require("@actions/core");
/**
* @param {NodeJS.ProcessEnv} env
* @param {string} body
* @param {fs} fs
* @param {core} core
*/
async function run(env, body, fs, core) {
body = body ?? ""
let form = {};
try {
const templatePath = core.getInput("template-path");
if (templatePath) {
form = yaml.load(fs.readFileSync(templatePath, "utf8"));
}
} catch (err) {
core.error(err);
}
function getIDsFromIssueTemplate(formTemplate) {
if (!formTemplate.body) {
return {};
}
return Object.fromEntries(
formTemplate.body
.map((item) => {
if (item.type === "markdown") {
return null;
}
return [item.attributes.label, item.id, item.type];
})
.filter(Boolean)
);
}
function getIDTypesFromIssueTemplate(formTemplate) {
if (!formTemplate.body) {
return {};
}
return Object.fromEntries(
formTemplate.body
.map((item) => {
if (item.type === "markdown") {
return null;
}
return [toKey(item.attributes.label), item.type];
})
.filter(Boolean)
);
}
let result;
const idMapping = getIDsFromIssueTemplate(form);
const idTypes = getIDTypesFromIssueTemplate(form);
function toKey(str) {
if (idMapping[str]) {
return idMapping[str];
}
return str
.toLowerCase()
.trim()
.replace(/[^\w\s]/gi, "")
.replace(/\s/g, "_");
}
function toValue(input, ...extraLines) {
if (typeof input !== "string") {
return input;
}
const value = [input, ...extraLines].join("\n\n").trim();
if (value.toLowerCase() === "_no response_") {
return "";
}
return value;
}
//toObject merges checkbox results in an array and spits out correctly formatted object
function toObject(array) {
let result = {};
array.forEach((item) => {
const key = item && item[0];
const value = item && item[1];
if (key in result) {
const content = result[key];
if (value !== undefined) {
result[key] = content.concat(value);
}
return;
}
if (idTypes[key] == "checkboxes") {
result[key] = value === undefined ? [] : [value];
} else {
result[key] = value;
}
});
return result
}
result = body
.trim()
.split("###")
.filter(Boolean)
.map((line) => {
return line
.split(/\r?\n\r?\n/)
.filter(Boolean)
.map((item, index, arr) => {
const line = item.trim();
if (line.startsWith("- [")) {
return line.split(/\r?\n/).map((check) => {
const field = check.replace(/- \[[X\s]\]\s+/i, "");
const previousIndex = index === 0 ? index : index - 1;
const key = arr[previousIndex].trim();
if (check.toUpperCase().startsWith("- [X] ")) {
return [key, field];
}
return [key];
});
}
return line;
});
})
.reduce((prev, curr) => {
if (Array.isArray(curr[1])) {
return [...prev, ...curr[1]];
}
return [...prev, curr];
}, []);
result = result.map(([key, ...lines]) => {
const checkListValue = lines.find((line) => Array.isArray(line));
const value = checkListValue ? toValue(checkListValue) : toValue(...lines);
return [toKey(key), value];
});
result = toObject(result);
Object.entries(result).forEach(([key, value]) => {
core.setOutput(`issueparser_${key}`, Array.isArray(value) ? value.join(',') : value);
})
function jsonStringify(json) {
return JSON.stringify(json, null, 2);
}
fs.writeFileSync(
`${env.USERPROFILE || env.HOME}/issue-parser-result.json`,
jsonStringify(result),
"utf-8"
);
core.setOutput("jsonString", jsonStringify(result));
}
// We wrap the code in a `run` function to enable testing.
// On GitHub Actions the `run` function is executed immediately.
// `NODE_ENV` is set when running tests on GitHub Actions as part of CI.
if (process.env.GITHUB_ACTIONS && process.env.NODE_ENV !== "test") {
const body = core.getInput("issue-body");
run(process.env, body, fs, core);
}
module.exports.run = run;