66from infrahub .core .protocols import CoreRepository
77from infrahub .core .registry import registry
88from infrahub .exceptions import RepositoryError
9+ from infrahub .message_bus import Meta , messages
910from infrahub .services import services
11+ from infrahub .worker import WORKER_IDENTITY
1012
11- from ..log import get_logger
13+ from ..log import get_log_data , get_logger
1214from ..tasks .artifact import define_artifact
1315from ..workflows .catalogue import REQUEST_ARTIFACT_DEFINITION_GENERATE , REQUEST_ARTIFACT_GENERATE
1416from ..workflows .utils import add_branch_tag
@@ -54,6 +56,17 @@ async def add_git_repository(model: GitRepositoryAdd) -> None:
5456 if model .internal_status == RepositoryInternalStatus .ACTIVE .value :
5557 await repo .sync ()
5658
59+ # Notify other workers they need to clone the repository
60+ notification = messages .RefreshGitFetch (
61+ meta = Meta (initiator_id = WORKER_IDENTITY , request_id = get_log_data ().get ("request_id" , "" )),
62+ location = model .location ,
63+ repository_id = model .repository_id ,
64+ repository_name = model .repository_name ,
65+ repository_kind = InfrahubKind .REPOSITORY ,
66+ infrahub_branch_name = model .infrahub_branch_name ,
67+ )
68+ await service .send (message = notification )
69+
5770
5871@flow (
5972 name = "git-repository-add-read-only" ,
@@ -81,6 +94,17 @@ async def add_git_repository_read_only(model: GitRepositoryAddReadOnly) -> None:
8194 if model .internal_status == RepositoryInternalStatus .ACTIVE .value :
8295 await repo .sync_from_remote ()
8396
97+ # Notify other workers they need to clone the repository
98+ notification = messages .RefreshGitFetch (
99+ meta = Meta (initiator_id = WORKER_IDENTITY , request_id = get_log_data ().get ("request_id" , "" )),
100+ location = model .location ,
101+ repository_id = model .repository_id ,
102+ repository_name = model .repository_name ,
103+ repository_kind = InfrahubKind .REPOSITORY ,
104+ infrahub_branch_name = model .infrahub_branch_name ,
105+ )
106+ await service .send (message = notification )
107+
84108
85109@flow (name = "git_repositories_create_branch" )
86110async def create_branch (branch : str , branch_id : str ) -> None :
@@ -166,6 +190,16 @@ async def sync_remote_repositories() -> None:
166190
167191 try :
168192 await repo .sync (staging_branch = staging_branch )
193+ # Tell workers to fetch to stay in sync
194+ message = messages .RefreshGitFetch (
195+ meta = Meta (initiator_id = WORKER_IDENTITY , request_id = get_log_data ().get ("request_id" , "" )),
196+ location = repository_data .repository .location .value ,
197+ repository_id = repository_data .repository .id ,
198+ repository_name = repository_data .repository .name .value ,
199+ repository_kind = repository_data .repository .get_kind (),
200+ infrahub_branch_name = infrahub_branch ,
201+ )
202+ await service .send (message = message )
169203 except RepositoryError as exc :
170204 error = exc
171205
@@ -178,9 +212,22 @@ async def sync_remote_repositories() -> None:
178212async def git_branch_create (
179213 client : InfrahubClient , branch : str , branch_id : str , repository_id : str , repository_name : str
180214) -> None :
215+ service = services .service
216+
181217 repo = await InfrahubRepository .init (id = repository_id , name = repository_name , client = client )
182218 async with lock .registry .get (name = repository_name , namespace = "repository" ):
183219 await repo .create_branch_in_git (branch_name = branch , branch_id = branch_id )
220+ if repo .location :
221+ # New branch has been pushed remotely, tell workers to fetch it
222+ message = messages .RefreshGitFetch (
223+ meta = Meta (initiator_id = WORKER_IDENTITY , request_id = get_log_data ().get ("request_id" , "" )),
224+ location = repo .location ,
225+ repository_id = str (repo .id ),
226+ repository_name = repo .name ,
227+ repository_kind = InfrahubKind .REPOSITORY ,
228+ infrahub_branch_name = branch ,
229+ )
230+ await service .send (message = message )
184231
185232
186233@flow (name = "artifact-definition-generate" )
@@ -340,6 +387,17 @@ async def pull_read_only(model: GitRepositoryPullReadOnly) -> None:
340387 await repo .import_objects_from_files (infrahub_branch_name = model .infrahub_branch_name , commit = model .commit )
341388 await repo .sync_from_remote (commit = model .commit )
342389
390+ # Tell workers to fetch to stay in sync
391+ message = messages .RefreshGitFetch (
392+ meta = Meta (initiator_id = WORKER_IDENTITY , request_id = get_log_data ().get ("request_id" , "" )),
393+ location = model .location ,
394+ repository_id = model .repository_id ,
395+ repository_name = model .repository_name ,
396+ repository_kind = InfrahubKind .READONLYREPOSITORY ,
397+ infrahub_branch_name = model .infrahub_branch_name ,
398+ )
399+ await service .send (message = message )
400+
343401
344402@flow (name = "git-repository-merge" )
345403async def merge_git_repository (model : GitRepositoryMerge ) -> None :
@@ -371,7 +429,17 @@ async def merge_git_repository(model: GitRepositoryMerge) -> None:
371429 repo_main .commit .value = commit
372430
373431 await repo_main .save ()
374-
375432 else :
376433 async with lock .registry .get (name = model .repository_name , namespace = "repository" ):
377434 await repo .merge (source_branch = model .source_branch , dest_branch = model .destination_branch )
435+ if repo .location :
436+ # Destination branch has changed and pushed remotely, tell workers to re-fetch
437+ message = messages .RefreshGitFetch (
438+ meta = Meta (initiator_id = WORKER_IDENTITY , request_id = get_log_data ().get ("request_id" , "" )),
439+ location = repo .location ,
440+ repository_id = str (repo .id ),
441+ repository_name = repo .name ,
442+ repository_kind = InfrahubKind .REPOSITORY ,
443+ infrahub_branch_name = model .destination_branch ,
444+ )
445+ await service .send (message = message )
0 commit comments