From 4330a51457bf4f12cc5e0a40756ac076eec008d6 Mon Sep 17 00:00:00 2001 From: blam Date: Sun, 10 Jan 2021 00:59:44 +0100 Subject: [PATCH] feat: porting bitbucket preparer to use integrations cofnig --- .../stages/prepare/bitbucket.test.ts | 104 +++++++++++++++--- .../scaffolder/stages/prepare/bitbucket.ts | 19 ++-- 2 files changed, 102 insertions(+), 21 deletions(-) diff --git a/plugins/scaffolder-backend/src/scaffolder/stages/prepare/bitbucket.test.ts b/plugins/scaffolder-backend/src/scaffolder/stages/prepare/bitbucket.test.ts index 809d18c808..0be61a9f82 100644 --- a/plugins/scaffolder-backend/src/scaffolder/stages/prepare/bitbucket.test.ts +++ b/plugins/scaffolder-backend/src/scaffolder/stages/prepare/bitbucket.test.ts @@ -30,6 +30,7 @@ import { ConfigReader } from '@backstage/config'; describe('BitbucketPreparer', () => { let mockEntity: TemplateEntityV1alpha1; + const logger = getVoidLogger(); const mockGitClient = { clone: jest.fn(), }; @@ -79,8 +80,8 @@ describe('BitbucketPreparer', () => { }); it('calls the clone command with the correct arguments for a repository', async () => { - const preparer = new BitbucketPreparer(new ConfigReader({})); - await preparer.prepare(mockEntity, { logger: getVoidLogger() }); + const preparer = new BitbucketPreparer(new ConfigReader({}), { logger }); + await preparer.prepare(mockEntity); expect(mockGitClient.clone).toHaveBeenCalledWith({ url: 'https://bitbucket.org/backstage-project/backstage-repo', dir: expect.any(String), @@ -100,18 +101,22 @@ describe('BitbucketPreparer', () => { ], }, }), + { logger }, ); - await preparer.prepare(mockEntity, { logger: getVoidLogger() }); - expect(mockGitClient.clone).toHaveBeenCalledWith({ - url: 'https://bitbucket.org/backstage-project/backstage-repo', - dir: expect.any(String), + + await preparer.prepare(mockEntity); + + expect(Git.fromAuth).toHaveBeenCalledWith({ + logger, + username: 'fake-user', + password: 'fake-password', }); }); it('calls the clone command with the correct arguments for a repository when no path is provided', async () => { - const preparer = new BitbucketPreparer(new ConfigReader({})); + const preparer = new BitbucketPreparer(new ConfigReader({}), { logger }); delete mockEntity.spec.path; - await preparer.prepare(mockEntity, { logger: getVoidLogger() }); + await preparer.prepare(mockEntity); expect(mockGitClient.clone).toHaveBeenCalledWith({ url: 'https://bitbucket.org/backstage-project/backstage-repo', dir: expect.any(String), @@ -119,22 +124,93 @@ describe('BitbucketPreparer', () => { }); it('return the temp directory with the path to the folder if it is specified', async () => { - const preparer = new BitbucketPreparer(new ConfigReader({})); + const preparer = new BitbucketPreparer(new ConfigReader({}), { logger }); mockEntity.spec.path = './template/test/1/2/3'; - const response = await preparer.prepare(mockEntity, { - logger: getVoidLogger(), - }); + const response = await preparer.prepare(mockEntity); expect(response.split('\\').join('/')).toMatch( /\/template\/test\/1\/2\/3$/, ); }); + it('calls the clone command with deprecated auth method', async () => { + const preparer = new BitbucketPreparer( + new ConfigReader({ + scaffolder: { + bitbucket: { + api: { + username: 'fakeusername', + token: 'faketoken', + }, + }, + }, + }), + { logger }, + ); + + await preparer.prepare(mockEntity); + + expect(Git.fromAuth).toHaveBeenCalledWith({ + logger, + username: 'fakeusername', + password: 'faketoken', + }); + }); + + it('calls the clone command with integrations config for auth method', async () => { + const preparer = new BitbucketPreparer( + new ConfigReader({ + integrations: { + bitbucket: [ + { + host: 'bitbucket.org', + username: 'asd3', + token: 'faketoken', + }, + ], + }, + }), + { logger }, + ); + + await preparer.prepare(mockEntity); + + expect(Git.fromAuth).toHaveBeenCalledWith({ + logger, + username: 'asd3', + password: 'faketoken', + }); + }); + + it('calls the clone command with integrations config with appPassword for auth method', async () => { + const preparer = new BitbucketPreparer( + new ConfigReader({ + integrations: { + bitbucket: [ + { + host: 'bitbucket.org', + username: 'asd3', + appPassword: 'myapppassword', + }, + ], + }, + }), + { logger }, + ); + + await preparer.prepare(mockEntity); + + expect(Git.fromAuth).toHaveBeenCalledWith({ + logger, + username: 'asd3', + password: 'myapppassword', + }); + }); + it('return the working directory with the path to the folder if it is specified', async () => { - const preparer = new BitbucketPreparer(new ConfigReader({})); + const preparer = new BitbucketPreparer(new ConfigReader({}), { logger }); mockEntity.spec.path = './template/test/1/2/3'; const response = await preparer.prepare(mockEntity, { - logger: getVoidLogger(), workingDirectory: '/workDir', }); diff --git a/plugins/scaffolder-backend/src/scaffolder/stages/prepare/bitbucket.ts b/plugins/scaffolder-backend/src/scaffolder/stages/prepare/bitbucket.ts index b992861c57..cc647982d8 100644 --- a/plugins/scaffolder-backend/src/scaffolder/stages/prepare/bitbucket.ts +++ b/plugins/scaffolder-backend/src/scaffolder/stages/prepare/bitbucket.ts @@ -59,7 +59,7 @@ export class BitbucketPreparer implements PreparerBase { async prepare( template: TemplateEntityV1alpha1, - opts: PreparerOptions, + opts?: PreparerOptions, ): Promise { const { protocol, location } = parseLocationAnnotation(template); const workingDirectory = opts?.workingDirectory ?? os.tmpdir(); @@ -113,20 +113,25 @@ export class BitbucketPreparer implements PreparerBase { c => c.host === host, ); - // TODO(blam): Not sure how appPassword fits in here. Just doing the most simple of - // implementations with the intergations config for now but can maybe fallback to - // appPassword instead maybe at a later stage. + if (!bitbucketIntegrationConfig) { + return undefined; + } + if ( - !bitbucketIntegrationConfig || !bitbucketIntegrationConfig.username || - !bitbucketIntegrationConfig.token + !( + bitbucketIntegrationConfig.token || + bitbucketIntegrationConfig.appPassword + ) ) { return undefined; } return { username: bitbucketIntegrationConfig.username, - password: bitbucketIntegrationConfig.token, + password: + bitbucketIntegrationConfig.token! || + bitbucketIntegrationConfig.appPassword!, }; } }