From 47bf28cf7f458cbcc6db25c7d7eaed62c4f8428a Mon Sep 17 00:00:00 2001 From: Anton Elmanov <45532870+antelman107@users.noreply.github.com> Date: Tue, 24 Sep 2024 15:06:21 +0300 Subject: [PATCH] feat: output a warning if the parent branch is protected (#280) --- src/commands/branches.ts | 43 ++++++++++++++++++++++------------------ src/log.ts | 3 +++ 2 files changed, 27 insertions(+), 19 deletions(-) diff --git a/src/commands/branches.ts b/src/commands/branches.ts index 3d97827b..86c0c5c9 100644 --- a/src/commands/branches.ts +++ b/src/commands/branches.ts @@ -284,17 +284,17 @@ const create = async ( '--'?: string[]; }, ) => { - const parentProps = await (() => { + const branches = await props.apiClient + .listProjectBranches(props.projectId) + .then(({ data }) => data.branches); + + const parentProps = (() => { if (!props.parent) { - return props.apiClient - .listProjectBranches(props.projectId) - .then(({ data }) => { - const branch = data.branches.find((b) => b.default); - if (!branch) { - throw new Error('No default branch found'); - } - return { parent_id: branch.id }; - }); + const branch = branches.find((b) => b.default); + if (!branch) { + throw new Error('No default branch found'); + } + return { parent_id: branch.id }; } if (looksLikeLSN(props.parent)) { @@ -308,15 +308,12 @@ const create = async ( if (looksLikeBranchId(props.parent)) { return { parent_id: props.parent }; } - return props.apiClient - .listProjectBranches(props.projectId) - .then(({ data }) => { - const branch = data.branches.find((b) => b.name === props.parent); - if (!branch) { - throw new Error(`Branch ${props.parent} not found`); - } - return { parent_id: branch.id }; - }); + + const branch = branches.find((b) => b.name === props.parent); + if (!branch) { + throw new Error(`Branch ${props.parent} not found`); + } + return { parent_id: branch.id }; })(); const { data } = await retryOnLock(() => @@ -341,7 +338,15 @@ const create = async ( }), ); + const parent = branches.find((b) => b.id === data.branch.parent_id); + if (parent?.protected) { + log.warning( + 'The parent branch is protected; a unique role password has been generated for the new branch.', + ); + } + const out = writer(props); + out.write(data.branch, { fields: BRANCH_FIELDS, title: 'branch', diff --git a/src/log.ts b/src/log.ts index 7e3640c2..035a7c31 100644 --- a/src/log.ts +++ b/src/log.ts @@ -7,6 +7,9 @@ export const log = { process.stderr.write(`DEBUG: ${format(...args)}\n`); } }, + warning: (...args: unknown[]) => { + process.stderr.write(`WARNING: ${format(...args)}\n`); + }, info: (...args: unknown[]) => { process.stderr.write(`INFO: ${format(...args)}\n`); },