Skip to content

Commit 488ba12

Browse files
authored
fix: fail gracefully on private non pro (#423)
<!-- 👋 Hi, thanks for sending a PR to template-typescript-node-package! 💖. Please fill out all fields below and make sure each item is true and [x] checked. Otherwise we may not be able to review your PR. --> ## PR Checklist - [x] Addresses an existing open issue: fixes #401 - [x] That issue was marked as [`status: accepting prs`](https://github.com/JoshuaKGoldberg/template-typescript-node-package/issues?q=is%3Aopen+is%3Aissue+label%3A%22status%3A+accepting+prs%22) - [x] Steps in [CONTRIBUTING.md](https://github.com/JoshuaKGoldberg/template-typescript-node-package/blob/main/.github/CONTRIBUTING.md) were taken ## Overview Fail gracefully for branch protection settings for private repos (non-Pro). - Added `warnText` field to the "withSpinner" options - `withSpinner` will now store its callback return value, and will show `warnText` in case the value is `false` Bonus: I changed all the `catch (err)` to `catch (error)` just for consistency (git blame on me) I'm honestly not so excited about this solution, but submitting it anyway to hear your thoughts
1 parent 424e108 commit 488ba12

File tree

1 file changed

+44
-38
lines changed

1 file changed

+44
-38
lines changed

script/setup.js

+44-38
Original file line numberDiff line numberDiff line change
@@ -38,14 +38,14 @@ function successSpinnerBlock(blockText) {
3838

3939
async function withSpinner(
4040
callback,
41-
{ startText, successText, stopText, errorText }
41+
{ startText, successText, stopText, errorText, warnText }
4242
) {
4343
s.start(startText);
4444

4545
try {
46-
await callback();
47-
48-
s.stop(chalk.green("✅ " + successText));
46+
const success = await callback();
47+
if (success === false) s.stop(chalk.yellow("⚠️ " + warnText));
48+
else s.stop(chalk.green("✅ " + successText));
4949
} catch (error) {
5050
s.stop(chalk.red("❌ " + stopText));
5151

@@ -275,7 +275,7 @@ try {
275275
let user;
276276
try {
277277
user = JSON.parse((await $`gh api user`).stdout).login;
278-
} catch (err) {
278+
} catch (error) {
279279
console.warn(
280280
chalk.gray(
281281
`Couldn't authenticate GitHub user, falling back to the provided owner name '${owner}'`
@@ -574,45 +574,51 @@ try {
574574
// Note: keep this inline script in sync with .github/workflows/release.yml!
575575
// Todo: it would be nice to not have two sources of truth...
576576
// https://github.com/JoshuaKGoldberg/template-typescript-node-package/issues/145
577-
await octokit.request(
578-
`PUT /repos/${owner}/${repository}/branches/main/protection`,
579-
{
580-
allow_deletions: false,
581-
allow_force_pushes: true,
582-
allow_fork_pushes: false,
583-
allow_fork_syncing: true,
584-
block_creations: false,
585-
branch: "main",
586-
enforce_admins: false,
587-
owner,
588-
repo: repository,
589-
required_conversation_resolution: true,
590-
required_linear_history: false,
591-
required_pull_request_reviews: null,
592-
required_status_checks: {
593-
checks: [
594-
{ context: "build" },
595-
{ context: "compliance" },
596-
{ context: "knip" },
597-
{ context: "lint" },
598-
{ context: "markdown" },
599-
{ context: "package" },
600-
{ context: "packages" },
601-
{ context: "prettier" },
602-
{ context: "spelling" },
603-
{ context: "test" },
604-
],
605-
strict: false,
606-
},
607-
restrictions: null,
608-
}
609-
);
577+
try {
578+
await octokit.request(
579+
`PUT /repos/${owner}/${repository}/branches/main/protection`,
580+
{
581+
allow_deletions: false,
582+
allow_force_pushes: true,
583+
allow_fork_pushes: false,
584+
allow_fork_syncing: true,
585+
block_creations: false,
586+
branch: "main",
587+
enforce_admins: false,
588+
owner,
589+
repo: repository,
590+
required_conversation_resolution: true,
591+
required_linear_history: false,
592+
required_pull_request_reviews: null,
593+
required_status_checks: {
594+
checks: [
595+
{ context: "build" },
596+
{ context: "compliance" },
597+
{ context: "knip" },
598+
{ context: "lint" },
599+
{ context: "markdown" },
600+
{ context: "package" },
601+
{ context: "packages" },
602+
{ context: "prettier" },
603+
{ context: "spelling" },
604+
{ context: "test" },
605+
],
606+
strict: false,
607+
},
608+
restrictions: null,
609+
}
610+
);
611+
} catch (error) {
612+
if (error.status === 403) return false;
613+
throw error;
614+
}
610615
},
611616
{
612617
startText: `Hydrating branch protection settings...`,
613618
successText: `Hydrated branch protection settings.`,
614619
stopText: `Error hydrating branch protection settings.`,
615620
errorText: `Could not hydrate branch protection settings. `,
621+
warnText: `Could not hydrate branch protection settings: private repositories require GitHub Pro for that API.`,
616622
}
617623
);
618624

0 commit comments

Comments
 (0)