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 <tsoslow@shutterstock.com>
This commit is contained in:
@@ -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 <TError>(call: () => unknown): Promise<TError> => {
|
||||
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<typeof fetch>;
|
||||
@@ -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<any>;
|
||||
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);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
@@ -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<HeaderInit> {
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user