From 9f4c735b5bb8b77404819b9260afeb604d936f9b Mon Sep 17 00:00:00 2001 From: Migot Florian Date: Fri, 28 Jan 2022 12:10:40 +0100 Subject: [PATCH] fix: Update creation of tag link in default success template (#90) fixes: #82 --- .gitignore | 1 + lib/getRepoInfo.js | 3 ++- lib/success.js | 10 +++++++--- test/getRepoInfo.test.js | 21 ++++++++++++++------- 4 files changed, 24 insertions(+), 11 deletions(-) diff --git a/.gitignore b/.gitignore index 3c3629e..944c283 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ node_modules +.idea/ diff --git a/lib/getRepoInfo.js b/lib/getRepoInfo.js index d91ea0a..854057b 100644 --- a/lib/getRepoInfo.js +++ b/lib/getRepoInfo.js @@ -15,6 +15,7 @@ module.exports = repositoryUrl => { .substring(1) // remove leading "/" .replace('.git', '') // remove .git .replace(':', '') // remove any colons from path (present in github for example) + const hostname = parsedUrl.hostname const URL = `https://${parsedUrl.host}/${path}` - return { path, URL } + return { path, URL, hostname } } diff --git a/lib/success.js b/lib/success.js index cbb5d73..6212012 100644 --- a/lib/success.js +++ b/lib/success.js @@ -94,6 +94,10 @@ module.exports = async (pluginConfig, context) => { if (repo.path) { const gitTag = nextRelease.gitTag + const gitTagPrefix = repo.hostname.startsWith('gitlab') + ? '/-/releases/' + : '/releases/tag/' + const gitTagUrl = repo.URL + gitTagPrefix + gitTag slackMessage.attachments = [ { @@ -104,9 +108,9 @@ module.exports = async (pluginConfig, context) => { elements: [ { type: 'mrkdwn', - text: `:package: *<${repo.URL}|${repo.path}>:* <${ - repo.URL - }/releases/tag/${gitTag}|${gitTag}>` + text: `:package: *<${repo.URL}|${ + repo.path + }>:* <${gitTagUrl}|${gitTag}>` } ] } diff --git a/test/getRepoInfo.test.js b/test/getRepoInfo.test.js index 70dfa3c..47fa8ce 100644 --- a/test/getRepoInfo.test.js +++ b/test/getRepoInfo.test.js @@ -1,11 +1,12 @@ const assert = require('assert') const getRepoInfo = require('../lib/getRepoInfo') -const runAssert = (repoUrl, path, url) => { +const runAssert = (repoUrl, path, url, hostname) => { const actual = getRepoInfo(repoUrl) assert.equal(path, actual.path) assert.equal(url, actual.URL) + assert.equal(hostname, actual.hostname) } describe('test getRepoInfo', () => { @@ -13,41 +14,47 @@ describe('test getRepoInfo', () => { const repositoryUrl = 'ssh://git@github.com:hello/world.git' const expectedPath = 'hello/world' const expectedUrl = 'https://github.com/hello/world' - runAssert(repositoryUrl, expectedPath, expectedUrl) + const expectedHostname = 'github.com' + runAssert(repositoryUrl, expectedPath, expectedUrl, expectedHostname) }) it('should work for bitbucket', () => { const repositoryUrl = 'ssh://hg@bitbucket.org/hello/world.git' const expectedPath = 'hello/world' const expectedUrl = 'https://bitbucket.org/hello/world' - runAssert(repositoryUrl, expectedPath, expectedUrl) + const expectedHostname = 'bitbucket.org' + runAssert(repositoryUrl, expectedPath, expectedUrl, expectedHostname) }) it('should work for gitlab', () => { const repositoryUrl = 'ssh://git@gitlab.com:hello/world.git' const expectedPath = 'hello/world' const expectedUrl = 'https://gitlab.com/hello/world' - runAssert(repositoryUrl, expectedPath, expectedUrl) + const expectedHostname = 'gitlab.com' + runAssert(repositoryUrl, expectedPath, expectedUrl, expectedHostname) }) it('should work for repo url with https', () => { const repositoryUrl = 'https://github.com/hello/world.git' const expectedPath = 'hello/world' const expectedUrl = 'https://github.com/hello/world' - runAssert(repositoryUrl, expectedPath, expectedUrl) + const expectedHostname = 'github.com' + runAssert(repositoryUrl, expectedPath, expectedUrl, expectedHostname) }) it('should work for repo url with git@', () => { const repositoryUrl = 'git@github.com:hello/world.git' const expectedPath = 'hello/world' const expectedUrl = 'https://github.com/hello/world' - runAssert(repositoryUrl, expectedPath, expectedUrl) + const expectedHostname = 'github.com' + runAssert(repositoryUrl, expectedPath, expectedUrl, expectedHostname) }) it('should work for repo url with other TLD', () => { const repositoryUrl = 'git@github.pl:hello/world.git' const expectedPath = 'hello/world' const expectedUrl = 'https://github.pl/hello/world' - runAssert(repositoryUrl, expectedPath, expectedUrl) + const expectedHostname = 'github.pl' + runAssert(repositoryUrl, expectedPath, expectedUrl, expectedHostname) }) })