Skip to content

Commit

Permalink
Fix dependency printing issue (#83)
Browse files Browse the repository at this point in the history
  • Loading branch information
danerwilliams committed Feb 9, 2024
1 parent 58495fb commit ef17ffc
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 10 deletions.
34 changes: 24 additions & 10 deletions apps/cli/src/actions/submit/submit_action.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
)}'
`
);

Expand All @@ -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[]
Expand Down
46 changes: 46 additions & 0 deletions apps/cli/test/actions/submit_action.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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);
});
});
});

0 comments on commit ef17ffc

Please sign in to comment.