diff --git a/src/steps/initializeBranchProtectionSettings.test.ts b/src/steps/initializeBranchProtectionSettings.test.ts index fbbd977e1..0eec86ae9 100644 --- a/src/steps/initializeBranchProtectionSettings.test.ts +++ b/src/steps/initializeBranchProtectionSettings.test.ts @@ -1,6 +1,7 @@ import { Octokit } from "octokit"; import { MockInstance, describe, expect, it, vi } from "vitest"; +import { Options } from "../shared/types.js"; import { initializeBranchProtectionSettings } from "./initializeGitHubRepository/initializeBranchProtectionSettings.js"; const createMockOctokit = (request: MockInstance) => @@ -8,7 +9,19 @@ const createMockOctokit = (request: MockInstance) => request, }) as unknown as Octokit; -const stubValues = { owner: "", repository: "" }; +const stubOptions = { + access: "public", + description: "", + directory: "", + email: { + github: "", + npm: "", + }, + mode: "create", + owner: "", + repository: "", + title: "", +} satisfies Options; describe("migrateBranchProtectionSettings", () => { it("does not throw when the request receives a non-error response", async () => { @@ -17,9 +30,64 @@ describe("migrateBranchProtectionSettings", () => { await expect( initializeBranchProtectionSettings( createMockOctokit(mockRequest), - stubValues, + stubOptions, ), ).resolves.not.toThrow(); + + expect(mockRequest.mock.calls).toMatchInlineSnapshot(` + [ + [ + "PUT /repos///branches/main/protection", + { + "allow_deletions": false, + "allow_force_pushes": true, + "allow_fork_pushes": false, + "allow_fork_syncing": true, + "block_creations": false, + "branch": "main", + "enforce_admins": false, + "owner": "", + "repo": "", + "required_conversation_resolution": true, + "required_linear_history": false, + "required_pull_request_reviews": null, + "required_status_checks": { + "checks": [ + { + "context": "build", + }, + { + "context": "lint", + }, + { + "context": "prettier", + }, + { + "context": "compliance", + }, + { + "context": "lint_knip", + }, + { + "context": "lint_markdown", + }, + { + "context": "lint_packages", + }, + { + "context": "lint_spelling", + }, + { + "context": "test", + }, + ], + "strict": false, + }, + "restrictions": null, + }, + ], + ] + `); }); it("returns false when the request receives a 403 response", async () => { @@ -27,7 +95,7 @@ describe("migrateBranchProtectionSettings", () => { const actual = await initializeBranchProtectionSettings( createMockOctokit(mockRequest), - stubValues, + stubOptions, ); expect(actual).toBe(false); @@ -40,8 +108,59 @@ describe("migrateBranchProtectionSettings", () => { await expect(() => initializeBranchProtectionSettings( createMockOctokit(mockRequest), - stubValues, + stubOptions, ), ).rejects.toBe(error); }); + + it("doesn't create workflows for excluded options when specified", async () => { + const mockRequest = vi.fn().mockResolvedValue({ status: 200 }); + + await initializeBranchProtectionSettings(createMockOctokit(mockRequest), { + ...stubOptions, + excludeCompliance: true, + excludeLintKnip: true, + excludeLintMd: true, + excludeLintPackages: true, + excludeLintSpelling: true, + excludeTests: true, + }); + + expect(mockRequest.mock.calls).toMatchInlineSnapshot(` + [ + [ + "PUT /repos///branches/main/protection", + { + "allow_deletions": false, + "allow_force_pushes": true, + "allow_fork_pushes": false, + "allow_fork_syncing": true, + "block_creations": false, + "branch": "main", + "enforce_admins": false, + "owner": "", + "repo": "", + "required_conversation_resolution": true, + "required_linear_history": false, + "required_pull_request_reviews": null, + "required_status_checks": { + "checks": [ + { + "context": "build", + }, + { + "context": "lint", + }, + { + "context": "prettier", + }, + ], + "strict": false, + }, + "restrictions": null, + }, + ], + ] + `); + }); }); diff --git a/src/steps/initializeGitHubRepository/initializeBranchProtectionSettings.ts b/src/steps/initializeGitHubRepository/initializeBranchProtectionSettings.ts index a67866116..45d9495d7 100644 --- a/src/steps/initializeGitHubRepository/initializeBranchProtectionSettings.ts +++ b/src/steps/initializeGitHubRepository/initializeBranchProtectionSettings.ts @@ -5,11 +5,11 @@ import { Options } from "../../shared/types.js"; export async function initializeBranchProtectionSettings( octokit: Octokit, - { owner, repository }: Pick, + options: Options, ) { try { await octokit.request( - `PUT /repos/${owner}/${repository}/branches/main/protection`, + `PUT /repos/${options.owner}/${options.repository}/branches/main/protection`, { allow_deletions: false, allow_force_pushes: true, @@ -18,22 +18,26 @@ export async function initializeBranchProtectionSettings( block_creations: false, branch: "main", enforce_admins: false, - owner, - repo: repository, + owner: options.owner, + repo: options.repository, required_conversation_resolution: true, required_linear_history: false, required_pull_request_reviews: null, required_status_checks: { checks: [ { context: "build" }, - { context: "compliance" }, { context: "lint" }, - { context: "lint_knip" }, - { context: "lint_markdown" }, - { context: "lint_packages" }, - { context: "lint_spelling" }, { context: "prettier" }, - { context: "test" }, + ...(options.excludeCompliance ? [] : [{ context: "compliance" }]), + ...(options.excludeLintKnip ? [] : [{ context: "lint_knip" }]), + ...(options.excludeLintMd ? [] : [{ context: "lint_markdown" }]), + ...(options.excludeLintPackages + ? [] + : [{ context: "lint_packages" }]), + ...(options.excludeLintSpelling + ? [] + : [{ context: "lint_spelling" }]), + ...(options.excludeTests ? [] : [{ context: "test" }]), ], strict: false, },