Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -294,4 +294,87 @@ describe('Bitbucket Service', () => {
nockDone();
});
});

it('should preserve scheme and port for Bitbucket Server API calls', async () => {
const gitSource: GitSource = {
url: 'http://bb.example.com:7990/scm/PROJ/repo.git',
};
const gitService = new BitbucketService(gitSource);

const scope = nock('http://bb.example.com:7990')
.get('/rest/api/1.0/projects/PROJ/repos/repo')
.reply(200, { slug: 'repo' });

const status = await gitService.isRepoReachable();
expect(status).toEqual(RepoStatus.Reachable);
scope.done();
});

describe('getRepoMetadata - Protocol and Port Handling', () => {
it('should use HTTPS protocol for standard Bitbucket URL', () => {
const gitSource: GitSource = { url: 'https://bitbucket.org/owner/repo' };
const gitService = new BitbucketService(gitSource);
const metadata = gitService.getRepoMetadata();

expect(metadata.host).toBe('https://bitbucket.org');
});

it('should preserve HTTP protocol', () => {
const gitSource: GitSource = { url: 'http://bitbucket.example.com/owner/repo' };
const gitService = new BitbucketService(gitSource);
const metadata = gitService.getRepoMetadata();

expect(metadata.host).toBe('http://bitbucket.example.com');
});

it('should preserve custom port with HTTPS', () => {
const gitSource: GitSource = { url: 'https://bitbucket.example.com:8443/owner/repo' };
const gitService = new BitbucketService(gitSource);
const metadata = gitService.getRepoMetadata();

expect(metadata.host).toBe('https://bitbucket.example.com:8443');
});

it('should preserve custom port with HTTP', () => {
const gitSource: GitSource = { url: 'http://bitbucket.example.com:8080/owner/repo' };
const gitService = new BitbucketService(gitSource);
const metadata = gitService.getRepoMetadata();

expect(metadata.host).toBe('http://bitbucket.example.com:8080');
});

it('should default to HTTPS for SSH URLs and preserve port', () => {
const gitSource: GitSource = { url: 'git@bitbucket.example.com:2222/owner/repo.git' };
const gitService = new BitbucketService(gitSource);
const metadata = gitService.getRepoMetadata();

expect(metadata.host).toBe('https://bitbucket.example.com:2222');
});

it('should default to HTTPS for git:// protocol URLs', () => {
const gitSource: GitSource = { url: 'git://bitbucket.example.com/owner/repo.git' };
const gitService = new BitbucketService(gitSource);
const metadata = gitService.getRepoMetadata();

expect(metadata.host).toBe('https://bitbucket.example.com');
});

it('should preserve non-standard port regardless of original protocol', () => {
const gitSource: GitSource = { url: 'ssh://git@bitbucket.example.com:9999/owner/repo.git' };
const gitService = new BitbucketService(gitSource);
const metadata = gitService.getRepoMetadata();

expect(metadata.host).toBe('https://bitbucket.example.com:9999');
});

it('should handle Bitbucket Server URL with custom port', () => {
const gitSource: GitSource = {
url: 'http://bb.example.com:7990/scm/proj/repo.git',
};
const gitService = new BitbucketService(gitSource);
const metadata = gitService.getRepoMetadata();

expect(metadata.host).toBe('http://bb.example.com:7990');
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -281,4 +281,62 @@ describe('Gitea Service', () => {
nockDone();
});
});

describe('getRepoMetadata - Protocol and Port Handling', () => {
it('should use HTTPS protocol for standard Gitea URL', () => {
const gitSource: GitSource = { url: 'https://gitea.com/owner/repo' };
const gitService = new GiteaService(gitSource);
const metadata = gitService.getRepoMetadata();

expect(metadata.host).toBe('https://gitea.com');
});

it('should preserve HTTP protocol', () => {
const gitSource: GitSource = { url: 'http://gitea.example.com/owner/repo' };
const gitService = new GiteaService(gitSource);
const metadata = gitService.getRepoMetadata();

expect(metadata.host).toBe('http://gitea.example.com');
});

it('should preserve custom port with HTTPS', () => {
const gitSource: GitSource = { url: 'https://gitea.example.com:8443/owner/repo' };
const gitService = new GiteaService(gitSource);
const metadata = gitService.getRepoMetadata();

expect(metadata.host).toBe('https://gitea.example.com:8443');
});

it('should preserve custom port with HTTP', () => {
const gitSource: GitSource = { url: 'http://gitea.example.com:8080/owner/repo' };
const gitService = new GiteaService(gitSource);
const metadata = gitService.getRepoMetadata();

expect(metadata.host).toBe('http://gitea.example.com:8080');
});

it('should default to HTTPS for SSH URLs and preserve port', () => {
const gitSource: GitSource = { url: 'git@gitea.example.com:2222/owner/repo.git' };
const gitService = new GiteaService(gitSource);
const metadata = gitService.getRepoMetadata();

expect(metadata.host).toBe('https://gitea.example.com:2222');
});

it('should default to HTTPS for git:// protocol URLs', () => {
const gitSource: GitSource = { url: 'git://gitea.example.com/owner/repo.git' };
const gitService = new GiteaService(gitSource);
const metadata = gitService.getRepoMetadata();

expect(metadata.host).toBe('https://gitea.example.com');
});

it('should preserve non-standard port regardless of original protocol', () => {
const gitSource: GitSource = { url: 'ssh://git@gitea.example.com:9999/owner/repo.git' };
const gitService = new GiteaService(gitSource);
const metadata = gitService.getRepoMetadata();

expect(metadata.host).toBe('https://gitea.example.com:9999');
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,17 @@ describe('Github Service', () => {
scope.done();
});

it('should preserve scheme and port when making API call to specified hostname', async () => {
const gitSource: GitSource = { url: 'http://example.com:3000/test/repo' };
const gitService = new GithubService(gitSource);

const scope = nock('http://example.com:3000/api/v3').get('/repos/test/repo').reply(200);

const status = await gitService.isRepoReachable();
expect(status).toEqual(RepoStatus.Reachable);
scope.done();
});

it('should detect .tekton folder', () => {
const gitSource = {
url: 'https://github.com/Lucifergene/oc-pipe',
Expand Down Expand Up @@ -324,4 +335,62 @@ describe('Github Service', () => {
nockDone();
});
});

describe('getRepoMetadata - Protocol and Port Handling', () => {
it('should use HTTPS protocol for standard GitHub URL', () => {
const gitSource: GitSource = { url: 'https://github.com/owner/repo' };
const gitService = new GithubService(gitSource);
const metadata = gitService.getRepoMetadata();

expect(metadata.host).toBe('https://github.com');
});

it('should preserve HTTP protocol', () => {
const gitSource: GitSource = { url: 'http://github.example.com/owner/repo' };
const gitService = new GithubService(gitSource);
const metadata = gitService.getRepoMetadata();

expect(metadata.host).toBe('http://github.example.com');
});

it('should preserve custom port with HTTPS', () => {
const gitSource: GitSource = { url: 'https://github.example.com:8443/owner/repo' };
const gitService = new GithubService(gitSource);
const metadata = gitService.getRepoMetadata();

expect(metadata.host).toBe('https://github.example.com:8443');
});

it('should preserve custom port with HTTP', () => {
const gitSource: GitSource = { url: 'http://github.example.com:8080/owner/repo' };
const gitService = new GithubService(gitSource);
const metadata = gitService.getRepoMetadata();

expect(metadata.host).toBe('http://github.example.com:8080');
});

it('should default to HTTPS for SSH URLs and preserve port', () => {
const gitSource: GitSource = { url: 'git@github.example.com:2222/owner/repo.git' };
const gitService = new GithubService(gitSource);
const metadata = gitService.getRepoMetadata();

expect(metadata.host).toBe('https://github.example.com:2222');
});

it('should default to HTTPS for git:// protocol URLs', () => {
const gitSource: GitSource = { url: 'git://github.example.com/owner/repo.git' };
const gitService = new GithubService(gitSource);
const metadata = gitService.getRepoMetadata();

expect(metadata.host).toBe('https://github.example.com');
});

it('should preserve non-standard port regardless of original protocol', () => {
const gitSource: GitSource = { url: 'ssh://git@github.example.com:9999/owner/repo.git' };
const gitService = new GithubService(gitSource);
const metadata = gitService.getRepoMetadata();

expect(metadata.host).toBe('https://github.example.com:9999');
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -316,4 +316,84 @@ describe('Gitlab Service', () => {
expect(status).toEqual(RepoStatus.Reachable);
scope.done();
});

it('should preserve scheme and port when making API call to specified hostname', async () => {
const gitSource: GitSource = { url: 'http://example.com:3000/test/repo' };
const gitService = new GitlabService(gitSource);

const scope = nock('http://example.com:3000/api/v4')
.get('/projects/test%2Frepo')
// eslint-disable-next-line @typescript-eslint/naming-convention
.reply(200, { path_with_namespace: 'test/repo' });

const status = await gitService.isRepoReachable();
expect(status).toEqual(RepoStatus.Reachable);
scope.done();
});

describe('getRepoMetadata - Protocol and Port Handling', () => {
it('should use HTTPS protocol for standard GitLab URL', () => {
const gitSource: GitSource = { url: 'https://gitlab.com/owner/repo' };
const gitService = new GitlabService(gitSource);
const metadata = gitService.getRepoMetadata();

expect(metadata.host).toBe('https://gitlab.com');
});

it('should preserve HTTP protocol', () => {
const gitSource: GitSource = { url: 'http://gitlab.example.com/owner/repo' };
const gitService = new GitlabService(gitSource);
const metadata = gitService.getRepoMetadata();

expect(metadata.host).toBe('http://gitlab.example.com');
});

it('should preserve custom port with HTTPS', () => {
const gitSource: GitSource = { url: 'https://gitlab.example.com:8443/owner/repo' };
const gitService = new GitlabService(gitSource);
const metadata = gitService.getRepoMetadata();

expect(metadata.host).toBe('https://gitlab.example.com:8443');
});

it('should preserve custom port with HTTP', () => {
const gitSource: GitSource = { url: 'http://gitlab.example.com:8080/owner/repo' };
const gitService = new GitlabService(gitSource);
const metadata = gitService.getRepoMetadata();

expect(metadata.host).toBe('http://gitlab.example.com:8080');
});

it('should default to HTTPS for SSH URLs and preserve port', () => {
const gitSource: GitSource = { url: 'git@gitlab.example.com:2222/owner/repo.git' };
const gitService = new GitlabService(gitSource);
const metadata = gitService.getRepoMetadata();

expect(metadata.host).toBe('https://gitlab.example.com:2222');
});

it('should default to HTTPS for git:// protocol URLs', () => {
const gitSource: GitSource = { url: 'git://gitlab.example.com/owner/repo.git' };
const gitService = new GitlabService(gitSource);
const metadata = gitService.getRepoMetadata();

expect(metadata.host).toBe('https://gitlab.example.com');
});

it('should preserve non-standard port regardless of original protocol', () => {
const gitSource: GitSource = { url: 'ssh://git@gitlab.example.com:9999/owner/repo.git' };
const gitService = new GitlabService(gitSource);
const metadata = gitService.getRepoMetadata();

expect(metadata.host).toBe('https://gitlab.example.com:9999');
});

it('should handle subdomains with custom port', () => {
const gitSource: GitSource = { url: 'https://version.helsinki.fi:3000/owner/repo' };
const gitService = new GitlabService(gitSource);
const metadata = gitService.getRepoMetadata();

expect(metadata.host).toBe('https://version.helsinki.fi:3000');
});
});
});
14 changes: 10 additions & 4 deletions frontend/packages/git-service/src/services/bitbucket-service.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as GitUrlParse from 'git-url-parse';
import { Base64 } from 'js-base64';
import * as ParseBitbucketUrl from 'parse-bitbucket-url';
import { consoleFetchJSON } from '@console/dynamic-plugin-sdk/src/lib-core';
import { DevConsoleEndpointResponse } from '@console/shared/src';
import {
Expand Down Expand Up @@ -41,8 +41,8 @@ export class BitbucketService extends BaseService {
constructor(gitsource: GitSource) {
super(gitsource);
this.metadata = this.getRepoMetadata();
if (this.metadata.host !== 'bitbucket.org') {
this.baseURL = `https://${this.metadata.host}/rest/api/1.0`;
if (!this.metadata.host.includes('bitbucket.org')) {
this.baseURL = `${this.metadata.host}/rest/api/1.0`;
this.isServer = true;
}
}
Expand Down Expand Up @@ -88,8 +88,14 @@ export class BitbucketService extends BaseService {
};

getRepoMetadata = (): RepoMetadata => {
const { name, owner, host } = ParseBitbucketUrl(this.gitsource.url);
const { name, owner, resource, protocols, port } = GitUrlParse(this.gitsource.url);
const contextDir = this.gitsource.contextDir?.replace(/\/$/, '') || '';
const rawProtocol = protocols?.[0];
const isHttpProtocol = rawProtocol === 'http' || rawProtocol === 'https';
const protocol = isHttpProtocol ? rawProtocol : 'https';

const host = port ? `${protocol}://${resource}:${port}` : `${protocol}://${resource}`;

return {
repoName: name,
owner,
Expand Down
11 changes: 9 additions & 2 deletions frontend/packages/git-service/src/services/gitea-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,16 @@ export class GiteaService extends BaseService {
};

getRepoMetadata = (): RepoMetadata => {
const { name, owner, resource, full_name: fullName } = GitUrlParse(this.gitsource.url);
const { name, owner, resource, protocols, port, full_name: fullName } = GitUrlParse(
this.gitsource.url,
);
const contextDir = this.gitsource.contextDir?.replace(/\/$/, '') || '';
const host = `https://${resource}`;
const rawProtocol = protocols?.[0];
const isHttpProtocol = rawProtocol === 'http' || rawProtocol === 'https';
const protocol = isHttpProtocol ? rawProtocol : 'https';

const host = port ? `${protocol}://${resource}:${port}` : `${protocol}://${resource}`;

return {
repoName: name,
owner,
Expand Down
Loading