Convert issue form responses to JSON
npm install @github/issue-parser!Check dist/
!Code Coverage
!CodeQL
!Continuous Integration
!Continuous Delivery
!Linter
Convert issue form responses to JSON
This package can be used to parse GitHub issues into machine-readable JSON for
processing. In particular, it is designed to work with
issue forms
to perform basic transformations on the issue body, resulting in a consistent
JSON output.
Issues submitted using issue forms use a structured Markdown format. **So long
as the issue body is not heavily modified by the user,** we can reliably parse
the issue body into a JSON object.
``bash`
npm i @github/issue-parser
Here is a simple example of how to use this package in your project.
`typescript
import { parseIssue } from '@github/issue-parser'
const issue = parseIssue('
`
Assuming the issue and template look like these examples:
- Example Issue Markdown
- Example Template YAML
The resulting issue object will look like the following:
`typescript`
{
name: 'this-thing',
nickname: 'thing',
color: ['blue'],
shape: ['square'],
sounds: ['re', 'mi'],
topics: [],
description: "This is a description.\n\nIt has multiple lines.\n\nIt's pretty cool!",
notes: '- Note\n- Another note\n- Lots of notes',
code: 'const thing = new Thing()\nthing.doThing()',
'code-string': 'thing.toString()',
'is-thing': {
selected: ['Yes'],
unselected: ['No']
},
'is-thing-useful': {
selected: ['Sometimes'],
unselected: ['Yes', 'No']
},
'read-team': 'IssueOps-Demo-Readers',
'write-team': 'IssueOps-Demo-Writers'
}
This is the main function of the package. It takes two arguments:
- issue: string - The body of the issue to be parsedtemplate: string
- - (Optional) The issue form template used to create theoptions?: { slugify?: boolean }
issue
- - (Optional) Additional parsing options to
use when processing the issue body
If the template value is provided, the package will attempt to transform thetype
issue response values into different types based on the property of thetemplate
specific field in the template. If the value is omitted, all parsed
values will be returned as strings. For information on the transformations that
are applied, see the Transformations section.
#### Parsing Options
- slugify: boolean - If set to true, any parsed keys that are not found in
the issue forms template (if provided) will be converted to
slugs using the
slugify package. Otherwise, the
original header value will be used as the object key.
Parses an issue form template and returns an object. This can be used to match
form responses in the issue body with the fields, so that you can perform
additional validation.
When parsing an issue and the associated form template, this package will
attempt to match field IDs with response values. If a match is found, the field
ID will be used in the parsed object keys (instead of the header value from the
markdown response). If no match is found, the header text is used as the object
key.
For example, if you have the following issue form template:
`yaml
name: Example Request
description: Submit an example request
title: '[Request] Example'
body:
- type: input
id: name
attributes:
label: The Name of the Thing
description: The name of the thing you want to create.
placeholder: this-is-the-thing
validations:
required: true
`
And the following issue body:
`markdown$3
this-thing
thing
`
The resulting parsed issue would be:
`jsonc`
{
// Uses the ID value from the issue form template as the key
"name": "this-thing",
// Uses the original header value from the issue body
"The Nickname of the Thing": "thing"
}
The following transformations will take place for responses, depending on the
input type. The type is inferred from the issue form template. For information
on each specific type, see
Syntax for GitHub's form schema.
Before:
`plain`
This is a response
After (no change):
`plain`
This is a response
> [!NOTE]
>
> Empty lines are preserved in multiline responses.
Before:
`plain
First line :D
Third line!
`
After:
`plain`
First line :D\n\nThird line!
Before:
`plain`
red, blue, green
After:
`json`
["red", "blue", "green"]
Before:
`plain`
- [x] Pick me!
- [ ] Don't pick me D:
After:
`json`
{
"selected": ["Pick me!"],
"unselected": ["Don't pick me D:"]
}
In the following situations, an input will be omitted from the output JSON:
| Scenario | Example |
| --------------- | ----------------------- |
| Invalid Heading | ## This is invalid |###
| Empty Heading | |This is a value
| | |### This is a heading
| No Value | |### This is another
| | |
| | |This is a value
| | |
Normally, if a form is submitted with empty field(s), they will be included in
the issue body as one of the following, depending on the input type in the form
template.
| Type | No Response |
| ---------- | --------------- |
| dropdown | None |input
| | _No response_ |textarea
| | _No response_ |
`markdown$3
None
_No response_
- [ ] Item A
- [ ] Item B
`
These will be converted to one of the following, based on the type of input
specified in the issue form template:
| Type | Output |
| ------------ | ------------------------------------- |
| checkboxes | { "selected": [], "unselected": []} |dropdown
| | [] |input
| | undefined |textarea
| | undefined` |