From e18acb722867190889719c0572e92d655688ac20 Mon Sep 17 00:00:00 2001 From: Lucas Guillermou Date: Fri, 25 Oct 2024 12:42:55 +0200 Subject: [PATCH 1/2] Migrate RequestDiffUpdate to prefect --- .../diff/models.py} | 6 +-- backend/infrahub/core/diff/tasks.py | 28 +++++++++++++ backend/infrahub/graphql/mutations/diff.py | 8 ++-- .../infrahub/message_bus/messages/__init__.py | 2 - .../message_bus/operations/__init__.py | 1 - .../message_bus/operations/event/branch.py | 7 +++- .../message_bus/operations/requests/diff.py | 17 -------- backend/infrahub/workflows/catalogue.py | 8 ++++ .../operations/event/test_branch.py | 19 +++++---- docs/docs/reference/message-bus-events.mdx | 41 ------------------- 10 files changed, 60 insertions(+), 77 deletions(-) rename backend/infrahub/{message_bus/messages/request_diff_update.py => core/diff/models.py} (75%) create mode 100644 backend/infrahub/core/diff/tasks.py diff --git a/backend/infrahub/message_bus/messages/request_diff_update.py b/backend/infrahub/core/diff/models.py similarity index 75% rename from backend/infrahub/message_bus/messages/request_diff_update.py rename to backend/infrahub/core/diff/models.py index 7310473f77..1db1bcbb5c 100644 --- a/backend/infrahub/message_bus/messages/request_diff_update.py +++ b/backend/infrahub/core/diff/models.py @@ -1,9 +1,7 @@ -from pydantic import Field +from pydantic import BaseModel, Field -from infrahub.message_bus import InfrahubMessage - -class RequestDiffUpdate(InfrahubMessage): +class RequestDiffUpdate(BaseModel): """ Request diff to be updated. diff --git a/backend/infrahub/core/diff/tasks.py b/backend/infrahub/core/diff/tasks.py new file mode 100644 index 0000000000..c8cdc9c908 --- /dev/null +++ b/backend/infrahub/core/diff/tasks.py @@ -0,0 +1,28 @@ +from prefect import flow + +from infrahub.core import registry +from infrahub.core.diff.coordinator import DiffCoordinator +from infrahub.core.diff.models import RequestDiffUpdate +from infrahub.dependencies.registry import get_component_registry +from infrahub.log import get_logger +from infrahub.services import services + +log = get_logger() + + +@flow(name="diff-update") +async def update_diff(model: RequestDiffUpdate) -> None: + service = services.service + component_registry = get_component_registry() + base_branch = await registry.get_branch(db=service.database, branch=registry.default_branch) + diff_branch = await registry.get_branch(db=service.database, branch=model.branch_name) + + diff_coordinator = await component_registry.get_component(DiffCoordinator, db=service.database, branch=diff_branch) + + await diff_coordinator.run_update( + base_branch=base_branch, + diff_branch=diff_branch, + from_time=model.from_time, + to_time=model.to_time, + name=model.name, + ) diff --git a/backend/infrahub/graphql/mutations/diff.py b/backend/infrahub/graphql/mutations/diff.py index 8941b999ff..5adf149da3 100644 --- a/backend/infrahub/graphql/mutations/diff.py +++ b/backend/infrahub/graphql/mutations/diff.py @@ -6,7 +6,9 @@ from infrahub.core import registry from infrahub.core.diff.coordinator import DiffCoordinator from infrahub.dependencies.registry import get_component_registry -from infrahub.message_bus import messages + +from ...core.diff.models import RequestDiffUpdate +from ...workflows.catalogue import REQUEST_DIFF_UPDATE if TYPE_CHECKING: from ..initialization import GraphqlContext @@ -55,13 +57,13 @@ async def mutate( return {"ok": True} - message = messages.RequestDiffUpdate( + model = RequestDiffUpdate( branch_name=str(data.branch), name=data.name, from_time=from_timestamp_str, to_time=to_timestamp_str, ) if context.service: - await context.service.send(message=message) + await context.service.workflow.submit_workflow(workflow=REQUEST_DIFF_UPDATE, parameters={"model": model}) return {"ok": True} diff --git a/backend/infrahub/message_bus/messages/__init__.py b/backend/infrahub/message_bus/messages/__init__.py index 1585672245..164d1df842 100644 --- a/backend/infrahub/message_bus/messages/__init__.py +++ b/backend/infrahub/message_bus/messages/__init__.py @@ -32,7 +32,6 @@ from .refresh_webhook_configuration import RefreshWebhookConfiguration from .request_artifactdefinition_check import RequestArtifactDefinitionCheck from .request_diff_refresh import RequestDiffRefresh -from .request_diff_update import RequestDiffUpdate from .request_generatordefinition_check import RequestGeneratorDefinitionCheck from .request_generatordefinition_run import RequestGeneratorDefinitionRun from .request_graphqlquerygroup_update import RequestGraphQLQueryGroupUpdate @@ -76,7 +75,6 @@ "refresh.registry.rebased_branch": RefreshRegistryRebasedBranch, "refresh.webhook.configuration": RefreshWebhookConfiguration, "request.artifact_definition.check": RequestArtifactDefinitionCheck, - "request.diff.update": RequestDiffUpdate, "request.diff.refresh": RequestDiffRefresh, "request.generator_definition.check": RequestGeneratorDefinitionCheck, "request.generator_definition.run": RequestGeneratorDefinitionRun, diff --git a/backend/infrahub/message_bus/operations/__init__.py b/backend/infrahub/message_bus/operations/__init__.py index 323e46c0f6..2a8695360c 100644 --- a/backend/infrahub/message_bus/operations/__init__.py +++ b/backend/infrahub/message_bus/operations/__init__.py @@ -45,7 +45,6 @@ "refresh.registry.rebased_branch": refresh.registry.rebased_branch, "refresh.webhook.configuration": refresh.webhook.configuration, "request.diff.refresh": requests.diff.refresh, - "request.diff.update": requests.diff.update, "request.generator_definition.check": requests.generator_definition.check, "request.generator_definition.run": requests.generator_definition.run, "request.graphql_query_group.update": requests.graphql_query_group.update, diff --git a/backend/infrahub/message_bus/operations/event/branch.py b/backend/infrahub/message_bus/operations/event/branch.py index 12e8f602de..cfa133991e 100644 --- a/backend/infrahub/message_bus/operations/event/branch.py +++ b/backend/infrahub/message_bus/operations/event/branch.py @@ -4,6 +4,7 @@ from infrahub.core import registry from infrahub.core.diff.model.path import BranchTrackingId +from infrahub.core.diff.models import RequestDiffUpdate from infrahub.core.diff.repository.repository import DiffRepository from infrahub.dependencies.registry import get_component_registry from infrahub.log import get_logger @@ -11,6 +12,7 @@ from infrahub.services import InfrahubServices from infrahub.workflows.catalogue import ( GIT_REPOSITORIES_CREATE_BRANCH, + REQUEST_DIFF_UPDATE, TRIGGER_ARTIFACT_DEFINITION_GENERATE, ) @@ -72,7 +74,10 @@ async def merge(message: messages.EventBranchMerge, service: InfrahubServices) - and diff_root.tracking_id and isinstance(diff_root.tracking_id, BranchTrackingId) ): - events.append(messages.RequestDiffUpdate(branch_name=diff_root.diff_branch_name)) + request_diff_update_model = RequestDiffUpdate(branch_name=diff_root.diff_branch_name) + await service.workflow.submit_workflow( + workflow=REQUEST_DIFF_UPDATE, parameters={"model": request_diff_update_model} + ) for event in events: event.assign_meta(parent=message) diff --git a/backend/infrahub/message_bus/operations/requests/diff.py b/backend/infrahub/message_bus/operations/requests/diff.py index f15f016eb2..0b140b44f0 100644 --- a/backend/infrahub/message_bus/operations/requests/diff.py +++ b/backend/infrahub/message_bus/operations/requests/diff.py @@ -10,23 +10,6 @@ log = get_logger() -@flow(name="diff-update") -async def update(message: messages.RequestDiffUpdate, service: InfrahubServices) -> None: - component_registry = get_component_registry() - base_branch = await registry.get_branch(db=service.database, branch=registry.default_branch) - diff_branch = await registry.get_branch(db=service.database, branch=message.branch_name) - - diff_coordinator = await component_registry.get_component(DiffCoordinator, db=service.database, branch=diff_branch) - - await diff_coordinator.run_update( - base_branch=base_branch, - diff_branch=diff_branch, - from_time=message.from_time, - to_time=message.to_time, - name=message.name, - ) - - @flow(name="diff-refresh") async def refresh(message: messages.RequestDiffRefresh, service: InfrahubServices) -> None: component_registry = get_component_registry() diff --git a/backend/infrahub/workflows/catalogue.py b/backend/infrahub/workflows/catalogue.py index 7b1d5da03f..0a9e840fa2 100644 --- a/backend/infrahub/workflows/catalogue.py +++ b/backend/infrahub/workflows/catalogue.py @@ -92,6 +92,13 @@ function="generate_request_artifact_definition", ) +REQUEST_DIFF_UPDATE = WorkflowDefinition( + name="diff-update", + type=WorkflowType.INTERNAL, + module="infrahub.core.diff.tasks", + function="update_diff", +) + GIT_REPOSITORIES_SYNC = WorkflowDefinition( name="git_repositories_sync", type=WorkflowType.INTERNAL, @@ -144,4 +151,5 @@ BRANCH_MERGE, REQUEST_ARTIFACT_DEFINITION_GENERATE, REQUEST_GENERATOR_RUN, + REQUEST_DIFF_UPDATE, ] diff --git a/backend/tests/unit/message_bus/operations/event/test_branch.py b/backend/tests/unit/message_bus/operations/event/test_branch.py index aeec413319..1a249974bf 100644 --- a/backend/tests/unit/message_bus/operations/event/test_branch.py +++ b/backend/tests/unit/message_bus/operations/event/test_branch.py @@ -5,6 +5,7 @@ from infrahub.core.branch import Branch from infrahub.core.diff.model.path import BranchTrackingId, EnrichedDiffRoot +from infrahub.core.diff.models import RequestDiffUpdate from infrahub.core.diff.repository.repository import DiffRepository from infrahub.core.timestamp import Timestamp from infrahub.dependencies.component.registry import ComponentDependencyRegistry @@ -12,7 +13,7 @@ from infrahub.message_bus.operations.event.branch import delete, merge, rebased from infrahub.services import InfrahubServices, services from infrahub.services.adapters.workflow.local import WorkflowLocalExecution -from infrahub.workflows.catalogue import TRIGGER_ARTIFACT_DEFINITION_GENERATE +from infrahub.workflows.catalogue import REQUEST_DIFF_UPDATE, TRIGGER_ARTIFACT_DEFINITION_GENERATE from tests.adapters.message_bus import BusRecorder @@ -100,6 +101,14 @@ async def test_merged(default_branch: Branch, init_service: InfrahubServices, pr expected_calls = [ call(workflow=TRIGGER_ARTIFACT_DEFINITION_GENERATE, parameters={"branch": message.target_branch}), + call( + workflow=REQUEST_DIFF_UPDATE, + parameters={"model": RequestDiffUpdate(branch_name=tracked_diff_roots[0].diff_branch_name)}, + ), + call( + workflow=REQUEST_DIFF_UPDATE, + parameters={"model": RequestDiffUpdate(branch_name=tracked_diff_roots[1].diff_branch_name)}, + ), ] mock_submit_workflow.assert_has_calls(expected_calls) assert mock_submit_workflow.call_count == len(expected_calls) @@ -109,15 +118,9 @@ async def test_merged(default_branch: Branch, init_service: InfrahubServices, pr ) diff_repo.get_empty_roots.assert_awaited_once_with(base_branch_names=[target_branch_name]) - assert len(service.message_bus.messages) == 4 + assert len(service.message_bus.messages) == 2 assert service.message_bus.messages[0] == messages.RefreshRegistryBranches() assert service.message_bus.messages[1] == messages.TriggerGeneratorDefinitionRun(branch=target_branch_name) - assert service.message_bus.messages[2] == messages.RequestDiffUpdate( - branch_name=tracked_diff_roots[0].diff_branch_name - ) - assert service.message_bus.messages[3] == messages.RequestDiffUpdate( - branch_name=tracked_diff_roots[1].diff_branch_name - ) async def test_rebased(default_branch: Branch, prefect_test_fixture): diff --git a/docs/docs/reference/message-bus-events.mdx b/docs/docs/reference/message-bus-events.mdx index 0fceadd482..800b3f2080 100644 --- a/docs/docs/reference/message-bus-events.mdx +++ b/docs/docs/reference/message-bus-events.mdx @@ -590,26 +590,6 @@ For more detailed explanations on how to use these events within Infrahub, see t ### Request Diff - -#### Event request.diff.update - - -**Description**: Request diff to be updated. - - If the message only include a branch_name, it is assumed to be for updating the diff that tracks - the lifetime changes of a branch - -**Priority**: 3 - - -| Key | Description | Type | Default Value | -|-----|-------------|------|---------------| -| **meta** | Meta properties for the message | N/A | None | -| **branch_name** | The branch associated with the diff | string | None | -| **name** | N/A | N/A | None | -| **from_time** | N/A | N/A | None | -| **to_time** | N/A | N/A | None | - #### Event request.diff.refresh @@ -1585,27 +1565,6 @@ For more detailed explanations on how to use these events within Infrahub, see t ### Request Diff - -#### Event request.diff.update - - -**Description**: Request diff to be updated. - - If the message only include a branch_name, it is assumed to be for updating the diff that tracks - the lifetime changes of a branch - -**Priority**: 3 - - - -| Key | Description | Type | Default Value | -|-----|-------------|------|---------------| -| **meta** | Meta properties for the message | N/A | None | -| **branch_name** | The branch associated with the diff | string | None | -| **name** | N/A | N/A | None | -| **from_time** | N/A | N/A | None | -| **to_time** | N/A | N/A | None | - #### Event request.diff.refresh From 604f8e961fbc7be4e75a4a653a649b3005563bf1 Mon Sep 17 00:00:00 2001 From: Lucas Guillermou Date: Fri, 25 Oct 2024 13:14:12 +0200 Subject: [PATCH 2/2] Migrate RequestDiffRefresh to prefect --- backend/infrahub/core/diff/models.py | 7 ++++ backend/infrahub/core/diff/tasks.py | 14 ++++++- .../graphql/mutations/artifact_definition.py | 4 +- backend/infrahub/graphql/mutations/diff.py | 5 +-- .../infrahub/message_bus/messages/__init__.py | 2 - .../message_bus/operations/__init__.py | 1 - .../message_bus/operations/event/branch.py | 10 ++++- .../operations/requests/__init__.py | 2 - .../message_bus/operations/requests/diff.py | 20 --------- backend/infrahub/workflows/catalogue.py | 8 ++++ .../operations/event/test_branch.py | 30 ++++++++++---- docs/docs/reference/message-bus-events.mdx | 41 ------------------- 12 files changed, 63 insertions(+), 81 deletions(-) delete mode 100644 backend/infrahub/message_bus/operations/requests/diff.py diff --git a/backend/infrahub/core/diff/models.py b/backend/infrahub/core/diff/models.py index 1db1bcbb5c..fa898455b1 100644 --- a/backend/infrahub/core/diff/models.py +++ b/backend/infrahub/core/diff/models.py @@ -13,3 +13,10 @@ class RequestDiffUpdate(BaseModel): name: str | None = None from_time: str | None = None to_time: str | None = None + + +class RequestDiffRefresh(BaseModel): + """Request diff be recalculated from scratch.""" + + branch_name: str = Field(..., description="The branch associated with the diff") + diff_id: str = Field(..., description="The id for this diff") diff --git a/backend/infrahub/core/diff/tasks.py b/backend/infrahub/core/diff/tasks.py index c8cdc9c908..eda8137918 100644 --- a/backend/infrahub/core/diff/tasks.py +++ b/backend/infrahub/core/diff/tasks.py @@ -2,7 +2,7 @@ from infrahub.core import registry from infrahub.core.diff.coordinator import DiffCoordinator -from infrahub.core.diff.models import RequestDiffUpdate +from infrahub.core.diff.models import RequestDiffRefresh, RequestDiffUpdate from infrahub.dependencies.registry import get_component_registry from infrahub.log import get_logger from infrahub.services import services @@ -26,3 +26,15 @@ async def update_diff(model: RequestDiffUpdate) -> None: to_time=model.to_time, name=model.name, ) + + +@flow(name="diff-refresh") +async def refresh_diff(model: RequestDiffRefresh) -> None: + service = services.service + + component_registry = get_component_registry() + base_branch = await registry.get_branch(db=service.database, branch=registry.default_branch) + diff_branch = await registry.get_branch(db=service.database, branch=model.branch_name) + + diff_coordinator = await component_registry.get_component(DiffCoordinator, db=service.database, branch=diff_branch) + await diff_coordinator.recalculate(base_branch=base_branch, diff_branch=diff_branch, diff_id=model.diff_id) diff --git a/backend/infrahub/graphql/mutations/artifact_definition.py b/backend/infrahub/graphql/mutations/artifact_definition.py index ee2ef0eb69..ff4daa262c 100644 --- a/backend/infrahub/graphql/mutations/artifact_definition.py +++ b/backend/infrahub/graphql/mutations/artifact_definition.py @@ -6,10 +6,10 @@ from typing_extensions import Self from infrahub.core.schema import NodeSchema +from infrahub.git.models import RequestArtifactDefinitionGenerate from infrahub.log import get_logger +from infrahub.workflows.catalogue import REQUEST_ARTIFACT_DEFINITION_GENERATE -from ...git.models import RequestArtifactDefinitionGenerate -from ...workflows.catalogue import REQUEST_ARTIFACT_DEFINITION_GENERATE from .main import InfrahubMutationMixin, InfrahubMutationOptions if TYPE_CHECKING: diff --git a/backend/infrahub/graphql/mutations/diff.py b/backend/infrahub/graphql/mutations/diff.py index 5adf149da3..631bc3d3ca 100644 --- a/backend/infrahub/graphql/mutations/diff.py +++ b/backend/infrahub/graphql/mutations/diff.py @@ -5,10 +5,9 @@ from infrahub.core import registry from infrahub.core.diff.coordinator import DiffCoordinator +from infrahub.core.diff.models import RequestDiffUpdate from infrahub.dependencies.registry import get_component_registry - -from ...core.diff.models import RequestDiffUpdate -from ...workflows.catalogue import REQUEST_DIFF_UPDATE +from infrahub.workflows.catalogue import REQUEST_DIFF_UPDATE if TYPE_CHECKING: from ..initialization import GraphqlContext diff --git a/backend/infrahub/message_bus/messages/__init__.py b/backend/infrahub/message_bus/messages/__init__.py index 164d1df842..bcf9d23a2a 100644 --- a/backend/infrahub/message_bus/messages/__init__.py +++ b/backend/infrahub/message_bus/messages/__init__.py @@ -31,7 +31,6 @@ from .refresh_registry_rebasedbranch import RefreshRegistryRebasedBranch from .refresh_webhook_configuration import RefreshWebhookConfiguration from .request_artifactdefinition_check import RequestArtifactDefinitionCheck -from .request_diff_refresh import RequestDiffRefresh from .request_generatordefinition_check import RequestGeneratorDefinitionCheck from .request_generatordefinition_run import RequestGeneratorDefinitionRun from .request_graphqlquerygroup_update import RequestGraphQLQueryGroupUpdate @@ -75,7 +74,6 @@ "refresh.registry.rebased_branch": RefreshRegistryRebasedBranch, "refresh.webhook.configuration": RefreshWebhookConfiguration, "request.artifact_definition.check": RequestArtifactDefinitionCheck, - "request.diff.refresh": RequestDiffRefresh, "request.generator_definition.check": RequestGeneratorDefinitionCheck, "request.generator_definition.run": RequestGeneratorDefinitionRun, "request.graphql_query_group.update": RequestGraphQLQueryGroupUpdate, diff --git a/backend/infrahub/message_bus/operations/__init__.py b/backend/infrahub/message_bus/operations/__init__.py index 2a8695360c..2edf893113 100644 --- a/backend/infrahub/message_bus/operations/__init__.py +++ b/backend/infrahub/message_bus/operations/__init__.py @@ -44,7 +44,6 @@ "refresh.registry.branches": refresh.registry.branches, "refresh.registry.rebased_branch": refresh.registry.rebased_branch, "refresh.webhook.configuration": refresh.webhook.configuration, - "request.diff.refresh": requests.diff.refresh, "request.generator_definition.check": requests.generator_definition.check, "request.generator_definition.run": requests.generator_definition.run, "request.graphql_query_group.update": requests.graphql_query_group.update, diff --git a/backend/infrahub/message_bus/operations/event/branch.py b/backend/infrahub/message_bus/operations/event/branch.py index cfa133991e..976c9d70b0 100644 --- a/backend/infrahub/message_bus/operations/event/branch.py +++ b/backend/infrahub/message_bus/operations/event/branch.py @@ -4,7 +4,7 @@ from infrahub.core import registry from infrahub.core.diff.model.path import BranchTrackingId -from infrahub.core.diff.models import RequestDiffUpdate +from infrahub.core.diff.models import RequestDiffRefresh, RequestDiffUpdate from infrahub.core.diff.repository.repository import DiffRepository from infrahub.dependencies.registry import get_component_registry from infrahub.log import get_logger @@ -12,6 +12,7 @@ from infrahub.services import InfrahubServices from infrahub.workflows.catalogue import ( GIT_REPOSITORIES_CREATE_BRANCH, + REQUEST_DIFF_REFRESH, REQUEST_DIFF_UPDATE, TRIGGER_ARTIFACT_DEFINITION_GENERATE, ) @@ -100,7 +101,12 @@ async def rebased(message: messages.EventBranchRebased, service: InfrahubService for diff_root in diff_roots_to_refresh: if diff_root.base_branch_name != diff_root.diff_branch_name: - events.append(messages.RequestDiffRefresh(branch_name=diff_root.diff_branch_name, diff_id=diff_root.uuid)) + request_diff_refresh_model = RequestDiffRefresh( + branch_name=diff_root.diff_branch_name, diff_id=diff_root.uuid + ) + await service.workflow.submit_workflow( + workflow=REQUEST_DIFF_REFRESH, parameters={"model": request_diff_refresh_model} + ) for event in events: event.assign_meta(parent=message) diff --git a/backend/infrahub/message_bus/operations/requests/__init__.py b/backend/infrahub/message_bus/operations/requests/__init__.py index a8d4976093..52eb1ebe8b 100644 --- a/backend/infrahub/message_bus/operations/requests/__init__.py +++ b/backend/infrahub/message_bus/operations/requests/__init__.py @@ -1,6 +1,5 @@ from . import ( artifact_definition, - diff, generator_definition, graphql_query_group, proposed_change, @@ -9,7 +8,6 @@ __all__ = [ "artifact_definition", - "diff", "generator_definition", "graphql_query_group", "proposed_change", diff --git a/backend/infrahub/message_bus/operations/requests/diff.py b/backend/infrahub/message_bus/operations/requests/diff.py deleted file mode 100644 index 0b140b44f0..0000000000 --- a/backend/infrahub/message_bus/operations/requests/diff.py +++ /dev/null @@ -1,20 +0,0 @@ -from prefect import flow - -from infrahub.core import registry -from infrahub.core.diff.coordinator import DiffCoordinator -from infrahub.dependencies.registry import get_component_registry -from infrahub.log import get_logger -from infrahub.message_bus import messages -from infrahub.services import InfrahubServices - -log = get_logger() - - -@flow(name="diff-refresh") -async def refresh(message: messages.RequestDiffRefresh, service: InfrahubServices) -> None: - component_registry = get_component_registry() - base_branch = await registry.get_branch(db=service.database, branch=registry.default_branch) - diff_branch = await registry.get_branch(db=service.database, branch=message.branch_name) - - diff_coordinator = await component_registry.get_component(DiffCoordinator, db=service.database, branch=diff_branch) - await diff_coordinator.recalculate(base_branch=base_branch, diff_branch=diff_branch, diff_id=message.diff_id) diff --git a/backend/infrahub/workflows/catalogue.py b/backend/infrahub/workflows/catalogue.py index 0a9e840fa2..52dcb39ba3 100644 --- a/backend/infrahub/workflows/catalogue.py +++ b/backend/infrahub/workflows/catalogue.py @@ -99,6 +99,13 @@ function="update_diff", ) +REQUEST_DIFF_REFRESH = WorkflowDefinition( + name="diff-refresh", + type=WorkflowType.INTERNAL, + module="infrahub.core.diff.tasks", + function="refresh_diff", +) + GIT_REPOSITORIES_SYNC = WorkflowDefinition( name="git_repositories_sync", type=WorkflowType.INTERNAL, @@ -152,4 +159,5 @@ REQUEST_ARTIFACT_DEFINITION_GENERATE, REQUEST_GENERATOR_RUN, REQUEST_DIFF_UPDATE, + REQUEST_DIFF_REFRESH, ] diff --git a/backend/tests/unit/message_bus/operations/event/test_branch.py b/backend/tests/unit/message_bus/operations/event/test_branch.py index 1a249974bf..7644008caa 100644 --- a/backend/tests/unit/message_bus/operations/event/test_branch.py +++ b/backend/tests/unit/message_bus/operations/event/test_branch.py @@ -5,7 +5,7 @@ from infrahub.core.branch import Branch from infrahub.core.diff.model.path import BranchTrackingId, EnrichedDiffRoot -from infrahub.core.diff.models import RequestDiffUpdate +from infrahub.core.diff.models import RequestDiffRefresh, RequestDiffUpdate from infrahub.core.diff.repository.repository import DiffRepository from infrahub.core.timestamp import Timestamp from infrahub.dependencies.component.registry import ComponentDependencyRegistry @@ -13,7 +13,7 @@ from infrahub.message_bus.operations.event.branch import delete, merge, rebased from infrahub.services import InfrahubServices, services from infrahub.services.adapters.workflow.local import WorkflowLocalExecution -from infrahub.workflows.catalogue import REQUEST_DIFF_UPDATE, TRIGGER_ARTIFACT_DEFINITION_GENERATE +from infrahub.workflows.catalogue import REQUEST_DIFF_REFRESH, REQUEST_DIFF_UPDATE, TRIGGER_ARTIFACT_DEFINITION_GENERATE from tests.adapters.message_bus import BusRecorder @@ -131,7 +131,7 @@ async def test_rebased(default_branch: Branch, prefect_test_fixture): recorder = BusRecorder() database = MagicMock() - service = InfrahubServices(message_bus=recorder, database=database) + service = InfrahubServices(message_bus=recorder, database=database, workflow=WorkflowLocalExecution()) diff_roots = [ EnrichedDiffRoot( base_branch_name="main", @@ -149,14 +149,30 @@ async def test_rebased(default_branch: Branch, prefect_test_fixture): mock_get_component_registry = MagicMock(return_value=mock_component_registry) mock_component_registry.get_component.return_value = diff_repo - with patch("infrahub.message_bus.operations.event.branch.get_component_registry", new=mock_get_component_registry): + with ( + patch("infrahub.message_bus.operations.event.branch.get_component_registry", new=mock_get_component_registry), + patch( + "infrahub.services.adapters.workflow.local.WorkflowLocalExecution.submit_workflow" + ) as mock_submit_workflow, + ): await rebased(message=message, service=service) + expected_calls = [ + call( + workflow=REQUEST_DIFF_REFRESH, + parameters={"model": RequestDiffRefresh(branch_name=branch_name, diff_id=diff_roots[0].uuid)}, + ), + call( + workflow=REQUEST_DIFF_REFRESH, + parameters={"model": RequestDiffRefresh(branch_name=branch_name, diff_id=diff_roots[1].uuid)}, + ), + ] + mock_submit_workflow.assert_has_calls(expected_calls) + assert mock_submit_workflow.call_count == len(expected_calls) + mock_component_registry.get_component.assert_awaited_once_with(DiffRepository, db=database, branch=default_branch) diff_repo.get_empty_roots.assert_awaited_once_with(diff_branch_names=[branch_name]) - assert len(recorder.messages) == 3 + assert len(recorder.messages) == 1 assert isinstance(recorder.messages[0], messages.RefreshRegistryRebasedBranch) refresh_message: messages.RefreshRegistryRebasedBranch = recorder.messages[0] assert refresh_message.branch == "cr1234" - assert recorder.messages[1] == messages.RequestDiffRefresh(branch_name=branch_name, diff_id=diff_roots[0].uuid) - assert recorder.messages[2] == messages.RequestDiffRefresh(branch_name=branch_name, diff_id=diff_roots[1].uuid) diff --git a/docs/docs/reference/message-bus-events.mdx b/docs/docs/reference/message-bus-events.mdx index 800b3f2080..8033669853 100644 --- a/docs/docs/reference/message-bus-events.mdx +++ b/docs/docs/reference/message-bus-events.mdx @@ -586,26 +586,6 @@ For more detailed explanations on how to use these events within Infrahub, see t | **destination_branch** | The target branch | string | None | - -### Request Diff - - - -#### Event request.diff.refresh - - -**Description**: Request diff be recalculated from scratch. - -**Priority**: 3 - - -| Key | Description | Type | Default Value | -|-----|-------------|------|---------------| -| **meta** | Meta properties for the message | N/A | None | -| **branch_name** | The branch associated with the diff | string | None | -| **diff_id** | The id for this diff | string | None | - - ### Request Generator Definition @@ -1561,27 +1541,6 @@ For more detailed explanations on how to use these events within Infrahub, see t | **destination_branch** | The target branch | string | None | - -### Request Diff - - - -#### Event request.diff.refresh - - -**Description**: Request diff be recalculated from scratch. - -**Priority**: 3 - - - -| Key | Description | Type | Default Value | -|-----|-------------|------|---------------| -| **meta** | Meta properties for the message | N/A | None | -| **branch_name** | The branch associated with the diff | string | None | -| **diff_id** | The id for this diff | string | None | - - ### Request Generator Definition