Skip to content

Commit

Permalink
fix: children case-sensitivity and PR support
Browse files Browse the repository at this point in the history
* Children can be PRs now
* Correctly check for case-insensitive "children:"

fixes #360
  • Loading branch information
SgtPooki committed May 4, 2023
1 parent 3dc6509 commit 5b39ebf
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 12 deletions.
46 changes: 39 additions & 7 deletions lib/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,39 @@ export const getDueDate = (issue: Pick<GithubIssueDataWithChildren, 'html_url' |
};
};

function getSectionLines(text: string, sectionHeader: string): string {
const sectionIndex = text.indexOf(sectionHeader);
if (sectionIndex === -1) {
function regexIndexOf(string: string, regex: RegExp, startpos = 0) {
const indexOf = string.substring(startpos).search(regex);
if (indexOf >= 0) {
return indexOf + startpos
}
return indexOf;
}

function indexOf(string: string, strOrRegex: string | RegExp, startpos = 0) {
if (typeof strOrRegex === 'string') {
return string.indexOf(strOrRegex, startpos)
}
return regexIndexOf(string, strOrRegex, startpos)
}

function getSectionLines(text: string, sectionHeader: string | RegExp): string {
const sectionStartIndex = indexOf(text, sectionHeader);
if (sectionStartIndex === -1) {
return '';
}
return text.substring(sectionIndex)
const startText = text.substring(sectionStartIndex)
/**
* sectionEndIndex marks the location of the first double line break
* i.e. first empty line
*/
const sectionEndIndex = regexIndexOf(startText, /^[\r\n]{2,}$/gm)
if (sectionEndIndex === -1) {
return startText
}
return startText.substring(0, sectionEndIndex)
}

function getCleanedSectionLines(text: string, sectionHeader: string) {
function getCleanedSectionLines(text: string, sectionHeader: string | RegExp) {
const lines = getSectionLines(text, sectionHeader)
if (typeof lines === 'string') {
return lines.split(/[\r\n]+/).slice(1)
Expand All @@ -55,6 +79,14 @@ function getUrlStringForChildrenLine(line: string, issue: Pick<GithubIssueData,
const { owner, repo } = paramsFromUrl(issue.html_url)
line = `${owner}/${repo}${line}`
}
if (line.includes('/pull/')) {
/**
* getValidUrlFromInput requires a valid issue url, so we replace /pull/
* with /issues/ since github will recognize the pull request URL as a valid
* issue url
*/
line = line.replace('/pull/', '/issues/')
}
const url = getValidUrlFromInput(line)
if (!url.host.includes('github.com')) {
throw new Error('Invalid host for children item')
Expand Down Expand Up @@ -95,7 +127,7 @@ function getChildrenNew(issue: Pick<GithubIssueData, 'body' | 'html_url'>): Pars
// Could not find children using new tasklist format,
// try to look for "children:" section
}
const lines = getCleanedSectionLines(issue.body, 'children:').map(splitAndGetLastItem).filter(Boolean);
const lines = getCleanedSectionLines(issue.body, /children:/i).map(splitAndGetLastItem).filter(Boolean);
if (lines.length === 0) {
throw new Error('Section missing or has no children')
}
Expand Down Expand Up @@ -186,7 +218,7 @@ export const getDescription = (issueBodyText: string): string => {
}

for (const line of linesToParse.map((line) => line.trim())) {
if (line === '' || line.includes('children:') || line.includes('```[tasklist]') || line.includes('eta:')) {
if (line === '' || /children:/i.test(line) || line.includes('```[tasklist]') || /eta:/i.test(line)) {
break
}
descriptionLines.push(line.trim())
Expand Down
12 changes: 12 additions & 0 deletions tests/fixtures/getIssueResult/filecoin-station-roadmap-15.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"html_url": "https://github.com/filecoin-station/roadmap/issues/15",
"title": "Station Core 👉 Station Desktop integration",
"state": "open",
"node_id": "I_kwDOIUF5GM5fK-t-",
"body_html": "<p dir=\"auto\">ETA: 2023-05-01</p>\n<p dir=\"auto\">description: Greatly simplify <code class=\"notranslate\">Station Desktop</code> and the product ecosystem in general by letting it leverage <code class=\"notranslate\">Station Core</code>.</p>\n<p dir=\"auto\">Children:</p>\n<ul class=\"contains-task-list\">\n<li class=\"task-list-item\"><input type=\"checkbox\" id=\"\" disabled=\"\" class=\"task-list-item-checkbox\" checked=\"\"> <a class=\"issue-link js-issue-link\" data-error-text=\"Failed to load title\" data-id=\"1653584777\" data-permission-text=\"Title is private\" data-url=\"https://github.com/filecoin-station/core/issues/83\" data-hovercard-type=\"pull_request\" data-hovercard-url=\"/filecoin-station/core/pull/83/hovercard\" href=\"https://github.com/filecoin-station/core/pull/83\">filecoin-station/core#83</a></li>\n<li class=\"task-list-item\"><input type=\"checkbox\" id=\"\" disabled=\"\" class=\"task-list-item-checkbox\" checked=\"\"> <a class=\"issue-link js-issue-link\" data-error-text=\"Failed to load title\" data-id=\"1676397485\" data-permission-text=\"Title is private\" data-url=\"https://github.com/filecoin-station/core/issues/100\" data-hovercard-type=\"pull_request\" data-hovercard-url=\"/filecoin-station/core/pull/100/hovercard\" href=\"https://github.com/filecoin-station/core/pull/100\">filecoin-station/core#100</a></li>\n<li class=\"task-list-item\"><input type=\"checkbox\" id=\"\" disabled=\"\" class=\"task-list-item-checkbox\"> <a class=\"issue-link js-issue-link\" data-error-text=\"Failed to load title\" data-id=\"1655290530\" data-permission-text=\"Title is private\" data-url=\"https://github.com/filecoin-station/desktop/issues/621\" data-hovercard-type=\"pull_request\" data-hovercard-url=\"/filecoin-station/desktop/pull/621/hovercard\" href=\"https://github.com/filecoin-station/desktop/pull/621\">filecoin-station/desktop#621</a></li>\n</ul>\n<p dir=\"auto\">Milestone: <a href=\"https://github.com/filecoin-station/core/milestone/4\">https://github.com/filecoin-station/core/milestone/4</a></p>",
"body": "ETA: 2023-05-01\r\n\r\ndescription: Greatly simplify `Station Desktop` and the product ecosystem in general by letting it leverage `Station Core`.\r\n\r\nChildren:\r\n- [x] https://github.com/filecoin-station/core/pull/83\r\n- [x] https://github.com/filecoin-station/core/pull/100\r\n- [ ] https://github.com/filecoin-station/desktop/pull/621\r\n\r\nMilestone: https://github.com/filecoin-station/core/milestone/4",
"labels": [
"station core"
],
"description": "Greatly simplify `Station Desktop` and the product ecosystem in general by letting it leverage `Station Core`."
}
16 changes: 11 additions & 5 deletions tests/unit/parser.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const example_body = 'children: \r\n- https://github.com/protocol/bedrock/issues
/**
* Test data obtained from calling getIssue() on github.com/ipfs/ipfs-gui/issues/106 on 2023-02-10 @ 5:52pm PST
*/
const example_tasklist_body = 'eta: 2023Q4\r\n\r\nchildren:\r\n- [ ] #121\r\n- [ ] #122\r\n- [ ] #123\r\n- [ ] #124\r\n\r\n```[tasklist]\r\n### Tasks\r\n- [ ] #121\r\n- [ ] #122\r\n- [ ] #123\r\n- [ ] #124\r\n```\r\n'
const example_tasklist_body = 'eta: 2023Q4\r\n\r\nchildren:\r\n- [ ] #121\r\n- [ ] #122\r\n- [ ] #123\r\n- [ ] #124\r\n\r\n```[tasklist]\r\n### Tasks\r\n- [ ] #125\r\n- [ ] #126\r\n- [ ] #127\r\n- [ ] #128\r\n```\r\n'

/**
* Test data manually removing "children:" from the above example_tasklist_body
Expand Down Expand Up @@ -133,10 +133,10 @@ describe('parser', function() {
expect(Array.isArray(children)).toBe(true);
expect(children).toHaveLength(4);
expect(children).toStrictEqual([
{ group: 'tasklist', html_url: 'https://github.com/ipfs/ipfs-gui/issues/121' },
{ group: 'tasklist', html_url: 'https://github.com/ipfs/ipfs-gui/issues/122' },
{ group: 'tasklist', html_url: 'https://github.com/ipfs/ipfs-gui/issues/123' },
{ group: 'tasklist', html_url: 'https://github.com/ipfs/ipfs-gui/issues/124' }
{ group: 'tasklist', html_url: 'https://github.com/ipfs/ipfs-gui/issues/125' },
{ group: 'tasklist', html_url: 'https://github.com/ipfs/ipfs-gui/issues/126' },
{ group: 'tasklist', html_url: 'https://github.com/ipfs/ipfs-gui/issues/127' },
{ group: 'tasklist', html_url: 'https://github.com/ipfs/ipfs-gui/issues/128' }
])
})

Expand Down Expand Up @@ -196,6 +196,12 @@ describe('parser', function() {
{ group: 'children:', html_url: 'https://github.com/testground/testground/issues/1523' },
])
})

it('Can parse pull-request children properly', async function() {
const issueJson = await import('../fixtures/getIssueResult/filecoin-station-roadmap-15.json');
const children = getChildren(issueJson);
expect(children).toHaveLength(3)
})
})

describe('getDescription', function() {
Expand Down

0 comments on commit 5b39ebf

Please sign in to comment.