From 0b487a62b76d160a95703080a89aea94694d6e3d Mon Sep 17 00:00:00 2001 From: Max Linke Date: Thu, 8 Jul 2021 07:10:21 +0000 Subject: [PATCH] fix: api calls fail when instance is on custom path --- src/git/git_remote_parser.test.ts | 13 +++++++++++++ src/git/git_remote_parser.ts | 8 ++++++-- 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/src/git/git_remote_parser.test.ts b/src/git/git_remote_parser.test.ts index 33cba5a..722eaa6 100644 --- a/src/git/git_remote_parser.test.ts +++ b/src/git/git_remote_parser.test.ts @@ -81,4 +81,17 @@ describe('git_remote_parser', () => { project: 'gitlab-vscode-extension', }); }); + // For more details see: https://gitlab.com/gitlab-org/gitlab-vscode-extension/-/issues/103 + it('should parse remote URLs without custom path even if the instance has custom path', () => { + expect( + parseGitRemote( + 'git@example.com:fatihacet/gitlab-vscode-extension.git', + 'https://example.com/gitlab', + ), + ).toEqual({ + host: 'example.com', + namespace: 'fatihacet', + project: 'gitlab-vscode-extension', + }); + }); }); diff --git a/src/git/git_remote_parser.ts b/src/git/git_remote_parser.ts index 2503dfa..d92403b 100644 --- a/src/git/git_remote_parser.ts +++ b/src/git/git_remote_parser.ts @@ -25,9 +25,13 @@ export function parseGitRemote(remote: string, instanceUrl?: string): GitRemote if (!host || !pathname) { return undefined; } - + // The instance url might have a custom route, i.e. www.company.com/gitlab. This route is + // optional in the remote url. This regex extracts namespace and project from the remote + // url while ignoring any custom route, if present. For more information see: + // - https://gitlab.com/gitlab-org/gitlab-vscode-extension/-/merge_requests/11 + // - https://gitlab.com/gitlab-org/gitlab-vscode-extension/-/issues/103 const pathRegExp = instanceUrl ? escapeForRegExp(getInstancePath(instanceUrl)) : ''; - const match = pathname.match(`${pathRegExp}/:?(.+)/([^/]+?)(?:.git)?/?$`); + const match = pathname.match(`(?:${pathRegExp})?/:?(.+)/([^/]+?)(?:.git)?/?$`); if (!match) { return undefined; } -- GitLab