Skip to content

Commit 0b1a94a

Browse files
authored
IFC-1908: Add new error message for git connection error (#7467)
* IFC-1908: Add new error message for git connection error FIxes #7392 This PR adds a new error message `Failed to connect to github.com` to be raise as a connection error. * update changelog message * Add task tag * fix mypy * remove flow from repository class * fix mypy * remove callback, use exception
1 parent d012feb commit 0b1a94a

File tree

3 files changed

+57
-3
lines changed

3 files changed

+57
-3
lines changed

backend/infrahub/git/base.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -932,7 +932,10 @@ async def _raise_enriched_error(self, error: GitCommandError, branch_name: str |
932932
def _raise_enriched_error_static(
933933
error: GitCommandError, name: str, location: str, branch_name: str | None = None
934934
) -> NoReturn:
935-
if "Repository not found" in error.stderr or "does not appear to be a git" in error.stderr:
935+
if any(
936+
err in error.stderr
937+
for err in ("Repository not found", "does not appear to be a git", "Failed to connect to")
938+
):
936939
raise RepositoryConnectionError(identifier=name) from error
937940

938941
if "error: pathspec" in error.stderr:

backend/infrahub/git/tasks.py

Lines changed: 52 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from typing import Any
2+
13
from infrahub_sdk import InfrahubClient
24
from infrahub_sdk.protocols import (
35
CoreArtifact,
@@ -14,7 +16,12 @@
1416

1517
from infrahub import lock
1618
from infrahub.context import InfrahubContext
17-
from infrahub.core.constants import InfrahubKind, RepositoryInternalStatus, ValidatorConclusion
19+
from infrahub.core.constants import (
20+
InfrahubKind,
21+
RepositoryInternalStatus,
22+
RepositoryOperationalStatus,
23+
ValidatorConclusion,
24+
)
1825
from infrahub.core.manager import NodeManager
1926
from infrahub.core.registry import registry
2027
from infrahub.exceptions import CheckError, RepositoryError
@@ -152,6 +159,39 @@ async def create_branch(branch: str, branch_id: str) -> None:
152159
pass
153160

154161

162+
@flow(name="sync-git-repo-with-origin", flow_run_name="Sync git repo with origin")
163+
async def sync_git_repo_with_origin_and_tag_on_failure(
164+
client: InfrahubClient,
165+
repository_id: str,
166+
repository_name: str,
167+
repository_location: str,
168+
internal_status: str,
169+
default_branch_name: str,
170+
operational_status: str,
171+
staging_branch: str | None = None,
172+
infrahub_branch: str | None = None,
173+
) -> None:
174+
repo = await InfrahubRepository.init(
175+
id=repository_id,
176+
name=repository_name,
177+
location=repository_location,
178+
client=client,
179+
internal_status=internal_status,
180+
default_branch_name=default_branch_name,
181+
)
182+
183+
try:
184+
await repo.sync(staging_branch=staging_branch)
185+
except RepositoryError:
186+
if operational_status == RepositoryOperationalStatus.ONLINE.value:
187+
params: dict[str, Any] = {
188+
"branches": [infrahub_branch] if infrahub_branch else [],
189+
"nodes": [str(repository_id)],
190+
}
191+
await add_tags(**params)
192+
raise
193+
194+
155195
@flow(name="git_repositories_sync", flow_run_name="Sync Git Repositories")
156196
async def sync_remote_repositories() -> None:
157197
log = get_run_logger()
@@ -204,7 +244,17 @@ async def sync_remote_repositories() -> None:
204244
continue
205245

206246
try:
207-
await repo.sync(staging_branch=staging_branch)
247+
await sync_git_repo_with_origin_and_tag_on_failure(
248+
client=client,
249+
repository_id=repository_data.repository.id,
250+
repository_name=repository_data.repository.name.value,
251+
repository_location=repository_data.repository.location.value,
252+
internal_status=active_internal_status,
253+
default_branch_name=repository_data.repository.default_branch.value,
254+
operational_status=repository_data.repository.operational_status.value,
255+
staging_branch=staging_branch,
256+
infrahub_branch=infrahub_branch,
257+
)
208258
# Tell workers to fetch to stay in sync
209259
message = messages.RefreshGitFetch(
210260
meta=Meta(initiator_id=WORKER_IDENTITY, request_id=get_log_data().get("request_id", "")),

changelog/7392.added.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Added new error message for git connection error

0 commit comments

Comments
 (0)