Skip to content

Add delete branch after merge#8613

Open
solababs wants to merge 5 commits intosb-13032026-add-config-settings-ifc-2336from
sb-13032026-delete-infrahub-branch-after-merge-ifc-2336
Open

Add delete branch after merge#8613
solababs wants to merge 5 commits intosb-13032026-add-config-settings-ifc-2336from
sb-13032026-delete-infrahub-branch-after-merge-ifc-2336

Conversation

@solababs
Copy link
Contributor

@solababs solababs commented Mar 16, 2026

Why

After a branch is merged, it typically has no further use but remains in the branch list until manually deleted. There is no way to automatically clean it up today.

This PR wires up the delete_branch_after_merge config setting (added in Phase 1) to the actual merge paths — both direct BranchMerge and proposed change merge — so that the Infrahub branch is automatically deleted after a successful merge when the setting is enabled.

What changed

  • backend/infrahub/core/branch/tasks.py — after a successful BranchMerge, if delete_branch_after_merge=True and the branch is not the default branch and there is no associated proposed change, submits the BRANCH_DELETE workflow.
  • backend/infrahub/proposed_change/tasks.py — after a proposed change is merged, if delete_branch_after_merge=True, checks that no other open proposed changes exist for the source branch before submitting BRANCH_DELETE.
  • backend/tests/functional/branch/test_branch_delete_after_merge.py — 4 functional tests covering: config-enabled standard merge triggers delete, config-disabled standard merge does not, proposed change merge triggers delete, and a skipped test for the multi-PC guard (multiple PCs on the same branch are not currently allowed).

The default value of delete_branch_after_merge remains False, so existing deployments are unaffected.

Suggested review order

  1. backend/infrahub/core/branch/tasks.pymerge_branch change
  2. backend/infrahub/proposed_change/tasks.pymerge_proposed_change change
  3. backend/tests/functional/branch/test_branch_delete_after_merge.py — functional tests

How to review

  • In proposed_change/tasks.py: confirm the open-PC guard logic is correct — we query for open PCs on the source branch before deciding to delete. Worth checking whether the PC being merged is already in a non-open state at this point and would be excluded from the query.
  • The proposed_change_id is None guard in branch/tasks.py prevents a double-delete when a proposed change merge internally calls merge_branch.

How to test

uv run pytest backend/tests/functional/branch/test_branch_delete_after_merge.py -v

Manual flow:

  1. Set delete_branch_after_merge = true in infrahub.toml, restart service.
  2. Create a branch, make a change, merge via BranchMerge GraphQL mutation — branch should no longer appear in the branch list.
  3. Repeat by merging a proposed change — same result.
  4. Confirm with delete_branch_after_merge = false (default) — branch is retained after merge.

Impact & rollout

  • Backward compatibility: No breaking changes. delete_branch_after_merge defaults to false; existing behaviour is unchanged.
  • Config/env changes: Behaviour is controlled by delete_branch_after_merge in MainSettings (introduced in Phase 1).
  • Deployment notes: Safe to deploy. Feature is opt-in via config.

Checklist

  • Tests added/updated
  • Changelog entry added (uv run towncrier create ...)
  • External docs updated (if user-facing or ops-facing change)
  • Internal .md docs updated (internal knowledge and AI code tools knowledge)

@github-actions github-actions bot added the group/backend Issue related to the backend (API Server, Git Agent) label Mar 16, 2026
@coderabbitai
Copy link
Contributor

coderabbitai bot commented Mar 16, 2026

Important

Review skipped

Auto reviews are disabled on base/target branches other than the default branch.

🗂️ Base branches to auto review (4)
  • main
  • stable
  • develop
  • release-.*

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 0237c32f-0459-4f33-91df-105a1191155d

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • 🔍 Trigger review
📝 Coding Plan
  • Generate coding plan for human review comments

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@codspeed-hq
Copy link

codspeed-hq bot commented Mar 16, 2026

Merging this PR will improve performance by 38.81%

⚠️ Different runtime environments detected

Some benchmarks with significant performance changes were compared across different runtime environments,
which may affect the accuracy of the results.

Open the report in CodSpeed to investigate

⚡ 12 improved benchmarks

Performance Changes

Benchmark BASE HEAD Efficiency
test_nodemanager_querypeers 1.5 ms 1.2 ms +21.8%
test_schemabranch_process 1,040.4 ms 795.5 ms +30.78%
test_get_schema 327.3 ms 251.7 ms +30.03%
test_graphql_generate_schema 516.1 ms 371.8 ms +38.81%
test_query_rel_many 693.2 ms 523.6 ms +32.38%
test_query_rel_one 662 ms 501.1 ms +32.1%
test_get_menu 246 ms 188.3 ms +30.62%
test_query_one_model 464.1 ms 353.5 ms +31.29%
test_base_schema_duplicate_CoreProposedChange 2.1 ms 1.7 ms +23.66%
test_load_node_to_db_node_schema 67.1 ms 52.2 ms +28.57%
test_schemabranch_duplicate 7.2 ms 5.5 ms +31.28%
test_relationshipmanager_getpeer 157.4 µs 131 µs +20.2%

Comparing sb-13032026-delete-infrahub-branch-after-merge-ifc-2336 (3462605) with sb-13032026-add-config-settings-ifc-2336 (b07039d)

Open in CodSpeed

@solababs solababs marked this pull request as ready for review March 17, 2026 11:46
@solababs solababs requested a review from a team as a code owner March 17, 2026 11:46
…026-delete-infrahub-branch-after-merge-ifc-2336
…026-delete-infrahub-branch-after-merge-ifc-2336
parameters={"branch_name": obj.name},
)

if config.SETTINGS.main.delete_branch_after_merge and not obj.is_default and proposed_change_id is None:
Copy link
Contributor

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_id need to be None here?

await get_workflow().submit_workflow(
workflow=BRANCH_DELETE,
context=context,
parameters={"branch": source_branch.name},
Copy link
Contributor

Choose a reason for hiding this comment

The 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
maybe the right thing to do in this case is to set any open PCs on this branch to closed and then allow deleting it

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

group/backend Issue related to the backend (API Server, Git Agent)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants