Skip to content

Commit f5d8dec

Browse files
committed
fix: don't transform textarea with checkboxes into array
Allow the use of checkboxes in a textarea and transform the section into an array only when the section starts with a checkbox. This doesn't cover the case when the textarea starts with a checkbox followed by text.
1 parent cb6e971 commit f5d8dec

6 files changed

Lines changed: 69 additions & 2 deletions

File tree

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"description": "Some text:\n\n- [ ] Red\n- [ ] Green\n- [ ] Blue\n\nMore text"
3+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
body:
2+
- type: textarea
3+
id: description
4+
attributes:
5+
label: Description
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
### Description
2+
3+
Some text:
4+
5+
- [ ] Red
6+
- [ ] Green
7+
- [ ] Blue
8+
9+
More text
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import { readFileSync } from "node:fs";
2+
import { resolve } from "node:path";
3+
import { fileURLToPath } from "node:url";
4+
5+
const __dirname = fileURLToPath(new URL(".", import.meta.url));
6+
const issueBodyPath = resolve(__dirname, "issue-body.md");
7+
8+
export default readFileSync(issueBodyPath, "utf-8");

index.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -148,10 +148,12 @@ export async function run(env, body, fs, core) {
148148
.map((item, index, arr) => {
149149
const line = item.trim();
150150

151-
if (line.startsWith("- [")) {
151+
// Allow the use of checkboxes in a textarea and transform the section into an array only when the section starts with a checkbox.
152+
// This doesn't cover the case when the textarea starts with a checkbox followed by text.
153+
if (index === 1 && line.startsWith("- [")) {
152154
return line.split(/\r?\n/).map((check) => {
153155
const field = check.replace(/- \[[X\s]\]\s+/i, "");
154-
const previousIndex = index === 0 ? index : index - 1;
156+
const previousIndex = Math.max(0, index - 1);
155157
const key = arr[previousIndex].trim();
156158
// set the type of the field to checkboxes to ensure that values will be represented as an array
157159
// even when issue-form template is not provided or wrong template is provided

test.spec.js

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import multipleParagraphsIssue from "./fixtures/multiple-paragraphs/issue.js";
1010
import paragraphConfusingHashesIssue from "./fixtures/paragraph-confusing-####/issue.js";
1111
import paragraphIgnoreCodeblockIssue from "./fixtures/paragraph-ignore-```/issue.js";
1212
import paragraphIgnoreCodeblockShIssue from "./fixtures/paragraph-ignore-```sh/issue.js";
13+
import checkboxesInTextareaIssue from "./fixtures/checkboxes-in-textarea/issue.js";
1314

1415
function loadJson(path) {
1516
return JSON.parse(readFileSync(path, "utf-8"));
@@ -304,6 +305,45 @@ it("paragraph with ```sh section", () => {
304305
expect(core.setOutput.mock.calls.length).toBe(2)
305306
});
306307

308+
it("checkboxes in textarea", () => {
309+
const expectedOutput = loadJson("./fixtures/checkboxes-in-textarea/expected.json");
310+
const expectedOutputJson = JSON.stringify(expectedOutput, null, 2);
311+
312+
// mock ENV
313+
const env = {
314+
HOME: "<home path>",
315+
};
316+
317+
// mock event payload
318+
const eventPayload = checkboxesInTextareaIssue;
319+
320+
// mock fs
321+
const fs = {
322+
readFileSync(path, encoding) {
323+
expect(path).toBe("<template-path>");
324+
expect(encoding).toBe("utf8");
325+
return readFileSync("fixtures/checkboxes-in-textarea/form.yml", "utf-8");
326+
},
327+
writeFileSync(path, content) {
328+
expect(path).toBe("<home path>/issue-parser-result.json");
329+
expect(content).toBe(expectedOutputJson);
330+
},
331+
};
332+
333+
// mock core
334+
const core = {
335+
getInput: jest.fn(() => '<template-path>'),
336+
setOutput: jest.fn(),
337+
};
338+
339+
run(env, eventPayload, fs, core);
340+
341+
expect(core.getInput).toHaveBeenCalledWith('template-path')
342+
expect(core.setOutput).toHaveBeenCalledWith('jsonString', JSON.stringify(expectedOutput, null, 2))
343+
expect(core.setOutput).toHaveBeenCalledWith('issueparser_description', 'Some text:\n\n- [ ] Red\n- [ ] Green\n- [ ] Blue\n\nMore text')
344+
expect(core.setOutput.mock.calls.length).toBe(2)
345+
});
346+
307347
it("blank", () => {
308348
const expectedOutput = loadJson("./fixtures/blank/expected.json");
309349
const expectedOutputJson = JSON.stringify(expectedOutput, null, 2);

0 commit comments

Comments
 (0)