From 74ded4247d5129bebb3930515ff72a25eed69678 Mon Sep 17 00:00:00 2001 From: SethCohen <47002293+SethCohen@users.noreply.github.com> Date: Tue, 17 Jun 2025 00:10:47 -0400 Subject: [PATCH] Add option to strip PR and commit links (#48) * feat: add option to remove PR and commit links * feat(github-action): rename input from remove_pr_commit_links to remove_github_reference_links for clarity and expand functionality to include issue links fix(github-action): update removeGithubReferenceLinks function to remove all GitHub PR, commit, and issue links from the text, including markdown links resolves #41 --- .github/workflows/test.yml | 2 +- README.md | 3 +++ action.yml | 4 ++++ index.js | 21 +++++++++++++++++++++ 4 files changed, 29 insertions(+), 1 deletion(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 03a8713..0415352 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -21,4 +21,4 @@ jobs: footer_timestamp: ${{ inputs.FOOTER_TIMESTAMP }} max_description: ${{ inputs.MAX_DESCRIPTION }} reduce_headings: ${{ inputs.REDUCE_HEADINGS }} - \ No newline at end of file + remove_github_reference_links: ${{ inputs.REMOVE_GITHUB_REFERENCE_LINKS }} diff --git a/README.md b/README.md index aa910b2..c1e4d7a 100644 --- a/README.md +++ b/README.md @@ -10,6 +10,7 @@ A GitHub Action that sends a stylized Discord webhook containing the description - **Whitespace Optimization:** Reduces redundant newlines and excess spaces while preserving proper paragraph spacing. - **Mention Conversion:** Converts GitHub mentions (e.g., `@username`) into clickable GitHub profile links for easy navigation. - **Markdown Link Conversion:** +- **Link Removal (Optional):** Remove PR and commit links from the changelog. - **PR Links:** Converts pull request URLs into Markdown links (e.g., `[PR #1](https://github.com/OWNER/REPO/pull/1)`). - **Issue Links:** Converts issue URLs into Markdown links (e.g., `[Issue #1](https://github.com/OWNER/REPO/issues/1)`). - **Changelog Links:** Converts changelog comparison URLs into concise Markdown links (e.g., `[v1.0.0...v1.1.0](https://github.com/OWNER/REPO/compare/v1.0.0...v1.1.0)`). @@ -42,6 +43,7 @@ A GitHub Action that sends a stylized Discord webhook containing the description | footer_icon_url | ❌ | | String url for the webhook footer picture. | | footer_timestamp| ❌ | | Boolean to enable footer timestamp. | | max_description | ❌ | "4096" | Max length for the description. | +| remove_github_reference_links | ❌ | false | Remove any PR, commit, and issue links from the description. | | reduce_headings | ❌ | false | Converts H3 to bold, h2 to bold & underline. | ## Example Usage @@ -71,6 +73,7 @@ jobs: footer_icon_url: "https://cdn.discordapp.com/avatars/487431320314576937/bd64361e4ba6313d561d54e78c9e7171.png" footer_timestamp: true max_description: '4096' + remove_github_reference_links: true reduce_headings: true ``` diff --git a/action.yml b/action.yml index fe0e570..50b05d2 100644 --- a/action.yml +++ b/action.yml @@ -31,6 +31,10 @@ inputs: description: Max length for the description. required: false default: '4096' + remove_github_reference_links: + description: Remove any PR, commit, and issue links from the description. + required: false + default: 'false' reduce_headings: description: Converts H3 to bold, h2 to bold & underline. required: false diff --git a/index.js b/index.js index 9137c43..e8758bf 100644 --- a/index.js +++ b/index.js @@ -37,6 +37,23 @@ const convertMentionsToLinks = (text) => text.replace( (match, name) => `[@${name}](https://github.com/${name})` ); +/** + * Removes any GitHub PR, commit, or issue links from the text, including markdown links. + * @param {string} text The input text. + * @returns {string} The text without GitHub PR and commit links. + */ +const removeGithubReferenceLinks = (text) => text + // Remove markdown links to PRs, commits, and issues + .replace(/\[[^\]]*\]\(https:\/\/github\.com\/[^(\s)]+\/pull\/\d+\)/g, '') + .replace(/\[[^\]]*\]\(https:\/\/github\.com\/[^(\s)]+\/commit\/\w+\)/g, '') + .replace(/\[[^\]]*\]\(https:\/\/github\.com\/[^(\s)]+\/issues\/\d+\)/g, '') + // Remove bare PR, commit, and issue URLs + .replace(/https:\/\/github\.com\/[^(\s)]+\/pull\/\d+/g, '') + .replace(/https:\/\/github\.com\/[^(\s)]+\/commit\/\w+/g, '') + .replace(/https:\/\/github\.com\/[^(\s)]+\/issues\/\d+/g, '') + // Remove empty parentheses left behind + .replace(/\(\s*\)/g, ''); + /** * Reduces headings to a smaller format if 'reduce_headings' is enabled. * Converts H3 to bold+underline, H2 to bold. @@ -82,6 +99,10 @@ const formatDescription = (description) => { let edit = removeCarriageReturn(description); edit = removeHTMLComments(edit); edit = reduceNewlines(edit); + + if (core.getBooleanInput('remove_github_reference_links')) { + edit = removeGithubReferenceLinks(edit); + } edit = convertMentionsToLinks(edit); edit = convertLinksToMarkdown(edit); edit = edit.trim();