From 5fc87a693c49ab26d7cc2fd16bda09b28aea9b84 Mon Sep 17 00:00:00 2001 From: blam Date: Thu, 20 May 2021 15:41:30 +0200 Subject: [PATCH 1/2] fix(scaffolder): added some logging if we can't enable branch protection for free repos Signed-off-by: blam --- .../scaffolder/actions/builtin/publish/github.ts | 1 + .../src/scaffolder/stages/publish/github.ts | 1 + .../src/scaffolder/stages/publish/helpers.ts | 13 +++++++++++++ 3 files changed, 15 insertions(+) diff --git a/plugins/scaffolder-backend/src/scaffolder/actions/builtin/publish/github.ts b/plugins/scaffolder-backend/src/scaffolder/actions/builtin/publish/github.ts index e56c94a5b0..a3befa0dfe 100644 --- a/plugins/scaffolder-backend/src/scaffolder/actions/builtin/publish/github.ts +++ b/plugins/scaffolder-backend/src/scaffolder/actions/builtin/publish/github.ts @@ -233,6 +233,7 @@ export function createPublishGithubAction(options: { owner, client, repoName: newRepo.name, + logger: ctx.logger, }); } catch (e) { throw new Error( diff --git a/plugins/scaffolder-backend/src/scaffolder/stages/publish/github.ts b/plugins/scaffolder-backend/src/scaffolder/stages/publish/github.ts index 2647284698..d859d97dc3 100644 --- a/plugins/scaffolder-backend/src/scaffolder/stages/publish/github.ts +++ b/plugins/scaffolder-backend/src/scaffolder/stages/publish/github.ts @@ -109,6 +109,7 @@ export class GithubPublisher implements PublisherBase { owner, client, repoName: name, + logger, }); } catch (e) { throw new Error(`Failed to add branch protection to '${name}', ${e}`); diff --git a/plugins/scaffolder-backend/src/scaffolder/stages/publish/helpers.ts b/plugins/scaffolder-backend/src/scaffolder/stages/publish/helpers.ts index d373189ec6..59d288eedc 100644 --- a/plugins/scaffolder-backend/src/scaffolder/stages/publish/helpers.ts +++ b/plugins/scaffolder-backend/src/scaffolder/stages/publish/helpers.ts @@ -73,12 +73,14 @@ type BranchProtectionOptions = { client: Octokit; owner: string; repoName: string; + logger: Logger; }; export const enableBranchProtectionOnDefaultRepoBranch = async ({ repoName, client, owner, + logger, }: BranchProtectionOptions): Promise => { const tryOnce = () => { return client.repos.updateBranchProtection({ @@ -105,6 +107,17 @@ export const enableBranchProtectionOnDefaultRepoBranch = async ({ try { await tryOnce(); } catch (e) { + if ( + e.message.includes( + 'Upgrade to GitHub Pro or make this repository public to enable this feature', + ) + ) { + logger.warn( + 'Branch protection was not enabled as it requires GitHub Pro for private repositories', + ); + return; + } + if (!e.message.includes('Branch not found')) { throw e; } From 021eb366a8ad15dd6050d970c23504b7290ac426 Mon Sep 17 00:00:00 2001 From: blam Date: Thu, 20 May 2021 15:49:21 +0200 Subject: [PATCH 2/2] chore: reworking the logic Signed-off-by: blam --- .changeset/long-keys-heal.md | 5 ++ .../src/scaffolder/stages/publish/helpers.ts | 65 ++++++++++--------- 2 files changed, 39 insertions(+), 31 deletions(-) create mode 100644 .changeset/long-keys-heal.md diff --git a/.changeset/long-keys-heal.md b/.changeset/long-keys-heal.md new file mode 100644 index 0000000000..c70600d439 --- /dev/null +++ b/.changeset/long-keys-heal.md @@ -0,0 +1,5 @@ +--- +'@backstage/plugin-scaffolder-backend': patch +--- + +Instead of failing, warn when you need to pay for GitHub Pro. diff --git a/plugins/scaffolder-backend/src/scaffolder/stages/publish/helpers.ts b/plugins/scaffolder-backend/src/scaffolder/stages/publish/helpers.ts index 59d288eedc..8531dbcdc8 100644 --- a/plugins/scaffolder-backend/src/scaffolder/stages/publish/helpers.ts +++ b/plugins/scaffolder-backend/src/scaffolder/stages/publish/helpers.ts @@ -82,42 +82,45 @@ export const enableBranchProtectionOnDefaultRepoBranch = async ({ owner, logger, }: BranchProtectionOptions): Promise => { - const tryOnce = () => { - return client.repos.updateBranchProtection({ - mediaType: { - /** - * 👇 we need this preview because allowing a custom - * reviewer count on branch protection is a preview - * feature - * - * More here: https://docs.github.com/en/rest/overview/api-previews#require-multiple-approving-reviews - */ - previews: ['luke-cage-preview'], - }, - owner, - repo: repoName, - branch: 'master', - required_status_checks: { strict: true, contexts: [] }, - restrictions: null, - enforce_admins: true, - required_pull_request_reviews: { required_approving_review_count: 1 }, - }); + const tryOnce = async () => { + try { + await client.repos.updateBranchProtection({ + mediaType: { + /** + * 👇 we need this preview because allowing a custom + * reviewer count on branch protection is a preview + * feature + * + * More here: https://docs.github.com/en/rest/overview/api-previews#require-multiple-approving-reviews + */ + previews: ['luke-cage-preview'], + }, + owner, + repo: repoName, + branch: 'master', + required_status_checks: { strict: true, contexts: [] }, + restrictions: null, + enforce_admins: true, + required_pull_request_reviews: { required_approving_review_count: 1 }, + }); + } catch (e) { + if ( + e.message.includes( + 'Upgrade to GitHub Pro or make this repository public to enable this feature', + ) + ) { + logger.warn( + 'Branch protection was not enabled as it requires GitHub Pro for private repositories', + ); + } else { + throw e; + } + } }; try { await tryOnce(); } catch (e) { - if ( - e.message.includes( - 'Upgrade to GitHub Pro or make this repository public to enable this feature', - ) - ) { - logger.warn( - 'Branch protection was not enabled as it requires GitHub Pro for private repositories', - ); - return; - } - if (!e.message.includes('Branch not found')) { throw e; }