From 8f21ebe6295a739dcefaaeabac70f5e03e7158ae Mon Sep 17 00:00:00 2001 From: Tim Soslow Date: Thu, 11 May 2023 23:22:26 -0500 Subject: [PATCH] Remove crumbIssuer support in rebuildProject Crumb issuer support is not required for API calls using a token, so I am simplifying the code to remove it. Signed-off-by: Tim Soslow --- .../src/service/jenkinsApi.test.ts | 69 ------------------- .../jenkins-backend/src/service/jenkinsApi.ts | 36 +--------- 2 files changed, 1 insertion(+), 104 deletions(-) diff --git a/plugins/jenkins-backend/src/service/jenkinsApi.test.ts b/plugins/jenkins-backend/src/service/jenkinsApi.test.ts index 15f4bc651f..76ed1e32e1 100644 --- a/plugins/jenkins-backend/src/service/jenkinsApi.test.ts +++ b/plugins/jenkins-backend/src/service/jenkinsApi.test.ts @@ -20,7 +20,6 @@ import { JenkinsInfo } from './jenkinsInfoProvider'; import { JenkinsBuild, JenkinsProject } from '../types'; import { AuthorizeResult } from '@backstage/plugin-permission-common'; import fetch, { Response } from 'node-fetch'; -import { ResponseError } from '@backstage/errors'; jest.mock('jenkins'); jest.mock('node-fetch'); @@ -54,17 +53,6 @@ const fakePermissionApi = { authorizeConditional: jest.fn(), }; -class NoErrorThrownError extends Error {} - -const getError = async (call: () => unknown): Promise => { - try { - await call(); - throw new NoErrorThrownError(); - } catch (error: unknown) { - return error as TError; - } -}; - describe('JenkinsApi', () => { const jenkinsApi = new JenkinsApiImpl(fakePermissionApi); const mockFetch = fetch as jest.MockedFunction; @@ -722,30 +710,6 @@ describe('JenkinsApi', () => { 'https://jenkins.example.com/job/example-jobName/job/foo/job/bar/19', ); }); - describe('getHeaders', () => { - const crumb = { crumb: 'foobar', crumbRequestField: '.crumb' }; - const json = jest.fn() as jest.MockedFunction; - json.mockResolvedValue(crumb); - const jenkinsInfoCrumb: JenkinsInfo = { ...jenkinsInfo, crumbIssuer: true }; - const jenkinsApiProto = Object.getPrototypeOf(jenkinsApi); - - it('adds crumb', async () => { - mockFetch.mockResolvedValueOnce({ ok: true, json } as Response); - const response = await jenkinsApiProto.getHeaders(jenkinsInfoCrumb); - expect(response).toEqual({ ...jenkinsInfo.headers, '.crumb': 'foobar' }); - }); - it('does not add crumb', async () => { - const response = await jenkinsApiProto.getHeaders(jenkinsInfo); - expect(response).toEqual(jenkinsInfo.headers); - }); - it('fails to get crumb', async () => { - mockFetch.mockResolvedValueOnce({ ok: false } as Response); - const error = await getError(async () => - jenkinsApiProto.getHeaders(jenkinsInfoCrumb), - ); - expect(error).toBeInstanceOf(ResponseError); - }); - }); describe('rebuildProject', () => { it('successfully rebuilds', async () => { mockFetch.mockResolvedValueOnce({ status: 200 } as Response); @@ -784,38 +748,5 @@ describe('JenkinsApi', () => { ); expect(status).toEqual(401); }); - - it('with crumbIssuer option', async () => { - const info: JenkinsInfo = { ...jenkinsInfo, crumbIssuer: true }; - mockFetch.mockResolvedValueOnce({ status: 200 } as Response); - const crumbHeaders = { headerName: 'headerValue', '.crumb': 'bar' }; - - const privateGetHeaders = jest.spyOn( - JenkinsApiImpl.prototype as any, - 'getHeaders', - ); - privateGetHeaders.mockImplementation(() => { - return crumbHeaders; - }); - - const status = await jenkinsApi.rebuildProject( - info, - jobFullName, - buildNumber, - resourceRef, - ); - expect(status).toEqual(200); - - type HeaderResponse = { - headerName: string; - '.crumb': string; - }; - type OptionResponse = { - headers: HeaderResponse; - method: string; - }; - const requestOptions: OptionResponse = mockFetch.mock.calls[0][1] as any; - expect(requestOptions.headers).toStrictEqual(crumbHeaders); - }); }); }); diff --git a/plugins/jenkins-backend/src/service/jenkinsApi.ts b/plugins/jenkins-backend/src/service/jenkinsApi.ts index 135d695e18..53a35c974b 100644 --- a/plugins/jenkins-backend/src/service/jenkinsApi.ts +++ b/plugins/jenkins-backend/src/service/jenkinsApi.ts @@ -28,7 +28,6 @@ import { PermissionEvaluator, } from '@backstage/plugin-permission-common'; import { jenkinsExecutePermission } from '@backstage/plugin-jenkins-common'; -import { ResponseError } from '@backstage/errors'; import fetch, { HeaderInit } from 'node-fetch'; export class JenkinsApiImpl { @@ -165,13 +164,12 @@ export class JenkinsApiImpl { } const buildUrl = this.getBuildUrl(jenkinsInfo, jobFullName, buildNumber); - const headers = await this.getHeaders(jenkinsInfo); // the current SDK only supports triggering a new build // replay the job by triggering request directly from Jenkins api const response = await fetch(`${buildUrl}/replay/rebuild`, { method: 'post', - headers: headers, + headers: jenkinsInfo.headers as HeaderInit, }); return response.status; } @@ -331,36 +329,4 @@ export class JenkinsApiImpl { const jobs = jobFullName.split('/'); return `${jenkinsInfo.baseUrl}/job/${jobs.join('/job/')}/${buildId}`; } - - private async getHeaders(jenkinsInfo: JenkinsInfo): Promise { - let headers = jenkinsInfo.headers as HeaderInit; - if (!jenkinsInfo.crumbIssuer) { - return headers; - } - const response = await fetch( - `${jenkinsInfo.baseUrl}/crumbIssuer/api/json`, - { - method: 'get', - headers: headers, - }, - ); - if (!response.ok) { - throw ResponseError.fromResponse(response); - } - type CrumbResponse = { - crumb: string; - crumbRequestField: string; - }; - - const crumbJson: CrumbResponse = await response.json(); - if ('crumb' in crumbJson && 'crumbRequestField' in crumbJson) { - const headerObject = { - ...jenkinsInfo.headers, - [crumbJson.crumbRequestField]: crumbJson.crumb, - }; - headers = headerObject as HeaderInit; - } - - return headers; - } }