-
Notifications
You must be signed in to change notification settings - Fork 43
Add delete branch after merge #8613
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
solababs
wants to merge
5
commits into
sb-13032026-add-config-settings-ifc-2336
Choose a base branch
from
sb-13032026-delete-infrahub-branch-after-merge-ifc-2336
base: sb-13032026-add-config-settings-ifc-2336
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+177
−7
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
845ab6c
Add delete branch after merge
solababs 45e1a4f
Merge branch 'sb-13032026-add-config-settings-ifc-2336' into sb-13032…
solababs 83702fa
update implementation plan and phases
solababs 464c783
Merge branch 'sb-13032026-add-config-settings-ifc-2336' into sb-13032…
solababs 3462605
Merge branch 'sb-13032026-add-config-settings-ifc-2336' into sb-13032…
solababs File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -93,6 +93,7 @@ | |
| from infrahub.validators.tasks import start_validator | ||
| from infrahub.workers.dependencies import get_cache, get_client, get_database, get_event_service, get_workflow | ||
| from infrahub.workflows.catalogue import ( | ||
| BRANCH_DELETE, | ||
| GIT_REPOSITORIES_CHECK_ARTIFACT_CREATE, | ||
| GIT_REPOSITORY_INTERNAL_CHECKS_TRIGGER, | ||
| GIT_REPOSITORY_USER_CHECKS_TRIGGER, | ||
|
|
@@ -245,6 +246,22 @@ async def merge_proposed_change( | |
| user_id=context.account.account_id, | ||
| ) | ||
|
|
||
| if config.SETTINGS.main.delete_branch_after_merge and not source_branch.is_default: | ||
| open_pcs = await NodeManager.query( | ||
| db=db, | ||
| schema=InfrahubKind.PROPOSEDCHANGE, | ||
| filters={ | ||
| "source_branch__value": source_branch.name, | ||
| "state__value": ProposedChangeState.OPEN.value, | ||
| }, | ||
| ) | ||
| if not open_pcs: | ||
| await get_workflow().submit_workflow( | ||
| workflow=BRANCH_DELETE, | ||
| context=context, | ||
| parameters={"branch": source_branch.name}, | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm guessing that the logic here is to prevent breaking open proposed changes, but any open PCs will be unmergeable once this PC finishes merging |
||
| ) | ||
|
|
||
| current_user = await NodeManager.get_one_by_id_or_default_filter( | ||
| id=context.account.account_id, kind=InfrahubKind.GENERICACCOUNT, db=db | ||
| ) | ||
|
|
||
145 changes: 145 additions & 0 deletions
145
backend/tests/functional/branch/test_branch_delete_after_merge.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,145 @@ | ||
| from __future__ import annotations | ||
|
|
||
| from typing import TYPE_CHECKING | ||
| from unittest.mock import ANY, AsyncMock, patch | ||
|
|
||
| import pytest | ||
| from infrahub_sdk.graphql import Mutation | ||
|
|
||
| from infrahub import config | ||
| from infrahub.core import registry | ||
| from infrahub.core.constants import InfrahubKind | ||
| from infrahub.services.adapters.workflow.local import WorkflowLocalExecution | ||
| from infrahub.workflows.catalogue import BRANCH_DELETE | ||
| from tests.helpers.schema import CAR_SCHEMA, load_schema | ||
| from tests.helpers.test_app import TestInfrahubApp | ||
|
|
||
| if TYPE_CHECKING: | ||
| from pathlib import Path | ||
|
|
||
| from infrahub_sdk import InfrahubClient | ||
|
|
||
| from infrahub.database import InfrahubDatabase | ||
| from tests.adapters.message_bus import BusSimulator | ||
|
|
||
|
|
||
| class TestAutoDeleteBranchAfterMerge(TestInfrahubApp): | ||
| @pytest.fixture(scope="class") | ||
| async def initial_dataset( | ||
| self, | ||
| db: InfrahubDatabase, | ||
| initialize_registry: None, | ||
| git_repos_source_dir_module_scope: Path, | ||
| client: InfrahubClient, | ||
| bus_simulator: BusSimulator, | ||
| prefect_test_fixture: None, | ||
| ) -> None: | ||
| await load_schema(db, schema=CAR_SCHEMA) | ||
|
|
||
| async def test_branch_auto_deleted_after_standard_merge_when_config_enabled( | ||
| self, initial_dataset: None, client: InfrahubClient, db: InfrahubDatabase | ||
| ) -> None: | ||
| config.SETTINGS.main.delete_branch_after_merge = True | ||
| branch = await client.branch.create(branch_name="auto_delete_standard_enabled") | ||
|
|
||
| with patch.object(WorkflowLocalExecution, "submit_workflow", new_callable=AsyncMock) as mock_submit: | ||
| query = Mutation( | ||
| mutation="BranchMerge", | ||
| input_data={"data": {"name": branch.name}}, | ||
| query={"ok": None, "task": {"id": None}, "object": {"id": None}}, | ||
| ) | ||
| await client.execute_graphql(query=query.render()) | ||
|
|
||
| mock_submit.assert_any_call( | ||
| workflow=BRANCH_DELETE, | ||
| context=ANY, | ||
| parameters={"branch": branch.name}, | ||
| ) | ||
|
|
||
| async def test_branch_not_deleted_after_standard_merge_when_config_disabled( | ||
| self, initial_dataset: None, client: InfrahubClient, db: InfrahubDatabase | ||
| ) -> None: | ||
| config.SETTINGS.main.delete_branch_after_merge = False | ||
| branch = await client.branch.create(branch_name="auto_delete_standard_disabled") | ||
|
|
||
| with patch.object(WorkflowLocalExecution, "submit_workflow", new_callable=AsyncMock) as mock_submit: | ||
| query = Mutation( | ||
| mutation="BranchMerge", | ||
| input_data={"data": {"name": branch.name}}, | ||
| query={"ok": None, "task": {"id": None}, "object": {"id": None}}, | ||
| ) | ||
| await client.execute_graphql(query=query.render()) | ||
|
|
||
| delete_calls = [c for c in mock_submit.call_args_list if c.kwargs.get("workflow") == BRANCH_DELETE] | ||
| assert not delete_calls | ||
|
|
||
| async def test_branch_auto_deleted_after_proposed_change_merge( | ||
| self, initial_dataset: None, client: InfrahubClient, db: InfrahubDatabase | ||
| ) -> None: | ||
| config.SETTINGS.main.delete_branch_after_merge = True | ||
| branch = await client.branch.create(branch_name="auto_delete_pc_enabled") | ||
|
|
||
| pc = await client.create( | ||
| kind=InfrahubKind.PROPOSEDCHANGE, | ||
| data={ | ||
| "name": {"value": "test-pc-auto-delete"}, | ||
| "source_branch": {"value": branch.name}, | ||
| "destination_branch": {"value": registry.default_branch}, | ||
| "is_draft": {"value": False}, | ||
| }, | ||
| ) | ||
| await pc.save() | ||
|
|
||
| with patch.object(WorkflowLocalExecution, "submit_workflow", new_callable=AsyncMock) as mock_submit: | ||
| update_query = Mutation( | ||
| mutation="CoreProposedChangeUpdate", | ||
| input_data={"data": {"id": pc.id, "state": {"value": "merged"}}}, | ||
| query={"ok": None, "object": {"state": {"value": None}}}, | ||
| ) | ||
| await client.execute_graphql(query=update_query.render()) | ||
|
|
||
| mock_submit.assert_any_call( | ||
| workflow=BRANCH_DELETE, | ||
| context=ANY, | ||
| parameters={"branch": branch.name}, | ||
| ) | ||
|
|
||
| @pytest.mark.skip("Multiple proposed changes are not allowed for the same branch") | ||
| async def test_branch_not_deleted_when_other_open_proposed_changes_exist( | ||
| self, initial_dataset: None, client: InfrahubClient, db: InfrahubDatabase | ||
| ) -> None: | ||
| config.SETTINGS.main.delete_branch_after_merge = True | ||
| branch = await client.branch.create(branch_name="auto_delete_pc_other_open") | ||
|
|
||
| pc1 = await client.create( | ||
| kind=InfrahubKind.PROPOSEDCHANGE, | ||
| data={ | ||
| "name": {"value": "pc-open-1"}, | ||
| "source_branch": {"value": branch.name}, | ||
| "destination_branch": {"value": registry.default_branch}, | ||
| "is_draft": {"value": False}, | ||
| }, | ||
| ) | ||
| await pc1.save() | ||
|
|
||
| pc2 = await client.create( | ||
| kind=InfrahubKind.PROPOSEDCHANGE, | ||
| data={ | ||
| "name": {"value": "pc-open-2"}, | ||
| "source_branch": {"value": branch.name}, | ||
| "destination_branch": {"value": "pc-open-1"}, | ||
| "is_draft": {"value": False}, | ||
| }, | ||
| ) | ||
| await pc2.save() | ||
|
|
||
| with patch.object(WorkflowLocalExecution, "submit_workflow", new_callable=AsyncMock) as mock_submit: | ||
| update_query = Mutation( | ||
| mutation="CoreProposedChangeUpdate", | ||
| input_data={"data": {"id": pc1.id, "state": {"value": "merged"}}}, | ||
| query={"ok": None, "object": {"state": {"value": None}}}, | ||
| ) | ||
| await client.execute_graphql(query=update_query.render()) | ||
|
|
||
| delete_calls = [c for c in mock_submit.call_args_list if c.kwargs.get("workflow") == BRANCH_DELETE] | ||
| assert not delete_calls |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why does the
proposed_change_idneed to beNonehere?