Skip to content

fix(graphql): Validate origin_branch on BranchCreate(#5674) #6703

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

Open
wants to merge 1 commit into
base: stable
Choose a base branch
from
Open
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
10 changes: 10 additions & 0 deletions backend/infrahub/graphql/mutations/branch.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

from infrahub.core.branch import Branch
from infrahub.database import retry_db_transaction
from infrahub.exceptions import InfrahubError
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is not a valid import. I'd suggest replacing InfrahubError with ProcessingError

from infrahub.graphql.context import apply_external_context
from infrahub.graphql.types.context import ContextInput
from infrahub.log import get_logger
Expand Down Expand Up @@ -70,6 +71,15 @@ async def mutate(

model = BranchCreateModel(**data)
await apply_external_context(graphql_context=graphql_context, context_input=context)
if model.origin_branch:
if model.origin_branch == model.name:
raise InfrahubError(f"A branch cannot originate from itself: {model.name}")
origin_branch_obj = await Branch.get_by_name(db=graphql_context.db, name=model.origin_branch)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this call will raise a BranchNotFoundError if the branch does not exist, so the following conditional won't ever be reached

if not origin_branch_obj:
raise InfrahubError(f"Origin branch '{model.origin_branch}' does not exist.")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know the bug report does not make this clear, but we actually only allow branching from the default branch. It would be good to check for that in this conditional, something like

if not origin_branch_obj.is_default:
    raise ProcessingError(...)





if background_execution or not wait_until_completion:
workflow = await graphql_context.active_service.workflow.submit_workflow(
Expand Down
Loading