Merge pull request #23431 from backstage/scm-resolve-improvements

@backstage/integration: fix issue with resolving SCM URLs against base with trailing slash
This commit is contained in:
Ben Lambert
2024-03-07 11:09:29 +01:00
committed by GitHub
3 changed files with 103 additions and 2 deletions
+5
View File
@@ -0,0 +1,5 @@
---
'@backstage/integration': patch
---
Fixed an issue with resolution of SCM URLs against a base URL with a trailing slash.
+94
View File
@@ -116,6 +116,82 @@ describe('defaultScmResolveUrl', () => {
);
});
it('works for the root path', () => {
expect(
defaultScmResolveUrl({
url: '/',
base: 'https://gitlab.com/groupA/teams/teamA/subgroupA/repoA/-/blob/branch/folder',
}),
).toBe(
'https://gitlab.com/groupA/teams/teamA/subgroupA/repoA/-/blob/branch/',
);
expect(
defaultScmResolveUrl({
url: '/',
base: 'https://gitlab.com/groupA/teams/teamA/subgroupA/repoA/-/blob/branch/folder/',
}),
).toBe(
'https://gitlab.com/groupA/teams/teamA/subgroupA/repoA/-/blob/branch/',
);
expect(
defaultScmResolveUrl({
url: '/',
base: 'https://gitlab.com/groupA/teams/teamA/subgroupA/repoA/-/blob/branch/folder/a.yaml',
}),
).toBe(
'https://gitlab.com/groupA/teams/teamA/subgroupA/repoA/-/blob/branch/',
);
expect(
defaultScmResolveUrl({
url: '/',
base: 'https://gitlab.com/groupA/teams/teamA/subgroupA/repoA/-/blob/branch/folder/a.yaml?at=master',
}),
).toBe(
'https://gitlab.com/groupA/teams/teamA/subgroupA/repoA/-/blob/branch/?at=master',
);
});
it('works for files in the repo root', () => {
expect(
defaultScmResolveUrl({
url: '/b.yaml',
base: 'https://gitlab.com/groupA/teams/teamA/subgroupA/repoA/-/blob/branch/folder',
}),
).toBe(
'https://gitlab.com/groupA/teams/teamA/subgroupA/repoA/-/blob/branch/b.yaml',
);
expect(
defaultScmResolveUrl({
url: '/b.yaml',
base: 'https://gitlab.com/groupA/teams/teamA/subgroupA/repoA/-/blob/branch/folder/',
}),
).toBe(
'https://gitlab.com/groupA/teams/teamA/subgroupA/repoA/-/blob/branch/b.yaml',
);
expect(
defaultScmResolveUrl({
url: '/b.yaml',
base: 'https://gitlab.com/groupA/teams/teamA/subgroupA/repoA/-/blob/branch/folder/a.yaml',
}),
).toBe(
'https://gitlab.com/groupA/teams/teamA/subgroupA/repoA/-/blob/branch/b.yaml',
);
expect(
defaultScmResolveUrl({
url: '/b.yaml',
base: 'https://gitlab.com/groupA/teams/teamA/subgroupA/repoA/-/blob/branch/folder/a.yaml?at=master',
}),
).toBe(
'https://gitlab.com/groupA/teams/teamA/subgroupA/repoA/-/blob/branch/b.yaml?at=master',
);
});
it('works for absolute paths and retains query params', () => {
expect(
defaultScmResolveUrl({
@@ -126,6 +202,24 @@ describe('defaultScmResolveUrl', () => {
'https://gitlab.com/groupA/teams/teamA/subgroupA/repoA/-/blob/branch/other/b.yaml',
);
expect(
defaultScmResolveUrl({
url: '/other/b.yaml',
base: 'https://gitlab.com/groupA/teams/teamA/subgroupA/repoA/-/blob/branch/folder',
}),
).toBe(
'https://gitlab.com/groupA/teams/teamA/subgroupA/repoA/-/blob/branch/other/b.yaml',
);
expect(
defaultScmResolveUrl({
url: '/other/b.yaml',
base: 'https://gitlab.com/groupA/teams/teamA/subgroupA/repoA/-/blob/branch/folder/',
}),
).toBe(
'https://gitlab.com/groupA/teams/teamA/subgroupA/repoA/-/blob/branch/other/b.yaml',
);
expect(
defaultScmResolveUrl({
url: '/other/b.yaml',
+4 -2
View File
@@ -84,8 +84,10 @@ export function defaultScmResolveUrl(options: {
if (url.startsWith('/')) {
// If it is an absolute path, move relative to the repo root
const { filepath } = parseGitUrl(base);
updated = new URL(base);
const { href, filepath } = parseGitUrl(base);
updated = new URL(href);
const repoRootPath = trimEnd(
updated.pathname.substring(0, updated.pathname.length - filepath.length),
'/',