Skip to content

Commit ac9a238

Browse files
committed
Multiple fixes
1 parent ed1f8ca commit ac9a238

File tree

10 files changed

+257
-250
lines changed

10 files changed

+257
-250
lines changed
File renamed without changes.

backend/infrahub/core/checks/models.py renamed to backend/infrahub/artifacts/models.py

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,6 @@
11
from typing import Optional
22

3-
from pydantic import BaseModel, ConfigDict, Field
4-
5-
from infrahub.message_bus.types import ProposedChangeArtifactDefinition, ProposedChangeBranchDiff
6-
7-
8-
class RequestArtifactDefinitionCheck(BaseModel):
9-
"""Sent to validate the generation of artifacts in relation to a proposed change."""
10-
11-
model_config = ConfigDict(arbitrary_types_allowed=True)
12-
13-
artifact_definition: ProposedChangeArtifactDefinition = Field(..., description="The Artifact Definition")
14-
branch_diff: ProposedChangeBranchDiff = Field(..., description="The calculated diff between the two branches")
15-
proposed_change: str = Field(..., description="The unique ID of the Proposed Change")
16-
source_branch: str = Field(..., description="The source branch")
17-
source_branch_sync_with_git: bool = Field(..., description="Indicates if the source branch should sync with git")
18-
destination_branch: str = Field(..., description="The target branch")
3+
from pydantic import BaseModel, Field
194

205

216
class CheckArtifactCreate(BaseModel):
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
from typing import Union
2+
3+
from prefect import flow
4+
5+
from infrahub.artifacts.models import CheckArtifactCreate
6+
from infrahub.core.constants import InfrahubKind, ValidatorConclusion
7+
from infrahub.core.timestamp import Timestamp
8+
from infrahub.git import InfrahubReadOnlyRepository, InfrahubRepository
9+
from infrahub.services import InfrahubServices
10+
from infrahub.tasks.artifact import define_artifact
11+
from infrahub.workflows.utils import add_tags
12+
13+
14+
@flow(name="git-repository-check-artifact-create", flow_run_name="Check artifact creation")
15+
async def create(model: CheckArtifactCreate, service: InfrahubServices) -> ValidatorConclusion:
16+
await add_tags(branches=[model.branch_name], nodes=[model.target_id])
17+
validator = await service.client.get(kind=InfrahubKind.ARTIFACTVALIDATOR, id=model.validator_id, include=["checks"])
18+
19+
repo: InfrahubReadOnlyRepository | InfrahubRepository
20+
if InfrahubKind.READONLYREPOSITORY:
21+
repo = await InfrahubReadOnlyRepository.init(
22+
id=model.repository_id,
23+
name=model.repository_name,
24+
client=service.client,
25+
service=service,
26+
)
27+
else:
28+
repo = await InfrahubRepository.init(
29+
id=model.repository_id,
30+
name=model.repository_name,
31+
client=service.client,
32+
service=service,
33+
)
34+
35+
artifact = await define_artifact(model=model, service=service)
36+
37+
severity = "info"
38+
artifact_result: dict[str, Union[str, bool, None]] = {
39+
"changed": None,
40+
"checksum": None,
41+
"artifact_id": None,
42+
"storage_id": None,
43+
}
44+
check_message = "Failed to render artifact"
45+
46+
try:
47+
result = await repo.render_artifact(artifact=artifact, message=model)
48+
artifact_result["changed"] = result.changed
49+
artifact_result["checksum"] = result.checksum
50+
artifact_result["artifact_id"] = result.artifact_id
51+
artifact_result["storage_id"] = result.storage_id
52+
check_message = "Artifact rendered successfully"
53+
conclusion = ValidatorConclusion.SUCCESS
54+
55+
except Exception as exc:
56+
artifact.status.value = "Error"
57+
await artifact.save()
58+
severity = "critical"
59+
conclusion = ValidatorConclusion.FAILURE
60+
check_message += f": {str(exc)}"
61+
62+
check = None
63+
check_name = f"{model.artifact_name}: {model.target_name}"
64+
existing_check = await service.client.filters(
65+
kind=InfrahubKind.ARTIFACTCHECK, validator__ids=validator.id, name__value=check_name
66+
)
67+
if existing_check:
68+
check = existing_check[0]
69+
70+
if check:
71+
check.created_at.value = Timestamp().to_string()
72+
check.conclusion.value = conclusion.value
73+
check.severity.value = severity
74+
check.changed.value = artifact_result["changed"]
75+
check.checksum.value = artifact_result["checksum"]
76+
check.artifact_id.value = artifact_result["artifact_id"]
77+
check.storage_id.value = artifact_result["storage_id"]
78+
await check.save()
79+
else:
80+
check = await service.client.create(
81+
kind=InfrahubKind.ARTIFACTCHECK,
82+
data={
83+
"name": check_name,
84+
"origin": model.repository_id,
85+
"kind": "ArtifactDefinition",
86+
"validator": model.validator_id,
87+
"created_at": Timestamp().to_string(),
88+
"message": check_message,
89+
"conclusion": conclusion.value,
90+
"severity": severity,
91+
"changed": artifact_result["changed"],
92+
"checksum": artifact_result["checksum"],
93+
"artifact_id": artifact_result["artifact_id"],
94+
"storage_id": artifact_result["storage_id"],
95+
},
96+
)
97+
await check.save()
98+
99+
return conclusion

backend/infrahub/core/checks/tasks.py

Lines changed: 0 additions & 222 deletions
This file was deleted.

backend/infrahub/core/validators/checks_runner.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
import asyncio
2-
from typing import Coroutine
2+
from typing import Any, Coroutine
33

44
from infrahub_sdk.node import InfrahubNode
55

66
from infrahub.core.constants import ValidatorConclusion, ValidatorState
77
from infrahub.core.timestamp import Timestamp
88

99

10-
async def run_checks_and_update_validator(checks: list[Coroutine], validator: InfrahubNode) -> None:
10+
async def run_checks_and_update_validator(
11+
checks: list[Coroutine[Any, None, ValidatorConclusion]], validator: InfrahubNode
12+
) -> None:
1113
"""
1214
Execute a list of checks coroutines, and set validator fields accordingly.
1315
Tasks are retrieved by completion order so as soon as we detect a failing check,
@@ -22,7 +24,7 @@ async def run_checks_and_update_validator(checks: list[Coroutine], validator: In
2224

2325
for earliest_task in asyncio.as_completed(checks):
2426
result = await earliest_task
25-
if result == ValidatorConclusion.FAILURE:
27+
if validator.conclusion.value != ValidatorConclusion.FAILURE.value and result == ValidatorConclusion.FAILURE:
2628
validator.conclusion.value = ValidatorConclusion.FAILURE.value
2729
await validator.save()
2830
# Continue to iterate to wait for the end of all checks

backend/infrahub/message_bus/operations/requests/proposed_change.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
from pydantic import BaseModel
88

99
from infrahub import lock
10-
from infrahub.core.checks.models import RequestArtifactDefinitionCheck
1110
from infrahub.core.constants import CheckType, InfrahubKind, RepositoryInternalStatus
1211
from infrahub.core.diff.coordinator import DiffCoordinator
1312
from infrahub.core.registry import registry
@@ -21,6 +20,7 @@
2120
ProposedChangeSubscriber,
2221
)
2322
from infrahub.proposed_change.models import (
23+
RequestArtifactDefinitionCheck,
2424
RequestProposedChangeDataIntegrity,
2525
RequestProposedChangeRepositoryChecks,
2626
RequestProposedChangeRunGenerators,

0 commit comments

Comments
 (0)