From ebce9fdbeef552544540a0c304c6aa85860d5524 Mon Sep 17 00:00:00 2001 From: Andrew Johnson Date: Thu, 8 Apr 2021 12:30:13 +0100 Subject: [PATCH] update test Signed-off-by: Andrew Johnson --- plugins/github-deployments/src/api/index.ts | 20 +++++--- .../components/GithubDeploymentsCard.test.tsx | 50 ++++++++++++++----- .../src/components/GithubDeploymentsCard.tsx | 15 +++--- 3 files changed, 57 insertions(+), 28 deletions(-) diff --git a/plugins/github-deployments/src/api/index.ts b/plugins/github-deployments/src/api/index.ts index 31e3defa47..221eb3be8a 100644 --- a/plugins/github-deployments/src/api/index.ts +++ b/plugins/github-deployments/src/api/index.ts @@ -21,15 +21,19 @@ import { graphql } from '@octokit/graphql'; const getBaseUrl = ( scmIntegrationsApi: ScmIntegrationRegistry, - location?: string, + locations: string[], ): string => { - if (!location) { + const targets = locations + .map(parseLocationReference) + .filter(location => location.type === 'url') + .map(location => location.target); + + if (targets.length === 0) { return 'https://api.github.com'; } - const config = scmIntegrationsApi.github.byUrl( - parseLocationReference(location).target, - ); + const location = targets[0]; + const config = scmIntegrationsApi.github.byUrl(location); if (!config) { throw new InputError( @@ -65,7 +69,7 @@ type QueryOptions = { export interface GithubDeploymentsApi { listDeployments( options: QueryOptions, - location?: string, + location: string[], ): Promise; } @@ -116,9 +120,9 @@ export class GithubDeploymentsApiClient implements GithubDeploymentsApi { async listDeployments( options: QueryOptions, - location?: string, + locations: string[], ): Promise { - const baseUrl = getBaseUrl(this.scmIntegrationsApi, location); + const baseUrl = getBaseUrl(this.scmIntegrationsApi, locations); const token = await this.githubAuthApi.getAccessToken(['repo']); const graphQLWithAuth = graphql.defaults({ diff --git a/plugins/github-deployments/src/components/GithubDeploymentsCard.test.tsx b/plugins/github-deployments/src/components/GithubDeploymentsCard.test.tsx index 040a9b6571..d2f6b1c4b6 100644 --- a/plugins/github-deployments/src/components/GithubDeploymentsCard.test.tsx +++ b/plugins/github-deployments/src/components/GithubDeploymentsCard.test.tsx @@ -71,8 +71,12 @@ const configApi: ConfigApi = new ConfigReader({ }); const GRAPHQL_GITHUB_API = graphql.link('https://api.github.com/graphql'); -const GRAPHQL_CUSTOM_API_1 = graphql.link('https://api.github-1.com/graphql'); -const GRAPHQL_CUSTOM_API_2 = graphql.link('https://api.github-2.com/graphql'); +const GRAPHQL_CUSTOM_API_1 = graphql.link( + 'https://api.my-github-1.com/graphql', +); +const GRAPHQL_CUSTOM_API_2 = graphql.link( + 'https://api.my-github-2.com/graphql', +); const scmIntegrationsApi = ScmIntegrations.fromConfig(configApi); const githubAuthApi: OAuthApi = { @@ -167,7 +171,7 @@ describe('github-deployments', () => { ).toBeInTheDocument(); }); - it('should shows new data on reload', async () => { + it('should show new data on reload', async () => { worker.use( GRAPHQL_GITHUB_API.query('deployments', (_, res, ctx) => res(ctx.data(responseStub)), @@ -212,10 +216,8 @@ describe('github-deployments', () => { }); it('should fetch data using custom api', async () => { - const customAPI = graphql.link('https://api.my-github-1.com/graphql'); - worker.use( - customAPI.query('deployments', (_, res, ctx) => + GRAPHQL_CUSTOM_API_1.query('deployments', (_, res, ctx) => res(ctx.data(responseStub)), ), ); @@ -236,10 +238,8 @@ describe('github-deployments', () => { }); it('should fetch data using custom api', async () => { - const customAPI = graphql.link('https://api.my-github-2.com/graphql'); - worker.use( - customAPI.query('deployments', (_, res, ctx) => + GRAPHQL_CUSTOM_API_2.query('deployments', (_, res, ctx) => res(ctx.data(responseStub)), ), ); @@ -259,7 +259,7 @@ describe('github-deployments', () => { }; }); - it('shows error when no matching github host', async () => { + it('shows no apiBaseUrl error', async () => { const rendered = await renderInTestApp( @@ -268,7 +268,7 @@ describe('github-deployments', () => { expect( await rendered.findByText( - 'Warning: No apiBaseUrl available for location url:https://my-github-3.com/org/repo, please check your integrations config', + 'Warning: No apiBaseUrl available for location https://my-github-3.com/org/repo, please check your integrations config', ), ).toBeInTheDocument(); }); @@ -284,7 +284,7 @@ describe('github-deployments', () => { }; }); - it('shows error when no matching github host', async () => { + it('shows no matching host error', async () => { const rendered = await renderInTestApp( @@ -293,10 +293,34 @@ describe('github-deployments', () => { expect( await rendered.findByText( - 'Warning: No matching GitHub integration configuration for location url:https://my-github-unknown.com/org/repo, please check your integrations config', + 'Warning: No matching GitHub integration configuration for location https://my-github-unknown.com/org/repo, please check your integrations config', ), ).toBeInTheDocument(); }); }); + + describe('entity with non url location', () => { + beforeEach(() => { + entity = entityStub; + entity.entity.metadata.annotations = { + 'github.com/project-slug': 'org/repo', + 'backstage.io/source-location': + 'some-other-managed-location:my-favourite-location/org/repo', + 'backstage.io/managed-by-location': + 'file:my-favourite-file/org/repo.yaml', + }; + }); + + it('displays fetched data using default github api', async () => { + worker.use( + GRAPHQL_GITHUB_API.query('deployments', (_, res, ctx) => + res(ctx.data(responseStub)), + ), + ); + + await assertFetchedData(); + expect.assertions(7); + }); + }); }); }); diff --git a/plugins/github-deployments/src/components/GithubDeploymentsCard.tsx b/plugins/github-deployments/src/components/GithubDeploymentsCard.tsx index f7ba6293ee..eccab4089a 100644 --- a/plugins/github-deployments/src/components/GithubDeploymentsCard.tsx +++ b/plugins/github-deployments/src/components/GithubDeploymentsCard.tsx @@ -35,17 +35,17 @@ import { const GithubDeploymentsComponent = ({ projectSlug, last, - location, + locations, }: { projectSlug: string; last: number; - location?: string; + locations: string[]; }) => { const api = useApi(githubDeploymentsApiRef); const [owner, repo] = projectSlug.split('/'); const { loading, value, error, retry: reload } = useAsyncRetry( - async () => await api.listDeployments({ owner, repo, last }, location), + async () => await api.listDeployments({ owner, repo, last }, locations), ); if (error) { @@ -63,9 +63,10 @@ const GithubDeploymentsComponent = ({ export const GithubDeploymentsCard = ({ last }: { last?: number }) => { const { entity } = useEntity(); - const location = - entity?.metadata.annotations?.[SOURCE_LOCATION_ANNOTATION] || - entity?.metadata.annotations?.[LOCATION_ANNOTATION]; + const locations = [ + entity?.metadata.annotations?.[SOURCE_LOCATION_ANNOTATION], + entity?.metadata.annotations?.[LOCATION_ANNOTATION], + ].filter(location => location !== undefined) as string[]; return !isGithubDeploymentsAvailable(entity) ? ( @@ -75,7 +76,7 @@ export const GithubDeploymentsCard = ({ last }: { last?: number }) => { entity?.metadata.annotations?.[GITHUB_PROJECT_SLUG_ANNOTATION] || '' } last={last || 10} - location={location} + locations={locations} /> ); };