reviewstack: fix parsing logic to hide stack info from PR body

Summary:
Updates `parseSaplingStackBody()` to keep track of the format
of the PR body that it parsed so that
`stripStackInfoFromSaplingBodyHTML()` can do the right thing
in both cases.

Reviewed By: quark-zju

Differential Revision: D42183664

fbshipit-source-id: 41bb47bfc5034deae357a444fa5816b674a6329b
This commit is contained in:
Michael Bolin 2022-12-21 21:32:14 -08:00 committed by Facebook GitHub Bot
parent cc3b2901d6
commit f32ae61f72
3 changed files with 35 additions and 8 deletions

View File

@ -111,7 +111,7 @@ function PullRequestDetails() {
pullRequestBodyHTML = bodyHTML;
break;
case 'sapling':
pullRequestBodyHTML = stripStackInfoFromSaplingBodyHTML(bodyHTML);
pullRequestBodyHTML = stripStackInfoFromSaplingBodyHTML(bodyHTML, stack.body.format);
break;
case 'ghstack':
pullRequestBodyHTML = stripStackInfoFromBodyHTML(bodyHTML);

View File

@ -35,6 +35,7 @@ so that each commit in the stack can be reviewed individually.
{number: 456, numCommits: 1},
{number: 789, numCommits: 2},
],
format: 'prefix',
currentStackEntry: 1,
commitMessage: 'This would be the original commit message of this fictitious commit.\n',
});
@ -67,6 +68,7 @@ so that each commit in the stack can be reviewed individually.
{number: 456, numCommits: 1},
{number: 789, numCommits: 2},
],
format: 'hr-suffix',
currentStackEntry: 1,
commitMessage: 'This would be the original commit message of this fictitious commit.\n',
});
@ -87,6 +89,7 @@ Stack created with [Sapling](https://sapling-scm.com/github).
{number: 1, numCommits: 1},
{number: 123, numCommits: 1},
],
format: 'hr-suffix',
currentStackEntry: 1,
commitMessage: '',
});

View File

@ -5,13 +5,34 @@
* LICENSE file in the root directory of this source tree.
*/
export function stripStackInfoFromSaplingBodyHTML(bodyHTML: string): string {
const _STACK_SECTION_START = 'Stack created with [Sapling]';
/**
* `prefix`: body starts with _STACK_SECTION_START, followed by stack info
* `hr-suffix`: body ends with horizontal rule, followed by _STACK_SECTION_START
* and then stack info
*/
type SaplingPullRequestBodyFormat = 'prefix' | 'hr-suffix';
export function stripStackInfoFromSaplingBodyHTML(
bodyHTML: string,
format: SaplingPullRequestBodyFormat,
): string {
// This uses the same heuristic as ghstack, though note that it will NOT
// work in the presence of sub-bullets.
switch (format) {
case 'prefix': {
const delimiter = '</li>\n</ul>\n';
const index = bodyHTML.indexOf(delimiter);
// Retain any other lists that may be present as part of the commit message
return index !== -1 ? bodyHTML.slice(index + delimiter.length) : bodyHTML;
}
case 'hr-suffix': {
const delimiter = '<hr>';
const index = bodyHTML.lastIndexOf(delimiter);
return index !== -1 ? bodyHTML.slice(0, index) : bodyHTML;
}
}
}
/**
@ -49,16 +70,16 @@ export type SaplingPullRequestBody = {
* (from Sapling's perspective).
*/
stack: Array<{number: number; numCommits: number}>;
format: SaplingPullRequestBodyFormat;
currentStackEntry: number;
commitMessage: string;
};
const _STACK_SECTION_START = 'Stack created with [Sapling]';
export function parseSaplingStackBody(body: string): SaplingPullRequestBody | null {
const lines = body.split(/\r?\n/);
let firstLine: string;
let format: SaplingPullRequestBodyFormat;
let index: number;
let commitMessage = null;
@ -66,6 +87,7 @@ export function parseSaplingStackBody(body: string): SaplingPullRequestBody | nu
// a line starting with _STACK_SECTION_START after a horizontal rule.
if (body.startsWith(_STACK_SECTION_START)) {
firstLine = lines[0];
format = 'prefix';
index = 1;
} else {
const lastHRIndex = lines.lastIndexOf('---');
@ -74,6 +96,7 @@ export function parseSaplingStackBody(body: string): SaplingPullRequestBody | nu
}
firstLine = lines[lastHRIndex + 1];
format = 'hr-suffix';
index = lastHRIndex + 2;
commitMessage = lines.slice(0, lastHRIndex).join('\n');
if (commitMessage !== '') {
@ -137,6 +160,7 @@ export function parseSaplingStackBody(body: string): SaplingPullRequestBody | nu
firstLine,
introduction: introductionLines.join('\n'),
stack,
format,
currentStackEntry,
commitMessage,
};