generated from actions/typescript-action
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- handle better special characters - improved readibility
- Loading branch information
Showing
7 changed files
with
91 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,34 +1,47 @@ | ||
import * as core from "@actions/core" | ||
import { sanitazeString } from "./util"; | ||
|
||
type IssueFormPayload = Record<string, string | undefined> | ||
|
||
// SectionNameOneWord\r\n\r\nvalue => ["SectionNameOneWord", "value"] | ||
// SectionName\r\n\r\nsection value option 1 => ["SectionName", "section value option 1"] | ||
// SectionNameUndefinedValue\r\n\r\n => ["SectionNameUndefinedValue", undefined] | ||
function parseSection(section: string): { name: string, value?: string } { | ||
console | ||
const trimmed = section.trim(); | ||
const sanitazed = sanitazeString(section); | ||
|
||
// Split on the first \r\n\r\n | ||
const re = /\r\n\r\n/; | ||
const split = trimmed.search(re); | ||
return split !== -1 // True? Has a name and value | ||
? { name: trimmed.substring(0, split), value: trimmed.substring(split + 1).trim() } | ||
: { name: trimmed, value: undefined }; | ||
const split = sanitazed.search(re); | ||
|
||
if (split !== -1) { | ||
// There is an answer | ||
const name = sanitazed.substring(0, split); | ||
const value = sanitazed.substring(split + 1).trim(); | ||
return { name, value }; | ||
} else { | ||
// No answer | ||
return { name: sanitazed, value: undefined }; | ||
} | ||
} | ||
|
||
// ### NameSection1\r\n\r\nvalue1\r\n\r\n ### NameSection2\r\n\r\nvalue2: With a long value\r\n\r\n### NameSection3\r\n\r\n | ||
// => { NameSection1: "value1", NameSection2: "value2: With a long value", NameSection3: undefined } | ||
export function parseBody(body: string): IssueFormPayload { | ||
core.debug(`Body: ${body}`) | ||
const sections = body.split("###").filter((s) => s.trim().length > 0); | ||
core.debug(`Found ${sections.length} sections in the form.`); | ||
|
||
if (sections.length < 2) { | ||
throw new Error(`No section in the form. Make sure to have '###' in the body of your issue. What we got ${body}`); | ||
if (!body.startsWith("###")) { | ||
throw new Error(` | ||
Invalid issue body format. Body must start with '###' followed by the name of the section and its value. | ||
See /~https://github.com/onmax/issue-form-parser/issues. The body we got is: ${body} | ||
`); | ||
} | ||
|
||
const sections = body.split("###").filter((s) => s.trim().length > 0); | ||
core.debug(`Found ${sections.length} sections in the form.`); | ||
|
||
const res: IssueFormPayload = {}; | ||
sections.map(parseSection).forEach(({ name, value }) => res[name] = value); | ||
|
||
core.debug(`Payload JSON: ${JSON.stringify(res)}`); | ||
|
||
return res; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
// Some characters are not treated well in Bash, so we sanitize them | ||
// For example, the use of ` will break the workflow if you try to print it | ||
export function sanitazeString(value: string) { | ||
let trimmed = value.trim(); | ||
const bashChars = ["`", "$"]; | ||
return bashChars.reduce((acc, char) => acc.replace(char, ""), trimmed).trim(); | ||
} |