Skip to content

Commit

Permalink
Move all PR body dependency tree changes to after PR submission (#63)
Browse files Browse the repository at this point in the history
  • Loading branch information
danerwilliams committed Oct 10, 2023
1 parent 33f7422 commit d9817ea
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 62 deletions.
18 changes: 2 additions & 16 deletions apps/cli/src/actions/create_pr_body_footer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,13 @@ export const footerTitle = '\n\n\n#### PR Dependency Tree\n\n';
export const footerFooter =
'\n\nThis tree was auto-generated by [Charcoal](https://github.com/danerwilliams/charcoal)';

export function createPrBodyFooter(
context: TContext,
branch: string,
prNumber?: number
): string {
export function createPrBodyFooter(context: TContext, branch: string): string {
const terminalParent = findTerminalParent(context, branch);

const tree = buildBranchTree({
context,
currentBranches: [terminalParent],
prBranch: branch,
prNumber,
currentDepth: 0,
});

Expand All @@ -26,13 +21,11 @@ function buildBranchTree({
context,
currentBranches,
prBranch,
prNumber,
currentDepth,
}: {
context: TContext;
currentBranches: string[];
prBranch: string;
prNumber?: number;
currentDepth: number;
}): string {
let tree = '';
Expand All @@ -58,7 +51,6 @@ function buildBranchTree({
branch,
depth: currentDepth,
prBranch,
prNumber,
})}`;

const children = context.engine.getChildren(branch);
Expand All @@ -68,7 +60,6 @@ function buildBranchTree({
context,
currentBranches: children,
prBranch,
prNumber,
currentDepth: currentDepth + 1,
})}`;
}
Expand All @@ -82,20 +73,15 @@ function buildLeaf({
branch,
depth,
prBranch,
prNumber,
}: {
context: TContext;
branch: string;
depth: number;
prBranch: string;
prNumber?: number;
}): string {
const prInfo = context.engine.getPrInfo(branch);

// If the PR is being created for the first time, it hasn't been assigned a number yet.
// We allow for passing the number to do an update to the body after PR creation to handle that case.
// The PR hasn't been persisted by the context.engine yet, so we manually pass it as a parameter to this method
const number = prInfo?.number ?? prNumber;
const number = prInfo?.number;

if (!number) {
throw new Error('PR number is undefined');
Expand Down
10 changes: 1 addition & 9 deletions apps/cli/src/actions/submit/submit_action.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,19 +144,11 @@ export async function submitAction(
);
}

const alreadyUpdatedBranches = new Set(
submissionInfos.map((submissionInfo) => submissionInfo.head)
);

const branchesToUpdateBodyFooter = branchNames.filter(
(branchName) => !alreadyUpdatedBranches.has(branchName)
);

context.splog.info(
chalk.blueBright('\n🌳 Updating dependency trees in PR bodies...')
);

for (const branch of branchesToUpdateBodyFooter) {
for (const branch of branchNames) {
const prInfo = context.engine.getPrInfo(branch);
const footer = createPrBodyFooter(context, branch);

Expand Down
39 changes: 2 additions & 37 deletions apps/cli/src/actions/submit/submit_prs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,6 @@ import { TContext } from '../../lib/context';
import { ExitFailedError } from '../../lib/errors';
import { Unpacked } from '../../lib/utils/ts_helpers';
import { execSync } from 'child_process';
import {
createPrBodyFooter,
footerFooter,
footerTitle,
} from '../create_pr_body_footer';

export type TPRSubmissionInfo = t.UnwrapSchemaMap<
typeof API_ROUTES.submitPullRequests.params
Expand All @@ -36,7 +31,6 @@ export async function submitPullRequest(
): Promise<void> {
const pr = await requestServerToSubmitPR({
submissionInfo: args.submissionInfo,
context,
});

if (pr.response.status === 'error') {
Expand Down Expand Up @@ -69,17 +63,14 @@ export async function submitPullRequest(

async function requestServerToSubmitPR({
submissionInfo,
context,
}: {
submissionInfo: TPRSubmissionInfo;
context: TContext;
}): Promise<TSubmittedPR> {
const request = submissionInfo[0];

try {
const response = await submitPrToGithub({
request,
context,
});

return {
Expand All @@ -100,10 +91,8 @@ async function requestServerToSubmitPR({

async function submitPrToGithub({
request,
context,
}: {
request: TSubmittedPRRequest;
context: TContext;
}): Promise<TSubmittedPRResponse> {
try {
const prInfo = await JSON.parse(
Expand All @@ -118,31 +107,10 @@ async function submitPrToGithub({
);
}

const footer = createPrBodyFooter(context, request.head);

const prBaseChanged = prInfo.baseRefName !== request.base;
const prFooterChanged = !prInfo.body.includes(footer);

if (prBaseChanged || prFooterChanged) {
execSync(
`gh pr edit ${prInfo.headRefName} ${
prBaseChanged ? `--base ${request.base}` : ''
} ${
prFooterChanged
? `--body '${
prInfo.body.replace(
new RegExp(
footerTitle +
'[\\s\\S]*' +
footerFooter.replace(/[.*+?^${}()|[\]\\]/g, '\\$&') // footerFooter contains special regex characters that must be escaped
),
'' // instead of just replacing with footer we handle the case where there is no existing footer
) + footer
}'`
: ''
}
`
);
if (prBaseChanged) {
execSync(`gh pr edit ${prInfo.headRefName} --base ${request.base}`);
}

return {
Expand All @@ -168,9 +136,6 @@ async function submitPrToGithub({

const prNumber = getPrNumberFromUrl(result);

const footer = createPrBodyFooter(context, request.head, prNumber);
execSync(`gh pr edit ${prNumber} --body '${request.body + footer}'`);

return {
head: request.head,
status: 'created',
Expand Down

0 comments on commit d9817ea

Please sign in to comment.