mirror of
https://github.com/gitbutlerapp/gitbutler.git
synced 2024-12-18 14:31:30 +03:00
Extract cleanUrl and add tests
This commit is contained in:
parent
f52c5ae358
commit
b779f6fbbf
20
app/src/lib/utils/url.test.ts
Normal file
20
app/src/lib/utils/url.test.ts
Normal file
@ -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');
|
||||||
|
});
|
||||||
|
});
|
@ -22,3 +22,19 @@ export function openExternalUrl(href: string) {
|
|||||||
throw e;
|
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();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@ -3,6 +3,7 @@ import { splitMessage } from '$lib/utils/commitMessage';
|
|||||||
import { hashCode } from '$lib/utils/string';
|
import { hashCode } from '$lib/utils/string';
|
||||||
import { isDefined, notNull } from '$lib/utils/typeguards';
|
import { isDefined, notNull } from '$lib/utils/typeguards';
|
||||||
import { Type, Transform } from 'class-transformer';
|
import { Type, Transform } from 'class-transformer';
|
||||||
|
import { cleanUrl } from '$lib/utils/url';
|
||||||
|
|
||||||
export type ChangeType =
|
export type ChangeType =
|
||||||
/// Entry does not exist in old version
|
/// Entry does not exist in old version
|
||||||
@ -370,27 +371,11 @@ export class BaseBranch {
|
|||||||
}
|
}
|
||||||
|
|
||||||
get pushRepoBaseUrl(): string {
|
get pushRepoBaseUrl(): string {
|
||||||
return this.cleanUrl(this.pushRemoteUrl);
|
return cleanUrl(this.pushRemoteUrl);
|
||||||
}
|
}
|
||||||
|
|
||||||
get repoBaseUrl(): string {
|
get repoBaseUrl(): string {
|
||||||
return this.cleanUrl(this.remoteUrl);
|
return 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();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
commitUrl(commitId: string): string | undefined {
|
commitUrl(commitId: string): string | undefined {
|
||||||
|
Loading…
Reference in New Issue
Block a user