Add utility function getFile in BitbucketServerClient
Can be used to fetch arbitrary files given a project and a repository which is helpful when using custom repository parsing. Signed-off-by: Otto Nordander <otto.nordander@resurs.se>
This commit is contained in:
@@ -20,6 +20,12 @@ export class BitbucketServerClient {
|
||||
config: BitbucketServerIntegrationConfig;
|
||||
}): BitbucketServerClient;
|
||||
// (undocumented)
|
||||
getFile(options: {
|
||||
projectKey: string;
|
||||
repo: string;
|
||||
path: string;
|
||||
}): Promise<Response>;
|
||||
// (undocumented)
|
||||
getRepository(options: {
|
||||
projectKey: string;
|
||||
repo: string;
|
||||
|
||||
@@ -26,6 +26,18 @@ import { BitbucketServerProject, BitbucketServerRepository } from './types';
|
||||
|
||||
const server = setupServer();
|
||||
|
||||
const catalogInfoFile = `
|
||||
apiVersion: backstage.io/v1alpha1
|
||||
kind: Component
|
||||
metadata:
|
||||
name: "backstage"
|
||||
description: "A Backstage service"
|
||||
spec:
|
||||
type: service
|
||||
lifecycle: experimental
|
||||
owner: "backstage"
|
||||
`;
|
||||
|
||||
describe('BitbucketServerClient', () => {
|
||||
const config: BitbucketServerIntegrationConfig = {
|
||||
host: 'bitbucket.mycompany.com',
|
||||
@@ -138,6 +150,31 @@ describe('BitbucketServerClient', () => {
|
||||
);
|
||||
});
|
||||
|
||||
it('getFile', async () => {
|
||||
server.use(
|
||||
rest.get(
|
||||
`https://${config.host}/projects/test-project/repos/test-repo/raw/catalog-info.yaml`,
|
||||
(req, res, ctx) => {
|
||||
if (
|
||||
req.headers.get('authorization') !==
|
||||
'Basic dGVzdC11c2VyOnRlc3QtcHc='
|
||||
) {
|
||||
return res(ctx.status(400));
|
||||
}
|
||||
|
||||
return res(ctx.text(catalogInfoFile));
|
||||
},
|
||||
),
|
||||
);
|
||||
|
||||
const response = await client.getFile({
|
||||
projectKey: 'test-project',
|
||||
repo: 'test-repo',
|
||||
path: 'catalog-info.yaml',
|
||||
});
|
||||
expect(await response.text()).toEqual(catalogInfoFile);
|
||||
});
|
||||
|
||||
it('getRepository', async () => {
|
||||
server.use(
|
||||
rest.get(
|
||||
|
||||
@@ -60,6 +60,18 @@ export class BitbucketServerClient {
|
||||
);
|
||||
}
|
||||
|
||||
async getFile(options: {
|
||||
projectKey: string;
|
||||
repo: string;
|
||||
path: string;
|
||||
}): Promise<Response> {
|
||||
const base = new URL(this.config.apiBaseUrl);
|
||||
return fetch(
|
||||
`${base.protocol}//${base.host}/projects/${options.projectKey}/repos/${options.repo}/raw/${options.path}`,
|
||||
getBitbucketServerRequestOptions(this.config),
|
||||
);
|
||||
}
|
||||
|
||||
async getRepository(options: {
|
||||
projectKey: string;
|
||||
repo: string;
|
||||
|
||||
Reference in New Issue
Block a user