Make sure that changing the url type doesn't collide with repo or owner names

Signed-off-by: Oliver Sand <oliver.sand@sda-se.com>
This commit is contained in:
Oliver Sand
2021-03-05 11:41:16 +01:00
parent 93c62c755c
commit 69ad77869c
4 changed files with 79 additions and 5 deletions
@@ -15,7 +15,7 @@
*/
import { ConfigReader } from '@backstage/config';
import { GitHubIntegration } from './GitHubIntegration';
import { GitHubIntegration, replaceUrlType } from './GitHubIntegration';
describe('GitHubIntegration', () => {
it('has a working factory', () => {
@@ -80,3 +80,29 @@ describe('GitHubIntegration', () => {
).toBe('https://github.com/backstage/backstage/edit/master/README.md');
});
});
describe('replaceUrlType', () => {
it('should replace with expected type', () => {
expect(
replaceUrlType(
'https://github.com/backstage/backstage/blob/master/README.md',
'edit',
),
).toBe('https://github.com/backstage/backstage/edit/master/README.md');
expect(
replaceUrlType(
'https://github.com/webmodules/blob/blob/master/test',
'tree',
),
).toBe('https://github.com/webmodules/blob/tree/master/test');
expect(
replaceUrlType('https://github.com/blob/blob/blob/master/test', 'tree'),
).toBe('https://github.com/blob/blob/tree/master/test');
expect(
replaceUrlType(
'https://github.com/backstage/backstage/edit/tree/README.md',
'blob',
),
).toBe('https://github.com/backstage/backstage/blob/tree/README.md');
});
});
@@ -50,10 +50,22 @@ export class GitHubIntegration implements ScmIntegration {
// GitHub uses blob URLs for files and tree urls for directory listings. But
// there is a redirect from tree to blob for files, so we can always return
// tree urls here.
return defaultScmResolveUrl(options).replace('/blob/', '/tree/');
return replaceUrlType(defaultScmResolveUrl(options), 'tree');
}
resolveEditUrl(url: string): string {
return url.replace('/blob/', '/edit/');
return replaceUrlType(url, 'edit');
}
}
export function replaceUrlType(
url: string,
type: 'blob' | 'tree' | 'edit',
): string {
return url.replace(
/\/\/([^/]+)\/([^/]+)\/([^/]+)\/(blob|tree|edit)\//,
(_, host, owner, repo) => {
return `//${host}/${owner}/${repo}/${type}/`;
},
);
}
@@ -15,7 +15,7 @@
*/
import { ConfigReader } from '@backstage/config';
import { GitLabIntegration } from './GitLabIntegration';
import { GitLabIntegration, replaceUrlType } from './GitLabIntegration';
describe('GitLabIntegration', () => {
it('has a working factory', () => {
@@ -54,3 +54,32 @@ describe('GitLabIntegration', () => {
).toBe('https://gitlab.com/my-org/my-project/-/edit/develop/README.md');
});
});
describe('replaceUrlType', () => {
it('should replace with expected type', () => {
expect(
replaceUrlType(
'https://gitlab.com/my-org/my-project/-/blob/develop/README.md',
'edit',
),
).toBe('https://gitlab.com/my-org/my-project/-/edit/develop/README.md');
expect(
replaceUrlType(
'https://gitlab.com/webmodules/blob/-/blob/develop/test',
'tree',
),
).toBe('https://gitlab.com/webmodules/blob/-/tree/develop/test');
expect(
replaceUrlType(
'https://gitlab.com/blob/blob/-/blob/develop/test',
'tree',
),
).toBe('https://gitlab.com/blob/blob/-/tree/develop/test');
expect(
replaceUrlType(
'https://gitlab.com/blob/blob/-/edit/develop/README.md',
'tree',
),
).toBe('https://gitlab.com/blob/blob/-/tree/develop/README.md');
});
});
@@ -51,6 +51,13 @@ export class GitLabIntegration implements ScmIntegration {
}
resolveEditUrl(url: string): string {
return url.replace('/blob/', '/edit/');
return replaceUrlType(url, 'edit');
}
}
export function replaceUrlType(
url: string,
type: 'blob' | 'tree' | 'edit',
): string {
return url.replace(/\/\-\/(blob|tree|edit)\//, `/-/${type}/`);
}