|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +from typing import TYPE_CHECKING |
| 4 | + |
| 5 | +from graphene import Boolean, Field, InputField, InputObjectType, List, Mutation, NonNull, String |
| 6 | + |
| 7 | +from infrahub.core.manager import NodeManager |
| 8 | +from infrahub.generators.models import ProposedChangeGeneratorDefinition, RequestGeneratorDefinitionRun |
| 9 | +from infrahub.graphql.types.task import TaskInfo |
| 10 | +from infrahub.workflows.catalogue import REQUEST_GENERATOR_DEFINITION_RUN |
| 11 | + |
| 12 | +if TYPE_CHECKING: |
| 13 | + from graphql import GraphQLResolveInfo |
| 14 | + |
| 15 | + from ..initialization import GraphqlContext |
| 16 | + |
| 17 | + |
| 18 | +class GeneratorDefinitionRequestRunInput(InputObjectType): |
| 19 | + id = InputField(String(required=True), description="ID of the generator definition to run") |
| 20 | + nodes = InputField(List(of_type=NonNull(String)), description="ID list of targets to run the generator for") |
| 21 | + |
| 22 | + |
| 23 | +class GeneratorDefinitionRequestRun(Mutation): |
| 24 | + class Arguments: |
| 25 | + data = GeneratorDefinitionRequestRunInput(required=True) |
| 26 | + wait_until_completion = Boolean(required=False) |
| 27 | + |
| 28 | + ok = Boolean() |
| 29 | + task = Field(TaskInfo, required=False) |
| 30 | + |
| 31 | + @classmethod |
| 32 | + async def mutate( |
| 33 | + cls, |
| 34 | + root: dict, # noqa: ARG003 |
| 35 | + info: GraphQLResolveInfo, |
| 36 | + data: GeneratorDefinitionRequestRunInput, |
| 37 | + wait_until_completion: bool = True, |
| 38 | + ) -> GeneratorDefinitionRequestRun: |
| 39 | + graphql_context: GraphqlContext = info.context |
| 40 | + db = graphql_context.db |
| 41 | + |
| 42 | + generator_definition = await NodeManager.get_one( |
| 43 | + id=str(data.id), db=db, branch=graphql_context.branch, prefetch_relationships=True, raise_on_error=True |
| 44 | + ) |
| 45 | + query = await generator_definition.query.get_peer(db=db) |
| 46 | + repository = await generator_definition.repository.get_peer(db=db) |
| 47 | + group = await generator_definition.targets.get_peer(db=db) |
| 48 | + |
| 49 | + request_model = RequestGeneratorDefinitionRun( |
| 50 | + generator_definition=ProposedChangeGeneratorDefinition( |
| 51 | + definition_id=generator_definition.id, |
| 52 | + definition_name=generator_definition.name.value, |
| 53 | + class_name=generator_definition.class_name.value, |
| 54 | + file_path=generator_definition.file_path.value, |
| 55 | + query_name=query.name.value, |
| 56 | + query_models=query.models.value, |
| 57 | + repository_id=repository.id, |
| 58 | + parameters=generator_definition.parameters.value, |
| 59 | + group_id=group.id, |
| 60 | + convert_query_response=generator_definition.convert_query_response.value or False, |
| 61 | + ), |
| 62 | + branch=graphql_context.branch.name, |
| 63 | + target_members=data.get("nodes", []), |
| 64 | + ) |
| 65 | + |
| 66 | + if not wait_until_completion: |
| 67 | + workflow = await graphql_context.active_service.workflow.submit_workflow( |
| 68 | + workflow=REQUEST_GENERATOR_DEFINITION_RUN, |
| 69 | + context=graphql_context.get_context(), |
| 70 | + parameters={"model": request_model}, |
| 71 | + ) |
| 72 | + return cls(ok=True, task={"id": workflow.id}) |
| 73 | + |
| 74 | + await graphql_context.active_service.workflow.execute_workflow( |
| 75 | + workflow=REQUEST_GENERATOR_DEFINITION_RUN, |
| 76 | + context=graphql_context.get_context(), |
| 77 | + parameters={"model": request_model}, |
| 78 | + ) |
| 79 | + return cls(ok=True) |
0 commit comments