Support GitHub tree URLs in getGitHubFileFetchUrl
Co-authored-by: Oliver Sand <oliver.sand@sda-se.com> Signed-off-by: Dominik Henneke <dominik.henneke@sda-se.com>
This commit is contained in:
@@ -0,0 +1,5 @@
|
||||
---
|
||||
'@backstage/integration': patch
|
||||
---
|
||||
|
||||
Support GitHub `tree` URLs in `getGitHubFileFetchUrl`.
|
||||
@@ -84,6 +84,36 @@ describe('github core', () => {
|
||||
);
|
||||
});
|
||||
|
||||
it('happy path for github tree', () => {
|
||||
const config: GitHubIntegrationConfig = {
|
||||
host: 'github.com',
|
||||
apiBaseUrl: 'https://api.github.com',
|
||||
};
|
||||
expect(
|
||||
getGitHubFileFetchUrl(
|
||||
'https://github.com/a/b/tree/branchname/path/to/c.yaml',
|
||||
config,
|
||||
),
|
||||
).toEqual(
|
||||
'https://api.github.com/repos/a/b/contents/path/to/c.yaml?ref=branchname',
|
||||
);
|
||||
});
|
||||
|
||||
it('happy path for ghe tree', () => {
|
||||
const config: GitHubIntegrationConfig = {
|
||||
host: 'ghe.mycompany.net',
|
||||
apiBaseUrl: 'https://ghe.mycompany.net/api/v3',
|
||||
};
|
||||
expect(
|
||||
getGitHubFileFetchUrl(
|
||||
'https://ghe.mycompany.net/a/b/tree/branchname/path/to/c.yaml',
|
||||
config,
|
||||
),
|
||||
).toEqual(
|
||||
'https://ghe.mycompany.net/api/v3/repos/a/b/contents/path/to/c.yaml?ref=branchname',
|
||||
);
|
||||
});
|
||||
|
||||
it('happy path for github raw', () => {
|
||||
const config: GitHubIntegrationConfig = {
|
||||
host: 'github.com',
|
||||
|
||||
@@ -39,7 +39,11 @@ export function getGitHubFileFetchUrl(
|
||||
!owner ||
|
||||
!name ||
|
||||
!ref ||
|
||||
(filepathtype !== 'blob' && filepathtype !== 'raw')
|
||||
// GitHub is automatically redirecting tree urls to blob urls so it's
|
||||
// fine to pass a tree url.
|
||||
(filepathtype !== 'blob' &&
|
||||
filepathtype !== 'raw' &&
|
||||
filepathtype !== 'tree')
|
||||
) {
|
||||
throw new Error('Invalid GitHub URL or file path');
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user