Skip to content

Commit f2571de

Browse files
authored
Merge pull request #4806 from opsmill/lgu-migrate-git-repository-merge
Migrate GitRepositoryMerge to prefect
2 parents 6b81d0a + e985f95 commit f2571de

File tree

11 files changed

+89
-112
lines changed

11 files changed

+89
-112
lines changed

backend/infrahub/core/merge.py

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,9 @@
1010
from infrahub.core.schema import GenericSchema, NodeSchema
1111
from infrahub.core.timestamp import Timestamp
1212
from infrahub.exceptions import ValidationError
13-
from infrahub.message_bus import messages
1413

14+
from ..git.models import GitRepositoryMerge
15+
from ..workflows.catalogue import GIT_REPOSITORIES_MERGE
1516
from .diff.branch_differ import BranchDiffer
1617

1718
if TYPE_CHECKING:
@@ -261,7 +262,6 @@ async def merge_repositories(self) -> None:
261262
repos_in_main = {repo.id: repo for repo in repos_in_main_list}
262263

263264
repos_in_branch_list = await NodeManager.query(schema=CoreRepository, db=self.db, branch=self.source_branch)
264-
events = []
265265
for repo in repos_in_branch_list:
266266
# Check if the repo, exist in main, if not ignore this repo
267267
if repo.id not in repos_in_main:
@@ -271,16 +271,14 @@ async def merge_repositories(self) -> None:
271271
continue
272272

