-
Notifications
You must be signed in to change notification settings - Fork 550
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds and updates footer in stacked pull request descriptions
- Loading branch information
Showing
6 changed files
with
115 additions
and
7 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
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
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,49 @@ | ||
import { equalPrId } from './pullRequestId'; | ||
import type { DetailedPullRequest, PullRequestId } from '$lib/forge/interface/types'; | ||
import type { ForgePrService } from '../interface/forgePrService'; | ||
|
||
export const GITBUTLER_FOOTER_BOUNDARY = '<!-- GitButler Footer Boundary -->'; | ||
|
||
export async function updatePrDescriptionTables(prService: ForgePrService, prIds: PullRequestId[]) { | ||
if (prService && prIds.length > 1) { | ||
const prs = await Promise.all(prIds.map(async (prId) => await prService.get(prId))); | ||
await Promise.all( | ||
prIds.map(async (prId) => { | ||
const pr = prs.find((p) => equalPrId(p.id, prId)) as DetailedPullRequest; | ||
const currentDescription = pr.body ? stripFooter(pr.body.trim()) : ''; | ||
await prService.update(pr.id, { | ||
description: currentDescription + '\n' + generateFooter(prId, prs) | ||
}); | ||
}) | ||
); | ||
} | ||
} | ||
|
||
/** | ||
* Generates a footer for use in pull request descriptions when part of a stack. | ||
*/ | ||
export function generateFooter(id: PullRequestId, all: DetailedPullRequest[]) { | ||
const stackIndex = all.findIndex((pr) => equalPrId(pr.id, id)); | ||
let footer = ''; | ||
footer += GITBUTLER_FOOTER_BOUNDARY + '\n\n'; | ||
footer += `| # | PR |\n`; | ||
footer += '| --- | --- |\n'; | ||
all.forEach((pr, i) => { | ||
const current = i === stackIndex; | ||
const rankNumber = all.length - i; | ||
const rankStr = current ? bold(rankNumber) : rankNumber; | ||
const prNumber = `#${pr.number}`; | ||
const prStr = current ? bold(prNumber) : prNumber; | ||
footer += `| ${rankStr} | ${prStr} |\n`; | ||
}); | ||
return footer; | ||
} | ||
|
||
function stripFooter(description: string) { | ||
console.log(description.split(GITBUTLER_FOOTER_BOUNDARY)); | ||
return description.split(GITBUTLER_FOOTER_BOUNDARY)[0]; | ||
} | ||
|
||
function bold(text: string | number) { | ||
return `**${text}**`; | ||
} |
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