From d148022062dc1d9702855625c3395fbf49ec9228 Mon Sep 17 00:00:00 2001 From: Otto Nordander Date: Thu, 4 Aug 2022 12:56:20 +0200 Subject: [PATCH] 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 --- .../api-report.md | 6 +++ .../src/lib/BitbucketServerClient.test.ts | 37 +++++++++++++++++++ .../src/lib/BitbucketServerClient.ts | 12 ++++++ 3 files changed, 55 insertions(+) diff --git a/plugins/catalog-backend-module-bitbucket-server/api-report.md b/plugins/catalog-backend-module-bitbucket-server/api-report.md index b087da6746..1f40f22242 100644 --- a/plugins/catalog-backend-module-bitbucket-server/api-report.md +++ b/plugins/catalog-backend-module-bitbucket-server/api-report.md @@ -20,6 +20,12 @@ export class BitbucketServerClient { config: BitbucketServerIntegrationConfig; }): BitbucketServerClient; // (undocumented) + getFile(options: { + projectKey: string; + repo: string; + path: string; + }): Promise; + // (undocumented) getRepository(options: { projectKey: string; repo: string; diff --git a/plugins/catalog-backend-module-bitbucket-server/src/lib/BitbucketServerClient.test.ts b/plugins/catalog-backend-module-bitbucket-server/src/lib/BitbucketServerClient.test.ts index 6b8043409f..66ccdc8c9a 100644 --- a/plugins/catalog-backend-module-bitbucket-server/src/lib/BitbucketServerClient.test.ts +++ b/plugins/catalog-backend-module-bitbucket-server/src/lib/BitbucketServerClient.test.ts @@ -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( diff --git a/plugins/catalog-backend-module-bitbucket-server/src/lib/BitbucketServerClient.ts b/plugins/catalog-backend-module-bitbucket-server/src/lib/BitbucketServerClient.ts index fa233c6863..025cc555ce 100644 --- a/plugins/catalog-backend-module-bitbucket-server/src/lib/BitbucketServerClient.ts +++ b/plugins/catalog-backend-module-bitbucket-server/src/lib/BitbucketServerClient.ts @@ -60,6 +60,18 @@ export class BitbucketServerClient { ); } + async getFile(options: { + projectKey: string; + repo: string; + path: string; + }): Promise { + 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;