diff --git a/plugins/scaffolder-backend/src/scaffolder/actions/builtin/bitbucketCloud/bitbucketCloudPipelinesRun.examples.test.ts b/plugins/scaffolder-backend/src/scaffolder/actions/builtin/bitbucketCloud/bitbucketCloudPipelinesRun.examples.test.ts index 6205f04400..e53099eb2b 100644 --- a/plugins/scaffolder-backend/src/scaffolder/actions/builtin/bitbucketCloud/bitbucketCloudPipelinesRun.examples.test.ts +++ b/plugins/scaffolder-backend/src/scaffolder/actions/builtin/bitbucketCloud/bitbucketCloudPipelinesRun.examples.test.ts @@ -22,8 +22,6 @@ import { createBitbucketPipelinesRunAction } from './bitbucketCloudPipelinesRun' import fetch from 'node-fetch'; import yaml from 'yaml'; import { examples } from './bitbucketCloudPipelinesRun.examples'; -import { json } from 'stream/consumers'; -import { object } from 'zod'; jest.mock('node-fetch'); const { Response } = jest.requireActual('node-fetch'); @@ -70,36 +68,12 @@ describe('bitbucket:pipelines:run', () => { createTemporaryDirectory: jest.fn(), }; - const body = { - type: '', - uuid: '', - build_number: 33, - creator: { - type: '', - }, - repository: { - type: '', - }, - target: { - type: '', - }, - trigger: { - type: '', - }, - state: { - type: '', - }, - created_on: '', - completed_on: '', - build_seconds_used: 50, - }; - beforeEach(() => { jest.resetAllMocks(); action = createBitbucketPipelinesRunAction(); }); - it('should call the bitbucket api for running a pipeline', async () => { + it('should trigger a pipeline for a branch', async () => { const ctx = Object.assign({}, mockContext, { input: yaml.parse(examples[0].example).steps[0].input, }); @@ -114,7 +88,230 @@ describe('bitbucket:pipelines:run', () => { expect(fetch).toHaveBeenCalledWith( 'https://api.bitbucket.org/2.0/repositories/test-workspace/test-repo-slug/pipelines', { - body: JSON.stringify(body), + body: JSON.stringify({ + target: { + ref_type: 'branch', + type: 'pipeline_ref_target', + ref_name: 'master', + }, + }), + headers: { + Accept: 'application/json', + Authorization: 'Bearer testToken', + 'Content-Type': 'application/json', + }, + method: 'POST', + }, + ); + expect(logStream.write).toHaveBeenCalledTimes(2); + expect(logStream.write).toHaveBeenNthCalledWith(1, 'Response: 201 Created'); + expect(logStream.write).toHaveBeenNthCalledWith(2, createdResponse); + }); + + it('should trigger a pipeline for a commit on a branch', async () => { + const ctx = Object.assign({}, mockContext, { + input: yaml.parse(examples[1].example).steps[0].input, + }); + (fetch as jest.MockedFunction).mockResolvedValue( + new Response(JSON.stringify(createdResponse), { + url: 'url', + status: 201, + statusText: 'Created', + }), + ); + await action.handler(ctx); + expect(fetch).toHaveBeenCalledWith( + 'https://api.bitbucket.org/2.0/repositories/test-workspace/test-repo-slug/pipelines', + { + body: JSON.stringify({ + target: { + commit: { + type: 'commit', + hash: 'ce5b7431602f7cbba007062eeb55225c6e18e956', + }, + ref_type: 'branch', + type: 'pipeline_ref_target', + ref_name: 'master', + }, + }), + headers: { + Accept: 'application/json', + Authorization: 'Bearer testToken', + 'Content-Type': 'application/json', + }, + method: 'POST', + }, + ); + expect(logStream.write).toHaveBeenCalledTimes(2); + expect(logStream.write).toHaveBeenNthCalledWith(1, 'Response: 201 Created'); + expect(logStream.write).toHaveBeenNthCalledWith(2, createdResponse); + }); + + it('should trigger a specific pipeline definition for a commit', async () => { + const ctx = Object.assign({}, mockContext, { + input: yaml.parse(examples[2].example).steps[0].input, + }); + (fetch as jest.MockedFunction).mockResolvedValue( + new Response(JSON.stringify(createdResponse), { + url: 'url', + status: 201, + statusText: 'Created', + }), + ); + await action.handler(ctx); + expect(fetch).toHaveBeenCalledWith( + 'https://api.bitbucket.org/2.0/repositories/test-workspace/test-repo-slug/pipelines', + { + body: JSON.stringify({ + target: { + commit: { + type: 'commit', + hash: 'a3c4e02c9a3755eccdc3764e6ea13facdf30f923', + }, + selector: { + type: 'custom', + pattern: 'Deploy to production', + }, + type: 'pipeline_commit_target', + }, + }), + headers: { + Accept: 'application/json', + Authorization: 'Bearer testToken', + 'Content-Type': 'application/json', + }, + method: 'POST', + }, + ); + expect(logStream.write).toHaveBeenCalledTimes(2); + expect(logStream.write).toHaveBeenNthCalledWith(1, 'Response: 201 Created'); + expect(logStream.write).toHaveBeenNthCalledWith(2, createdResponse); + }); + + it('should trigger a specific pipeline definition for a commit on a branch or tag', async () => { + const ctx = Object.assign({}, mockContext, { + input: yaml.parse(examples[3].example).steps[0].input, + }); + (fetch as jest.MockedFunction).mockResolvedValue( + new Response(JSON.stringify(createdResponse), { + url: 'url', + status: 201, + statusText: 'Created', + }), + ); + await action.handler(ctx); + expect(fetch).toHaveBeenCalledWith( + 'https://api.bitbucket.org/2.0/repositories/test-workspace/test-repo-slug/pipelines', + { + body: JSON.stringify({ + target: { + commit: { + type: 'commit', + hash: 'a3c4e02c9a3755eccdc3764e6ea13facdf30f923', + }, + selector: { + type: 'custom', + pattern: 'Deploy to production', + }, + type: 'pipeline_ref_target', + ref_name: 'master', + ref_type: 'branch', + }, + }), + headers: { + Accept: 'application/json', + Authorization: 'Bearer testToken', + 'Content-Type': 'application/json', + }, + method: 'POST', + }, + ); + expect(logStream.write).toHaveBeenCalledTimes(2); + expect(logStream.write).toHaveBeenNthCalledWith(1, 'Response: 201 Created'); + expect(logStream.write).toHaveBeenNthCalledWith(2, createdResponse); + }); + + it('should trigger a custom pipeline with variables', async () => { + const ctx = Object.assign({}, mockContext, { + input: yaml.parse(examples[4].example).steps[0].input, + }); + (fetch as jest.MockedFunction).mockResolvedValue( + new Response(JSON.stringify(createdResponse), { + url: 'url', + status: 201, + statusText: 'Created', + }), + ); + await action.handler(ctx); + expect(fetch).toHaveBeenCalledWith( + 'https://api.bitbucket.org/2.0/repositories/test-workspace/test-repo-slug/pipelines', + { + body: JSON.stringify({ + target: { + type: 'pipeline_ref_target', + ref_name: 'master', + ref_type: 'branch', + selector: { + type: 'custom', + pattern: 'Deploy to production', + }, + }, + variables: [ + { key: 'var1key', value: 'var1value', secured: true }, + { + key: 'var2key', + value: 'var2value', + }, + ], + }), + headers: { + Accept: 'application/json', + Authorization: 'Bearer testToken', + 'Content-Type': 'application/json', + }, + method: 'POST', + }, + ); + expect(logStream.write).toHaveBeenCalledTimes(2); + expect(logStream.write).toHaveBeenNthCalledWith(1, 'Response: 201 Created'); + expect(logStream.write).toHaveBeenNthCalledWith(2, createdResponse); + }); + + it('should trigger a pull request pipeline', async () => { + const ctx = Object.assign({}, mockContext, { + input: yaml.parse(examples[5].example).steps[0].input, + }); + (fetch as jest.MockedFunction).mockResolvedValue( + new Response(JSON.stringify(createdResponse), { + url: 'url', + status: 201, + statusText: 'Created', + }), + ); + await action.handler(ctx); + expect(fetch).toHaveBeenCalledWith( + 'https://api.bitbucket.org/2.0/repositories/test-workspace/test-repo-slug/pipelines', + { + body: JSON.stringify({ + target: { + type: 'pipeline_pullrequest_target', + source: 'pull-request-branch', + destination: 'master', + destination_commit: { + hash: '9f848b7', + }, + commit: { + hash: '1a372fc', + }, + pull_request: { + id: '3', + }, + selector: { + type: 'pull-requests', + pattern: '**', + }, + }, + }), headers: { Accept: 'application/json', Authorization: 'Bearer testToken', diff --git a/plugins/scaffolder-backend/src/scaffolder/actions/builtin/bitbucketCloud/bitbucketCloudPipelinesRun.examples.ts b/plugins/scaffolder-backend/src/scaffolder/actions/builtin/bitbucketCloud/bitbucketCloudPipelinesRun.examples.ts index 81b8603c35..d6255526af 100644 --- a/plugins/scaffolder-backend/src/scaffolder/actions/builtin/bitbucketCloud/bitbucketCloudPipelinesRun.examples.ts +++ b/plugins/scaffolder-backend/src/scaffolder/actions/builtin/bitbucketCloud/bitbucketCloudPipelinesRun.examples.ts @@ -19,7 +19,7 @@ import yaml from 'yaml'; export const examples: TemplateExample[] = [ { - description: '', + description: 'Trigger a pipeline for a branch', example: yaml.stringify({ steps: [ { @@ -29,6 +29,171 @@ export const examples: TemplateExample[] = [ input: { workspace: 'test-workspace', repo_slug: 'test-repo-slug', + body: { + target: { + ref_type: 'branch', + type: 'pipeline_ref_target', + ref_name: 'master', + }, + }, + }, + }, + ], + }), + }, + { + description: 'Trigger a pipeline for a commit on a branch', + example: yaml.stringify({ + steps: [ + { + action: 'bitbucket:pipelines:run', + id: 'run-bitbucket-pipeline', + name: 'Run an example bitbucket pipeline', + input: { + workspace: 'test-workspace', + repo_slug: 'test-repo-slug', + body: { + target: { + commit: { + type: 'commit', + hash: 'ce5b7431602f7cbba007062eeb55225c6e18e956', + }, + ref_type: 'branch', + type: 'pipeline_ref_target', + ref_name: 'master', + }, + }, + }, + }, + ], + }), + }, + { + description: 'Trigger a specific pipeline definition for a commit', + example: yaml.stringify({ + steps: [ + { + action: 'bitbucket:pipelines:run', + id: 'run-bitbucket-pipeline', + name: 'Run an example bitbucket pipeline', + input: { + workspace: 'test-workspace', + repo_slug: 'test-repo-slug', + body: { + target: { + commit: { + type: 'commit', + hash: 'a3c4e02c9a3755eccdc3764e6ea13facdf30f923', + }, + selector: { + type: 'custom', + pattern: 'Deploy to production', + }, + type: 'pipeline_commit_target', + }, + }, + }, + }, + ], + }), + }, + { + description: + 'Trigger a specific pipeline definition for a commit on a branch or tag', + example: yaml.stringify({ + steps: [ + { + action: 'bitbucket:pipelines:run', + id: 'run-bitbucket-pipeline', + name: 'Run an example bitbucket pipeline', + input: { + workspace: 'test-workspace', + repo_slug: 'test-repo-slug', + body: { + target: { + commit: { + type: 'commit', + hash: 'a3c4e02c9a3755eccdc3764e6ea13facdf30f923', + }, + selector: { + type: 'custom', + pattern: 'Deploy to production', + }, + type: 'pipeline_ref_target', + ref_name: 'master', + ref_type: 'branch', + }, + }, + }, + }, + ], + }), + }, + { + description: 'Trigger a custom pipeline with variables', + example: yaml.stringify({ + steps: [ + { + action: 'bitbucket:pipelines:run', + id: 'run-bitbucket-pipeline', + name: 'Run an example bitbucket pipeline', + input: { + workspace: 'test-workspace', + repo_slug: 'test-repo-slug', + body: { + target: { + type: 'pipeline_ref_target', + ref_name: 'master', + ref_type: 'branch', + selector: { + type: 'custom', + pattern: 'Deploy to production', + }, + }, + variables: [ + { key: 'var1key', value: 'var1value', secured: true }, + { + key: 'var2key', + value: 'var2value', + }, + ], + }, + }, + }, + ], + }), + }, + { + description: 'Trigger a pull request pipeline', + example: yaml.stringify({ + steps: [ + { + action: 'bitbucket:pipelines:run', + id: 'run-bitbucket-pipeline', + name: 'Run an example bitbucket pipeline', + input: { + workspace: 'test-workspace', + repo_slug: 'test-repo-slug', + body: { + target: { + type: 'pipeline_pullrequest_target', + source: 'pull-request-branch', + destination: 'master', + destination_commit: { + hash: '9f848b7', + }, + commit: { + hash: '1a372fc', + }, + pull_request: { + id: '3', + }, + selector: { + type: 'pull-requests', + pattern: '**', + }, + }, + }, }, }, ], diff --git a/plugins/scaffolder-backend/src/scaffolder/actions/builtin/bitbucketCloud/bitbucketCloudPipelinesRun.test.ts b/plugins/scaffolder-backend/src/scaffolder/actions/builtin/bitbucketCloud/bitbucketCloudPipelinesRun.test.ts index 741ae02e92..d5f9da2f5d 100644 --- a/plugins/scaffolder-backend/src/scaffolder/actions/builtin/bitbucketCloud/bitbucketCloudPipelinesRun.test.ts +++ b/plugins/scaffolder-backend/src/scaffolder/actions/builtin/bitbucketCloud/bitbucketCloudPipelinesRun.test.ts @@ -66,30 +66,6 @@ describe('bitbucket:pipelines:run', () => { createTemporaryDirectory: jest.fn(), }; - const body = { - type: '', - uuid: '', - build_number: 33, - creator: { - type: '', - }, - repository: { - type: '', - }, - target: { - type: '', - }, - trigger: { - type: '', - }, - state: { - type: '', - }, - created_on: '', - completed_on: '', - build_seconds_used: 50, - }; - beforeEach(() => { jest.resetAllMocks(); action = createBitbucketPipelinesRunAction(); @@ -112,7 +88,7 @@ describe('bitbucket:pipelines:run', () => { expect(fetch).toHaveBeenCalledWith( 'https://api.bitbucket.org/2.0/repositories/test-workspace/test-repo-slug/pipelines', { - body: JSON.stringify(body), + body: {}, headers: { Accept: 'application/json', Authorization: 'Bearer testToken', diff --git a/plugins/scaffolder-backend/src/scaffolder/actions/builtin/bitbucketCloud/bitbucketCloudPipelinesRun.ts b/plugins/scaffolder-backend/src/scaffolder/actions/builtin/bitbucketCloud/bitbucketCloudPipelinesRun.ts index b732ce2689..fa6043621d 100644 --- a/plugins/scaffolder-backend/src/scaffolder/actions/builtin/bitbucketCloud/bitbucketCloudPipelinesRun.ts +++ b/plugins/scaffolder-backend/src/scaffolder/actions/builtin/bitbucketCloud/bitbucketCloudPipelinesRun.ts @@ -28,6 +28,7 @@ export const createBitbucketPipelinesRunAction = () => { return createTemplateAction<{ workspace: string; repo_slug: string; + body?: object; }>({ id, description: '', @@ -47,35 +48,114 @@ export const createBitbucketPipelinesRunAction = () => { description: 'The repository name', type: 'string', }, + body: { + title: '', + description: '', + type: 'object', + properties: { + target: { + title: 'target', + type: 'object', + properties: { + ref_type: { + title: 'ref_type', + type: 'string', + }, + type: { + title: 'type', + type: 'string', + }, + ref_name: { + title: 'ref_name', + type: 'string', + }, + source: { + title: 'source', + type: 'string', + }, + destination: { + title: 'destination', + type: 'string', + }, + destination_commit: { + title: 'destination_commit', + type: 'object', + properties: { + hash: { + title: 'hash', + type: 'string', + }, + }, + }, + commit: { + title: 'commit', + type: 'object', + properties: { + type: { + title: 'type', + type: 'string', + }, + hash: { + title: 'hash', + type: 'string', + }, + }, + }, + selector: { + title: 'selector', + type: 'object', + properties: { + type: { + title: 'type', + type: 'string', + }, + pattern: { + title: 'pattern', + type: 'string', + }, + }, + }, + pull_request: { + title: 'pull_request', + type: 'object', + properties: { + id: { + title: 'id', + type: 'string', + }, + }, + }, + }, + }, + variables: { + title: 'variables', + type: 'array', + items: { + type: 'object', + properties: { + key: { + title: 'key', + type: 'string', + }, + value: { + title: 'value', + type: 'string', + }, + secured: { + title: 'secured', + type: 'boolean', + }, + }, + }, + }, + }, + }, }, }, }, supportsDryRun: false, async handler(ctx) { - const { workspace, repo_slug } = ctx.input; - const bodyData = { - type: '', - uuid: '', - build_number: 33, - creator: { - type: '', - }, - repository: { - type: '', - }, - target: { - type: '', - }, - trigger: { - type: '', - }, - state: { - type: '', - }, - created_on: '', - completed_on: '', - build_seconds_used: 50, - }; + const { workspace, repo_slug, body } = ctx.input; try { const response = await fetch( `https://api.bitbucket.org/2.0/repositories/${workspace}/${repo_slug}/pipelines`, @@ -86,7 +166,7 @@ export const createBitbucketPipelinesRunAction = () => { Accept: 'application/json', 'Content-Type': 'application/json', }, - body: JSON.stringify(bodyData), + body: JSON.stringify(body) ?? {}, }, ); ctx.logStream.write(