From aae0097656154f1ff872f90fde16fedbdecd799c Mon Sep 17 00:00:00 2001 From: Matthias Rolke Date: Fri, 26 Jul 2024 13:27:05 +0200 Subject: [PATCH] fix: parse Bitbucket Server Git URLs Bitbucket Server Git URLs are different to GitHub URLs and might include a leading 'scp' in the URL path --- src/common.ts | 4 +++- test/common.test.ts | 17 +++++++++++++++-- 2 files changed, 18 insertions(+), 3 deletions(-) diff --git a/src/common.ts b/src/common.ts index 70367d7..782b16a 100644 --- a/src/common.ts +++ b/src/common.ts @@ -84,7 +84,9 @@ export function extractGithubRepoName(remoteUrl: string): string { if (url.slice(-4) === '.git') { url = url.slice(0, -4); } - return url; + // the last two parts of the url path + const repoName = url.split('/').slice(-2).join('/'); + return repoName; } export async function retryWithTimeout( diff --git a/test/common.test.ts b/test/common.test.ts index 4c07372..487503d 100644 --- a/test/common.test.ts +++ b/test/common.test.ts @@ -1,6 +1,6 @@ -import { describe, it } from 'mocha'; import { expect } from 'chai'; -import { retryWithTimeout } from '../src/common'; +import { describe, it } from 'mocha'; +import { extractGithubRepoName, retryWithTimeout } from '../src/common'; describe('common', () => { describe('retryWithTimeout', () => { @@ -72,4 +72,17 @@ describe('common', () => { expect(i).to.equal(2); }); }); + + describe('extractGithubRepoName', () => { + it('should parse a GitHub repo URL', () => { + expect(extractGithubRepoName('https://github.com/orgname/reponame.git')).to.deep.equal('orgname/reponame'); + expect(extractGithubRepoName('git@github.com:orgname/reponame.git')).to.deep.equal('orgname/reponame'); + }); + it('should parse a Bitbucket Server repo URL', () => { + expect(extractGithubRepoName('https://git.example.org/scm/orgname/reponame.git')).to.deep.equal( + 'orgname/reponame', + ); + expect(extractGithubRepoName('ssh://git@git.example.org/orgname/reponame.git')).to.deep.equal('orgname/reponame'); + }); + }); });