diff --git a/.changeset/smooth-vans-travel.md b/.changeset/smooth-vans-travel.md index eb074c8ecf..0038c35e8e 100644 --- a/.changeset/smooth-vans-travel.md +++ b/.changeset/smooth-vans-travel.md @@ -1,5 +1,5 @@ --- -'@backstage/plugin-github-deployments': minor +'@backstage/plugin-github-deployments': patch --- Support for GitHub Enterprise with GithubDeploymentsPlugin diff --git a/plugins/github-deployments/src/api/index.ts b/plugins/github-deployments/src/api/index.ts index 3a348f2b56..23a381f771 100644 --- a/plugins/github-deployments/src/api/index.ts +++ b/plugins/github-deployments/src/api/index.ts @@ -21,29 +21,28 @@ import { graphql } from '@octokit/graphql'; const getBaseUrl = ( scmIntegrationsApi: ScmIntegrationRegistry, - locations: string[], + host: string | undefined, ): string => { - const targets = locations - .map(parseLocationReference) - .filter(location => location.type === 'url') - .map(location => location.target); - - if (targets.length === 0) { + if (host === undefined) { return 'https://api.github.com'; } - const location = targets[0]; - const config = scmIntegrationsApi.github.byUrl(location); + const location = parseLocationReference(host); + if (location.type !== 'github') { + return 'https://api.github.com'; + } + + const config = scmIntegrationsApi.github.byHost(location.target); if (!config) { throw new InputError( - `No matching GitHub integration configuration for location ${location}, please check your integrations config`, + `No matching GitHub integration configuration for host ${host}, please check your integrations config`, ); } if (!config.config.apiBaseUrl) { throw new InputError( - `No apiBaseUrl available for location ${location}, please check your integrations config`, + `No apiBaseUrl available for host ${host}, please check your integrations config`, ); } @@ -65,20 +64,14 @@ export type GithubDeployment = { }; type QueryParams = { + host: string | undefined; owner: string; repo: string; last: number; }; -type QueryOptions = { - locations: string[]; -}; - export interface GithubDeploymentsApi { - listDeployments( - params: QueryParams, - options: QueryOptions, - ): Promise; + listDeployments(params: QueryParams): Promise; } export const githubDeploymentsApiRef = createApiRef({ @@ -130,11 +123,8 @@ export class GithubDeploymentsApiClient implements GithubDeploymentsApi { this.scmIntegrationsApi = options.scmIntegrationsApi; } - async listDeployments( - params: QueryParams, - options: QueryOptions, - ): Promise { - const baseUrl = getBaseUrl(this.scmIntegrationsApi, options.locations); + async listDeployments(params: QueryParams): Promise { + const baseUrl = getBaseUrl(this.scmIntegrationsApi, params.host); 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 cae8313422..5914e4b422 100644 --- a/plugins/github-deployments/src/components/GithubDeploymentsCard.test.tsx +++ b/plugins/github-deployments/src/components/GithubDeploymentsCard.test.tsx @@ -250,8 +250,7 @@ describe('github-deployments', () => { entity = entityStub; entity.entity.metadata.annotations = { 'github.com/project-slug': 'org/repo', - 'backstage.io/source-location': - 'url:https://my-github-1.com/org/repo', + 'backstage.io/source-location': 'github:my-github-1.com', }; }); @@ -272,8 +271,7 @@ describe('github-deployments', () => { entity = entityStub; entity.entity.metadata.annotations = { 'github.com/project-slug': 'org/repo', - 'backstage.io/managed-by-location': - 'url:https://my-github-2.com/org/repo', + 'backstage.io/managed-by-location': 'github:my-github-2.com', }; }); @@ -294,8 +292,7 @@ describe('github-deployments', () => { entity = entityStub; entity.entity.metadata.annotations = { 'github.com/project-slug': 'org/repo', - 'backstage.io/managed-by-location': - 'url:https://my-github-3.com/org/repo', + 'backstage.io/managed-by-location': 'github:my-github-3.com', }; }); @@ -308,7 +305,7 @@ describe('github-deployments', () => { expect( await rendered.findByText( - 'Warning: No apiBaseUrl available for location https://my-github-3.com/org/repo, please check your integrations config', + 'Warning: No apiBaseUrl available for host github:my-github-3.com, please check your integrations config', ), ).toBeInTheDocument(); }); @@ -320,7 +317,7 @@ describe('github-deployments', () => { entity.entity.metadata.annotations = { 'github.com/project-slug': 'org/repo', 'backstage.io/managed-by-location': - 'url:https://my-github-unknown.com/org/repo', + 'github:my-github-unknown.com/org/repo', }; }); @@ -333,7 +330,7 @@ describe('github-deployments', () => { expect( await rendered.findByText( - 'Warning: No matching GitHub integration configuration for location https://my-github-unknown.com/org/repo, please check your integrations config', + 'Warning: No matching GitHub integration configuration for host github:my-github-unknown.com/org/repo, please check your integrations config', ), ).toBeInTheDocument(); }); diff --git a/plugins/github-deployments/src/components/GithubDeploymentsCard.tsx b/plugins/github-deployments/src/components/GithubDeploymentsCard.tsx index 959926683f..b7f2e7b1ba 100644 --- a/plugins/github-deployments/src/components/GithubDeploymentsCard.tsx +++ b/plugins/github-deployments/src/components/GithubDeploymentsCard.tsx @@ -37,18 +37,18 @@ const GithubDeploymentsComponent = ({ projectSlug, last, columns, - locations, + host, }: { projectSlug: string; last: number; - locations: string[]; columns: TableColumn[]; + host: string | undefined; }) => { const api = useApi(githubDeploymentsApiRef); const [owner, repo] = projectSlug.split('/'); const { loading, value, error, retry: reload } = useAsyncRetry( - async () => await api.listDeployments({ owner, repo, last }, { locations }), + async () => await api.listDeployments({ host, owner, repo, last }), ); if (error) { @@ -73,10 +73,10 @@ export const GithubDeploymentsCard = ({ columns?: TableColumn[]; }) => { const { entity } = useEntity(); - const locations = [ + const host: string | undefined = [ entity?.metadata.annotations?.[SOURCE_LOCATION_ANNOTATION], entity?.metadata.annotations?.[LOCATION_ANNOTATION], - ].filter(location => location !== undefined) as string[]; + ].filter(Boolean)[0]; return !isGithubDeploymentsAvailable(entity) ? ( @@ -86,7 +86,7 @@ export const GithubDeploymentsCard = ({ entity?.metadata.annotations?.[GITHUB_PROJECT_SLUG_ANNOTATION] || '' } last={last || 10} - locations={locations} + host={host} columns={columns || GithubDeploymentsTable.defaultDeploymentColumns} /> );