Skip to content

Commit 44add20

Browse files
committed
Add branch migrated evemt
1 parent d853705 commit 44add20

File tree

6 files changed

+82
-3
lines changed

6 files changed

+82
-3
lines changed

backend/infrahub/core/branch/tasks.py

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,13 @@
3333
from infrahub.core.validators.models.validate_migration import SchemaValidateMigrationData
3434
from infrahub.core.validators.tasks import schema_validate_migrations
3535
from infrahub.dependencies.registry import get_component_registry
36-
from infrahub.events.branch_action import BranchCreatedEvent, BranchDeletedEvent, BranchMergedEvent, BranchRebasedEvent
36+
from infrahub.events.branch_action import (
37+
BranchCreatedEvent,
38+
BranchDeletedEvent,
39+
BranchMergedEvent,
40+
BranchMigratedEvent,
41+
BranchRebasedEvent,
42+
)
3743
from infrahub.events.models import EventMeta, InfrahubEvent
3844
from infrahub.events.node_action import get_node_event
3945
from infrahub.exceptions import BranchNotFoundError, ValidationError
@@ -236,6 +242,14 @@ async def migrate_branch(branch: str, context: InfrahubContext, send_events: boo
236242
obj.graph_version = GRAPH_VERSION
237243
await obj.save(db=db)
238244

245+
if send_events:
246+
event_service = await get_event_service()
247+
await event_service.send(
248+
BranchMigratedEvent(
249+
branch_name=obj.name, branch_id=str(obj.uuid), meta=EventMeta(branch=obj, context=context)
250+
)
251+
)
252+
239253

240254
@flow(name="branch-merge", flow_run_name="Merge branch {branch} into main")
241255
async def merge_branch(branch: str, context: InfrahubContext, proposed_change_id: str | None = None) -> None:

backend/infrahub/core/constants/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ class EventType(InfrahubStringEnum):
5050
BRANCH_CREATED = f"{EVENT_NAMESPACE}.branch.created"
5151
BRANCH_DELETED = f"{EVENT_NAMESPACE}.branch.deleted"
5252
BRANCH_MERGED = f"{EVENT_NAMESPACE}.branch.merged"
53+
BRANCH_MIGRATED = f"{EVENT_NAMESPACE}.branch.migrated"
5354
BRANCH_REBASED = f"{EVENT_NAMESPACE}.branch.rebased"
5455

5556
SCHEMA_UPDATED = f"{EVENT_NAMESPACE}.schema.updated"

backend/infrahub/events/branch_action.py

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ class BranchRebasedEvent(InfrahubEvent):
109109

110110
event_name: ClassVar[str] = f"{EVENT_NAMESPACE}.branch.rebased"
111111

112-
branch_id: str = Field(..., description="The ID of the mutated node")
112+
branch_id: str = Field(..., description="The ID of the branch")
113113
branch_name: str = Field(..., description="The name of the branch")
114114

115115
def get_resource(self) -> dict[str, str]:
@@ -128,3 +128,29 @@ def get_messages(self) -> list[InfrahubMessage]:
128128
RefreshRegistryRebasedBranch(branch=self.branch_name),
129129
]
130130
return events
131+
132+
133+
class BranchMigratedEvent(InfrahubEvent):
134+
"""Event generated when a branch has been migrated"""
135+
136+
event_name: ClassVar[str] = f"{EVENT_NAMESPACE}.branch.migrated"
137+
138+
branch_id: str = Field(..., description="The ID of the branch")
139+
branch_name: str = Field(..., description="The name of the branch")
140+
141+
def get_resource(self) -> dict[str, str]:
142+
return {
143+
"prefect.resource.id": f"infrahub.branch.{self.branch_name}",
144+
"infrahub.branch.id": self.branch_id,
145+
"infrahub.branch.name": self.branch_name,
146+
}
147+
148+
def get_messages(self) -> list[InfrahubMessage]:
149+
events: list[InfrahubMessage] = [
150+
# EventBranchMigrated(
151+
# branch=self.branch,
152+
# meta=self.get_message_meta(),
153+
# ),
154+
RefreshRegistryRebasedBranch(branch=self.branch_name),
155+
]
156+
return events

