add checks bulk operation

Signed-off-by: goenning <me@goenning.net>
This commit is contained in:
goenning
2022-01-11 09:58:07 +00:00
parent 4c9c45f351
commit a60eb0f0dd
8 changed files with 93 additions and 5 deletions
+6
View File
@@ -7,6 +7,7 @@
import { ApiRef } from '@backstage/core-plugin-api';
import { BackstagePlugin } from '@backstage/core-plugin-api';
import { BulkCheckResponse } from '@backstage/plugin-tech-insights-common';
import { CheckResult } from '@backstage/plugin-tech-insights-common';
import { EntityName } from '@backstage/catalog-model';
import { default as React_2 } from 'react';
@@ -50,6 +51,11 @@ export interface TechInsightsApi {
description?: string,
) => CheckResultRenderer | undefined;
// (undocumented)
runBulkChecks(
entities: EntityName[],
checks?: Check[],
): Promise<BulkCheckResponse>;
// (undocumented)
runChecks(entityParams: EntityName, checks?: Check[]): Promise<CheckResult[]>;
}
@@ -15,7 +15,10 @@
*/
import { createApiRef } from '@backstage/core-plugin-api';
import { CheckResult } from '@backstage/plugin-tech-insights-common';
import {
CheckResult,
BulkCheckResponse,
} from '@backstage/plugin-tech-insights-common';
import { Check } from './types';
import { CheckResultRenderer } from '../components/CheckResultRenderer';
import { EntityName } from '@backstage/catalog-model';
@@ -43,4 +46,8 @@ export interface TechInsightsApi {
) => CheckResultRenderer | undefined;
getAllChecks(): Promise<Check[]>;
runChecks(entityParams: EntityName, checks?: Check[]): Promise<CheckResult[]>;
runBulkChecks(
entities: EntityName[],
checks?: Check[],
): Promise<BulkCheckResponse>;
}
@@ -15,7 +15,10 @@
*/
import { TechInsightsApi } from './TechInsightsApi';
import { CheckResult } from '@backstage/plugin-tech-insights-common';
import {
BulkCheckResponse,
CheckResult,
} from '@backstage/plugin-tech-insights-common';
import { Check } from './types';
import { DiscoveryApi, IdentityApi } from '@backstage/core-plugin-api';
import { ResponseError } from '@backstage/errors';
@@ -97,4 +100,29 @@ export class TechInsightsClient implements TechInsightsApi {
}
return await response.json();
}
async runBulkChecks(
entities: EntityName[],
checks?: Check[],
): Promise<BulkCheckResponse> {
const url = await this.discoveryApi.getBaseUrl('tech-insights');
const token = await this.identityApi.getIdToken();
const checkIds = checks ? checks.map(check => check.id) : [];
const requestBody = {
entities,
checks: checkIds.length > 0 ? checkIds : undefined,
};
const response = await fetch(`${url}/checks/run`, {
method: 'POST',
body: JSON.stringify(requestBody),
headers: {
'Content-Type': 'application/json',
...(token && { Authorization: `Bearer ${token}` }),
},
});
if (!response.ok) {
throw await ResponseError.fromResponse(response);
}
return await response.json();
}
}