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(github): Massage links to issues, pulls or discussions #11214

Merged
2 changes: 1 addition & 1 deletion lib/platform/github/__snapshots__/index.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -7772,7 +7772,7 @@ Array [
]
`;

exports[`platform/github/index massageMarkdown(input) returns updated pr body 1`] = `"https://github.com/foo/bar/issues/5 plus also [a link](https://togithub.com/foo/bar/issues/5)"`;
exports[`platform/github/index massageMarkdown(input) returns updated pr body 1`] = `"[https://github.com/foo/bar/issues/5](https://togithub.com/foo/bar/issues/5) plus also [a link](https://togithub.com/foo/bar/issues/5)"`;

exports[`platform/github/index mergePr(prNo) - autodetection should give up 1`] = `
Array [
Expand Down
3 changes: 2 additions & 1 deletion lib/platform/github/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ import {
repoInfoQuery,
vulnerabilityAlertsQuery,
} from './graphql';
import { massageMarkdownLinks } from './massage-markdown-links';
import {
BranchProtection,
CombinedBranchStatus,
Expand Down Expand Up @@ -1569,7 +1570,7 @@ export function massageMarkdown(input: string): string {
if (config.isGhe) {
return smartTruncate(input, 60000);
}
const massagedInput = input
const massagedInput = massageMarkdownLinks(input)
// to be safe, replace all github.com links with renovatebot redirector
.replace(/href="https?:\/\/github.com\//g, 'href="https://togithub.com/')
.replace(/]\(https:\/\/github\.com\//g, '](https://togithub.com/')
Expand Down
76 changes: 76 additions & 0 deletions lib/platform/github/massage-markdown-links.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
import { getName } from '../../../test/util';
import { massageMarkdownLinks } from './massage-markdown-links';

describe(getName(), () => {
test.each`
input
${'github.com'}
${'github.com/foo/bar'}
${'github.com/foo/bar/'}
${'github.com/foo/bar/discussions'}
${'github.com/foo/bar/issues'}
${'github.com/foo/bar/pull'}
${'github.com/foo/bar/discussions/'}
${'github.com/foo/bar/issues/'}
${'github.com/foo/bar/pull/'}
${'www.github.com'}
${'www.github.com/foo/bar'}
${'www.github.com/foo/bar/'}
${'www.github.com/foo/bar/discussions'}
${'www.github.com/foo/bar/issues'}
${'www.github.com/foo/bar/pull'}
${'www.github.com/foo/bar/discussions/'}
${'www.github.com/foo/bar/issues/'}
${'www.github.com/foo/bar/pull/'}
${'https://github.com'}
${'https://github.com/foo/bar'}
${'https://github.com/foo/bar/'}
${'https://github.com/foo/bar/discussions'}
${'https://github.com/foo/bar/issues'}
${'https://github.com/foo/bar/pull'}
${'https://github.com/foo/bar/discussions/'}
${'https://github.com/foo/bar/issues/'}
${'https://github.com/foo/bar/pull/'}
${'api.github.com'}
${'togithub.com'}
${'www.togithub.com'}
${'https://togithub.com/foo/bar/releases/tag/v0.20.3'}
${'https://togithub.com/foo/bar/compare/v0.20.2...v0.20.3'}
`('Unchanged: $input', ({ input }: { input: string }) => {
const inputText = `Foo ${input}, bar.`;
expect(massageMarkdownLinks(inputText)).toEqual(inputText);

const inputLink = `[foobar](${input})`;
expect(massageMarkdownLinks(inputLink)).toEqual(inputLink);
});

test.each`
input | output
${'github.com/foo/bar/discussions/1'} | ${'[github.com/foo/bar/discussions/1](togithub.com/foo/bar/discussions/1)'}
${'github.com/foo/bar/issues/1'} | ${'[github.com/foo/bar/issues/1](togithub.com/foo/bar/issues/1)'}
${'github.com/foo/bar/pull/1'} | ${'[github.com/foo/bar/pull/1](togithub.com/foo/bar/pull/1)'}
${'www.github.com/foo/bar/discussions/1'} | ${'[www.github.com/foo/bar/discussions/1](www.togithub.com/foo/bar/discussions/1)'}
${'www.github.com/foo/bar/issues/1'} | ${'[www.github.com/foo/bar/issues/1](www.togithub.com/foo/bar/issues/1)'}
${'www.github.com/foo/bar/pull/1'} | ${'[www.github.com/foo/bar/pull/1](www.togithub.com/foo/bar/pull/1)'}
${'https://github.com/foo/bar/discussions/1'} | ${'[https://github.com/foo/bar/discussions/1](https://togithub.com/foo/bar/discussions/1)'}
${'https://github.com/foo/bar/issues/1'} | ${'[https://github.com/foo/bar/issues/1](https://togithub.com/foo/bar/issues/1)'}
${'https://github.com/foo/bar/pull/1'} | ${'[https://github.com/foo/bar/pull/1](https://togithub.com/foo/bar/pull/1)'}
${'https://github.com/foo/bar/discussions/1#comment-123'} | ${'[https://github.com/foo/bar/discussions/1#comment-123](https://togithub.com/foo/bar/discussions/1#comment-123)'}
${'https://github.com/foo/bar/issues/1#comment-123'} | ${'[https://github.com/foo/bar/issues/1#comment-123](https://togithub.com/foo/bar/issues/1#comment-123)'}
${'https://github.com/foo/bar/pull/1#comment-123'} | ${'[https://github.com/foo/bar/pull/1#comment-123](https://togithub.com/foo/bar/pull/1#comment-123)'}
${'[github.com/foo/bar/discussions/1](github.com/foo/bar/discussions/1)'} | ${'[github.com/foo/bar/discussions/1](togithub.com/foo/bar/discussions/1)'}
${'[github.com/foo/bar/issues/1](github.com/foo/bar/issues/1)'} | ${'[github.com/foo/bar/issues/1](togithub.com/foo/bar/issues/1)'}
${'[github.com/foo/bar/pull/1](github.com/foo/bar/pull/1)'} | ${'[github.com/foo/bar/pull/1](togithub.com/foo/bar/pull/1)'}
${'[www.github.com/foo/bar/discussions/1](www.github.com/foo/bar/discussions/1)'} | ${'[www.github.com/foo/bar/discussions/1](www.togithub.com/foo/bar/discussions/1)'}
${'[www.github.com/foo/bar/issues/1](www.github.com/foo/bar/issues/1)'} | ${'[www.github.com/foo/bar/issues/1](www.togithub.com/foo/bar/issues/1)'}
${'[www.github.com/foo/bar/pull/1](www.github.com/foo/bar/pull/1)'} | ${'[www.github.com/foo/bar/pull/1](www.togithub.com/foo/bar/pull/1)'}
${'[https://github.com/foo/bar/discussions/1](https://github.com/foo/bar/discussions/1)'} | ${'[https://github.com/foo/bar/discussions/1](https://togithub.com/foo/bar/discussions/1)'}
${'[https://github.com/foo/bar/issues/1](https://github.com/foo/bar/issues/1)'} | ${'[https://github.com/foo/bar/issues/1](https://togithub.com/foo/bar/issues/1)'}
${'[https://github.com/foo/bar/pull/1](https://github.com/foo/bar/pull/1)'} | ${'[https://github.com/foo/bar/pull/1](https://togithub.com/foo/bar/pull/1)'}
`(
'$input -> $output',
({ input, output }: { input: string; output: string }) => {
expect(massageMarkdownLinks(input)).toEqual(output);
}
);
});
56 changes: 56 additions & 0 deletions lib/platform/github/massage-markdown-links.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import findAndReplace from 'mdast-util-find-and-replace';
import remark from 'remark';
import type { Plugin, Transformer } from 'unified';
// eslint-disable-next-line import/no-unresolved
import type { Node } from 'unist';
import visit from 'unist-util-visit';
import { logger } from '../../logger';

const urlRegex =
/(?:https?:)?(?:\/\/)?(?:www\.)?(?<!api\.)(?:to)?github\.com\/[-_a-z0-9]+\/[-_a-z0-9]+\/(?:discussions|issues|pull)\/[0-9]+(?:#[-_a-z0-9]+)?/i;

function massageLink(input: string): string {
return urlRegex.test(input)
? input.replace(/(?:to)?github\.com/, 'togithub.com')
: /* istanbul ignore next */
input;
}

function linkifyText(url: string): Node | boolean {
const newUrl = massageLink(url);
if (newUrl !== url) {
const content = { type: 'text', value: url };
return {
type: 'link',
url: newUrl,
title: null,
children: [content],
} as Node;
}
return false;
}

function transformer(tree: Node): void {
findAndReplace(tree, urlRegex, linkifyText, {
ignore: ['link', 'linkReference'],
});
visit(tree, 'link', (node: any) => {
if (node?.url) {
// eslint-disable-next-line no-param-reassign
node.url = massageLink(node.url);
}
});
}

const githubExtra: Plugin<any> = (): Transformer => transformer;

export function massageMarkdownLinks(content: string): string {
try {
const output = remark().use(githubExtra).processSync(content);
const rightSpaces = content.replace(content.trimRight(), '');
return output.toString().trimRight() + rightSpaces;
} catch (err) /* istanbul ignore next */ {
logger.warn({ err }, `Unable to massage markdown text`);
return content;
}
}
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,7 @@
"luxon": "2.0.1",
"markdown-it": "12.2.0",
"markdown-table": "2.0.0",
"mdast-util-find-and-replace": "1.1.1",
"minimatch": "3.0.4",
"moo": "0.5.1",
"node-html-parser": "3.3.6",
Expand All @@ -187,6 +188,7 @@
"simple-git": "2.42.0",
"slugify": "1.6.0",
"traverse": "0.6.6",
"unist-util-visit": "2.0.3",
"upath": "2.0.1",
"url-join": "4.0.1",
"validate-npm-package-name": "3.0.0",
Expand Down
4 changes: 2 additions & 2 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -6507,7 +6507,7 @@ matcher@^3.0.0:
dependencies:
escape-string-regexp "^4.0.0"

mdast-util-find-and-replace@^1.0.0:
mdast-util-find-and-replace@1.1.1, mdast-util-find-and-replace@^1.0.0:
version "1.1.1"
resolved "https://registry.yarnpkg.com/mdast-util-find-and-replace/-/mdast-util-find-and-replace-1.1.1.tgz#b7db1e873f96f66588c321f1363069abf607d1b5"
integrity sha512-9cKl33Y21lyckGzpSmEQnIDjEfeeWelN5s1kUW1LwdB0Fkuq2u+4GdqcGEygYxJE8GVqCl0741bYXHgamfWAZA==
Expand Down Expand Up @@ -9455,7 +9455,7 @@ unist-util-visit-parents@^3.0.0:
"@types/unist" "^2.0.0"
unist-util-is "^4.0.0"

unist-util-visit@^2.0.0:
unist-util-visit@2.0.3, unist-util-visit@^2.0.0:
version "2.0.3"
resolved "https://registry.yarnpkg.com/unist-util-visit/-/unist-util-visit-2.0.3.tgz#c3703893146df47203bb8a9795af47d7b971208c"
integrity sha512-iJ4/RczbJMkD0712mGktuGpm/U4By4FfDonL7N/9tATGIF4imikjOuagyMY53tnZq3NP6BcmlrHhEKAfGWjh7Q==
Expand Down