backend/infrahub/task_manager/event.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,9 @@ def _return_branch_merged(self) -> dict[str, Any]:
160160
def _return_branch_rebased(self) -> dict[str, Any]:
161161
return {"rebased_branch": self._get_branch_name_from_resource()}
162162

163+
def _return_branch_migrated(self) -> dict[str, Any]:
164+
return {"migrated_branch": self._get_branch_name_from_resource()}
165+
163166
def _return_group_event(self) -> dict[str, Any]:
164167
members = []
165168
ancestors = []
@@ -228,6 +231,8 @@ def _return_event_specifics(self) -> dict[str, Any]:
228231
event_specifics = self._return_branch_deleted()
229232
case "infrahub.branch.merged":
230233
event_specifics = self._return_branch_merged()
234+
case "infrahub.branch.migrated":
235+
event_specifics = self._return_branch_migrated()
231236
case "infrahub.branch.rebased":
232237
event_specifics = self._return_branch_rebased()
233238
case "infrahub.group.member_added" | "infrahub.group.member_removed":

backend/infrahub/task_manager/models.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,13 @@ def add_event_type_filter(
141141
if branches:
142142
self.resource = EventResourceFilter(labels=ResourceSpecification({"infrahub.branch.name": branches}))
143143

144+
if branch_migrated := event_type_filter.get("branch_migrated"):
145+
branches = branch_migrated.get("branches") or []
146+
if "infrahub.branch.created" not in event_type:
147+
event_type.append("infrahub.branch.migrated")
148+
if branches:
149+
self.resource = EventResourceFilter(labels=ResourceSpecification({"infrahub.branch.name": branches}))
150+
144151
if branch_rebased := event_type_filter.get("branch_rebased"):
145152
branches = branch_rebased.get("branches") or []
146153
if "infrahub.branch.created" not in event_type:

docs/docs/reference/infrahub-events.mdx

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,32 @@ For more detailed explanations on how these events are used within Infrahub, see
164164
| **proposed_change_id** | The ID of the proposed change that merged this branch if applicable |
165165
<!-- vale on -->
166166
<!-- vale off -->
167+
### Branch Migrated Event
168+
<!-- vale on -->
169+
170+
**Type**: infrahub.branch.migrated
171+
**Description**: Event generated when a branch has been migrated
172+
<!-- vale off -->
173+
**Uses node_kind filter for webhooks**: `false`
174+
<!-- vale on -->
175+
176+
<!-- vale off -->
177+
| Key | Description |
178+
|-----|-------------|
179+
| **meta.branch** | The branch on which originate this event |
180+
| **meta.request_id** | N/A |
181+
| **meta.account_id** | The ID of the account triggering this event |
182+
| **meta.initiator_id** | The worker identity of the initial sender of this message |
183+
| **meta.context** | The context used when originating this event |
184+
| **meta.level** | N/A |
185+
| **meta.has_children** | Indicates if this event might potentially have child events under it. |
186+
| **meta.id** | UUID of the event |
187+
| **meta.parent** | The UUID of the parent event if applicable |
188+
| **meta.ancestors** | Any event used to trigger this event |
189+
| **branch_id** | The ID of the branch |
190+
| **branch_name** | The name of the branch |
191+
<!-- vale on -->
192+
<!-- vale off -->
167193
### Branch Rebased Event
168194
<!-- vale on -->
169195

@@ -186,7 +212,7 @@ For more detailed explanations on how these events are used within Infrahub, see
186212
| **meta.id** | UUID of the event |
187213
| **meta.parent** | The UUID of the parent event if applicable |
188214
| **meta.ancestors** | Any event used to trigger this event |
189-
| **branch_id** | The ID of the mutated node |
215+
| **branch_id** | The ID of the branch |
190216
| **branch_name** | The name of the branch |
191217
<!-- vale on -->
192218
## Group events

0 commit comments

Comments
 (0)