Skip to content

Commit e78326b

Browse files
authored
fix: multi paragraphs parsing (#15)
1 parent c4bc96d commit e78326b

6 files changed

Lines changed: 89 additions & 5 deletions

File tree

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"textarea_one": "1st paragraph\n\n2nd paragraph",
3+
"textarea_two": "1st paragraph\n2nd paragraph"
4+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
body:
2+
- type: textarea
3+
id: textarea-one
4+
attributes:
5+
label: My textarea input
6+
- type: textarea
7+
id: textarea-two
8+
attributes:
9+
label: Another textarea input
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
### My textarea input
2+
3+
1st paragraph
4+
5+
2nd paragraph
6+
7+
### Another textarea input
8+
9+
1st paragraph
10+
2nd paragraph
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
const { resolve } = require("path");
2+
const { readFileSync } = require("fs");
3+
4+
const issueBodyPath = resolve(__dirname, "issue-body.md");
5+
6+
module.exports = {
7+
issue: {
8+
body: readFileSync(issueBodyPath, "utf-8"),
9+
},
10+
};

index.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,12 +53,13 @@ async function run(env, eventPayload, fs, core) {
5353
.replace(/[^\w\s]/gi, "")
5454
.replace(/\s/g, "_");
5555
}
56-
function toValue(val) {
57-
if (typeof val !== "string") {
58-
return val;
56+
function toValue(input, ...extraLines) {
57+
if (typeof input !== "string") {
58+
return input;
5959
}
6060

61-
const value = val.trim();
61+
const value = [input, ...extraLines].join("\n\n").trim();
62+
6263
if (value.toLowerCase() === "_no response_") {
6364
return "";
6465
}

test.spec.js

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ const { readFileSync } = require("fs");
22

33
const { run } = require(".");
44

5-
it("Smoke test", () => {
5+
it("smoke test", () => {
66
expect(run).toBeDefined();
77
expect(typeof run).toBe("function");
88
});
@@ -56,3 +56,53 @@ it("readme example", () => {
5656

5757
run(env, eventPayload, fs, core);
5858
});
59+
60+
it("multiple paragraphs", () => {
61+
const expectedOutput = require("./fixtures/multiple-paragraphs/expected.json");
62+
const expectedOutputJson = JSON.stringify(expectedOutput, null, 2);
63+
64+
// mock ENV
65+
const env = {
66+
HOME: "<home path>",
67+
};
68+
69+
// mock event payload
70+
const eventPayload = require("./fixtures/multiple-paragraphs/issue");
71+
72+
// mock fs
73+
const fs = {
74+
readFileSync(path, encoding) {
75+
expect(path).toBe("<template-path>");
76+
expect(encoding).toBe("utf8");
77+
return readFileSync("fixtures/multiple-paragraphs/form.yml", "utf-8");
78+
},
79+
writeFileSync(path, content) {
80+
expect(path).toBe("<home path>/issue-parser-result.json");
81+
expect(content).toBe(expectedOutputJson);
82+
},
83+
};
84+
85+
// mock core
86+
const core = {
87+
getInput(inputName) {
88+
expect(inputName).toBe("template-path");
89+
return "<template-path>";
90+
},
91+
setOutput(outputName, outputValue) {
92+
if (outputName === "jsonString") {
93+
expect(outputValue).toBe(expectedOutputJson);
94+
return;
95+
}
96+
97+
if (outputName.startsWith("issueparser_")) {
98+
const key = outputName.substr("issueparser_".length);
99+
expect(Object.keys(expectedOutput)).toContain(key);
100+
101+
expect(outputValue).toBe(expectedOutput[key]);
102+
return;
103+
}
104+
},
105+
};
106+
107+
run(env, eventPayload, fs, core);
108+
});

0 commit comments

Comments
 (0)