273273
if self.source_branch.sync_with_git or repo.internal_status.value == RepositoryInternalStatus.STAGING.value:
274-
events.append(
275-
messages.GitRepositoryMerge(
276-
repository_id=repo.id,
277-
repository_name=repo.name.value,
278-
internal_status=repo.internal_status.value,
279-
source_branch=self.source_branch.name,
280-
destination_branch=registry.default_branch,
281-
default_branch=repo.default_branch.value,
282-
)
274+
model = GitRepositoryMerge(
275+
repository_id=repo.id,
276+
repository_name=repo.name.value,
277+
internal_status=repo.internal_status.value,
278+
source_branch=self.source_branch.name,
279+
destination_branch=registry.default_branch,
280+
default_branch=repo.default_branch.value,
281+
)
282+
await self.service.workflow.submit_workflow(
283+
workflow=GIT_REPOSITORIES_MERGE, parameters={"model": model}
283284
)
284-
285-
for event in events:
286-
await self.service.send(message=event)

backend/infrahub/git/models.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,3 +44,14 @@ class GitRepositoryPullReadOnly(BaseModel):
4444
ref: Optional[str] = Field(None, description="Ref to track on the external repository")
4545
commit: Optional[str] = Field(None, description="Specific commit to pull")
4646
infrahub_branch_name: str = Field(..., description="Infrahub branch on which to sync the remote repository")
47+
48+
49+
class GitRepositoryMerge(BaseModel):
50+
"""Merge one branch into another."""
51+
52+
repository_id: str = Field(..., description="The unique ID of the Repository")
53+
repository_name: str = Field(..., description="The name of the repository")
54+
internal_status: str = Field(..., description="Administrative status of the repository")
55+
source_branch: str = Field(..., description="The source branch")
56+
destination_branch: str = Field(..., description="The source branch")
57+
default_branch: str = Field(..., description="The default branch in Git")

backend/infrahub/git/tasks.py

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,12 @@
1212
from ..tasks.artifact import define_artifact
1313
from ..workflows.catalogue import REQUEST_ARTIFACT_DEFINITION_GENERATE, REQUEST_ARTIFACT_GENERATE
1414
from ..workflows.utils import add_branch_tag
15-
from .models import GitRepositoryPullReadOnly, RequestArtifactDefinitionGenerate, RequestArtifactGenerate
15+
from .models import (
16+
GitRepositoryMerge,
17+
GitRepositoryPullReadOnly,
18+
RequestArtifactDefinitionGenerate,
19+
RequestArtifactGenerate,
20+
)
1621
from .repository import InfrahubReadOnlyRepository, InfrahubRepository, get_initialized_repo
1722

1823
log = get_logger()
@@ -275,3 +280,39 @@ async def pull_read_only(model: GitRepositoryPullReadOnly) -> None:
275280

276281
await repo.import_objects_from_files(infrahub_branch_name=model.infrahub_branch_name, commit=model.commit)
277282
await repo.sync_from_remote(commit=model.commit)
283+
284+
285+
@flow(name="git-repository-merge")
286+
async def merge_git_repository(model: GitRepositoryMerge) -> None:
287+
service = services.service
288+
log.info(
289+
"Merging repository branch",
290+
repository_name=model.repository_name,
291+
repository_id=model.repository_id,
292+
source_branch=model.source_branch,
293+
destination_branch=model.destination_branch,
294+
)
295+
296+
repo = await InfrahubRepository.init(
297+
id=model.repository_id,
298+
name=model.repository_name,
299+
client=service.client,
300+
default_branch_name=model.default_branch,
301+
)
302+
303+
if model.internal_status == RepositoryInternalStatus.STAGING.value:
304+
repo_source = await service.client.get(
305+
kind=InfrahubKind.GENERICREPOSITORY, id=model.repository_id, branch=model.source_branch
306+
)
307+
repo_main = await service.client.get(kind=InfrahubKind.GENERICREPOSITORY, id=model.repository_id)
308+
repo_main.internal_status.value = RepositoryInternalStatus.ACTIVE.value
309+
repo_main.sync_status.value = repo_source.sync_status.value
310+
311+
commit = repo.get_commit_value(branch_name=repo.default_branch, remote=False)
312+
repo_main.commit.value = commit
313+
314+
await repo_main.save()
315+
316+
else:
317+
async with lock.registry.get(name=model.repository_name, namespace="repository"):
318+
await repo.merge(source_branch=model.source_branch, dest_branch=model.destination_branch)

backend/infrahub/message_bus/messages/__init__.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
from .git_repository_add import GitRepositoryAdd
1919
from .git_repository_connectivity import GitRepositoryConnectivity
2020
from .git_repository_importobjects import GitRepositoryImportObjects
21-
from .git_repository_merge import GitRepositoryMerge
2221
from .git_repository_read_only_add import GitRepositoryAddReadOnly
2322
from .proposed_change.request_proposedchange_dataintegrity import RequestProposedChangeDataIntegrity
2423
from .proposed_change.request_proposedchange_refreshartifacts import RequestProposedChangeRefreshArtifacts
@@ -59,7 +58,6 @@
5958
"git.file.get": GitFileGet,
6059
"git.repository.add": GitRepositoryAdd,
6160
"git.repository.connectivity": GitRepositoryConnectivity,
62-
"git.repository.merge": GitRepositoryMerge,
6361
"git.repository.add_read_only": GitRepositoryAddReadOnly,
6462
"git.repository.import_objects": GitRepositoryImportObjects,
6563
"schema.migration.path": SchemaMigrationPath,

backend/infrahub/message_bus/messages/git_repository_merge.py

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

backend/infrahub/message_bus/operations/__init__.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@
3838
"git.repository.add_read_only": git.repository.add_read_only,
3939
"git.repository.connectivity": git.repository.connectivity,
4040
"git.repository.import_objects": git.repository.import_objects,
41-
"git.repository.merge": git.repository.merge,
4241
"refresh.registry.branches": refresh.registry.branches,
4342
"refresh.registry.rebased_branch": refresh.registry.rebased_branch,
4443
"refresh.webhook.configuration": refresh.webhook.configuration,

backend/infrahub/message_bus/operations/git/repository.py

Lines changed: 1 addition & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from prefect import flow
22

33
from infrahub import lock
4-
from infrahub.core.constants import InfrahubKind, RepositoryInternalStatus
4+
from infrahub.core.constants import RepositoryInternalStatus
55
from infrahub.exceptions import RepositoryError
66
from infrahub.git.repository import InfrahubReadOnlyRepository, InfrahubRepository, get_initialized_repo
77
from infrahub.log import get_logger
@@ -102,38 +102,3 @@ async def import_objects(message: messages.GitRepositoryImportObjects, service:
102102
)
103103
repo.task_report = git_report
104104
await repo.import_objects_from_files(infrahub_branch_name=message.infrahub_branch_name, commit=message.commit)
105-
106-
107-
@flow(name="git-repository-merge")
108-
async def merge(message: messages.GitRepositoryMerge, service: InfrahubServices) -> None:
109-
log.info(
110-
"Merging repository branch",
111-
repository_name=message.repository_name,
112-
repository_id=message.repository_id,
113-
source_branch=message.source_branch,
114-
destination_branch=message.destination_branch,
115-
)
116-
117-
repo = await InfrahubRepository.init(
118-
id=message.repository_id,
119-
name=message.repository_name,
120-
client=service.client,
121-
default_branch_name=message.default_branch,
122-
)
123-
124-
if message.internal_status == RepositoryInternalStatus.STAGING.value:
125-
repo_source = await service.client.get(
126-
kind=InfrahubKind.GENERICREPOSITORY, id=message.repository_id, branch=message.source_branch
127-
)
128-
repo_main = await service.client.get(kind=InfrahubKind.GENERICREPOSITORY, id=message.repository_id)
129-
repo_main.internal_status.value = RepositoryInternalStatus.ACTIVE.value
130-
repo_main.sync_status.value = repo_source.sync_status.value
131-
132-
commit = repo.get_commit_value(branch_name=repo.default_branch, remote=False)
133-
repo_main.commit.value = commit
134-
135-
await repo_main.save()
136-
137-
else:
138-
async with lock.registry.get(name=message.repository_name, namespace="repository"):
139-
await repo.merge(source_branch=message.source_branch, dest_branch=message.destination_branch)

backend/infrahub/workflows/catalogue.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,13 @@
135135
type=WorkflowType.INTERNAL,
136136
module="infrahub.git.tasks",
137137
function="pull_read_only",
138+
)
139+
140+
GIT_REPOSITORIES_MERGE = WorkflowDefinition(
141+
name="git-repository-merge",
142+
type=WorkflowType.INTERNAL,
143+
module="infrahub.git.tasks",
144+
function="merge_git_repository",
138145
branch_support=BranchSupportType.AWARE,
139146
tags=[WorkflowTag.DATABASE_CHANGE],
140147
)
@@ -194,6 +201,7 @@
194201
REQUEST_DIFF_UPDATE,
195202
REQUEST_DIFF_REFRESH,
196203
GIT_REPOSITORIES_PULL_READ_ONLY,
204+
GIT_REPOSITORIES_MERGE,
197205
TRIGGER_GENERATOR_DEFINITION_RUN,
198206
BRANCH_CANCEL_PROPOSED_CHANGES,
199207
]

