Skip to content

Commit 38782d0

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 f474b11 commit 38782d0

7 files changed

Lines changed: 70 additions & 4 deletions

File tree

dist/index.js

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

131-
if (line.startsWith("- [")) {
131+
// Allow the use of checkboxes in a textarea and transform the section into an array only when the section starts with a checkbox.
132+
// This doesn't cover the case when the textarea starts with a checkbox followed by text.
133+
if (index === 1 && line.startsWith("- [")) {
132134
return line.split(/\r?\n/).map((check) => {
133135
const field = check.replace(/- \[[X\s]\]\s+/i, "");
134-
const previousIndex = index === 0 ? index : index - 1;
136+
const previousIndex = Math.max(0, index - 1);
135137
const key = arr[previousIndex].trim();
136138
// set the type of the field to checkboxes to ensure that values will be represented as an array
137139
// even when issue-form template is not provided or wrong template is provided
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: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
const { resolve } = require("path");
2+
const { readFileSync } = require("fs");
3+
4+
const issueBodyPath = resolve(__dirname, "issue-body.md");
5+
6+
module.exports = readFileSync(issueBodyPath, "utf-8")

index.js

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

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

test.spec.js

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,45 @@ it("multiple paragraphs", () => {
174174
expect(core.setOutput.mock.calls.length).toBe(3)
175175
});
176176

177+
it("checkboxes in textarea", () => {
178+
const expectedOutput = require("./fixtures/checkboxes-in-textarea/expected.json");
179+
const expectedOutputJson = JSON.stringify(expectedOutput, null, 2);
180+
181+
// mock ENV
182+
const env = {
183+
HOME: "<home path>",
184+
};
185+
186+
// mock event payload
187+
const eventPayload = require("./fixtures/checkboxes-in-textarea/issue");
188+
189+
// mock fs
190+
const fs = {
191+
readFileSync(path, encoding) {
192+
expect(path).toBe("<template-path>");
193+
expect(encoding).toBe("utf8");
194+
return readFileSync("fixtures/checkboxes-in-textarea/form.yml", "utf-8");
195+
},
196+
writeFileSync(path, content) {
197+
expect(path).toBe("<home path>/issue-parser-result.json");
198+
expect(content).toBe(expectedOutputJson);
199+
},
200+
};
201+
202+
// mock core
203+
const core = {
204+
getInput: jest.fn(() => '<template-path>'),
205+
setOutput: jest.fn(),
206+
};
207+
208+
run(env, eventPayload, fs, core);
209+
210+
expect(core.getInput).toHaveBeenCalledWith('template-path')
211+
expect(core.setOutput).toHaveBeenCalledWith('jsonString', JSON.stringify(expectedOutput, null, 2))
212+
expect(core.setOutput).toHaveBeenCalledWith('issueparser_description', 'Some text:\n\n- [ ] Red\n- [ ] Green\n- [ ] Blue\n\nMore text')
213+
expect(core.setOutput.mock.calls.length).toBe(2)
214+
});
215+
177216
it("blank", () => {
178217
const expectedOutput = require("./fixtures/blank/expected.json");
179218
const expectedOutputJson = JSON.stringify(expectedOutput, null, 2);

0 commit comments

Comments
 (0)