22
33from prefect import flow
44
5+ from infrahub .artifacts .models import CheckArtifactCreate
56from infrahub .core .constants import InfrahubKind , ValidatorConclusion
67from infrahub .core .timestamp import Timestamp
7- from infrahub .git .repository import InfrahubReadOnlyRepository , InfrahubRepository
8- from infrahub .log import get_logger
9- from infrahub .message_bus import messages
8+ from infrahub .git import InfrahubReadOnlyRepository , InfrahubRepository
109from infrahub .services import InfrahubServices
1110from infrahub .tasks .artifact import define_artifact
12- from infrahub .tasks .check import set_check_status
1311from infrahub .workflows .utils import add_tags
1412
15- log = get_logger ()
16-
1713
1814@flow (name = "git-repository-check-artifact-create" , flow_run_name = "Check artifact creation" )
19- async def create (message : messages .CheckArtifactCreate , service : InfrahubServices ) -> None :
20- await add_tags (branches = [message .branch_name ], nodes = [message .target_id ])
21- validator = await service .client .get (
22- kind = InfrahubKind .ARTIFACTVALIDATOR , id = message .validator_id , include = ["checks" ]
23- )
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" ])
2418
19+ repo : InfrahubReadOnlyRepository | InfrahubRepository
2520 if InfrahubKind .READONLYREPOSITORY :
2621 repo = await InfrahubReadOnlyRepository .init (
27- id = message .repository_id ,
28- name = message .repository_name ,
22+ id = model .repository_id ,
23+ name = model .repository_name ,
2924 client = service .client ,
3025 service = service ,
3126 )
3227 else :
3328 repo = await InfrahubRepository .init (
34- id = message .repository_id ,
35- name = message .repository_name ,
29+ id = model .repository_id ,
30+ name = model .repository_name ,
3631 client = service .client ,
3732 service = service ,
3833 )
3934
40- artifact = await define_artifact (message = message , service = service )
35+ artifact = await define_artifact (model = model , service = service )
4136
42- conclusion = ValidatorConclusion .SUCCESS .value
4337 severity = "info"
4438 artifact_result : dict [str , Union [str , bool , None ]] = {
4539 "changed" : None ,
@@ -50,22 +44,23 @@ async def create(message: messages.CheckArtifactCreate, service: InfrahubService
5044 check_message = "Failed to render artifact"
5145
5246 try :
53- result = await repo .render_artifact (artifact = artifact , message = message )
47+ result = await repo .render_artifact (artifact = artifact , message = model )
5448 artifact_result ["changed" ] = result .changed
5549 artifact_result ["checksum" ] = result .checksum
5650 artifact_result ["artifact_id" ] = result .artifact_id
5751 artifact_result ["storage_id" ] = result .storage_id
5852 check_message = "Artifact rendered successfully"
53+ conclusion = ValidatorConclusion .SUCCESS
5954
6055 except Exception as exc :
61- conclusion = ValidatorConclusion .FAILURE .value
6256 artifact .status .value = "Error"
57+ await artifact .save ()
6358 severity = "critical"
59+ conclusion = ValidatorConclusion .FAILURE
6460 check_message += f": { str (exc )} "
65- await artifact .save ()
6661
6762 check = None
68- check_name = f"{ message .artifact_name } : { message .target_name } "
63+ check_name = f"{ model .artifact_name } : { model .target_name } "
6964 existing_check = await service .client .filters (
7065 kind = InfrahubKind .ARTIFACTCHECK , validator__ids = validator .id , name__value = check_name
7166 )
@@ -74,7 +69,7 @@ async def create(message: messages.CheckArtifactCreate, service: InfrahubService
7469
7570 if check :
7671 check .created_at .value = Timestamp ().to_string ()
77- check .conclusion .value = conclusion
72+ check .conclusion .value = conclusion . value
7873 check .severity .value = severity
7974 check .changed .value = artifact_result ["changed" ]
8075 check .checksum .value = artifact_result ["checksum" ]
@@ -86,12 +81,12 @@ async def create(message: messages.CheckArtifactCreate, service: InfrahubService
8681 kind = InfrahubKind .ARTIFACTCHECK ,
8782 data = {
8883 "name" : check_name ,
89- "origin" : message .repository_id ,
84+ "origin" : model .repository_id ,
9085 "kind" : "ArtifactDefinition" ,
91- "validator" : message .validator_id ,
86+ "validator" : model .validator_id ,
9287 "created_at" : Timestamp ().to_string (),
9388 "message" : check_message ,
94- "conclusion" : conclusion ,
89+ "conclusion" : conclusion . value ,
9590 "severity" : severity ,
9691 "changed" : artifact_result ["changed" ],
9792 "checksum" : artifact_result ["checksum" ],
@@ -101,4 +96,4 @@ async def create(message: messages.CheckArtifactCreate, service: InfrahubService
10196 )
10297 await check .save ()
10398
104- await set_check_status ( message = message , conclusion = conclusion , service = service )
99+ return conclusion
0 commit comments