forked from GrantBirki/issue-template-parser
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparse.js
53 lines (39 loc) · 1.56 KB
/
parse.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import * as core from '@actions/core'
// Helper function to parse the body of the issue template
// :param body: The body of the issue template
// :return: A dictionary of the parsed body
export async function parse(body) {
var parsed_issue_body_dict = {}
body = String(body)
core.info(`parsing body: ${body}`)
// Split the body up by the section headers
const issue_body_sections_list = body.split('###')
core.debug(issue_body_sections_list)
// Remove the first element of the list, which is empty
issue_body_sections_list.shift()
// Loop over the list of sections
for (const section of issue_body_sections_list) {
// Split out the issue body sections
let issue_body = section.trim().split(/\r?\n/)
core.debug(issue_body)
// make the key lowercase and snake case
let key = issue_body[0].trim().toLowerCase().replace(/ /g, '_')
// Remove the first element of the list, which is the section header
issue_body.shift()
// Join the list back together with newlines
issue_body = issue_body.slice(1).join('\n')
// get the value from the body as well
let value = issue_body.trim()
// If the value for a field is empty, set it to null
if (value === '_No response_' || value === '_No response_"') {
value = null
}
// Add the key-value pair to the dictionary
parsed_issue_body_dict[key] = value
}
core.debug(parsed_issue_body_dict)
const parsed_json = JSON.stringify(parsed_issue_body_dict, null, 2)
core.info(`parsed json: ${parsed_json}`)
// Return the dictionary
return parsed_json
}