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: only PUT a ruleset when an ID already exists #1968

Merged
merged 1 commit into from
Mar 7, 2025
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
24 changes: 24 additions & 0 deletions src/blocks/blockRepositoryBranchRuleset.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,4 +104,28 @@ describe("blockRepositoryBranchRuleset", () => {
}
`);
});

test("with addons and a ruleset_id option when mode is transition", () => {
const creation = testBlock(blockRepositoryBranchRuleset, {
addons: {
requiredStatusChecks: ["build", "test"],
},
mode: "setup",
options: {
...optionsBase,
rulesetId: "1234",
},
});

expect(creation).toMatchInlineSnapshot(`
{
"requests": [
{
"id": "branch-ruleset-create",
"send": [Function],
},
],
}
`);
});
});
151 changes: 82 additions & 69 deletions src/blocks/blockRepositoryBranchRuleset.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { BlockCreation } from "bingo-stratum";
import { z } from "zod";

import { base, BaseOptions } from "../base.js";
Expand All @@ -11,22 +10,45 @@ export const blockRepositoryBranchRuleset = base.createBlock({
requiredStatusChecks: z.array(z.string()).default([]),
},
setup({ addons, options }) {
return createRequestSend(
addons.requiredStatusChecks,
"POST /repos/{owner}/{repo}/rulesets",
options,
"branch-ruleset-create",
undefined,
);
return {
requests: [
{
id: "branch-ruleset-create",
async send({ octokit }) {
await octokit.request(
"POST /repos/{owner}/{repo}/rulesets",
createInnerSend(addons.requiredStatusChecks, options),
);
},
},
],
};
},
transition({ addons, options }) {
return createRequestSend(
addons.requiredStatusChecks,
`PUT /repos/{owner}/{repo}/rulesets/{ruleset_id}`,
options,
"branch-ruleset-update",
options.rulesetId,
);
return {
requests: [
{
id: "branch-ruleset-update",
async send({ octokit }) {
if (options.rulesetId) {
await octokit.request(
`PUT /repos/{owner}/{repo}/rulesets/{ruleset_id}`,
createInnerSend(
addons.requiredStatusChecks,
options,
options.rulesetId,
),
);
} else {
await octokit.request(
`POST /repos/{owner}/{repo}/rulesets/{ruleset_id}`,
createInnerSend(addons.requiredStatusChecks, options),
);
}
},
},
],
};
},
// TODO: Make produce() optional
// This needs createBlock to be generic to know if block.produce({}) is ok
Expand All @@ -35,66 +57,57 @@ export const blockRepositoryBranchRuleset = base.createBlock({
},
});

function createRequestSend(
function createInnerSend(
contexts: string[],
endpoint: string,
options: BaseOptions,
requestId: string,
rulesetId: string | undefined,
): Partial<BlockCreation<BaseOptions>> {
rulesetId?: string,
) {
return {
requests: [
bypass_actors: [
{
// This *seems* to be the Repository Admin role always?
// https://github.com/github/rest-api-description/issues/4406
actor_id: 5,
actor_type: "RepositoryRole" as const,
bypass_mode: "always" as const,
},
],
conditions: {
ref_name: {
exclude: [],
include: ["refs/heads/main"],
},
},
enforcement: "active" as const,
name: "Branch protection for main",
owner: options.owner,
repo: options.repository,
rules: [
{ type: "deletion" as const },
{
parameters: {
allowed_merge_methods: ["squash"],
dismiss_stale_reviews_on_push: false,
require_code_owner_review: false,
require_last_push_approval: false,
required_approving_review_count: 0,
required_review_thread_resolution: false,
},
type: "pull_request" as const,
},
{
id: requestId,
async send({ octokit }) {
await octokit.request(endpoint, {
bypass_actors: [
{
// This *seems* to be the Repository Admin role always?
// https://github.com/github/rest-api-description/issues/4406
actor_id: 5,
actor_type: "RepositoryRole",
bypass_mode: "always",
},
],
conditions: {
ref_name: {
exclude: [],
include: ["refs/heads/main"],
},
},
enforcement: "active",
name: "Branch protection for main",
owner: options.owner,
repo: options.repository,
rules: [
{ type: "deletion" },
{
parameters: {
allowed_merge_methods: ["squash"],
dismiss_stale_reviews_on_push: false,
require_code_owner_review: false,
require_last_push_approval: false,
required_approving_review_count: 0,
required_review_thread_resolution: false,
},
type: "pull_request",
},
{
parameters: {
required_status_checks: contexts.map((context) => ({
context,
})),
strict_required_status_checks_policy: false,
},
type: "required_status_checks",
},
],
ruleset_id: rulesetId,
target: "branch",
});
parameters: {
required_status_checks: contexts.map((context) => ({
context,
})),
strict_required_status_checks_policy: false,
},
type: "required_status_checks" as const,
},
],
// Type fun because the GitHub Octokit APIs don't provide named types...
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
ruleset_id: (rulesetId === undefined ? rulesetId : Number(rulesetId))!,
target: "branch" as const,
};
}