Skip to content

Commit 58dad87

Browse files
committed
Convert BranchDelete task and event to Prefect
1 parent 58fc689 commit 58dad87

File tree

26 files changed

+154
-281
lines changed

26 files changed

+154
-281
lines changed

backend/infrahub/core/branch/tasks.py

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,13 @@
1616
from infrahub.core.validators.models.validate_migration import SchemaValidateMigrationData
1717
from infrahub.core.validators.tasks import schema_validate_migrations
1818
from infrahub.dependencies.registry import get_component_registry
19+
from infrahub.events.branch_action import BranchDeleteEvent
1920
from infrahub.exceptions import ValidationError
2021
from infrahub.log import get_log_data
2122
from infrahub.message_bus import Meta, messages
2223
from infrahub.services import services
2324
from infrahub.worker import WORKER_IDENTITY
24-
from infrahub.workflows.catalogue import IPAM_RECONCILIATION
25+
from infrahub.workflows.catalogue import BRANCH_CANCEL_PROPOSED_CHANGES, IPAM_RECONCILIATION
2526
from infrahub.workflows.utils import add_branch_tag
2627

2728

@@ -199,3 +200,18 @@ async def merge_branch(branch: str, conflict_resolution: dict[str, bool] | None
199200
meta=Meta(initiator_id=WORKER_IDENTITY, request_id=request_id),
200201
)
201202
await service.send(message=message)
203+
204+
205+
@flow(name="branch-delete")
206+
async def delete_branch(branch: str) -> None:
207+
service = services.service
208+
209+
await add_branch_tag(branch_name=branch)
210+
211+
obj = await Branch.get_by_name(db=service.database, name=str(branch))
212+
event = BranchDeleteEvent(branch=branch, branch_id=obj.get_id(), sync_with_git=obj.sync_with_git)
213+
await obj.delete(db=service.database)
214+
215+
await service.workflow.submit_workflow(workflow=BRANCH_CANCEL_PROPOSED_CHANGES, parameters={"branch_name": branch})
216+
217+
await service.event.send(event=event)

backend/infrahub/core/node/standard.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,11 @@ class StandardNode(BaseModel):
4141
def get_type(cls) -> str:
4242
return cls.__name__
4343

