|
| 1 | +from typing import Any |
| 2 | + |
1 | 3 | from infrahub_sdk import InfrahubClient |
2 | 4 | from infrahub_sdk.protocols import ( |
3 | 5 | CoreArtifact, |
|
14 | 16 |
|
15 | 17 | from infrahub import lock |
16 | 18 | 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 | +) |
18 | 25 | from infrahub.core.manager import NodeManager |
19 | 26 | from infrahub.core.registry import registry |
20 | 27 | from infrahub.exceptions import CheckError, RepositoryError |
@@ -152,6 +159,39 @@ async def create_branch(branch: str, branch_id: str) -> None: |
152 | 159 | pass |
153 | 160 |
|
154 | 161 |
|
| 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 | + |
155 | 195 | @flow(name="git_repositories_sync", flow_run_name="Sync Git Repositories") |
156 | 196 | async def sync_remote_repositories() -> None: |
157 | 197 | log = get_run_logger() |
@@ -204,7 +244,17 @@ async def sync_remote_repositories() -> None: |
204 | 244 | continue |
205 | 245 |
|
206 | 246 | 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 | + ) |
208 | 258 | # Tell workers to fetch to stay in sync |
209 | 259 | message = messages.RefreshGitFetch( |
210 | 260 | meta=Meta(initiator_id=WORKER_IDENTITY, request_id=get_log_data().get("request_id", "")), |
|
0 commit comments