test: failing test for #9931 -- git-sync aborts on merged read-only branch

Draft
OP
4mo ago
+119-0

Analyst's findings (summary)

Root cause: When a git branch that maps to an already-merged (read-only) Infrahub branch is collected as a "new" branch, collect_pending_imports calls update_commit_value, whose GraphQLError ("Branch ... has been merged and is read-only") falls outside the per-branch exception tuple and every enclosing catch, so it aborts the whole repository sync and the entire flow.

Affected files:

  • backend/infrahub/git/repository.py -- lines 162-193 (collect_pending_imports, new-branches loop): the except (RepositoryError, CommitNotFoundError, GitCommandError, ValueError) at line 185 does not include infrahub_sdk.exceptions.GraphQLError, so the read-only rejection raised at line 180 (update_commit_value) escapes the per-branch isolation.
  • backend/infrahub/git/base.py -- line 628-646 (update_commit_value): unconditionally calls sdk.repository_update_commit, which raises GraphQLError when the mapped Infrahub branch is read-only (merged/merging).
  • backend/infrahub/git/sync.py -- lines 108-110 (RepositorySyncer.sync): collect_pending_imports is awaited outside the per-branch try/except, so anything it raises propagates straight out of sync().
  • backend/infrahub/git/tasks.py -- lines 232, 366, 380: none of the enclosing frames catch GraphQLError, so it aborts the whole flow.

Replication test

Test file: backend/tests/unit/git/test_git_repository.py
Test name: test_collect_pending_imports_skips_merged_read_only_branch

What it tests: That collect_pending_imports skips a remote git branch whose matching Infrahub branch has already been merged (read-only) — recording no import and no permanent per-branch failure — instead of letting the server's read-only rejection abort the sync.

Verification: Test confirmed FAILING on current code.
Failure reason: On the current code the new-branches loop reaches update_commit_value for the merged branch and the SDK raises GraphQLError ("Branch 'branch01' has been merged and is read-only. No modifications are allowed."). That error is not isolated per branch, so it propagates straight out of collect_pending_imports — exactly the escape path in the root cause — and the test fails before its assertions run.

Failure output (last 20 lines)
>           raise GraphQLError(errors=response["errors"], query=query, variables=variables)
E           infrahub_sdk.exceptions.GraphQLError: An error occurred while executing the GraphQL Query
E               mutation ($repository_id: String!, $commit: String!) {
E                   CoreRepositoryUpdate(data: { id: $repository_id, commit: { is_protected: true, source: $repository_id, value: $commit } }) {
E                       ok
E                       object {
E                           commit {
E                               value
E                           }
E                       }
E                   }
E               , [{'message': "Branch 'branch01' has been merged and is read-only. No modifications are allowed."}]

python_sdk/infrahub_sdk/client.py:1262: GraphQLError
=========================== short test summary info ============================
FAILED backend/tests/unit/git/test_git_repository.py::test_collect_pending_imports_skips_merged_read_only_branch
1 failed, 5 passed in 0.91s

Test expectations

The test builds a real on-disk git source repo with main plus a working branch (branch01), then clones it into an InfrahubRepository. Because branch01 exists on the remote but is not tracked in the local clone, compare_local_remote classifies it as a new branch — the precise state that triggers the bug (asserted as a precondition).

The SDK's HTTP calls are stubbed with httpx_mock (no database or live server needed) to reproduce the merged-branch scenario:

  • BranchCreate is rejected with an "already exist" error, sending collection down the branch.get() lookup path.
  • branch.get() returns the branch with status: MERGED (read-only).
  • repository_update_commit returns the server's read-only rejection.

The test asserts the expected post-fix behavior:

  • result.imports contains no entry for the merged branch (it is not queued for import), and
  • result.failed_imports is empty (the merged branch is skipped as benign, not recorded as a permanent per-cycle failure that would tag the repository with a sync error every minute).

Edge cases covered by the setup: the merged branch is discovered specifically through the new-branches path (missing from the local clone while the Infrahub branch node still exists), and status: MERGED is the read-only state that the server rejects writes for. The repository_update_commit stub is registered but may go unused once the branch is skipped before that call (assert_all_responses_were_requested=False), so the test passes whether the fix skips before or defensively guards the write.

AGENT_TEST_COMPLETE

[!WARNING]

Firewall blocked 1 domain

The following domain was blocked by the firewall during workflow execution:

  • astral.sh

To allow these domains, add them to the network.allowed list in your workflow frontmatter:

network:
  allowed:
    - defaults
    - "astral.sh"

See Network Configuration for more information.

Generated by Bug test-writer agent for #9931 · 695.7 AIC · ⌖ 33.4 AIC · ⊞ 12.8K · ◷
Comment /bug-tdd to run again


Summary by cubic

Adds a failing unit test to reproduce #9931 where git-sync aborts if a “new” remote branch maps to an already merged (read-only) Infrahub branch. The test verifies collect_pending_imports skips the branch (no import and no failure) instead of letting a GraphQLError abort the sync.