From c701c34391973d4c6815ca8e44cdef258f9685f1 Mon Sep 17 00:00:00 2001 From: benjdlambert Date: Wed, 4 Jun 2025 11:25:25 +0200 Subject: [PATCH] chore: smaller changes Signed-off-by: benjdlambert --- .../report.api.md | 17 +-- .../src/actions/bitbucketCloudPullRequest.ts | 107 ++++++++---------- .../src/actions/githubPullRequest.test.ts | 40 +++---- .../scaffolder-node-test-utils/report.api.md | 6 +- .../src/actions/mockActionContext.ts | 22 ++-- 5 files changed, 87 insertions(+), 105 deletions(-) diff --git a/plugins/scaffolder-backend-module-bitbucket-cloud/report.api.md b/plugins/scaffolder-backend-module-bitbucket-cloud/report.api.md index 3a27006319..af34609213 100644 --- a/plugins/scaffolder-backend-module-bitbucket-cloud/report.api.md +++ b/plugins/scaffolder-backend-module-bitbucket-cloud/report.api.md @@ -5,7 +5,6 @@ ```ts import { BackendFeature } from '@backstage/backend-plugin-api'; import { Config } from '@backstage/config'; -import { JsonObject } from '@backstage/types'; import { ScmIntegrationRegistry } from '@backstage/integration'; import { TemplateAction } from '@backstage/plugin-scaffolder-node'; @@ -103,14 +102,16 @@ export function createPublishBitbucketCloudPullRequestAction(options: { { repoUrl: string; title: string; - description?: string; - targetBranch?: string; sourceBranch: string; - token?: string; - gitAuthorName?: string; - gitAuthorEmail?: string; + description?: string | undefined; + targetBranch?: string | undefined; + token?: string | undefined; + gitAuthorName?: string | undefined; + gitAuthorEmail?: string | undefined; }, - JsonObject, - 'v1' + { + pullRequestUrl: string; + }, + 'v2' >; ``` diff --git a/plugins/scaffolder-backend-module-bitbucket-cloud/src/actions/bitbucketCloudPullRequest.ts b/plugins/scaffolder-backend-module-bitbucket-cloud/src/actions/bitbucketCloudPullRequest.ts index b59aaa4e41..9969f13eb2 100644 --- a/plugins/scaffolder-backend-module-bitbucket-cloud/src/actions/bitbucketCloudPullRequest.ts +++ b/plugins/scaffolder-backend-module-bitbucket-cloud/src/actions/bitbucketCloudPullRequest.ts @@ -139,6 +139,7 @@ const findBranches = async (opts: { return r.values[0]; }; + const createBranch = async (opts: { workspace: string; repo: string; @@ -190,6 +191,7 @@ const createBranch = async (opts: { return await response.json(); }; + const getDefaultBranch = async (opts: { workspace: string; repo: string; @@ -233,73 +235,60 @@ export function createPublishBitbucketCloudPullRequestAction(options: { }) { const { integrations, config } = options; - return createTemplateAction<{ - repoUrl: string; - title: string; - description?: string; - targetBranch?: string; - sourceBranch: string; - token?: string; - gitAuthorName?: string; - gitAuthorEmail?: string; - }>({ + return createTemplateAction({ id: 'publish:bitbucketCloud:pull-request', examples, schema: { input: { - type: 'object', - required: ['repoUrl', 'title', 'sourceBranch'], - properties: { - repoUrl: { - title: 'Repository Location', - type: 'string', - }, - title: { - title: 'Pull Request title', - type: 'string', + repoUrl: z => + z.string({ + description: 'Repository Location', + }), + title: z => + z.string({ description: 'The title for the pull request', - }, - description: { - title: 'Pull Request Description', - type: 'string', - description: 'The description of the pull request', - }, - targetBranch: { - title: 'Target Branch', - type: 'string', - description: `Branch of repository to apply changes to. The default value is 'master'`, - }, - sourceBranch: { - title: 'Source Branch', - type: 'string', + }), + description: z => + z + .string({ + description: 'The description of the pull request', + }) + .optional(), + targetBranch: z => + z + .string({ + description: `Branch of repository to apply changes to. The default value is 'master'`, + }) + .optional(), + sourceBranch: z => + z.string({ description: 'Branch of repository to copy changes from', - }, - token: { - title: 'Authorization Token', - type: 'string', - description: - 'The token to use for authorization to BitBucket Cloud', - }, - gitAuthorName: { - title: 'Author Name', - type: 'string', - description: `Sets the author name for the commit. The default value is 'Scaffolder'`, - }, - gitAuthorEmail: { - title: 'Author Email', - type: 'string', - description: `Sets the author email for the commit.`, - }, - }, + }), + token: z => + z + .string({ + description: + 'The token to use for authorization to BitBucket Cloud', + }) + .optional(), + gitAuthorName: z => + z + .string({ + description: `Sets the author name for the commit. The default value is 'Scaffolder'`, + }) + .optional(), + gitAuthorEmail: z => + z + .string({ + description: `Sets the author email for the commit.`, + }) + .optional(), }, output: { - type: 'object', - properties: { - pullRequestUrl: { - title: 'A URL to the pull request with the provider', - type: 'string', - }, - }, + pullRequestUrl: z => + z.string({ + description: 'A URL to the pull request with the provider', + }), }, }, async handler(ctx) { diff --git a/plugins/scaffolder-backend-module-github/src/actions/githubPullRequest.test.ts b/plugins/scaffolder-backend-module-github/src/actions/githubPullRequest.test.ts index 8f85f08ea4..670af15595 100644 --- a/plugins/scaffolder-backend-module-github/src/actions/githubPullRequest.test.ts +++ b/plugins/scaffolder-backend-module-github/src/actions/githubPullRequest.test.ts @@ -97,7 +97,7 @@ describe('createPublishGithubPullRequestAction', () => { describe('with targetBranchName', () => { let input: GithubPullRequestActionInput; - let ctx: ActionContext; + let ctx: ActionContext; beforeEach(() => { fakeClient = { @@ -181,7 +181,7 @@ describe('createPublishGithubPullRequestAction', () => { describe('with no sourcePath', () => { let input: GithubPullRequestActionInput; - let ctx: ActionContext; + let ctx: ActionContext; beforeEach(() => { input = { @@ -250,7 +250,7 @@ describe('createPublishGithubPullRequestAction', () => { describe('with sourcePath', () => { let input: GithubPullRequestActionInput; - let ctx: ActionContext; + let ctx: ActionContext; beforeEach(() => { input = { @@ -306,7 +306,7 @@ describe('createPublishGithubPullRequestAction', () => { describe('with repoUrl', () => { let input: GithubPullRequestActionInput; - let ctx: ActionContext; + let ctx: ActionContext; beforeEach(() => { input = { @@ -359,7 +359,7 @@ describe('createPublishGithubPullRequestAction', () => { describe('with reviewers and teamReviewers', () => { let input: GithubPullRequestActionInput; - let ctx: ActionContext; + let ctx: ActionContext; beforeEach(() => { input = { @@ -406,7 +406,7 @@ describe('createPublishGithubPullRequestAction', () => { describe('with no reviewers and teamReviewers', () => { let input: GithubPullRequestActionInput; - let ctx: ActionContext; + let ctx: ActionContext; beforeEach(() => { input = { @@ -431,7 +431,7 @@ describe('createPublishGithubPullRequestAction', () => { describe('with assignees', () => { let input: GithubPullRequestActionInput; - let ctx: ActionContext; + let ctx: ActionContext; beforeEach(() => { input = { @@ -475,7 +475,7 @@ describe('createPublishGithubPullRequestAction', () => { describe('with broken symlink', () => { let input: GithubPullRequestActionInput; - let ctx: ActionContext; + let ctx: ActionContext; beforeEach(() => { input = { @@ -520,7 +520,7 @@ describe('createPublishGithubPullRequestAction', () => { describe('with executable file mode 755', () => { let input: GithubPullRequestActionInput; - let ctx: ActionContext; + let ctx: ActionContext; beforeEach(() => { input = { @@ -579,7 +579,7 @@ describe('createPublishGithubPullRequestAction', () => { describe('with executable file mode 775', () => { let input: GithubPullRequestActionInput; - let ctx: ActionContext; + let ctx: ActionContext; beforeEach(() => { input = { @@ -638,7 +638,7 @@ describe('createPublishGithubPullRequestAction', () => { describe('with commit message', () => { let input: GithubPullRequestActionInput; - let ctx: ActionContext; + let ctx: ActionContext; beforeEach(() => { input = { @@ -683,7 +683,7 @@ describe('createPublishGithubPullRequestAction', () => { describe('with force fork', () => { let input: GithubPullRequestActionInput; - let ctx: ActionContext; + let ctx: ActionContext; beforeEach(() => { input = { @@ -729,7 +729,7 @@ describe('createPublishGithubPullRequestAction', () => { describe('with author name and email', () => { let input: GithubPullRequestActionInput; - let ctx: ActionContext; + let ctx: ActionContext; beforeEach(() => { input = { @@ -779,7 +779,7 @@ describe('createPublishGithubPullRequestAction', () => { describe('with author name', () => { let input: GithubPullRequestActionInput; - let ctx: ActionContext; + let ctx: ActionContext; beforeEach(() => { input = { @@ -828,7 +828,7 @@ describe('createPublishGithubPullRequestAction', () => { describe('with author email', () => { let input: GithubPullRequestActionInput; - let ctx: ActionContext; + let ctx: ActionContext; beforeEach(() => { input = { @@ -877,7 +877,7 @@ describe('createPublishGithubPullRequestAction', () => { describe('with author from config file', () => { let input: GithubPullRequestActionInput; - let ctx: ActionContext; + let ctx: ActionContext; beforeEach(() => { input = { @@ -946,7 +946,7 @@ describe('createPublishGithubPullRequestAction', () => { describe('with author attributes and config file', () => { let input: GithubPullRequestActionInput; - let ctx: ActionContext; + let ctx: ActionContext; beforeEach(() => { input = { @@ -1017,7 +1017,7 @@ describe('createPublishGithubPullRequestAction', () => { describe('with author fallback and no config', () => { let input: GithubPullRequestActionInput; - let ctx: ActionContext; + let ctx: ActionContext; beforeEach(() => { input = { @@ -1112,7 +1112,7 @@ describe('createPublishGithubPullRequestAction', () => { describe('with createWhenEmpty equals true', () => { let input: GithubPullRequestActionInput; - let ctx: ActionContext; + let ctx: ActionContext; beforeEach(() => { input = { @@ -1175,7 +1175,7 @@ describe('createPublishGithubPullRequestAction', () => { describe('with createWhenEmpty equals false', () => { let input: GithubPullRequestActionInput; - let ctx: ActionContext; + let ctx: ActionContext; beforeEach(() => { fakeClient.createPullRequest.mockResolvedValueOnce(null); diff --git a/plugins/scaffolder-node-test-utils/report.api.md b/plugins/scaffolder-node-test-utils/report.api.md index b95d4cef0c..2780baf1d5 100644 --- a/plugins/scaffolder-node-test-utils/report.api.md +++ b/plugins/scaffolder-node-test-utils/report.api.md @@ -7,12 +7,12 @@ import { ActionContext } from '@backstage/plugin-scaffolder-node'; import { JsonObject } from '@backstage/types'; // @public -export const createMockActionContext: < +export function createMockActionContext< TActionInput extends JsonObject = JsonObject, - TActionOutput extends JsonObject = JsonObject, + TActionOutput extends JsonObject = any, >( options?: Partial>, -) => ActionContext; +): ActionContext; // (No @packageDocumentation comment for this package) ``` diff --git a/plugins/scaffolder-node-test-utils/src/actions/mockActionContext.ts b/plugins/scaffolder-node-test-utils/src/actions/mockActionContext.ts index bb6e3917f1..a6703a12d1 100644 --- a/plugins/scaffolder-node-test-utils/src/actions/mockActionContext.ts +++ b/plugins/scaffolder-node-test-utils/src/actions/mockActionContext.ts @@ -30,13 +30,14 @@ import { loggerToWinstonLogger } from './loggerToWinstonLogger'; * @public * @param options - optional parameters to override default mock context */ -export const createMockActionContext = < +export function createMockActionContext< TActionInput extends JsonObject = JsonObject, - TActionOutput extends JsonObject = JsonObject, + TActionOutput extends JsonObject = any, >( options?: Partial>, -): ActionContext => { +): ActionContext { const credentials = mockCredentials.user(); + const defaultContext = { logger: loggerToWinstonLogger(mockServices.logger.mock()), logStream: new PassThrough(), @@ -66,16 +67,8 @@ export const createMockActionContext = < }; } - const { - input, - logger, - logStream, - secrets, - templateInfo, - workspacePath, - task, - user, - } = options; + const { input, logger, secrets, templateInfo, workspacePath, task, user } = + options; return { ...defaultContext, @@ -84,11 +77,10 @@ export const createMockActionContext = < createTemporaryDirectory: jest.fn().mockResolvedValue(workspacePath), }), ...(logger && { logger }), - ...(logStream && { logStream }), ...(input && { input }), ...(secrets && { secrets }), ...(task && { task }), ...(user && { user }), templateInfo, }; -}; +}