diff --git a/dist/index.js b/dist/index.js index ff432d3..48e5905 100644 --- a/dist/index.js +++ b/dist/index.js @@ -128,10 +128,12 @@ async function run(env, body, fs, core) { .map((item, index, arr) => { const line = item.trim(); - if (line.startsWith("- [")) { + // 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. + if (index === 1 && 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 previousIndex = Math.max(0, index - 1); const key = arr[previousIndex].trim(); // set the type of the field to checkboxes to ensure that values will be represented as an array // even when issue-form template is not provided or wrong template is provided diff --git a/fixtures/checkboxes-in-textarea/expected.json b/fixtures/checkboxes-in-textarea/expected.json new file mode 100644 index 0000000..3e553bd --- /dev/null +++ b/fixtures/checkboxes-in-textarea/expected.json @@ -0,0 +1,3 @@ +{ + "description": "Some text:\n\n- [ ] Red\n- [ ] Green\n- [ ] Blue\n\nMore text" +} diff --git a/fixtures/checkboxes-in-textarea/form.yml b/fixtures/checkboxes-in-textarea/form.yml new file mode 100644 index 0000000..1eaedbe --- /dev/null +++ b/fixtures/checkboxes-in-textarea/form.yml @@ -0,0 +1,5 @@ +body: + - type: textarea + id: description + attributes: + label: Description diff --git a/fixtures/checkboxes-in-textarea/issue-body.md b/fixtures/checkboxes-in-textarea/issue-body.md new file mode 100644 index 0000000..6e1952b --- /dev/null +++ b/fixtures/checkboxes-in-textarea/issue-body.md @@ -0,0 +1,9 @@ +### Description + +Some text: + +- [ ] Red +- [ ] Green +- [ ] Blue + +More text diff --git a/fixtures/checkboxes-in-textarea/issue.js b/fixtures/checkboxes-in-textarea/issue.js new file mode 100644 index 0000000..6851c74 --- /dev/null +++ b/fixtures/checkboxes-in-textarea/issue.js @@ -0,0 +1,6 @@ +const { resolve } = require("path"); +const { readFileSync } = require("fs"); + +const issueBodyPath = resolve(__dirname, "issue-body.md"); + +module.exports = readFileSync(issueBodyPath, "utf-8") diff --git a/index.js b/index.js index 53cc884..54eaffb 100644 --- a/index.js +++ b/index.js @@ -122,10 +122,12 @@ async function run(env, body, fs, core) { .map((item, index, arr) => { const line = item.trim(); - if (line.startsWith("- [")) { + // 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. + if (index === 1 && 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 previousIndex = Math.max(0, index - 1); const key = arr[previousIndex].trim(); // set the type of the field to checkboxes to ensure that values will be represented as an array // even when issue-form template is not provided or wrong template is provided diff --git a/test.spec.js b/test.spec.js index c3ce9bc..60ac00b 100644 --- a/test.spec.js +++ b/test.spec.js @@ -174,6 +174,45 @@ it("multiple paragraphs", () => { expect(core.setOutput.mock.calls.length).toBe(3) }); +it("checkboxes in textarea", () => { + const expectedOutput = require("./fixtures/checkboxes-in-textarea/expected.json"); + const expectedOutputJson = JSON.stringify(expectedOutput, null, 2); + + // mock ENV + const env = { + HOME: "", + }; + + // mock event payload + const eventPayload = require("./fixtures/checkboxes-in-textarea/issue"); + + // mock fs + const fs = { + readFileSync(path, encoding) { + expect(path).toBe(""); + expect(encoding).toBe("utf8"); + return readFileSync("fixtures/checkboxes-in-textarea/form.yml", "utf-8"); + }, + writeFileSync(path, content) { + expect(path).toBe("/issue-parser-result.json"); + expect(content).toBe(expectedOutputJson); + }, + }; + + // mock core + const core = { + getInput: jest.fn(() => ''), + setOutput: jest.fn(), + }; + + run(env, eventPayload, fs, core); + + expect(core.getInput).toHaveBeenCalledWith('template-path') + expect(core.setOutput).toHaveBeenCalledWith('jsonString', JSON.stringify(expectedOutput, null, 2)) + expect(core.setOutput).toHaveBeenCalledWith('issueparser_description', 'Some text:\n\n- [ ] Red\n- [ ] Green\n- [ ] Blue\n\nMore text') + expect(core.setOutput.mock.calls.length).toBe(2) +}); + it("blank", () => { const expectedOutput = require("./fixtures/blank/expected.json"); const expectedOutputJson = JSON.stringify(expectedOutput, null, 2);