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:
Dominik Henneke
2021-03-09 17:48:38 +01:00
parent b028502c2d
commit 52f613030a
3 changed files with 40 additions and 1 deletions
+5
View File
@@ -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',
+5 -1
View File
@@ -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');
}