Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix warning message duplication #53

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions __tests__/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,4 +84,19 @@ ${issueInfo}
${HIDDEN_MARKER_END}
${oldPRBodyInformation}`);
});

it('does not duplicate WARNING_MESSAGE_ABOUT_HIDDEN_MARKERS in the body when run multiple times', () => {
const oldPRBodyInformation = 'old PR description body';
const oldPRBody = `${HIDDEN_MARKER_START}Here is some old issue information${HIDDEN_MARKER_END}${oldPRBodyInformation}`;
const issueInfo = 'new info about jira task';

const firstDescription = getPRDescription(oldPRBody, issueInfo);
const secondDescription = getPRDescription(firstDescription, issueInfo);

expect(secondDescription).toEqual(`${WARNING_MESSAGE_ABOUT_HIDDEN_MARKERS}
${HIDDEN_MARKER_START}
${issueInfo}
${HIDDEN_MARKER_END}
${oldPRBodyInformation}`);
})
});
8 changes: 7 additions & 1 deletion lib/index.js

Large diffs are not rendered by default.

6 changes: 4 additions & 2 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,11 @@ const escapeRegexp = (str: string): string => {
export const getPRDescription = (oldBody: string, details: string): string => {
const hiddenMarkerStartRg = escapeRegexp(HIDDEN_MARKER_START);
const hiddenMarkerEndRg = escapeRegexp(HIDDEN_MARKER_END);
const warningMsgRg = escapeRegexp(WARNING_MESSAGE_ABOUT_HIDDEN_MARKERS);

const rg = new RegExp(`${hiddenMarkerStartRg}([\\s\\S]+)${hiddenMarkerEndRg}`, 'igm');
const bodyWithoutJiraDetails = (oldBody ?? '').replace(rg, '');
const replaceDetailsRg = new RegExp(`${hiddenMarkerStartRg}([\\s\\S]+)${hiddenMarkerEndRg}[\\s]?`, 'igm');
const replaceWarningMessageRg = new RegExp(`${warningMsgRg}[\\s]?`, 'igm');
const bodyWithoutJiraDetails = (oldBody ?? '').replace(replaceDetailsRg, '').replace(replaceWarningMessageRg, '');

return `${WARNING_MESSAGE_ABOUT_HIDDEN_MARKERS}
${HIDDEN_MARKER_START}
Expand Down