as per comments

Signed-off-by: Andrew Johnson <ajohnson@gocardless.com>
This commit is contained in:
Andrew Johnson
2021-05-07 15:32:16 +01:00
parent 6b4b3c89e3
commit cb7a25f778
4 changed files with 27 additions and 40 deletions
+14 -24
View File
@@ -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<GithubDeployment[]>;
listDeployments(params: QueryParams): Promise<GithubDeployment[]>;
}
export const githubDeploymentsApiRef = createApiRef<GithubDeploymentsApi>({
@@ -130,11 +123,8 @@ export class GithubDeploymentsApiClient implements GithubDeploymentsApi {
this.scmIntegrationsApi = options.scmIntegrationsApi;
}
async listDeployments(
params: QueryParams,
options: QueryOptions,
): Promise<GithubDeployment[]> {
const baseUrl = getBaseUrl(this.scmIntegrationsApi, options.locations);
async listDeployments(params: QueryParams): Promise<GithubDeployment[]> {
const baseUrl = getBaseUrl(this.scmIntegrationsApi, params.host);
const token = await this.githubAuthApi.getAccessToken(['repo']);
const graphQLWithAuth = graphql.defaults({
@@ -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();
});
@@ -37,18 +37,18 @@ const GithubDeploymentsComponent = ({
projectSlug,
last,
columns,
locations,
host,
}: {
projectSlug: string;
last: number;
locations: string[];
columns: TableColumn<GithubDeployment>[];
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<GithubDeployment>[];
}) => {
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) ? (
<MissingAnnotationEmptyState annotation={GITHUB_PROJECT_SLUG_ANNOTATION} />
@@ -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}
/>
);