backend/tests/unit/git/test_git_rpc.py

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,15 @@
1010
from infrahub.core.constants import InfrahubKind, RepositoryInternalStatus
1111
from infrahub.exceptions import RepositoryError
1212
from infrahub.git import InfrahubRepository
13-
from infrahub.git.models import GitRepositoryPullReadOnly
13+
from infrahub.git.models import GitRepositoryMerge, GitRepositoryPullReadOnly
1414
from infrahub.git.repository import InfrahubReadOnlyRepository
1515
from infrahub.git.tasks import pull_read_only
1616
from infrahub.lock import InfrahubLockRegistry
1717
from infrahub.message_bus import Meta, messages
1818
from infrahub.message_bus.operations import git
1919
from infrahub.services import InfrahubServices, services
2020
from infrahub.services.adapters.workflow.local import WorkflowLocalExecution
21+
from infrahub.workflows.catalogue import GIT_REPOSITORIES_MERGE
2122
from tests.helpers.test_client import dummy_async_request
2223

2324
# pylint: disable=redefined-outer-name
@@ -116,7 +117,7 @@ async def test_git_rpc_merge(
116117

117118
commit_main_before = repo.get_commit_value(branch_name="main")
118119

119-
message = messages.GitRepositoryMerge(
120+
model = GitRepositoryMerge(
120121
repository_id=str(UUIDT()),
121122
repository_name=repo.name,
122123
source_branch="branch01",
@@ -127,9 +128,17 @@ async def test_git_rpc_merge(
127128

128129
client_config = Config(requester=dummy_async_request)
129130
bus_simulator = helper.get_message_bus_simulator()
130-
service = InfrahubServices(client=InfrahubClient(config=client_config), message_bus=bus_simulator)
131+
service = InfrahubServices(
132+
client=InfrahubClient(config=client_config), message_bus=bus_simulator, workflow=WorkflowLocalExecution()
133+
)
131134
bus_simulator.service = service
132-
await service.send(message=message)
135+
136+
original_services = services.service
137+
services.service = service
138+
await service.workflow.submit_workflow(workflow=GIT_REPOSITORIES_MERGE, parameters={"model": model})
139+
140+
# Restore original services to not impact other tests. This global variable might/should be redesigned at some point.
141+
service.service = original_services
133142

134143
commit_main_after = repo.get_commit_value(branch_name="main")
135144

backend/tests/unit/git/test_git_transform.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,15 @@
66
from infrahub.exceptions import RepositoryFileNotFoundError, TransformError
77
from infrahub.git import InfrahubRepository
88
from infrahub.services import InfrahubServices, services
9+
from infrahub.services.adapters.workflow.local import WorkflowLocalExecution
910
from infrahub.transformations.models import TransformJinjaTemplateData, TransformPythonData
1011
from infrahub.transformations.tasks import transform_python, transform_render_jinja2_template
1112

1213

1314
@pytest.fixture
1415
def init_service():
1516
original = services.service
16-
service = InfrahubServices(client=InfrahubClient())
17+
service = InfrahubServices(client=InfrahubClient(), workflow=WorkflowLocalExecution())
1718
services.service = service
1819
yield service
1920
services.service = original

0 commit comments

Comments
 (0)