From 8dd33a1d081063e8aabc3bf612ddb2fe4145b444 Mon Sep 17 00:00:00 2001 From: Billy Michael Date: Mon, 25 Mar 2024 18:31:40 +0100 Subject: [PATCH 1/3] chore: add examples for bitbucketCloud actions Signed-off-by: Billy Michael --- .changeset/famous-experts-warn.md | 5 + .../actions/bitbucketCloud.examples.test.ts | 248 ++++++++++++++++++ .../src/actions/bitbucketCloud.examples.ts | 132 ++++++++++ .../src/actions/bitbucketCloud.ts | 2 + 4 files changed, 387 insertions(+) create mode 100644 .changeset/famous-experts-warn.md create mode 100644 plugins/scaffolder-backend-module-bitbucket-cloud/src/actions/bitbucketCloud.examples.test.ts create mode 100644 plugins/scaffolder-backend-module-bitbucket-cloud/src/actions/bitbucketCloud.examples.ts diff --git a/.changeset/famous-experts-warn.md b/.changeset/famous-experts-warn.md new file mode 100644 index 0000000000..b8e0b0c40d --- /dev/null +++ b/.changeset/famous-experts-warn.md @@ -0,0 +1,5 @@ +--- +'@backstage/plugin-scaffolder-backend-module-bitbucket-cloud': patch +--- + +Added examples for publish:bitbucketCloud actions diff --git a/plugins/scaffolder-backend-module-bitbucket-cloud/src/actions/bitbucketCloud.examples.test.ts b/plugins/scaffolder-backend-module-bitbucket-cloud/src/actions/bitbucketCloud.examples.test.ts new file mode 100644 index 0000000000..31865904d3 --- /dev/null +++ b/plugins/scaffolder-backend-module-bitbucket-cloud/src/actions/bitbucketCloud.examples.test.ts @@ -0,0 +1,248 @@ +/* + * Copyright 2023 The Backstage Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +jest.mock('@backstage/plugin-scaffolder-node', () => { + return { + ...jest.requireActual('@backstage/plugin-scaffolder-node'), + initRepoAndPush: jest.fn().mockResolvedValue({ + commitHash: '220f19cc36b551763d157f1b5e4a4b446165dbd6', + }), + commitAndPushRepo: jest.fn().mockResolvedValue({ + commitHash: '220f19cc36b551763d157f1b5e4a4b446165dbd6', + }), + }; +}); + +import { createPublishBitbucketCloudAction } from './bitbucketCloud'; +import { rest } from 'msw'; +import { setupServer } from 'msw/node'; +import { setupRequestMockHandlers } from '@backstage/backend-test-utils'; +import { ScmIntegrations } from '@backstage/integration'; +import { ConfigReader } from '@backstage/config'; +import { initRepoAndPush } from '@backstage/plugin-scaffolder-node'; +import yaml from 'yaml'; +import { sep } from 'path'; +import { examples } from './bitbucketCloud.examples'; +import { createMockActionContext } from '@backstage/plugin-scaffolder-node-test-utils'; + +describe('publish:bitbucketCloud', () => { + const config = new ConfigReader({ + integrations: { + bitbucketCloud: [ + { + username: 'x-token-auth', + appPassword: 'your-default-auth-token', + }, + ], + }, + scaffolder: { + defaultCommitMessage: 'initial commit', + defaultAuthor: { + name: 'test_user', + email: 'test_user@BackstageBackend.com', + }, + }, + }); + + const integrations = ScmIntegrations.fromConfig(config); + const action = createPublishBitbucketCloudAction({ integrations, config }); + const mockContext = createMockActionContext({ + input: { + repoUrl: 'bitbucket.org?workspace=workspace&project=project&repo=repo', + repoVisibility: 'private' as const, + }, + }); + const server = setupServer(); + setupRequestMockHandlers(server); + + beforeEach(() => { + jest.resetAllMocks(); + }); + + it('should call initAndPush with the correct values', async () => { + server.use( + rest.post( + 'https://api.bitbucket.org/2.0/repositories/workspace/repo', + (_, res, ctx) => + res( + ctx.status(200), + ctx.set('Content-Type', 'application/json'), + ctx.json({ + links: { + html: { + href: 'https://bitbucket.org/workspace/repo', + }, + clone: [ + { + name: 'https', + href: 'https://bitbucket.org/workspace/cloneurl', + }, + ], + }, + }), + ), + ), + ); + + await action.handler({ + ...mockContext, + input: yaml.parse(examples[0].example).steps[0].input, + }); + + expect(initRepoAndPush).toHaveBeenCalledWith({ + dir: mockContext.workspacePath, + remoteUrl: 'https://bitbucket.org/workspace/cloneurl', + defaultBranch: 'master', + auth: { username: 'x-token-auth', password: 'your-default-auth-token' }, + logger: mockContext.logger, + commitMessage: 'initial commit', + gitAuthorInfo: { + email: 'test_user@BackstageBackend.com', + name: 'test_user', + }, + }); + }); + + it('should call initAndPush with the correct default branch', async () => { + server.use( + rest.post( + 'https://api.bitbucket.org/2.0/repositories/workspace/repo', + (_, res, ctx) => + res( + ctx.status(200), + ctx.set('Content-Type', 'application/json'), + ctx.json({ + links: { + html: { + href: 'https://bitbucket.org/workspace/repo', + }, + clone: [ + { + name: 'https', + href: 'https://bitbucket.org/workspace/cloneurl', + }, + ], + }, + }), + ), + ), + ); + + await action.handler({ + ...mockContext, + input: yaml.parse(examples[3].example).steps[0].input, + }); + + expect(initRepoAndPush).toHaveBeenCalledWith({ + dir: mockContext.workspacePath, + remoteUrl: 'https://bitbucket.org/workspace/cloneurl', + defaultBranch: 'main', + auth: { username: 'x-token-auth', password: 'your-default-auth-token' }, + logger: mockContext.logger, + commitMessage: 'initial commit', + gitAuthorInfo: { + email: 'test_user@BackstageBackend.com', + name: 'test_user', + }, + }); + }); + + it('should call initAndPush with the specified source path', async () => { + server.use( + rest.post( + 'https://api.bitbucket.org/2.0/repositories/workspace/repo', + (_, res, ctx) => + res( + ctx.status(200), + ctx.set('Content-Type', 'application/json'), + ctx.json({ + links: { + html: { + href: 'https://bitbucket.org/workspace/repo', + }, + clone: [ + { + name: 'https', + href: 'https://bitbucket.org/workspace/cloneurl', + }, + ], + }, + }), + ), + ), + ); + + await action.handler({ + ...mockContext, + input: yaml.parse(examples[4].example).steps[0].input, + }); + expect(initRepoAndPush).toHaveBeenCalledWith({ + dir: `${mockContext.workspacePath}${sep}repoRoot`, + remoteUrl: 'https://bitbucket.org/workspace/cloneurl', + auth: { username: 'x-token-auth', password: 'your-default-auth-token' }, + logger: mockContext.logger, + defaultBranch: 'main', + commitMessage: 'initial commit', + gitAuthorInfo: { + email: 'test_user@BackstageBackend.com', + name: 'test_user', + }, + }); + }); + + it('should call initAndPush with the authentication token', async () => { + server.use( + rest.post( + 'https://api.bitbucket.org/2.0/repositories/workspace/repo', + (_, res, ctx) => + res( + ctx.status(200), + ctx.set('Content-Type', 'application/json'), + ctx.json({ + links: { + html: { + href: 'https://bitbucket.org/workspace/repo', + }, + clone: [ + { + name: 'https', + href: 'https://bitbucket.org/workspace/cloneurl', + }, + ], + }, + }), + ), + ), + ); + + await action.handler({ + ...mockContext, + input: yaml.parse(examples[5].example).steps[0].input, + }); + expect(initRepoAndPush).toHaveBeenCalledWith({ + dir: mockContext.workspacePath, + remoteUrl: 'https://bitbucket.org/workspace/cloneurl', + auth: { username: 'x-token-auth', password: 'your-custom-auth-token' }, + logger: mockContext.logger, + defaultBranch: 'main', + commitMessage: 'initial commit', + gitAuthorInfo: { + email: 'test_user@BackstageBackend.com', + name: 'test_user', + }, + }); + }); +}); diff --git a/plugins/scaffolder-backend-module-bitbucket-cloud/src/actions/bitbucketCloud.examples.ts b/plugins/scaffolder-backend-module-bitbucket-cloud/src/actions/bitbucketCloud.examples.ts new file mode 100644 index 0000000000..c0807503cf --- /dev/null +++ b/plugins/scaffolder-backend-module-bitbucket-cloud/src/actions/bitbucketCloud.examples.ts @@ -0,0 +1,132 @@ +/* + * Copyright 2023 The Backstage Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { TemplateExample } from '@backstage/plugin-scaffolder-node'; +import yaml from 'yaml'; + +export const examples: TemplateExample[] = [ + { + description: + 'Initializes a git repository of contents in workspace and publish it to Bitbucket Cloud with default configuration.', + example: yaml.stringify({ + steps: [ + { + id: 'publish', + action: 'publish:bitbucketCloud', + name: 'Publish to Bitbucket Cloud', + input: { + repoUrl: + 'bitbucket.org?repo=repo&workspace=workspace&project=project', + }, + }, + ], + }), + }, + { + description: 'Add a description.', + example: yaml.stringify({ + steps: [ + { + id: 'publish', + action: 'publish:bitbucketCloud', + name: 'Publish to Bitbucket Cloud', + input: { + repoUrl: + 'bitbucket.org?repo=repo&workspace=workspace&project=project', + description: 'Initialize a git repository', + }, + }, + ], + }), + }, + { + description: 'Change visibility of the repository.', + example: yaml.stringify({ + steps: [ + { + id: 'publish', + action: 'publish:bitbucketCloud', + name: 'Publish to Bitbucket Cloud', + input: { + repoUrl: + 'bitbucket.org?repo=repo&workspace=workspace&project=project', + description: 'Initialize a git repository', + repoVisibility: 'public', + }, + }, + ], + }), + }, + { + description: 'Set the default branch.', + example: yaml.stringify({ + steps: [ + { + id: 'publish', + action: 'publish:bitbucketCloud', + name: 'Publish to Bitbucket Cloud', + input: { + repoUrl: + 'bitbucket.org?repo=repo&workspace=workspace&project=project', + description: 'Initialize a git repository', + repoVisibility: 'public', + defaultBranch: 'main', + }, + }, + ], + }), + }, + { + description: 'Specify a source path within the workspace.', + example: yaml.stringify({ + steps: [ + { + id: 'publish', + action: 'publish:bitbucketCloud', + name: 'Publish to Bitbucket Cloud', + input: { + repoUrl: + 'bitbucket.org?repo=repo&workspace=workspace&project=project', + description: 'Initialize a git repository', + repoVisibility: 'public', + defaultBranch: 'main', + sourcePath: './repoRoot', + }, + }, + ], + }), + }, + { + description: 'Provide an authentication token for Bitbucket.', + example: yaml.stringify({ + steps: [ + { + id: 'publish', + action: 'publish:bitbucketCloud', + name: 'Publish to Bitbucket Cloud', + input: { + repoUrl: + 'bitbucket.org?repo=repo&workspace=workspace&project=project', + description: 'Initialize a git repository', + repoVisibility: 'public', + defaultBranch: 'main', + token: 'your-custom-auth-token', + }, + }, + ], + }), + }, +]; diff --git a/plugins/scaffolder-backend-module-bitbucket-cloud/src/actions/bitbucketCloud.ts b/plugins/scaffolder-backend-module-bitbucket-cloud/src/actions/bitbucketCloud.ts index e9d815f5e8..99e8e52a43 100644 --- a/plugins/scaffolder-backend-module-bitbucket-cloud/src/actions/bitbucketCloud.ts +++ b/plugins/scaffolder-backend-module-bitbucket-cloud/src/actions/bitbucketCloud.ts @@ -26,6 +26,7 @@ import fetch, { Response, RequestInit } from 'node-fetch'; import { Config } from '@backstage/config'; import { getAuthorizationHeader } from './helpers'; +import { examples } from './bitbucketCloud.examples'; const createRepository = async (opts: { workspace: string; @@ -114,6 +115,7 @@ export function createPublishBitbucketCloudAction(options: { token?: string; }>({ id: 'publish:bitbucketCloud', + examples, description: 'Initializes a git repository of the content in the workspace, and publishes it to Bitbucket Cloud.', schema: { From b65517bf441daf27b9a45b466c5517c2f844ec3c Mon Sep 17 00:00:00 2001 From: Billy Michael Date: Wed, 27 Mar 2024 21:30:01 +0100 Subject: [PATCH 2/3] fix: update example descriptions to make them more descriptive Signed-off-by: Billy Michael --- .../actions/bitbucketCloud.examples.test.ts | 45 ++++++++++++++++++- .../src/actions/bitbucketCloud.examples.ts | 40 ++++++++++++----- 2 files changed, 72 insertions(+), 13 deletions(-) diff --git a/plugins/scaffolder-backend-module-bitbucket-cloud/src/actions/bitbucketCloud.examples.test.ts b/plugins/scaffolder-backend-module-bitbucket-cloud/src/actions/bitbucketCloud.examples.test.ts index 31865904d3..5e95f41751 100644 --- a/plugins/scaffolder-backend-module-bitbucket-cloud/src/actions/bitbucketCloud.examples.test.ts +++ b/plugins/scaffolder-backend-module-bitbucket-cloud/src/actions/bitbucketCloud.examples.test.ts @@ -194,7 +194,7 @@ describe('publish:bitbucketCloud', () => { remoteUrl: 'https://bitbucket.org/workspace/cloneurl', auth: { username: 'x-token-auth', password: 'your-default-auth-token' }, logger: mockContext.logger, - defaultBranch: 'main', + defaultBranch: 'master', commitMessage: 'initial commit', gitAuthorInfo: { email: 'test_user@BackstageBackend.com', @@ -232,6 +232,49 @@ describe('publish:bitbucketCloud', () => { ...mockContext, input: yaml.parse(examples[5].example).steps[0].input, }); + expect(initRepoAndPush).toHaveBeenCalledWith({ + dir: mockContext.workspacePath, + remoteUrl: 'https://bitbucket.org/workspace/cloneurl', + auth: { username: 'x-token-auth', password: 'your-custom-auth-token' }, + logger: mockContext.logger, + defaultBranch: 'master', + commitMessage: 'initial commit', + gitAuthorInfo: { + email: 'test_user@BackstageBackend.com', + name: 'test_user', + }, + }); + }); + + it('should call initAndPush with all available custom inputs', async () => { + server.use( + rest.post( + 'https://api.bitbucket.org/2.0/repositories/workspace/repo', + (_, res, ctx) => + res( + ctx.status(200), + ctx.set('Content-Type', 'application/json'), + ctx.json({ + links: { + html: { + href: 'https://bitbucket.org/workspace/repo', + }, + clone: [ + { + name: 'https', + href: 'https://bitbucket.org/workspace/cloneurl', + }, + ], + }, + }), + ), + ), + ); + + await action.handler({ + ...mockContext, + input: yaml.parse(examples[6].example).steps[0].input, + }); expect(initRepoAndPush).toHaveBeenCalledWith({ dir: mockContext.workspacePath, remoteUrl: 'https://bitbucket.org/workspace/cloneurl', diff --git a/plugins/scaffolder-backend-module-bitbucket-cloud/src/actions/bitbucketCloud.examples.ts b/plugins/scaffolder-backend-module-bitbucket-cloud/src/actions/bitbucketCloud.examples.ts index c0807503cf..041ea86beb 100644 --- a/plugins/scaffolder-backend-module-bitbucket-cloud/src/actions/bitbucketCloud.examples.ts +++ b/plugins/scaffolder-backend-module-bitbucket-cloud/src/actions/bitbucketCloud.examples.ts @@ -20,7 +20,7 @@ import yaml from 'yaml'; export const examples: TemplateExample[] = [ { description: - 'Initializes a git repository of contents in workspace and publish it to Bitbucket Cloud with default configuration.', + 'Initializes a git repository and publishes it to Bitbucket Cloud with the default configuration.', example: yaml.stringify({ steps: [ { @@ -36,7 +36,7 @@ export const examples: TemplateExample[] = [ }), }, { - description: 'Add a description.', + description: 'Initializes a git repository with a custom description.', example: yaml.stringify({ steps: [ { @@ -53,7 +53,8 @@ export const examples: TemplateExample[] = [ }), }, { - description: 'Change visibility of the repository.', + description: + 'Initializes a git repository and sets repo visibility to public.', example: yaml.stringify({ steps: [ { @@ -63,7 +64,6 @@ export const examples: TemplateExample[] = [ input: { repoUrl: 'bitbucket.org?repo=repo&workspace=workspace&project=project', - description: 'Initialize a git repository', repoVisibility: 'public', }, }, @@ -71,7 +71,8 @@ export const examples: TemplateExample[] = [ }), }, { - description: 'Set the default branch.', + description: + 'Initializes a git repository and sets default branch to main.', example: yaml.stringify({ steps: [ { @@ -81,8 +82,6 @@ export const examples: TemplateExample[] = [ input: { repoUrl: 'bitbucket.org?repo=repo&workspace=workspace&project=project', - description: 'Initialize a git repository', - repoVisibility: 'public', defaultBranch: 'main', }, }, @@ -90,7 +89,8 @@ export const examples: TemplateExample[] = [ }), }, { - description: 'Specify a source path within the workspace.', + description: + 'Initializes a git repository with the contents of the specified path.', example: yaml.stringify({ steps: [ { @@ -100,9 +100,6 @@ export const examples: TemplateExample[] = [ input: { repoUrl: 'bitbucket.org?repo=repo&workspace=workspace&project=project', - description: 'Initialize a git repository', - repoVisibility: 'public', - defaultBranch: 'main', sourcePath: './repoRoot', }, }, @@ -110,7 +107,26 @@ export const examples: TemplateExample[] = [ }), }, { - description: 'Provide an authentication token for Bitbucket.', + description: + 'Initializes a git repository using a custom authentication token', + example: yaml.stringify({ + steps: [ + { + id: 'publish', + action: 'publish:bitbucketCloud', + name: 'Publish to Bitbucket Cloud', + input: { + repoUrl: + 'bitbucket.org?repo=repo&workspace=workspace&project=project', + token: 'your-custom-auth-token', + }, + }, + ], + }), + }, + { + description: + 'Initializes a git repository using all of the custom options together', example: yaml.stringify({ steps: [ { From 310e7af41cb961732290f057d28e8d27ef09840c Mon Sep 17 00:00:00 2001 From: Billy Michael Date: Wed, 27 Mar 2024 22:35:38 +0100 Subject: [PATCH 3/3] chore: bring example descriptions in line with the Gerrit actions Signed-off-by: Billy Michael --- .../src/actions/bitbucketCloud.examples.ts | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/plugins/scaffolder-backend-module-bitbucket-cloud/src/actions/bitbucketCloud.examples.ts b/plugins/scaffolder-backend-module-bitbucket-cloud/src/actions/bitbucketCloud.examples.ts index 041ea86beb..2d48c99949 100644 --- a/plugins/scaffolder-backend-module-bitbucket-cloud/src/actions/bitbucketCloud.examples.ts +++ b/plugins/scaffolder-backend-module-bitbucket-cloud/src/actions/bitbucketCloud.examples.ts @@ -20,7 +20,7 @@ import yaml from 'yaml'; export const examples: TemplateExample[] = [ { description: - 'Initializes a git repository and publishes it to Bitbucket Cloud with the default configuration.', + 'Initializes a Bitbucket Cloud repository of contents in workspace and publish it to Bitbucket Cloud with default configuration.', example: yaml.stringify({ steps: [ { @@ -36,7 +36,7 @@ export const examples: TemplateExample[] = [ }), }, { - description: 'Initializes a git repository with a custom description.', + description: 'Initializes a Bitbucket Cloud repository with a description.', example: yaml.stringify({ steps: [ { @@ -54,7 +54,7 @@ export const examples: TemplateExample[] = [ }, { description: - 'Initializes a git repository and sets repo visibility to public.', + 'Initializes a Bitbucket Cloud repository with public repo visibility, if not set defaults to private', example: yaml.stringify({ steps: [ { @@ -72,7 +72,7 @@ export const examples: TemplateExample[] = [ }, { description: - 'Initializes a git repository and sets default branch to main.', + 'Initializes a Bitbucket Cloud repository with a default Branch, if not set defaults to master', example: yaml.stringify({ steps: [ { @@ -90,7 +90,7 @@ export const examples: TemplateExample[] = [ }, { description: - 'Initializes a git repository with the contents of the specified path.', + 'Path within the workspace that will be used as the repository root. If omitted, the entire workspace will be published as the repository', example: yaml.stringify({ steps: [ { @@ -108,7 +108,7 @@ export const examples: TemplateExample[] = [ }, { description: - 'Initializes a git repository using a custom authentication token', + 'Initializes a Bitbucket Cloud repository with a custom authentication token', example: yaml.stringify({ steps: [ { @@ -126,7 +126,7 @@ export const examples: TemplateExample[] = [ }, { description: - 'Initializes a git repository using all of the custom options together', + 'Initializes a Bitbucket Cloud repository with all proporties being set', example: yaml.stringify({ steps: [ {