Skip to content

Commit bf1b937

Browse files
authored
Fix set branch function for Gitlab repositories with subgroups (#1342)
Support Gitlab repository urls with multiple subgroups when handling branch name from Git Repo Options menu.
1 parent d00685a commit bf1b937

File tree

2 files changed

+16
-3
lines changed

2 files changed

+16
-3
lines changed

packages/dashboard-frontend/src/components/ImportFromGit/__tests__/helpers.spec.tsx

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,19 @@ describe('helpers', () => {
206206
helpers.setBranchToLocation('https://gitlab.com/eclipse-che/che-dashboard.git', branch),
207207
).toBe(`https://gitlab.com/eclipse-che/che-dashboard.git/-/tree/${branch}`);
208208
});
209+
test('repository with subgroups', () => {
210+
expect(
211+
helpers.setBranchToLocation('https://gitlab.com/group/subgroup/repository.git', branch),
212+
).toBe(`https://gitlab.com/group/subgroup/repository.git/-/tree/${branch}`);
213+
});
214+
test('repository with subgroups and branch', () => {
215+
expect(
216+
helpers.setBranchToLocation(
217+
'https://gitlab.com/group/subgroup/repository.git/-/tree/branch',
218+
branch,
219+
),
220+
).toBe(`https://gitlab.com/group/subgroup/repository.git/-/tree/${branch}`);
221+
});
209222
});
210223
describe('Bitbucket', () => {
211224
test('should return the location without branch', () => {

packages/dashboard-frontend/src/components/ImportFromGit/helpers.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -140,9 +140,9 @@ function setBranchToAzureDevOpsLocation(location: string, branch: string | undef
140140

141141
export function setBranchToLocation(location: string, branch: string | undefined): string {
142142
const url = new URL(location);
143-
const pathname = url.pathname;
143+
const pathname = url.pathname.replace(/^\//, '').replace(/\/$/, '');
144144

145-
const [user, project] = pathname.replace(/^\//, '').replace(/\/$/, '').split('/');
145+
const [user, project] = pathname.split('/');
146146

147147
const service = getSupportedGitService(location);
148148
if (!branch) {
@@ -157,7 +157,7 @@ export function setBranchToLocation(location: string, branch: string | undefined
157157
url.pathname = `${user}/${project}/tree/${branch}`;
158158
break;
159159
case 'gitlab':
160-
url.pathname = `${user}/${project}/-/tree/${branch}`;
160+
url.pathname = `${pathname.replace(/\/-\/tree\/.*$/, '')}/-/tree/${branch}`;
161161
break;
162162
case 'bitbucket-server':
163163
url.pathname = `${user}/${project}/src/${branch}`;

0 commit comments

Comments
 (0)