Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions fixtures/checkboxes-in-textarea/expected.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"description": "Some text:\n\n- [ ] Red\n- [ ] Green\n- [ ] Blue\n\nMore text"
}
5 changes: 5 additions & 0 deletions fixtures/checkboxes-in-textarea/form.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
body:
- type: textarea
id: description
attributes:
label: Description
9 changes: 9 additions & 0 deletions fixtures/checkboxes-in-textarea/issue-body.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
### Description

Some text:

- [ ] Red
- [ ] Green
- [ ] Blue

More text
6 changes: 6 additions & 0 deletions fixtures/checkboxes-in-textarea/issue.js
Original file line number Diff line number Diff line change
@@ -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")
6 changes: 4 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
39 changes: 39 additions & 0 deletions test.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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: "<home path>",
};

// mock event payload
const eventPayload = require("./fixtures/checkboxes-in-textarea/issue");

// mock fs
const fs = {
readFileSync(path, encoding) {
expect(path).toBe("<template-path>");
expect(encoding).toBe("utf8");
return readFileSync("fixtures/checkboxes-in-textarea/form.yml", "utf-8");
},
writeFileSync(path, content) {
expect(path).toBe("<home path>/issue-parser-result.json");
expect(content).toBe(expectedOutputJson);
},
};

// mock core
const core = {
getInput: jest.fn(() => '<template-path>'),
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);
Expand Down