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
3 changes: 3 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@ module.exports = {
memberSyntaxSortOrder: ['none', 'all', 'multiple', 'single'],
},
],

// mdast is a types only package `@types/mdast`
'import/no-unresolved': ['error', { ignore: ['^mdast$'] }],
'import/order': [
'error',
{
Expand Down
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
90 changes: 90 additions & 0 deletions lib/platform/github/massage-markdown-links.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
import { getName } from '../../../test/util';
import { massageMarkdownLinks } from './massage-markdown-links';

describe(getName(), () => {
it('performs multiple replacements', () => {
const input = [
'Link [foo/bar#1](https://github.com/foo/bar/pull/1) points to https://github.com/foo/bar/pull/1.',
'URL https://github.com/foo/bar/pull/1 becomes [foo/bar#1](https://github.com/foo/bar/pull/1).',
].join('\n');
const res = massageMarkdownLinks(input);
expect(res).toEqual(
[
'Link [foo/bar#1](https://togithub.com/foo/bar/pull/1) points to [https://github.com/foo/bar/pull/1](https://togithub.com/foo/bar/pull/1).',
'URL [https://github.com/foo/bar/pull/1](https://togithub.com/foo/bar/pull/1) becomes [foo/bar#1](https://togithub.com/foo/bar/pull/1).',
].join('\n')
);
});

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);
}
);
});
80 changes: 80 additions & 0 deletions lib/platform/github/massage-markdown-links.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
import type { Content } from 'mdast';
import remark from 'remark';
import type { Plugin, Transformer } from 'unified';
import { logger } from '../../logger';
import { hasKey } from '../../util/object';

interface UrlMatch {
start: number;
end: number;
replaceTo: string;
}

viceice marked this conversation as resolved.
Show resolved Hide resolved
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 input.replace(/(?:to)?github\.com/i, 'togithub.com');
}

function collectLinkPosition(input: string, matches: UrlMatch[]): Plugin {
const transformer = (tree: Content): void => {
const startOffset: number = tree.position.start.offset;
const endOffset: number = tree.position.end.offset;

if (tree.type === 'link') {
const substr = input.slice(startOffset, endOffset);
const url: string = tree.url;
const offset: number = startOffset + substr.lastIndexOf(url);
if (urlRegex.test(url)) {
matches.push({
start: offset,
end: offset + url.length,
replaceTo: massageLink(url),
});
}
} else if (tree.type === 'text') {
let text: string = tree.value;
let match = urlRegex.exec(text);
let currentOffset = 0;
while (match) {
const [url] = match;

currentOffset += match.index;
const start = startOffset + currentOffset;

currentOffset += url.length;
const end = startOffset + currentOffset;

const newUrl = massageLink(url);
matches.push({ start, end, replaceTo: `[${url}](${newUrl})` });

text = text.slice(currentOffset);
match = urlRegex.exec(text);
}
} else if (hasKey('children', tree)) {
tree.children.forEach((child: Content) => {
transformer(child);
});
}
};

return () => transformer as Transformer;
}

export function massageMarkdownLinks(content: string): string {
try {
const rightSpaces = content.replace(content.trimRight(), '');
const matches: UrlMatch[] = [];
remark().use(collectLinkPosition(content, matches)).processSync(content);
const result = matches.reduceRight((acc, { start, end, replaceTo }) => {
const leftPart = acc.slice(0, start);
const rightPart = acc.slice(end);
return leftPart + replaceTo + rightPart;
}, content);
return result.trimRight() + rightSpaces;
} catch (err) /* istanbul ignore next */ {
logger.warn({ err }, `Unable to massage markdown text`);
return content;
}
}