Skip to content

Commit

Permalink
feat: output a warning if the parent branch is protected (#280)
Browse files Browse the repository at this point in the history
  • Loading branch information
antelman107 authored Sep 24, 2024
1 parent 52c6ee8 commit 47bf28c
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 19 deletions.
43 changes: 24 additions & 19 deletions src/commands/branches.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand All @@ -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(() =>
Expand All @@ -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',
Expand Down
3 changes: 3 additions & 0 deletions src/log.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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`);
},
Expand Down

0 comments on commit 47bf28c

Please sign in to comment.