1515 PermissionAction ,
1616 PermissionDecision ,
1717 RelationshipCardinality ,
18+ RelationshipHierarchyDirection ,
1819)
1920from infrahub .core .manager import NodeManager
2021from infrahub .core .query .node import NodeGetKindQuery
2324 RelationshipPeerData ,
2425)
2526from infrahub .core .relationship import Relationship
27+ from infrahub .core .schema import NodeSchema
2628from infrahub .database import retry_db_transaction
2729from infrahub .events import EventMeta , NodeMutatedEvent
2830from infrahub .events .group_action import GroupMemberAddedEvent , GroupMemberRemovedEvent
@@ -89,6 +91,10 @@ async def mutate(
8991
9092 existing_peers = await _collect_current_peers (info = info , data = data , source_node = source )
9193
94+ group_event_type = _get_group_event_type (
95+ node = source , relationship_schema = rel_schema , relationship_name = relationship_name
96+ )
97+
9298 async with graphql_context .db .start_transaction () as db :
9399 peers : list [EventNode ] = []
94100 for node_data in data .get ("nodes" ):
@@ -99,19 +105,19 @@ async def mutate(
99105 await rel .resolve (db = db )
100106 # Save it only if it does not exist
101107 if rel .get_peer_id () not in existing_peers .keys ():
102- peers .append (EventNode (id = rel .get_peer_id (), kind = rel .get_peer_kind ()))
108+ if group_event_type != GroupUpdateType .NONE :
109+ peers .append (EventNode (id = rel .get_peer_id (), kind = nodes [rel .get_peer_id ()].get_kind ()))
103110 node_changelog .create_relationship (relationship = rel )
104111 await rel .save (db = db )
105112
106113 if config .SETTINGS .broker .enable and graphql_context .background and node_changelog .has_changes :
107- group_event_type = _get_group_event_type (
108- node = source , relationship_schema = rel_schema , relationship_name = relationship_name
109- )
110114 if group_event_type == GroupUpdateType .MEMBERS :
115+ ancestors = await _collect_ancestors (info = info , node_kind = source .get_schema ().kind , node_id = source .id )
111116 group_add_event = GroupMemberAddedEvent (
112117 node_id = source .id ,
113118 kind = source .get_schema ().kind ,
114119 members = peers ,
120+ ancestors = ancestors ,
115121 meta = EventMeta (branch = graphql_context .branch , context = graphql_context .get_context ()),
116122 )
117123 graphql_context .background .add_task (graphql_context .active_service .event .send , group_add_event )
@@ -124,9 +130,11 @@ async def mutate(
124130 node_kind_map = await node_kind_query .get_node_kind_map ()
125131
126132 for node_id , node_kind in node_kind_map .items ():
133+ ancestors = await _collect_ancestors (info = info , node_kind = node_kind , node_id = node_id )
127134 group_add_event = GroupMemberAddedEvent (
128135 node_id = node_id ,
129136 kind = node_kind ,
137+ ancestors = ancestors ,
130138 members = [EventNode (id = source .get_id (), kind = source .get_kind ())],
131139 meta = EventMeta (branch = graphql_context .branch , context = graphql_context .get_context ()),
132140 )
@@ -175,6 +183,9 @@ async def mutate(
175183 )
176184
177185 existing_peers = await _collect_current_peers (info = info , data = data , source_node = source )
186+ group_event_type = _get_group_event_type (
187+ node = source , relationship_schema = rel_schema , relationship_name = relationship_name
188+ )
178189
179190 async with graphql_context .db .start_transaction () as db :
180191 peers : list [EventNode ] = []
@@ -186,19 +197,19 @@ async def mutate(
186197 # it would be more query efficient
187198 rel = Relationship (schema = rel_schema , branch = graphql_context .branch , node = source )
188199 await rel .load (db = db , data = existing_peers [node_data .get ("id" )])
189- peers .append (EventNode (id = rel .get_peer_id (), kind = rel .get_peer_kind ()))
200+ if group_event_type != GroupUpdateType .NONE :
201+ peers .append (EventNode (id = rel .get_peer_id (), kind = nodes [rel .get_peer_id ()].get_kind ()))
190202 node_changelog .delete_relationship (relationship = rel )
191203 await rel .delete (db = db )
192204
193205 if config .SETTINGS .broker .enable and graphql_context .background and node_changelog .has_changes :
194- group_event_type = _get_group_event_type (
195- node = source , relationship_schema = rel_schema , relationship_name = relationship_name
196- )
197206 if group_event_type == GroupUpdateType .MEMBERS :
207+ ancestors = await _collect_ancestors (info = info , node_kind = source .get_schema ().kind , node_id = source .id )
198208 group_remove_event = GroupMemberRemovedEvent (
199209 node_id = source .id ,
200210 kind = source .get_schema ().kind ,
201211 members = peers ,
212+ ancestors = ancestors ,
202213 meta = EventMeta (branch = graphql_context .branch , context = graphql_context .get_context ()),
203214 )
204215 graphql_context .background .add_task (graphql_context .active_service .event .send , group_remove_event )
@@ -210,6 +221,7 @@ async def mutate(
210221 node_kind_map = await node_kind_query .get_node_kind_map ()
211222
212223 for node_id , node_kind in node_kind_map .items ():
224+ ancestors = await _collect_ancestors (info = info , node_kind = node_kind , node_id = node_id )
213225 group_remove_event = GroupMemberRemovedEvent (
214226 node_id = node_id ,
215227 kind = node_kind ,
@@ -332,6 +344,24 @@ async def _validate_peer_types(
332344 )
333345
334346
347+ async def _collect_ancestors (info : GraphQLResolveInfo , node_kind : str , node_id : str ) -> list [EventNode ]:
348+ graphql_context : GraphqlContext = info .context
349+ schema = graphql_context .db .schema .get (name = node_kind , branch = graphql_context .branch )
350+
351+ if not isinstance (schema , NodeSchema ):
352+ return []
353+
354+ ancestors = await NodeManager .query_hierarchy (
355+ db = graphql_context .db ,
356+ branch = graphql_context .branch ,
357+ direction = RelationshipHierarchyDirection .ANCESTORS ,
358+ id = node_id ,
359+ node_schema = schema ,
360+ filters = {"id" : None },
361+ )
362+ return [EventNode (id = ancestor .get_id (), kind = ancestor .get_kind ()) for ancestor in ancestors .values ()]
363+
364+
335365async def _collect_current_peers (
336366 info : GraphQLResolveInfo , data : RelationshipNodesInput , source_node : Node
337367) -> dict [str , RelationshipPeerData ]:
0 commit comments