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: don't crash if GitHub API call for rulesetId option fails #1953

Merged
merged 1 commit into from
Mar 4, 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
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@
"@vitest/eslint-plugin": "1.1.31",
"all-contributors-cli": "6.26.1",
"bingo-stratum-testers": "0.5.1",
"bingo-testers": "0.5.1",
"bingo-testers": "0.5.2",
"console-fail-test": "0.5.0",
"cspell": "8.17.3",
"eslint": "9.20.1",
Expand Down
16 changes: 8 additions & 8 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions src/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import lazyValue from "lazy-value";
import npmUser from "npm-user";
import { z } from "zod";

import { inputFromOctokit } from "./inputs/inputFromGitHub.js";
import { inputFromOctokit } from "./inputs/inputFromOctokit.js";
import { parsePackageAuthor } from "./options/parsePackageAuthor.js";
import { readDefaultsFromReadme } from "./options/readDefaultsFromReadme.js";
import { readDescription } from "./options/readDescription.js";
Expand Down Expand Up @@ -175,9 +175,9 @@ export const base = createBase({
owner: await owner(),
repo: await repository(),
},
})) as { id: string; name: string }[];
})) as undefined | { id: string; name: string }[];

return rulesets.find(
return rulesets?.find(
(ruleset) => ruleset.name === "Branch protection for main",
)?.id;
});
Expand Down
45 changes: 45 additions & 0 deletions src/inputs/inputFromOctokit.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import { createMockFetchers, testInput } from "bingo-testers";
import { describe, expect, it, vi } from "vitest";

import { inputFromOctokit } from "./inputFromOctokit.js";

describe("inputFromOctokit", () => {
it("returns data when the request resolves", async () => {
const data = JSON.stringify({ found: true });

const actual = await testInput(inputFromOctokit, {
args: {
endpoint: "GET /repos/{owner}/{repo}/rulesets",
options: {},
},
fetchers: createMockFetchers(
vi.fn().mockResolvedValueOnce(
// eslint-disable-next-line n/no-unsupported-features/node-builtins
new Response(data),
),
),
});

expect(actual).toEqual(data);
});

it("returns undefined when the request rejects", async () => {
const actual = await testInput(inputFromOctokit, {
args: {
endpoint: "GET /repos/{owner}/{repo}/rulesets",
options: {},
},
fetchers: createMockFetchers(
vi.fn().mockResolvedValueOnce(
// eslint-disable-next-line n/no-unsupported-features/node-builtins
new Response("", {
status: 404,
statusText: "Not found.",
}),
),
),
});

expect(actual).toBe(undefined);
});
});
18 changes: 11 additions & 7 deletions src/inputs/inputFromGitHub.ts → src/inputs/inputFromOctokit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,17 @@ export const inputFromOctokit = createInput({
// This will require smart types around GitHub endpoints, similar to:
// https://github.com/JoshuaKGoldberg/bingo/issues/65
async produce({ args, fetchers }): Promise<unknown> {
const response = await fetchers.octokit.request(args.endpoint, {
headers: {
"X-GitHub-Api-Version": "2022-11-28",
},
...args.options,
});
try {
const response = await fetchers.octokit.request(args.endpoint, {
headers: {
"X-GitHub-Api-Version": "2022-11-28",
},
...args.options,
});

return response.data;
return response.data;
} catch {
return undefined;
}
},
});
Loading