From 785ff84c318b1443505614bea35be21f7a7805a5 Mon Sep 17 00:00:00 2001 From: Rhys Arkins <rhys@arkins.net> Date: Wed, 25 Jul 2018 19:34:01 +0200 Subject: [PATCH] feat(github): replace github links with renovatebot redirects Converts to HTML then replaces hrefs to github.com with renovatebot.com/gh/. Skips if non-github.com endpoint detected. Closes #1804 --- lib/platform/github/index.js | 26 +++++++++++++------ .../github/__snapshots__/index.spec.js.snap | 2 +- test/platform/github/index.spec.js | 8 ++++++ 3 files changed, 27 insertions(+), 9 deletions(-) diff --git a/lib/platform/github/index.js b/lib/platform/github/index.js index 9f7ba21274..8e958d3b01 100644 --- a/lib/platform/github/index.js +++ b/lib/platform/github/index.js @@ -4,9 +4,14 @@ const moment = require('moment'); const openpgp = require('openpgp'); const delay = require('delay'); const path = require('path'); +const showdown = require('showdown'); + const get = require('./gh-got-wrapper'); const endpoints = require('../../util/endpoints'); +const converter = new showdown.Converter(); +converter.setFlavor('github'); + let config = {}; module.exports = { @@ -1110,15 +1115,20 @@ async function mergePr(prNo, branchName) { } function getPrBody(input) { - let prBody = input.replace( - /(https?:\/\/github.com\/[^/]*\/[^/]*\/(issues|pull)\/\w+)/g, - '`$1`' - ); - prBody = prBody.replace( - /]\(https?:\/\/github.com\/[a-z\d](?:[a-z\d]|-(?=[a-z\d])){0,38}\)/gi, - ']' + const endpoint = process.env.GITHUB_ENDPOINT; + if (endpoint && !endpoint.startsWith('https://api.github.com')) { + return input.substring(0, 60000); + } + return ( + converter + .makeHtml(input) + // to be safe, replace all github.com links with renovatebot redirector + .replace( + /href="https?:\/\/github.com\//g, + 'href="https://renovatebot.com/gh/' + ) + .substring(0, 60000) ); - return prBody.substring(0, 60000); } // Generic File operations diff --git a/test/platform/github/__snapshots__/index.spec.js.snap b/test/platform/github/__snapshots__/index.spec.js.snap index 161db623cc..bec94cc416 100644 --- a/test/platform/github/__snapshots__/index.spec.js.snap +++ b/test/platform/github/__snapshots__/index.spec.js.snap @@ -454,7 +454,7 @@ Object { } `; -exports[`platform/github getPrBody(input) returns updated pr body 1`] = `"\`https://github.com/foo/bar/issues/5\` plus also [a link](\`https://github.com/foo/bar/issues/5\`)"`; +exports[`platform/github getPrBody(input) returns updated pr body 1`] = `"<p><a href=\\"https://renovatebot.com/gh/foo/bar/issues/5\\">https://github.com/foo/bar/issues/5</a> plus also <a href=\\"https://renovatebot.com/gh/foo/bar/issues/5\\">a link</a></p>"`; exports[`platform/github getPrFiles() returns files 1`] = ` Array [ diff --git a/test/platform/github/index.spec.js b/test/platform/github/index.spec.js index 11a6e87184..a9c7f9d657 100644 --- a/test/platform/github/index.spec.js +++ b/test/platform/github/index.spec.js @@ -1487,6 +1487,14 @@ describe('platform/github', () => { 'https://github.com/foo/bar/issues/5 plus also [a link](https://github.com/foo/bar/issues/5)'; expect(github.getPrBody(input)).toMatchSnapshot(); }); + it('returns not-updated pr body for GHE', () => { + const endpoint = process.env.GITHUB_ENDPOINT; + process.env.GITHUB_ENDPOINT = 'https://github.mycompany.com'; + const input = + 'https://github.com/foo/bar/issues/5 plus also [a link](https://github.com/foo/bar/issues/5)'; + expect(github.getPrBody(input)).toEqual(input); + process.env.GITHUB_ENDPOINT = endpoint; + }); }); describe('mergePr(prNo) - autodetection', () => { beforeEach(async () => { -- GitLab