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

Conversation

SalarAmir
Copy link

This pull request addresses and resolves issue #5674.

The BranchCreate mutation previously allowed the creation of a branch with a non-existent origin_branch or with an origin_branch that was the same as the branch being created. This commit introduces validation to prevent these invalid states.

Changes:

Validation Logic: Added checks in the BranchCreate.mutate method to ensure:
The origin_branch exists before creating a new branch from it.
A branch cannot be its own origin_branch.
Error Handling: The mutation now raises an InfrahubError with a descriptive message if the validation fails.
Unit Tests: Added a new test class, TestBranchCreateWithOrigin, with three test cases to verify the correctness of the new validation logic.
These changes ensure the integrity of branch creation and provide clear feedback to the user in case of an error.

@SalarAmir SalarAmir requested a review from a team as a code owner June 13, 2025 15:41
Copy link

cla-assistant bot commented Jun 13, 2025

CLA assistant check
All committers have signed the CLA.

@petercrocker
Copy link
Contributor

Thanks @SalarAmir for your contribution! One of the CI pipeline checks failed, would you be able to make the quick change to lint the python file please and push to your fork? 🙏🏻

Copy link
Contributor

@ajtmccarty ajtmccarty left a comment

Choose a reason for hiding this comment

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

thanks for the PR @SalarAmir, I left a few comments to go with Pete's suggestion on fixing the linting issue

@@ -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

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

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)
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(...)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants