diff --git a/.changeset/shiny-windows-roll.md b/.changeset/shiny-windows-roll.md new file mode 100644 index 0000000000..46801542b5 --- /dev/null +++ b/.changeset/shiny-windows-roll.md @@ -0,0 +1,5 @@ +--- +'@backstage/plugin-scaffolder-backend': minor +--- + +Add an option to not protect the default branch. diff --git a/plugins/scaffolder-backend/src/scaffolder/actions/builtin/publish/github.test.ts b/plugins/scaffolder-backend/src/scaffolder/actions/builtin/publish/github.test.ts index f1a6c9bfe8..be1ac4b369 100644 --- a/plugins/scaffolder-backend/src/scaffolder/actions/builtin/publish/github.test.ts +++ b/plugins/scaffolder-backend/src/scaffolder/actions/builtin/publish/github.test.ts @@ -727,4 +727,26 @@ describe('publish:github', () => { requiredStatusCheckContexts: [], }); }); + + it('should not call enableBranchProtectionOnDefaultRepoBranch with protectDefaultBranch disabled', async () => { + mockOctokit.rest.users.getByUsername.mockResolvedValue({ + data: { type: 'User' }, + }); + + mockOctokit.rest.repos.createForAuthenticatedUser.mockResolvedValue({ + data: { + name: 'repository', + }, + }); + + await action.handler({ + ...mockContext, + input: { + ...mockContext.input, + protectDefaultBranch: false, + }, + }); + + expect(enableBranchProtectionOnDefaultRepoBranch).not.toHaveBeenCalled(); + }); }); diff --git a/plugins/scaffolder-backend/src/scaffolder/actions/builtin/publish/github.ts b/plugins/scaffolder-backend/src/scaffolder/actions/builtin/publish/github.ts index 1f9125d962..73c0b10bca 100644 --- a/plugins/scaffolder-backend/src/scaffolder/actions/builtin/publish/github.ts +++ b/plugins/scaffolder-backend/src/scaffolder/actions/builtin/publish/github.ts @@ -46,6 +46,7 @@ export function createPublishGithubAction(options: { description?: string; access?: string; defaultBranch?: string; + protectDefaultBranch?: boolean; deleteBranchOnMerge?: boolean; gitCommitMessage?: string; gitAuthorName?: string; @@ -111,6 +112,11 @@ export function createPublishGithubAction(options: { type: 'string', description: `Sets the default branch on the repository. The default value is 'master'`, }, + protectDefaultBranch: { + title: 'Protect Default Branch', + type: 'boolean', + description: `Protect the default branch after creating the repository. The default value is 'true'`, + }, deleteBranchOnMerge: { title: 'Delete Branch On Merge', type: 'boolean', @@ -209,6 +215,7 @@ export function createPublishGithubAction(options: { requiredStatusCheckContexts = [], repoVisibility = 'private', defaultBranch = 'master', + protectDefaultBranch = true, deleteBranchOnMerge = false, gitCommitMessage = 'initial commit', gitAuthorName, @@ -360,21 +367,23 @@ export function createPublishGithubAction(options: { gitAuthorInfo, }); - try { - await enableBranchProtectionOnDefaultRepoBranch({ - owner, - client, - repoName: newRepo.name, - logger: ctx.logger, - defaultBranch, - requireCodeOwnerReviews, - requiredStatusCheckContexts, - }); - } catch (e) { - assertError(e); - ctx.logger.warn( - `Skipping: default branch protection on '${newRepo.name}', ${e.message}`, - ); + if (protectDefaultBranch) { + try { + await enableBranchProtectionOnDefaultRepoBranch({ + owner, + client, + repoName: newRepo.name, + logger: ctx.logger, + defaultBranch, + requireCodeOwnerReviews, + requiredStatusCheckContexts, + }); + } catch (e) { + assertError(e); + ctx.logger.warn( + `Skipping: default branch protection on '${newRepo.name}', ${e.message}`, + ); + } } ctx.output('remoteUrl', remoteUrl);