diff --git a/plugins/jenkins/src/api/JenkinsApi.ts b/plugins/jenkins/src/api/JenkinsApi.ts index 48e506953f..ce63f84f13 100644 --- a/plugins/jenkins/src/api/JenkinsApi.ts +++ b/plugins/jenkins/src/api/JenkinsApi.ts @@ -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({ 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; // TODO rename to handle.* ? also, should this be on lastBuild? + onRestartClick: () => Promise; // 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; + }): Promise; } export class JenkinsClient implements JenkinsApi { @@ -198,7 +200,7 @@ export class JenkinsClient implements JenkinsApi { entity: EntityName; jobFullName: string; buildNumber: string; - }): Promise { + }): Promise { 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() { diff --git a/plugins/jenkins/src/components/BuildsPage/lib/CITable/CITable.tsx b/plugins/jenkins/src/components/BuildsPage/lib/CITable/CITable.tsx index e14b24912b..cbc46248cf 100644 --- a/plugins/jenkins/src/components/BuildsPage/lib/CITable/CITable.tsx +++ b/plugins/jenkins/src/components/BuildsPage/lib/CITable/CITable.tsx @@ -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);