remobe base response model and throw an error if jenkins:rebuild fails

Signed-off-by: Hasan Ozdemir <21654050+nodify-at@users.noreply.github.com>
This commit is contained in:
Hasan Ozdemir
2021-12-17 02:39:54 +01:00
parent cc0f94218a
commit bc3695fe7a
2 changed files with 20 additions and 15 deletions
+10 -4
View File
@@ -20,6 +20,8 @@ import {
IdentityApi,
} from '@backstage/core-plugin-api';
import type { EntityName, EntityRef } from '@backstage/catalog-model';
// eslint-disable-next-line import/no-extraneous-dependencies
import { ResponseError } from '@backstage/errors';
export const jenkinsApiRef = createApiRef<JenkinsApi>({
id: 'plugin.jenkins.service2',
@@ -66,7 +68,7 @@ export interface Project {
inQueue: string;
// added by us
status: string; // == inQueue ? 'queued' : lastBuild.building ? 'running' : lastBuild.result,
onRestartClick: () => Promise<Response>; // TODO rename to handle.* ? also, should this be on lastBuild?
onRestartClick: () => Promise<void>; // TODO rename to handle.* ? also, should this be on lastBuild?
}
export interface JenkinsApi {
@@ -106,7 +108,7 @@ export interface JenkinsApi {
entity: EntityName;
jobFullName: string;
buildNumber: string;
}): Promise<Response>;
}): Promise<void>;
}
export class JenkinsClient implements JenkinsApi {
@@ -198,7 +200,7 @@ export class JenkinsClient implements JenkinsApi {
entity: EntityName;
jobFullName: string;
buildNumber: string;
}): Promise<Response> {
}): Promise<void> {
const url = `${await this.discoveryApi.getBaseUrl(
'jenkins',
)}/v1/entity/${encodeURIComponent(entity.namespace)}/${encodeURIComponent(
@@ -208,12 +210,16 @@ export class JenkinsClient implements JenkinsApi {
)}/${encodeURIComponent(buildNumber)}:rebuild`;
const idToken = await this.getToken();
return fetch(url, {
const response = await fetch(url, {
method: 'POST',
headers: {
...(idToken && { Authorization: `Bearer ${idToken}` }),
},
});
if (!response.ok) {
throw await ResponseError.fromResponse(response);
}
}
private async getToken() {
@@ -24,6 +24,8 @@ import { buildRouteRef } from '../../../../plugin';
import { Progress, Table, TableColumn } from '@backstage/core-components';
import { Project } from '../../../../api/JenkinsApi';
import { alertApiRef, useApi, useRouteRef } from '@backstage/core-plugin-api';
// eslint-disable-next-line import/no-extraneous-dependencies
import { ResponseError } from '@backstage/errors';
const FailCount = ({ count }: { count: number }): JSX.Element | null => {
if (count !== 0) {
@@ -182,20 +184,17 @@ const generatedColumns: TableColumn[] = [
if (row.onRestartClick) {
setIsLoadingRebuild(true);
try {
const response = await row.onRestartClick();
const body = (await response.json()) as {
error?: { message: string };
};
if (response.status !== 200) {
await row.onRestartClick();
alertApi.post({
message: 'Jenkins re-build has been successfully executed',
severity: 'success',
});
} catch (e) {
if (e instanceof ResponseError) {
alertApi.post({
message: `Jenkins re-build has been failed. Reason: ${body.error?.message}`,
message: `Jenkins re-build has been failed. Error: ${e.message}`,
severity: 'error',
});
} else {
alertApi.post({
message: 'Jenkins re-build has been successfully executed',
severity: 'success',
});
}
} finally {
setIsLoadingRebuild(false);