Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: gate branch protection settings on options exclusions #1310

Merged
merged 2 commits into from
Feb 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
127 changes: 123 additions & 4 deletions src/steps/initializeBranchProtectionSettings.test.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,27 @@
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) =>
({
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 () => {
Expand All @@ -17,17 +30,72 @@ 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 () => {
const mockRequest = vi.fn().mockRejectedValue({ status: 403 });

const actual = await initializeBranchProtectionSettings(
createMockOctokit(mockRequest),
stubValues,
stubOptions,
);

expect(actual).toBe(false);
Expand All @@ -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,
},
],
]
`);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ import { Options } from "../../shared/types.js";

export async function initializeBranchProtectionSettings(
octokit: Octokit,
{ owner, repository }: Pick<Options, "owner" | "repository">,
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,
Expand All @@ -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,
},
Expand Down