44+
def get_id(self) -> str:
45+
if not self.id:
46+
raise ValueError("id isn't defined yet")
47+
return self.id
48+
4449
@staticmethod
4550
def guess_field_type(field: FieldInfo) -> Any:
4651
"""Return the type of a Pydantic model field.
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
from pydantic import Field
2+
3+
from infrahub.message_bus import InfrahubMessage
4+
from infrahub.message_bus.messages.event_branch_delete import EventBranchDelete
5+
from infrahub.message_bus.messages.refresh_registry_branches import RefreshRegistryBranches
6+
7+
from .models import InfrahubBranchEvent
8+
9+
10+
class BranchDeleteEvent(InfrahubBranchEvent):
11+
"""Event generated when a branch has been deleted"""
12+
13+
branch_id: str = Field(..., description="The ID of the mutated node")
14+
sync_with_git: bool = Field(..., description="Indicates if the branch was extended to Git")
15+
16+
def get_name(self) -> str:
17+
return f"{self.get_event_namespace()}.branch.deleted"
18+
19+
def get_resource(self) -> dict[str, str]:
20+
return {
21+
"prefect.resource.id": f"infrahub.branch.{self.branch}",
22+
"infrahub.branch.id": self.branch_id,
23+
}
24+
25+
def get_messages(self) -> list[InfrahubMessage]:
26+
events = [
27+
EventBranchDelete(
28+
branch=self.branch,
29+
branch_id=self.branch_id,
30+
sync_with_git=self.sync_with_git,
31+
meta=self.get_message_meta(),
32+
),
33+
RefreshRegistryBranches(),
34+
]
35+
return events

backend/infrahub/events/models.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ def get_name(self) -> str:
2727
def get_resource(self) -> dict[str, str]:
2828
raise NotImplementedError
2929

30-
def get_message(self) -> InfrahubMessage:
30+
def get_messages(self) -> list[InfrahubMessage]:
3131
raise NotImplementedError
3232

3333
def get_related(self) -> list[dict[str, str]]:

backend/infrahub/events/node_action.py

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
from pydantic import Field
44

55
from infrahub.core.constants import MutationAction
6+
from infrahub.message_bus import InfrahubMessage
67
from infrahub.message_bus.messages.event_node_mutated import EventNodeMutated
78

89
from .models import InfrahubBranchEvent
@@ -29,12 +30,14 @@ def get_resource(self) -> dict[str, str]:
2930
def get_payload(self) -> dict[str, Any]:
3031
return self.data
3132

32-
def get_message(self) -> EventNodeMutated:
33-
return EventNodeMutated(
34-
branch=self.branch,
35-
kind=self.kind,
36-
node_id=self.node_id,
37-
action=self.action.value,
38-
data=self.data,
39-
meta=self.get_message_meta(),
40-
)
33+
def get_messages(self) -> list[InfrahubMessage]:
34+
return [
35+
EventNodeMutated(
36+
branch=self.branch,
37+
kind=self.kind,
38+
node_id=self.node_id,
39+
action=self.action.value,
40+
data=self.data,
41+
meta=self.get_message_meta(),
42+
)
43+
]

backend/infrahub/graphql/mutations/branch.py

Lines changed: 5 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
from infrahub.log import get_log_data, get_logger
2727
from infrahub.message_bus import Meta, messages
2828
from infrahub.worker import WORKER_IDENTITY
29-
from infrahub.workflows.catalogue import BRANCH_MERGE, BRANCH_REBASE
29+
from infrahub.workflows.catalogue import BRANCH_DELETE, BRANCH_MERGE, BRANCH_REBASE
3030

3131
from ..types import BranchType
3232

@@ -137,22 +137,10 @@ class Arguments:
137137
async def mutate(cls, root: dict, info: GraphQLResolveInfo, data: BranchNameInput) -> Self:
138138
context: GraphqlContext = info.context
139139

140-
async with UserTask.from_graphql_context(title=f"Delete branch: {data['name']}", context=context):
141-
obj = await Branch.get_by_name(db=context.db, name=str(data.name))
142-
await obj.delete(db=context.db)
143-
144-
if context.service:
145-
log_data = get_log_data()
146-
request_id = log_data.get("request_id", "")
147-
message = messages.EventBranchDelete(
148-
branch=obj.name,
149-
branch_id=str(obj.id),
150-
sync_with_git=obj.sync_with_git,
151-
meta=Meta(request_id=request_id),
152-
)
153-
await context.service.send(message=message)
154-
155-
return cls(ok=True)
140+
obj = await Branch.get_by_name(db=context.db, name=str(data.name))
141+
assert context.service
142+
await context.service.workflow.execute_workflow(workflow=BRANCH_DELETE, parameters={"branch": obj.name})
143+
return cls(ok=True)
156144

157145

158146
class BranchUpdate(Mutation):

backend/infrahub/message_bus/messages/__init__.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,14 +33,12 @@
3333
from .request_generatordefinition_check import RequestGeneratorDefinitionCheck
3434
from .request_generatordefinition_run import RequestGeneratorDefinitionRun
3535
from .request_graphqlquerygroup_update import RequestGraphQLQueryGroupUpdate
36-
from .request_proposed_change_cancel import RequestProposedChangeCancel
3736
from .request_proposedchange_pipeline import RequestProposedChangePipeline
3837
from .request_repository_checks import RequestRepositoryChecks
3938
from .request_repository_userchecks import RequestRepositoryUserChecks
4039
from .schema_migration_path import SchemaMigrationPath, SchemaMigrationPathResponse
4140
from .schema_validator_path import SchemaValidatorPath, SchemaValidatorPathResponse
4241
from .send_echo_request import SendEchoRequest, SendEchoRequestResponse
43-
from .trigger_proposed_change_cancel import TriggerProposedChangeCancel
4442
from .trigger_webhook_actions import TriggerWebhookActions
4543

4644
MESSAGE_MAP: dict[str, type[InfrahubMessage]] = {
@@ -73,7 +71,6 @@
7371
"request.generator_definition.check": RequestGeneratorDefinitionCheck,
7472
"request.generator_definition.run": RequestGeneratorDefinitionRun,
7573
"request.graphql_query_group.update": RequestGraphQLQueryGroupUpdate,
76-
"request.proposed_change.cancel": RequestProposedChangeCancel,
7774
"request.proposed_change.data_integrity": RequestProposedChangeDataIntegrity,
7875
"request.proposed_change.pipeline": RequestProposedChangePipeline,
7976
"request.proposed_change.refresh_artifacts": RequestProposedChangeRefreshArtifacts,
@@ -84,7 +81,6 @@
8481
"request.repository.checks": RequestRepositoryChecks,
8582
"request.repository.user_checks": RequestRepositoryUserChecks,
8683
"send.echo.request": SendEchoRequest,
87-
"trigger.proposed_change.cancel": TriggerProposedChangeCancel,
8884
"trigger.webhook.actions": TriggerWebhookActions,
8985
}
9086

backend/infrahub/message_bus/messages/request_proposed_change_cancel.py

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

backend/infrahub/message_bus/messages/trigger_generatordefinition_run.py

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

backend/infrahub/message_bus/messages/trigger_proposed_change_cancel.py

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

0 commit comments

Comments
 (0)