From 8fcfd7b31668857e310afa761987a1c323044902 Mon Sep 17 00:00:00 2001 From: Jussi Hallila Date: Wed, 13 Jan 2021 16:36:50 +0100 Subject: [PATCH] Add logic to not contact GitHub on non-GitHub urls Wrap in tests. --- .../components/ImportComponentPage.test.tsx | 65 ++++++++++++++++--- .../src/mocks/locations-POST-response.json | 4 +- .../catalog-import/src/util/useGithubRepos.ts | 8 ++- 3 files changed, 66 insertions(+), 11 deletions(-) diff --git a/plugins/catalog-import/src/components/ImportComponentPage.test.tsx b/plugins/catalog-import/src/components/ImportComponentPage.test.tsx index 3f6f878b29..5c4b2e8538 100644 --- a/plugins/catalog-import/src/components/ImportComponentPage.test.tsx +++ b/plugins/catalog-import/src/components/ImportComponentPage.test.tsx @@ -64,6 +64,7 @@ jest.mock('@octokit/rest', () => ({ }), })); +const OUR_GITHUB_TEST_REPO = 'https://github.com/someuser/somerepo'; describe('', () => { const server = setupServer(); msw.setupDefaultHandlers(server); @@ -136,6 +137,56 @@ describe('', () => { ); } + it('Should not explode on non-Github URLs', async () => { + await renderSUT(); + await waitFor(() => { + fireEvent.input( + screen.getByPlaceholderText('https://github.com/backstage/backstage'), + { + target: { + value: 'https://test-git-provider.localhost/someuser/somerepo', + }, + }, + ); + }); + + fireEvent.click(screen.getByText('Next')); + await waitFor(() => { + const firstStepInput = screen.queryByPlaceholderText( + 'https://github.com/backstage/backstage', + ); + expect(firstStepInput).not.toBeInTheDocument(); + }); + }); + + it('Should offer direct file import from non-Github URLs', async () => { + await renderSUT(); + await waitFor(() => { + fireEvent.input( + screen.getByPlaceholderText('https://github.com/backstage/backstage'), + { + target: { + value: + 'https://test-git-provider.localhost/someuser/somerepo/catalog-info.yaml', + }, + }, + ); + }); + + fireEvent.click(screen.getByText('Next')); + await waitFor(() => { + const firstStepInput = screen.queryByPlaceholderText( + 'https://github.com/backstage/backstage', + ); + expect(firstStepInput).not.toBeInTheDocument(); + }); + expect( + screen.getByText( + 'https://test-git-provider.localhost/someuser/somerepo/catalog-info.yaml', + ), + ).toBeInTheDocument(); + }); + it('Should use found yaml file directly and not create a pull request if GitHub api returns one', async () => { codeSearchMockResponse = () => Promise.resolve({ @@ -152,7 +203,7 @@ describe('', () => { await waitFor(() => { fireEvent.input( screen.getByPlaceholderText('https://github.com/backstage/backstage'), - { target: { value: 'https://test.localhost/someuser/somerepo' } }, + { target: { value: OUR_GITHUB_TEST_REPO } }, ); }); @@ -160,7 +211,7 @@ describe('', () => { await waitFor(() => { expect( screen.getByText( - 'https://test.localhost/someusername/somerepo/blob/master/src/catalog-info.yaml', + 'https://github.com/someusername/somerepo/blob/master/src/catalog-info.yaml', ), ).toBeInTheDocument(); @@ -181,25 +232,23 @@ describe('', () => { await waitFor(() => { fireEvent.input( screen.getByPlaceholderText('https://github.com/backstage/backstage'), - { target: { value: 'https://test.localhost/someuser/somerepo' } }, + { target: { value: OUR_GITHUB_TEST_REPO } }, ); }); fireEvent.click(screen.getByText('Next')); await waitFor(() => { - expect( - screen.getByText('https://test.localhost/someuser/somerepo'), - ).toBeInTheDocument(); + expect(screen.getByText(OUR_GITHUB_TEST_REPO)).toBeInTheDocument(); }); const textNode = container - .querySelector('a[href="https://test.localhost/someuser/somerepo"]') + .querySelector('a[href="https://github.com/someuser/somerepo"]') ?.closest('p'); expect(textNode?.innerHTML).toContain( 'Following config object will be submitted in a pull request to the repository', ); expect( screen.queryByText( - 'https://test.localhost/someusername/somerepo/blob/master/src/catalog-info.yaml', + 'https://github.com/someusername/somerepo/blob/master/src/catalog-info.yaml', ), ).not.toBeInTheDocument(); }); diff --git a/plugins/catalog-import/src/mocks/locations-POST-response.json b/plugins/catalog-import/src/mocks/locations-POST-response.json index 20e4e1582c..c44a0f4d61 100644 --- a/plugins/catalog-import/src/mocks/locations-POST-response.json +++ b/plugins/catalog-import/src/mocks/locations-POST-response.json @@ -2,14 +2,14 @@ "location": { "id": "d4a64359-a709-4c91-a9de-0905a033bf22", "type": "url", - "target": "https://test.localhost/someusername/somerepo/blob/master/src/catalog-info.yaml" + "target": "https://github.com/someusername/somerepo/blob/master/src/catalog-info.yaml" }, "entities": [ { "metadata": { "namespace": "default", "annotations": { - "backstage.io/managed-by-location": "url:https://test.localhost/someusername/somerepo/blob/master/src/catalog-info.yaml", + "backstage.io/managed-by-location": "url:https://github.com/someusername/somerepo/blob/master/src/catalog-info.yaml", "github.com/project-slug": "someusername/somerepo" }, "name": "somerepo", diff --git a/plugins/catalog-import/src/util/useGithubRepos.ts b/plugins/catalog-import/src/util/useGithubRepos.ts index 56cf101c35..319457dfe8 100644 --- a/plugins/catalog-import/src/util/useGithubRepos.ts +++ b/plugins/catalog-import/src/util/useGithubRepos.ts @@ -81,7 +81,13 @@ export function useGithubRepos() { return submitPRResponse; }; - const checkForExistingCatalogInfo = async (location: string) => { + const checkForExistingCatalogInfo = async ( + location: string, + ): Promise<{ exists: boolean; url?: string }> => { + const { source } = parseGitUri(location); + if (source !== 'github.com') { + return Promise.resolve({ exists: false }); + } const { repoName, ownerName,