1111from infrahub .context import InfrahubContext # noqa: TC001 needed for prefect flow
1212from infrahub .core import registry
1313from infrahub .core .branch import Branch
14+ from infrahub .core .changelog .diff import DiffChangelogCollector
15+ from infrahub .core .constants import MutationAction
1416from infrahub .core .diff .coordinator import DiffCoordinator
1517from infrahub .core .diff .ipam_diff_parser import IpamDiffParser
1618from infrahub .core .diff .merger .merger import DiffMerger
2325from infrahub .core .validators .models .validate_migration import SchemaValidateMigrationData
2426from infrahub .core .validators .tasks import schema_validate_migrations
2527from infrahub .dependencies .registry import get_component_registry
26- from infrahub .events .branch_action import BranchCreatedEvent , BranchDeletedEvent , BranchRebasedEvent
27- from infrahub .events .models import EventMeta
28+ from infrahub .events .branch_action import BranchCreatedEvent , BranchDeletedEvent , BranchMergedEvent , BranchRebasedEvent
29+ from infrahub .events .models import EventMeta , InfrahubEvent
30+ from infrahub .events .node_action import NodeMutatedEvent
2831from infrahub .exceptions import BranchNotFoundError , MergeFailedError , ValidationError
2932from infrahub .graphql .mutations .models import BranchCreateModel # noqa: TC001
3033from infrahub .log import get_log_data
@@ -169,7 +172,9 @@ async def merge_branch(branch: str, context: InfrahubContext, service: InfrahubS
169172 await add_tags (branches = [branch , registry .default_branch ])
170173
171174 obj = await Branch .get_by_name (db = db , name = branch )
175+ default_branch = await registry .get_branch (db = db , branch = registry .default_branch )
172176 component_registry = get_component_registry ()
177+ merge_event = BranchMergedEvent (meta = EventMeta .from_context (context = context , branch = obj ))
173178
174179 merger : BranchMerger | None = None
175180 async with lock .registry .global_graph_lock ():
@@ -187,13 +192,15 @@ async def merge_branch(branch: str, context: InfrahubContext, service: InfrahubS
187192 service = service ,
188193 )
189194 try :
190- await merger .merge ()
195+ branch_diff = await merger .merge ()
191196 except Exception as exc :
192197 log .exception ("Merge failed, beginning rollback" )
193198 await merger .rollback ()
194199 raise MergeFailedError (branch_name = branch ) from exc
195200 await merger .update_schema ()
196201
202+ changelog_collector = DiffChangelogCollector (diff = branch_diff , branch = obj , db = db )
203+ node_events = changelog_collector .collect_changelogs ()
197204 if merger and merger .migrations :
198205 errors = await schema_apply_migrations (
199206 message = SchemaApplyMigrationData (
@@ -242,6 +249,24 @@ async def merge_branch(branch: str, context: InfrahubContext, service: InfrahubS
242249 )
243250 await service .message_bus .send (message = message )
244251
252+ events : list [InfrahubEvent ] = [merge_event ]
253+
254+ for action , node_changelog in node_events :
255+ meta = EventMeta .from_parent (parent = merge_event )
256+ mutate_event = NodeMutatedEvent (
257+ kind = node_changelog .node_kind ,
258+ node_id = node_changelog .node_id ,
259+ data = node_changelog ,
260+ action = MutationAction .from_diff_action (diff_action = action ),
261+ fields = node_changelog .updated_fields ,
262+ meta = meta ,
263+ )
264+ mutate_event .set_context_branch (branch = default_branch )
265+ events .append (mutate_event )
266+
267+ for event in events :
268+ await service .event .send (event = event )
269+
245270
246271@flow (name = "branch-delete" , flow_run_name = "Delete branch {branch}" )
247272async def delete_branch (branch : str , context : InfrahubContext , service : InfrahubServices ) -> None :
0 commit comments