From 038dba67100cbff2f2b38a72a9448c49e7a84711 Mon Sep 17 00:00:00 2001 From: lisiur Date: Wed, 22 May 2024 14:43:25 +0800 Subject: [PATCH 1/5] fix: Update cleanUrl method to handle ssh URLs --- app/src/lib/vbranches/types.ts | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/app/src/lib/vbranches/types.ts b/app/src/lib/vbranches/types.ts index 8f7bed67c..e232a1938 100644 --- a/app/src/lib/vbranches/types.ts +++ b/app/src/lib/vbranches/types.ts @@ -381,6 +381,13 @@ export class BaseBranch { private cleanUrl(url: string): string { if (url.startsWith('http')) { return url.replace('.git', '').trim(); + } else if (url.startsWith('ssh')) { + url = url.replace('ssh://git@', ''); + const [host, ...paths] = url.split('/'); + const path = paths.join('/').replace('.git', ''); + const protocol = /\d+\.\d+\.\d+\.\d+/.test(host) ? 'http' : 'https'; + const [hostname, _port] = host.split(':'); + return `${protocol}://${hostname}/${path}` } else { return url.replace(':', '/').replace('git@', 'https://').replace('.git', '').trim(); } From f52c5ae358900b9b523237e3a6756a7ea615ac72 Mon Sep 17 00:00:00 2001 From: LisiurDay Date: Wed, 22 May 2024 14:50:41 +0800 Subject: [PATCH 2/5] lint --- app/src/lib/vbranches/types.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/src/lib/vbranches/types.ts b/app/src/lib/vbranches/types.ts index e232a1938..4b4fdcf2f 100644 --- a/app/src/lib/vbranches/types.ts +++ b/app/src/lib/vbranches/types.ts @@ -387,7 +387,7 @@ export class BaseBranch { const path = paths.join('/').replace('.git', ''); const protocol = /\d+\.\d+\.\d+\.\d+/.test(host) ? 'http' : 'https'; const [hostname, _port] = host.split(':'); - return `${protocol}://${hostname}/${path}` + return `${protocol}://${hostname}/${path}`; } else { return url.replace(':', '/').replace('git@', 'https://').replace('.git', '').trim(); } From b779f6fbbfb5fe4c66ca500a37042182a913f1e9 Mon Sep 17 00:00:00 2001 From: lisiur Date: Wed, 22 May 2024 16:08:59 +0800 Subject: [PATCH 3/5] Extract cleanUrl and add tests --- app/src/lib/utils/url.test.ts | 20 ++++++++++++++++++++ app/src/lib/utils/url.ts | 16 ++++++++++++++++ app/src/lib/vbranches/types.ts | 21 +++------------------ 3 files changed, 39 insertions(+), 18 deletions(-) create mode 100644 app/src/lib/utils/url.test.ts diff --git a/app/src/lib/utils/url.test.ts b/app/src/lib/utils/url.test.ts new file mode 100644 index 000000000..b242a1ce8 --- /dev/null +++ b/app/src/lib/utils/url.test.ts @@ -0,0 +1,20 @@ +import { cleanUrl } from '$lib/utils/url'; +import { describe, expect, test } from 'vitest'; + +describe.concurrent('cleanUrl', () => { + test('it should handle url starts with http(s)?', () => { + expect(cleanUrl('https://github.com/user/repo.git')).toEqual('https://github.com/user/repo'); + }); + + test('it should handle complete ssh url with domain name', () => { + expect(cleanUrl('ssh://git@github.com:22/user/repo.git')).toEqual('https://github.com/user/repo'); + }); + + test('it should handle complete ssh url with ip', () => { + expect(cleanUrl('ssh://git@192.168.1.1:22/user/repo.git')).toEqual('http://192.168.1.1/user/repo'); + }); + + test('it should handle SCP-like url', () => { + expect(cleanUrl('git@github.com:user/repo.git')).toEqual('https://github.com/user/repo'); + }); +}); diff --git a/app/src/lib/utils/url.ts b/app/src/lib/utils/url.ts index 4258d7e40..d22c365ee 100644 --- a/app/src/lib/utils/url.ts +++ b/app/src/lib/utils/url.ts @@ -22,3 +22,19 @@ export function openExternalUrl(href: string) { throw e; } } + +// turn a git remote url into a web url (github, gitlab, bitbucket, etc) +export function cleanUrl(url: string): string { + if (url.startsWith('http')) { + return url.replace('.git', '').trim(); + } else if (url.startsWith('ssh')) { + url = url.replace('ssh://git@', ''); + const [host, ...paths] = url.split('/'); + const path = paths.join('/').replace('.git', ''); + const protocol = /\d+\.\d+\.\d+\.\d+/.test(host) ? 'http' : 'https'; + const [hostname, _port] = host.split(':'); + return `${protocol}://${hostname}/${path}`; + } else { + return url.replace(':', '/').replace('git@', 'https://').replace('.git', '').trim(); + } +} diff --git a/app/src/lib/vbranches/types.ts b/app/src/lib/vbranches/types.ts index 4b4fdcf2f..19d13ec64 100644 --- a/app/src/lib/vbranches/types.ts +++ b/app/src/lib/vbranches/types.ts @@ -3,6 +3,7 @@ import { splitMessage } from '$lib/utils/commitMessage'; import { hashCode } from '$lib/utils/string'; import { isDefined, notNull } from '$lib/utils/typeguards'; import { Type, Transform } from 'class-transformer'; +import { cleanUrl } from '$lib/utils/url'; export type ChangeType = /// Entry does not exist in old version @@ -370,27 +371,11 @@ export class BaseBranch { } get pushRepoBaseUrl(): string { - return this.cleanUrl(this.pushRemoteUrl); + return cleanUrl(this.pushRemoteUrl); } get repoBaseUrl(): string { - return this.cleanUrl(this.remoteUrl); - } - - // turn a git remote url into a web url (github, gitlab, bitbucket, etc) - private cleanUrl(url: string): string { - if (url.startsWith('http')) { - return url.replace('.git', '').trim(); - } else if (url.startsWith('ssh')) { - url = url.replace('ssh://git@', ''); - const [host, ...paths] = url.split('/'); - const path = paths.join('/').replace('.git', ''); - const protocol = /\d+\.\d+\.\d+\.\d+/.test(host) ? 'http' : 'https'; - const [hostname, _port] = host.split(':'); - return `${protocol}://${hostname}/${path}`; - } else { - return url.replace(':', '/').replace('git@', 'https://').replace('.git', '').trim(); - } + return cleanUrl(this.remoteUrl); } commitUrl(commitId: string): string | undefined { From bcdd5f68fe160d59aa0a0b05a0fe679c720261b4 Mon Sep 17 00:00:00 2001 From: lisiur Date: Wed, 22 May 2024 16:37:18 +0800 Subject: [PATCH 4/5] lint --- app/src/lib/utils/url.test.ts | 8 ++++++-- app/src/lib/utils/url.ts | 24 ++++++++++++------------ 2 files changed, 18 insertions(+), 14 deletions(-) diff --git a/app/src/lib/utils/url.test.ts b/app/src/lib/utils/url.test.ts index b242a1ce8..1dcefbf09 100644 --- a/app/src/lib/utils/url.test.ts +++ b/app/src/lib/utils/url.test.ts @@ -7,11 +7,15 @@ describe.concurrent('cleanUrl', () => { }); test('it should handle complete ssh url with domain name', () => { - expect(cleanUrl('ssh://git@github.com:22/user/repo.git')).toEqual('https://github.com/user/repo'); + expect(cleanUrl('ssh://git@github.com:22/user/repo.git')).toEqual( + 'https://github.com/user/repo' + ); }); test('it should handle complete ssh url with ip', () => { - expect(cleanUrl('ssh://git@192.168.1.1:22/user/repo.git')).toEqual('http://192.168.1.1/user/repo'); + expect(cleanUrl('ssh://git@192.168.1.1:22/user/repo.git')).toEqual( + 'http://192.168.1.1/user/repo' + ); }); test('it should handle SCP-like url', () => { diff --git a/app/src/lib/utils/url.ts b/app/src/lib/utils/url.ts index d22c365ee..d818f13f5 100644 --- a/app/src/lib/utils/url.ts +++ b/app/src/lib/utils/url.ts @@ -25,16 +25,16 @@ export function openExternalUrl(href: string) { // turn a git remote url into a web url (github, gitlab, bitbucket, etc) export function cleanUrl(url: string): string { - if (url.startsWith('http')) { - return url.replace('.git', '').trim(); - } else if (url.startsWith('ssh')) { - url = url.replace('ssh://git@', ''); - const [host, ...paths] = url.split('/'); - const path = paths.join('/').replace('.git', ''); - const protocol = /\d+\.\d+\.\d+\.\d+/.test(host) ? 'http' : 'https'; - const [hostname, _port] = host.split(':'); - return `${protocol}://${hostname}/${path}`; - } else { - return url.replace(':', '/').replace('git@', 'https://').replace('.git', '').trim(); - } + if (url.startsWith('http')) { + return url.replace('.git', '').trim(); + } else if (url.startsWith('ssh')) { + url = url.replace('ssh://git@', ''); + const [host, ...paths] = url.split('/'); + const path = paths.join('/').replace('.git', ''); + const protocol = /\d+\.\d+\.\d+\.\d+/.test(host) ? 'http' : 'https'; + const [hostname, _port] = host.split(':'); + return `${protocol}://${hostname}/${path}`; + } else { + return url.replace(':', '/').replace('git@', 'https://').replace('.git', '').trim(); + } } From 31d9576991b42753a05b5f17870850d408ddd938 Mon Sep 17 00:00:00 2001 From: lisiur Date: Wed, 22 May 2024 16:44:51 +0800 Subject: [PATCH 5/5] lint --- app/src/lib/vbranches/types.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/src/lib/vbranches/types.ts b/app/src/lib/vbranches/types.ts index 19d13ec64..1235bfb2d 100644 --- a/app/src/lib/vbranches/types.ts +++ b/app/src/lib/vbranches/types.ts @@ -2,8 +2,8 @@ import 'reflect-metadata'; import { splitMessage } from '$lib/utils/commitMessage'; import { hashCode } from '$lib/utils/string'; import { isDefined, notNull } from '$lib/utils/typeguards'; -import { Type, Transform } from 'class-transformer'; import { cleanUrl } from '$lib/utils/url'; +import { Type, Transform } from 'class-transformer'; export type ChangeType = /// Entry does not exist in old version