Skip to content

Commit

Permalink
Migrate RequestDiffRefresh to prefect
Browse files Browse the repository at this point in the history
  • Loading branch information
LucasG0 committed Oct 25, 2024
1 parent e18acb7 commit 604f8e9
Show file tree
Hide file tree
Showing 12 changed files with 63 additions and 81 deletions.
7 changes: 7 additions & 0 deletions backend/infrahub/core/diff/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
14 changes: 13 additions & 1 deletion backend/infrahub/core/diff/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
4 changes: 2 additions & 2 deletions backend/infrahub/graphql/mutations/artifact_definition.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
5 changes: 2 additions & 3 deletions backend/infrahub/graphql/mutations/diff.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 0 additions & 2 deletions backend/infrahub/message_bus/messages/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand Down
1 change: 0 additions & 1 deletion backend/infrahub/message_bus/operations/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
10 changes: 8 additions & 2 deletions backend/infrahub/message_bus/operations/event/branch.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@

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
from infrahub.message_bus import InfrahubMessage, messages
from infrahub.services import InfrahubServices
from infrahub.workflows.catalogue import (
GIT_REPOSITORIES_CREATE_BRANCH,
REQUEST_DIFF_REFRESH,
REQUEST_DIFF_UPDATE,
TRIGGER_ARTIFACT_DEFINITION_GENERATE,
)
Expand Down Expand Up @@ -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)
Expand Down
2 changes: 0 additions & 2 deletions backend/infrahub/message_bus/operations/requests/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
from . import (
artifact_definition,
diff,
generator_definition,
graphql_query_group,
proposed_change,
Expand All @@ -9,7 +8,6 @@

__all__ = [
"artifact_definition",
"diff",
"generator_definition",
"graphql_query_group",
"proposed_change",
Expand Down
20 changes: 0 additions & 20 deletions backend/infrahub/message_bus/operations/requests/diff.py

This file was deleted.

8 changes: 8 additions & 0 deletions backend/infrahub/workflows/catalogue.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -152,4 +159,5 @@
REQUEST_ARTIFACT_DEFINITION_GENERATE,
REQUEST_GENERATOR_RUN,
REQUEST_DIFF_UPDATE,
REQUEST_DIFF_REFRESH,
]
30 changes: 23 additions & 7 deletions backend/tests/unit/message_bus/operations/event/test_branch.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@

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
from infrahub.message_bus import messages
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


Expand Down Expand Up @@ -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",
Expand All @@ -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)
41 changes: 0 additions & 41 deletions docs/docs/reference/message-bus-events.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -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 |
<!-- vale on -->

<!-- vale off -->
### Request Diff
<!-- vale on -->

<!-- vale off -->
#### Event request.diff.refresh
<!-- vale on -->

**Description**: Request diff be recalculated from scratch.

**Priority**: 3

<!-- vale off -->
| 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 |
<!-- vale on -->

<!-- vale off -->
### Request Generator Definition
<!-- vale on -->
Expand Down Expand Up @@ -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 |
<!-- vale on -->

<!-- vale off -->
### Request Diff
<!-- vale on -->

<!-- vale off -->
#### Event request.diff.refresh
<!-- vale on -->

**Description**: Request diff be recalculated from scratch.

**Priority**: 3


<!-- vale off -->
| 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 |
<!-- vale on -->

<!-- vale off -->
### Request Generator Definition
<!-- vale on -->
Expand Down

0 comments on commit 604f8e9

Please sign in to comment.