fix(backend): log git import failures once as readable messages

Merged
OG
2mo ago
+1919-348

Why

When the import of a Git repository branch fails, the user reads a Python traceback in the task log instead of the reason. The same failure is often logged two or three times. A failed import of the default branch also stops the import of the repository's other branches.

Goal: a user who views the task log of a failed import reads one line that says what is wrong and where, for example:

Failed to import branch 'feature-x': GraphQL query 'backbone_service' (queries/backbone.gql): Violates uniqueness constraint 'name'

A failure on one branch never stops the import of another branch.

Non-goals:

  • Retrying a failed import in a later sync cycle. Users can run "import current commit again" on the repository.
  • Deciding how infrastructure failures should behave, such as an unreachable Infrahub API or a lock timeout.
  • Errors in one repository that stop the scheduled sync of the other repositories. This happens on develop too and will be filed as a separate bug.

Closes IFC-3304

What changed

What users view differently:

  • Readable failures: a failure caused by the repository content writes one error entry to the task log with no traceback. The entry names the .infrahub.yml entry and its file. Examples are:
    • an invalid schema file or an invalid object in an object file;
    • a GraphQL query whose name already exists;
    • a check or transform that references a GraphQL query that does not exist;
    • a Python file with a syntax error;
    • a missing object file;
    • a commit that is not in the local clone.
  • Unknown failures: a failure that is not recognised is logged once, with its traceback. That traceback shows which error type still needs a readable message.
  • Branch status: the failed branch shows ERROR_IMPORT in both cases.
  • Failure on one branch: an import failure on one branch, of any type, no longer stops the import of the other branches. This covers the scheduled sync, adding a repository, and the first sync of a new clone.
  • Failed default branch when adding a repository: the add flow still imports the other branches and tells the other workers to fetch, then ends as failed.
  • Connection and credential errors: these still stop the sync of that repository for the cycle, as before. The next cycle tries again. They do not mark every remaining branch ERROR_IMPORT, because nothing would reset that status until a new commit is pushed.
  • Commit event failure: if sending the commit event fails after every object is imported, the branch keeps IN_SYNC instead of showing ERROR_IMPORT.
  • Wrong class_name (behavior change): a check or Python transform whose class is not defined in its module now fails the branch import with "The specified class X was not found within the module". Before, the entry was skipped without any message and its existing definition was deleted from Infrahub. Generators already failed this way.
  • Removed definitions: if deleting a definition that the repository no longer declares fails, the message names it, for example Deleting CoreGraphQLQuery 'old_query', removed from the repository: <reason>.
  • Jinja2 dependency analysis: a failure caused by the repository content is logged without a traceback, and its message includes the cause. Other failures keep their traceback.

Implementation notes:

  • One mapping function: describe_import_error in backend/infrahub/git/import_errors.py turns an exception into a readable message. To give a new error type a readable message, add a case to this function only.
  • One boundary: building and applying every import run inside the same boundary in integrator.py. The boundary sets ERROR_IMPORT, logs the failure once, and raises RepositoryImportError. A failed ERROR_IMPORT write is logged and does not replace the import failure.
  • Entry names: each loop over .infrahub.yml entries adds the entry name and file to the exception as a note. Each kind of entry has one label helper, so an entry is named the same way whichever step fails.
  • Containment does not depend on the boundary: import_branch in sync.py imports one branch and returns any failure instead of raising it. The sync loop, the add flow and the bootstrap of a new clone all use it.
  • One log entry per failure: the import error entry carries repository, branch, step and reason as log record fields. raise_if_branches_failed now writes its warning only for branches that failed before the import (step collection). Any alert rule that matched that warning for import failures needs to match the error entry instead.
  • Prefect tracebacks: the import steps are no longer Prefect tasks, because Prefect logs a traceback when an exception leaves a task. RepositoryImportError and RepositoryBranchesFailedError are registered with the existing traceback filter that webhooks use.
  • #10822: the branch is rebased on develop after #10822 (delete removed definitions after their dependents). The deletions run inside the boundary, in #10822's order.

What stayed the same:

  • No schema, GraphQL or API changes.
  • Prefect's own Finished in state Failed(...) entry is still written when a flow fails.

Suggested review order

  1. backend/infrahub/git/import_errors.py: the mapping function, the logging function and the exception type.
  2. backend/infrahub/git/integrator.py: the boundary in build_import_plan and apply_import_plan, and the import_entry labels in each loop.
  3. backend/infrahub/git/sync.py and tasks.py: per-branch containment.
  4. dev/knowledge/backend/git-sync.md: the "Import failures" section describes the design.
  5. Tests.

How to review

  • Check that every place that imports a branch catches failures per branch: RepositorySyncer.sync, RepositoryAdder.add and bootstrap_local_repository.

  • Check the error types that describe_import_error recognises. Some content errors still log a traceback, for example:

    • an unknown kind in an object file (SchemaNotFoundError);
    • a plain ImportError in a transform module;
    • a file of the wrong kind listed under objects, which the SDK raises as a plain ValueError. IHS-301 asks the SDK to raise ValidationError instead.

    Each can be added later with one case.

  • The wrong-class_name change makes imports fail that used to pass with the entry skipped.

Findings from the local cubic review that were not fixed

  • Declined: "every pydantic ValidationError is mapped without a traceback". IFC-3304 lists pydantic ValidationError in the first set of mapped errors, because most of them come from validating .infrahub.yml and the repository's files. A pydantic error raised by Infrahub's own models also loses its traceback; this can be narrowed later if it hides a real bug.
  • Deferred: "a failure in add_tags or get_commit_value marks the branch ERROR_IMPORT". These are infrastructure failures, and how they should behave is not decided in IFC-3304.

How to test

uv run pytest backend/tests/unit/git
INFRAHUB_USE_TEST_CONTAINERS=true uv run pytest backend/tests/component/git -n 0
INFRAHUB_USE_TEST_CONTAINERS=true uv run pytest backend/tests/integration/git/test_import_removed_definitions.py -n 0

Local results on the final commit:

  • Git unit, component and #10822 integration tests: 500 passed, and 1 test that was already expected to fail.
  • Lint and type checks: ruff and mypy passed.
  • Regression checks: the containment tests fail when the per-branch catch or the guarded status write is removed. The new unknown-query and wrong-class tests fail when their fix is removed.
  • Not re-run after the rebase: the full backend unit suite and the other integration tests. CI covers them.

Impact & rollout

  • Backward compatibility: a check or Python transform with a class_name that is not in its module now fails the import (see above). Otherwise, a branch whose import fails still shows ERROR_IMPORT, as before, and the task log has fewer entries.
  • Config/env changes: none.
  • Deployment notes: safe to deploy.

Checklist

  • Tests added/updated
  • Changelog entry added (uv run towncrier create ...)
  • External docs updated (if user-facing or ops-facing change): not needed, the change affects log messages and import failures only
  • Internal .md docs updated (internal knowledge and AI code tools knowledge)
  • I have reviewed AI generated content