From ef17ffc84fc034a34be8fb35495511c087f2576c Mon Sep 17 00:00:00 2001 From: Dane Williams Date: Thu, 8 Feb 2024 22:21:52 -0500 Subject: [PATCH] Fix dependency printing issue (#83) --- apps/cli/src/actions/submit/submit_action.ts | 34 ++++++++++----- apps/cli/test/actions/submit_action.test.ts | 46 ++++++++++++++++++++ 2 files changed, 70 insertions(+), 10 deletions(-) diff --git a/apps/cli/src/actions/submit/submit_action.ts b/apps/cli/src/actions/submit/submit_action.ts index c04aa395e..3c6288764 100644 --- a/apps/cli/src/actions/submit/submit_action.ts +++ b/apps/cli/src/actions/submit/submit_action.ts @@ -160,16 +160,10 @@ export async function submitAction( if (prFooterChanged) { execSync( - `gh pr edit ${prInfo.number} --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 - }' + `gh pr edit ${prInfo.number} --body '${updatePrBodyFooter( + prInfo.body, + footer + )}' ` ); @@ -186,6 +180,26 @@ export async function submitAction( } } +export function updatePrBodyFooter( + body: string | undefined, + footer: string +): string { + if (!body) { + return footer; + } + + const regex = new RegExp( + `${footerTitle}[\\s\\S]*${footerFooter.replace( + /[.*+?^${}()|[\]\\]/g, + '\\$&' + )}` + ); + + const updatedBody = body.replace(regex, footer); + + return updatedBody; +} + async function selectBranches( context: TContext, branchNames: string[] diff --git a/apps/cli/test/actions/submit_action.test.ts b/apps/cli/test/actions/submit_action.test.ts index 6299c138e..99314ff93 100644 --- a/apps/cli/test/actions/submit_action.test.ts +++ b/apps/cli/test/actions/submit_action.test.ts @@ -2,6 +2,7 @@ import { expect, use } from 'chai'; import chaiAsPromised from 'chai-as-promised'; import { inferPRBody } from '../../src/actions/submit/pr_body'; import { getPRTitle } from '../../src/actions/submit/pr_title'; +import { updatePrBodyFooter } from '../../src/actions/submit/submit_action'; import { validateNoEmptyBranches } from '../../src/actions/submit/validate_branches'; import { BasicScene } from '../lib/scenes/basic_scene'; import { configureTest } from '../lib/utils/configure_test'; @@ -119,3 +120,48 @@ for (const scene of [new BasicScene()]) { }); }); } + +describe('updatePrBodyFooter', () => { + const newFooter = ` + #### PR Dependency Tree + + + * **PR #83** 👈 + + This tree was auto-generated by [Charcoal](https://github.com/danerwilliams/charcoal) + `; + + describe('when there is no existing body', () => { + it('adds the footer', () => { + const updatedPrBodyFooter = updatePrBodyFooter(undefined, newFooter); + + expect(newFooter).equal(updatedPrBodyFooter); + }); + }); + + describe('when there is already a body', () => { + it('replaces the footer', () => { + const existingBody = ` + Fix issue where dependency tree prints multiple times + + **Changes In This Pull Request:** + + **Test Plan:** + + ![CleanShot 2024-02-08 at 21 18 49@2x](https://github.com/danerwilliams/charcoal/assets/22798229/3821e8dc-d6df-4e1d-bd4b-2ed5ccf7aec9) + + + #### PR Dependency Tree + + + * **PR #83** 👈 + + This tree was auto-generated by [Charcoal](https://github.com/danerwilliams/charcoal) + `; + + const updatedPrBody = updatePrBodyFooter(existingBody, newFooter); + + expect(existingBody).equal(updatedPrBody); + }); + }); +});