Skip to content

Commit

Permalink
Merge pull request backstage#4895 from SDA-SE/feat/tree-urls
Browse files Browse the repository at this point in the history
Support GitHub tree URLs in getGitHubFileFetchUrl
  • Loading branch information
Fox32 authored Mar 9, 2021
2 parents b028502 + 52f6130 commit c111312
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/friendly-schools-sing.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@backstage/integration': patch
---

Support GitHub `tree` URLs in `getGitHubFileFetchUrl`.
30 changes: 30 additions & 0 deletions packages/integration/src/github/core.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
6 changes: 5 additions & 1 deletion packages/integration/src/github/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}
Expand Down

0 comments on commit c111312

Please sign in to comment.