Skip to content

Add delete branch from git#8616

Open
solababs wants to merge 4 commits intosb-13032026-delete-infrahub-branch-after-merge-ifc-2336from
sb-13032026-delete-git-branch-after-merge-ifc-2336
Open

Add delete branch from git#8616
solababs wants to merge 4 commits intosb-13032026-delete-infrahub-branch-after-merge-ifc-2336from
sb-13032026-delete-git-branch-after-merge-ifc-2336

Conversation

@solababs
Copy link
Contributor

@solababs solababs commented Mar 16, 2026

Why

When delete_branch_after_merge is enabled and a branch has sync_with_git=True, the Infrahub branch is cleaned up automatically but the corresponding branch in the Git repositories is left behind. There is no way to have both sides cleaned up automatically today.

This PR adds the GIT_REPOSITORIES_DELETE_BRANCH workflow and wires delete_git_branch_after_merge into the delete_branch() flow so that all synced Git repositories have the branch removed after the Infrahub branch is deleted. Per-repo failures are logged but never block the deletion.

What changed

  • backend/infrahub/git/base.py — two new methods on InfrahubRepositoryBase: origin_has_branch(branch_name) (checks whether a branch exists on the remote) and delete_remote_branch(branch_name) (pushes --delete to origin and removes the local tracking ref).
  • backend/infrahub/git/tasks.py — new delete_git_branch() flow and git_branch_delete task. The flow fans out across all CoreRepository instances via a batch; the task skips repos that don't have the branch and logs (without re-raising) any deletion failures.
  • backend/infrahub/workflows/catalogue.py — new GIT_REPOSITORIES_DELETE_BRANCH workflow definition registered in the WORKFLOWS list.
  • backend/infrahub/core/branch/tasks.py — after BranchDeletedEvent is sent in delete_branch(), submits GIT_REPOSITORIES_DELETE_BRANCH when delete_git_branch_after_merge=True and the branch has sync_with_git=True.
  • backend/tests/component/git/test_delete_git_branch.py — 4 component tests for origin_has_branch and delete_remote_branch against a real local Git repo.
  • backend/tests/functional/branch/test_delete_git_branch.py — 3 functional tests: config enabled + sync_with_git=True triggers workflow; config disabled does not; sync_with_git=False does not.

No schema, API contract, or database changes. Default for delete_git_branch_after_merge remains False.

Suggested review order

  1. backend/infrahub/git/base.pyorigin_has_branch + delete_remote_branch
  2. backend/infrahub/git/tasks.pydelete_git_branch flow + git_branch_delete task
  3. backend/infrahub/core/branch/tasks.py — trigger in delete_branch()
  4. backend/infrahub/workflows/catalogue.py — new definition + WORKFLOWS registration
  5. Tests

How to review

  • In git_branch_delete task: confirm that swallowing exceptions with log.exception is the right behaviour (FR-013 says per-repo failures must not block deletion). The async with lock.registry.get(...) mirrors git_branch_create and prevents concurrent git ops on the same repo.
  • origin_has_branch is synchronous — do not add await when calling it in the task.
  • The should_delete_git guard in delete_branch() checks both the config flag and obj.sync_with_git — branches not tracked in Git are silently skipped.

How to test

uv run pytest backend/tests/component/git/test_delete_git_branch.py -v
uv run pytest backend/tests/functional/branch/test_delete_git_branch.py -v

Manual flow:

  1. Set delete_branch_after_merge = true and delete_git_branch_after_merge = true in infrahub.toml, restart.
  2. Create a branch with sync_with_git=True, push a commit, merge via BranchMerge.
  3. Verify the Infrahub branch is gone and the branch no longer exists in the linked Git repository.
  4. Confirm with delete_git_branch_after_merge = false — Git branch is retained.

Impact & rollout

  • Backward compatibility: No breaking changes. delete_git_branch_after_merge defaults to false; requires delete_branch_after_merge=true to be set (validated at startup).
  • Config/env changes: Behaviour gated on delete_git_branch_after_merge in MainSettings (Phase 1).
  • Deployment notes: Safe to deploy. Feature is fully opt-in. Git deletion failures are non-fatal.

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)

@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: 86582610-30ad-4d36-9deb-f07ef7e01672

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.

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

codspeed-hq bot commented Mar 16, 2026

Merging this PR will not alter performance

✅ 12 untouched benchmarks


Comparing sb-13032026-delete-git-branch-after-merge-ifc-2336 (5001ed7) with sb-13032026-delete-infrahub-branch-after-merge-ifc-2336 (3462605)

Open in CodSpeed

@solababs solababs marked this pull request as ready for review March 17, 2026 12:07
@solababs solababs requested a review from a team as a code owner March 17, 2026 12:07
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.

1 participant