Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: improve body parsing logic #72

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
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
3 changes: 3 additions & 0 deletions fixtures/paragraph-confusing-####/expected.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"textarea-one": "Textarea input text 1 ####"
}
9 changes: 9 additions & 0 deletions fixtures/paragraph-confusing-####/form.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
body:
- type: textarea
id: textarea-one
attributes:
label: My textarea input
- type: textarea
id: textarea-two
attributes:
label: Another textarea input
3 changes: 3 additions & 0 deletions fixtures/paragraph-confusing-####/issue-body.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
### My textarea input

Textarea input text 1 ####
6 changes: 6 additions & 0 deletions fixtures/paragraph-confusing-####/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")
3 changes: 3 additions & 0 deletions fixtures/paragraph-ignore-```/expected.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"textarea-one": "Textarea input text 1\n\n```\n### To be ignored tag\n```"
}
9 changes: 9 additions & 0 deletions fixtures/paragraph-ignore-```/form.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
body:
- type: textarea
id: textarea-one
attributes:
label: My textarea input
- type: textarea
id: textarea-two
attributes:
label: Another textarea input
7 changes: 7 additions & 0 deletions fixtures/paragraph-ignore-```/issue-body.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
### My textarea input

Textarea input text 1

```
### To be ignored tag
```
6 changes: 6 additions & 0 deletions fixtures/paragraph-ignore-```/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")
32 changes: 29 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -111,9 +111,35 @@ async function run(env, body, fs, core) {
return result
}

result = body
.trim()
.split("###")
function parseBody(body) {
let result = [];
let ignore = false;

body.split("\n").reduce((str, line, idx, arr) => {
// ``` Section must be ignored and not parsed as new section
if (line == "```")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please also account for instances when the context of the code block is specified. For example:

```md

ignore = !ignore

// Parse new setion only if they start with ### SPACE
if (!ignore && line.startsWith("### ")) {
result.push(str.trim())

return line.replace(/^### /, "")+"\n";
}

str += line + "\n"

// Push the last string if we are at the end of lines
if (arr.length - 1 == idx)
result.push(str.trim())

return str;
}, "")

return result;
}

result = parseBody(body)
.filter(Boolean)
.map((line) => {
return line
Expand Down
78 changes: 78 additions & 0 deletions test.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,84 @@ it("multiple paragraphs", () => {
expect(core.setOutput.mock.calls.length).toBe(3)
});

it("paragraph with confusing ####", () => {
const expectedOutput = require("./fixtures/paragraph-confusing-####/expected.json");
const expectedOutputJson = JSON.stringify(expectedOutput, null, 2);

// mock ENV
const env = {
HOME: "<home path>",
};

// mock event payload
const eventPayload = require("./fixtures/paragraph-confusing-####/issue");

// mock fs
const fs = {
readFileSync(path, encoding) {
expect(path).toBe("<template-path>");
expect(encoding).toBe("utf8");
return readFileSync("fixtures/paragraph-confusing-####/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_textarea-one', 'Textarea input text 1 ####')
expect(core.setOutput.mock.calls.length).toBe(2)
});

it("paragraph with ``` section", () => {
const expectedOutput = require("./fixtures/paragraph-ignore-```/expected.json");
const expectedOutputJson = JSON.stringify(expectedOutput, null, 2);

// mock ENV
const env = {
HOME: "<home path>",
};

// mock event payload
const eventPayload = require("./fixtures/paragraph-ignore-```/issue");

// mock fs
const fs = {
readFileSync(path, encoding) {
expect(path).toBe("<template-path>");
expect(encoding).toBe("utf8");
return readFileSync("fixtures/paragraph-ignore-```/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_textarea-one', 'Textarea input text 1\n\n```\n### To be ignored tag\n```')
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