From 52f613030adab1900a575ba34083a68ab7689b59 Mon Sep 17 00:00:00 2001 From: Dominik Henneke Date: Tue, 9 Mar 2021 17:48:38 +0100 Subject: [PATCH] Support GitHub tree URLs in getGitHubFileFetchUrl Co-authored-by: Oliver Sand Signed-off-by: Dominik Henneke --- .changeset/friendly-schools-sing.md | 5 ++++ packages/integration/src/github/core.test.ts | 30 ++++++++++++++++++++ packages/integration/src/github/core.ts | 6 +++- 3 files changed, 40 insertions(+), 1 deletion(-) create mode 100644 .changeset/friendly-schools-sing.md diff --git a/.changeset/friendly-schools-sing.md b/.changeset/friendly-schools-sing.md new file mode 100644 index 0000000000..bb5bd78308 --- /dev/null +++ b/.changeset/friendly-schools-sing.md @@ -0,0 +1,5 @@ +--- +'@backstage/integration': patch +--- + +Support GitHub `tree` URLs in `getGitHubFileFetchUrl`. diff --git a/packages/integration/src/github/core.test.ts b/packages/integration/src/github/core.test.ts index 03235acfbb..f32c3c5bb4 100644 --- a/packages/integration/src/github/core.test.ts +++ b/packages/integration/src/github/core.test.ts @@ -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', diff --git a/packages/integration/src/github/core.ts b/packages/integration/src/github/core.ts index 239692e962..5048dd35f7 100644 --- a/packages/integration/src/github/core.ts +++ b/packages/integration/src/github/core.ts @@ -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'); }