Skip to content

Commit d8abe02

Browse files
authored
Merge pull request #4884 from opsmill/lgu-migrate-git-respo-add-read-only
Migrate GitRepositoryAddReadOnly to prefect
2 parents 8895880 + fa823de commit d8abe02

File tree

11 files changed

+83
-106
lines changed

11 files changed

+83
-106
lines changed

backend/infrahub/git/models.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,18 @@ class GitRepositoryAdd(BaseModel):
4747
internal_status: str = Field(..., description="Administrative status of the repository")
4848

4949

50+
class GitRepositoryAddReadOnly(BaseModel):
51+
"""Clone and sync an external repository after creation."""
52+
53+
location: str = Field(..., description="The external URL of the repository")
54+
repository_id: str = Field(..., description="The unique ID of the Repository")
55+
repository_name: str = Field(..., description="The name of the repository")
56+
ref: str = Field(..., description="Ref to track on the external repository")
57+
created_by: Optional[str] = Field(default=None, description="The user ID of the user that created the repository")
58+
infrahub_branch_name: str = Field(..., description="Infrahub branch on which to sync the remote repository")
59+
internal_status: str = Field(..., description="Internal status of the repository")
60+
61+
5062
class GitRepositoryPullReadOnly(BaseModel):
5163
"""Update a read-only repository to the latest commit for its ref"""
5264

backend/infrahub/git/tasks.py

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
from ..workflows.utils import add_branch_tag
1515
from .models import (
1616
GitRepositoryAdd,
17+
GitRepositoryAddReadOnly,
1718
GitRepositoryMerge,
1819
GitRepositoryPullReadOnly,
1920
RequestArtifactDefinitionGenerate,
@@ -24,9 +25,13 @@
2425
log = get_logger()
2526

2627

27-
@flow(name="git-repository-add-read-write")
28+
@flow(
29+
name="git-repository-add-read-write",
30+
flow_run_name="Adding repository {model.repository_name} in branch {model.infrahub_branch_name}",
31+
)
2832
async def add_git_repository(model: GitRepositoryAdd) -> None:
2933
service = services.service
34+
await add_branch_tag(model.infrahub_branch_name)
3035
async with service.git_report(
3136
related_node=model.repository_id,
3237
title=f"Initial import of the repository in branch: {model.infrahub_branch_name}",
@@ -50,6 +55,33 @@ async def add_git_repository(model: GitRepositoryAdd) -> None:
5055
await repo.sync()
5156

5257

58+
@flow(
59+
name="git-repository-add-read-only",
60+
flow_run_name="Adding read only repository {model.repository_name} in branch {model.infrahub_branch_name}",
61+
)
62+
async def add_git_repository_read_only(model: GitRepositoryAddReadOnly) -> None:
63+
service = services.service
64+
await add_branch_tag(model.infrahub_branch_name)
65+
async with service.git_report(
66+
related_node=model.repository_id,
67+
title="Adding Repository",
68+
created_by=model.created_by,
69+
) as git_report:
70+
async with lock.registry.get(name=model.repository_name, namespace="repository"):
71+
repo = await InfrahubReadOnlyRepository.new(
72+
id=model.repository_id,
73+
name=model.repository_name,
74+
location=model.location,
75+
client=service.client,
76+
ref=model.ref,
77+
infrahub_branch_name=model.infrahub_branch_name,
78+
task_report=git_report,
79+
)
80+
await repo.import_objects_from_files(infrahub_branch_name=model.infrahub_branch_name)
81+
if model.internal_status == RepositoryInternalStatus.ACTIVE.value:
82+
await repo.sync_from_remote()
83+
84+
5385
@flow(name="git_repositories_create_branch")
5486
async def create_branch(branch: str, branch_id: str) -> None:
5587
"""Request to the creation of git branches in available repositories."""

backend/infrahub/graphql/mutations/repository.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,16 @@
1010
from infrahub.core.protocols import CoreGenericRepository, CoreReadOnlyRepository, CoreRepository
1111
from infrahub.core.schema import NodeSchema
1212
from infrahub.exceptions import ValidationError
13-
from infrahub.git.models import GitRepositoryAdd, GitRepositoryPullReadOnly
13+
from infrahub.git.models import GitRepositoryAdd, GitRepositoryAddReadOnly, GitRepositoryPullReadOnly
1414
from infrahub.graphql.types.common import IdentifierInput
1515
from infrahub.log import get_logger
1616
from infrahub.message_bus import messages
1717
from infrahub.message_bus.messages.git_repository_connectivity import GitRepositoryConnectivityResponse
18-
from infrahub.workflows.catalogue import GIT_REPOSITORIES_PULL_READ_ONLY, GIT_REPOSITORY_ADD
18+
from infrahub.workflows.catalogue import (
19+
GIT_REPOSITORIES_PULL_READ_ONLY,
20+
GIT_REPOSITORY_ADD,
21+
GIT_REPOSITORY_ADD_READ_ONLY,
22+
)
1923

2024
from .main import InfrahubMutationMixin, InfrahubMutationOptions
2125

@@ -90,7 +94,7 @@ async def mutate_create(
9094
authenticated_user = context.account_session.account_id
9195
if obj.get_kind() == InfrahubKind.READONLYREPOSITORY:
9296
obj = cast(CoreReadOnlyRepository, obj)
93-
message = messages.GitRepositoryAddReadOnly(
97+
model = GitRepositoryAddReadOnly(
9498
repository_id=obj.id,
9599
repository_name=obj.name.value,
96100
location=obj.location.value,
@@ -100,7 +104,9 @@ async def mutate_create(
100104
created_by=authenticated_user,
101105
)
102106
if context.service:
103-
await context.service.send(message=message)
107+
await context.service.workflow.submit_workflow(
108+
workflow=GIT_REPOSITORY_ADD_READ_ONLY, parameters={"model": model}
109+
)
104110

105111
else:
106112
obj = cast(CoreRepository, obj)

backend/infrahub/message_bus/messages/__init__.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
from .git_file_get import GitFileGet, GitFileGetResponse
1818
from .git_repository_connectivity import GitRepositoryConnectivity
1919
from .git_repository_importobjects import GitRepositoryImportObjects
20-
from .git_repository_read_only_add import GitRepositoryAddReadOnly
2120
from .proposed_change.request_proposedchange_dataintegrity import RequestProposedChangeDataIntegrity
2221
from .proposed_change.request_proposedchange_refreshartifacts import RequestProposedChangeRefreshArtifacts
2322
from .proposed_change.request_proposedchange_repositorychecks import RequestProposedChangeRepositoryChecks
@@ -54,7 +53,6 @@
5453
"git.diff.names_only": GitDiffNamesOnly,
5554
"git.file.get": GitFileGet,
5655
"git.repository.connectivity": GitRepositoryConnectivity,
57-
"git.repository.add_read_only": GitRepositoryAddReadOnly,
5856
"git.repository.import_objects": GitRepositoryImportObjects,
5957
"schema.migration.path": SchemaMigrationPath,
6058
"schema.validator.path": SchemaValidatorPath,

backend/infrahub/message_bus/messages/git_repository_read_only_add.py

Lines changed: 0 additions & 17 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
@@ -34,7 +34,6 @@
3434
"finalize.validator.execution": finalize.validator.execution,
3535
"git.diff.names_only": git.diff.names_only,
3636
"git.file.get": git.file.get,
37-
"git.repository.add_read_only": git.repository.add_read_only,
3837
"git.repository.connectivity": git.repository.connectivity,
3938
"git.repository.import_objects": git.repository.import_objects,
4039
"refresh.registry.branches": refresh.registry.branches,

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

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

3-
from infrahub import lock
4-
from infrahub.core.constants import RepositoryInternalStatus
53
from infrahub.exceptions import RepositoryError
6-
from infrahub.git.repository import InfrahubReadOnlyRepository, InfrahubRepository, get_initialized_repo
4+
from infrahub.git.repository import InfrahubRepository, get_initialized_repo
75
from infrahub.log import get_logger
86
from infrahub.message_bus import messages
97
from infrahub.message_bus.messages.git_repository_connectivity import (
@@ -15,31 +13,6 @@
1513
log = get_logger()
1614

1715

18-
@flow(name="git-repository-add-read-only")
19-
async def add_read_only(message: messages.GitRepositoryAddReadOnly, service: InfrahubServices) -> None:
20-
log.info(
21-
"Cloning and importing read-only repository", repository=message.repository_name, location=message.location
22-
)
23-
async with service.git_report(
24-
related_node=message.repository_id,
25-
title="Adding Repository",
26-
created_by=message.created_by,
27-
) as git_report:
28-
async with lock.registry.get(name=message.repository_name, namespace="repository"):
29-
repo = await InfrahubReadOnlyRepository.new(
30-
id=message.repository_id,
31-
name=message.repository_name,
32-
location=message.location,
33-
client=service.client,
34-
ref=message.ref,
35-
infrahub_branch_name=message.infrahub_branch_name,
36-
task_report=git_report,
37-
)
38-
await repo.import_objects_from_files(infrahub_branch_name=message.infrahub_branch_name)
39-
if message.internal_status == RepositoryInternalStatus.ACTIVE.value:
40-
await repo.sync_from_remote()
41-
42-
4316
@flow(name="git-repository-check-connectivity")
4417
async def connectivity(message: messages.GitRepositoryConnectivity, service: InfrahubServices) -> None:
4518
response_data = GitRepositoryConnectivityResponseData(message="Successfully accessed repository", success=True)

backend/infrahub/workflows/catalogue.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,15 @@
147147
tags=[WorkflowTag.DATABASE_CHANGE],
148148
)
149149

150+
GIT_REPOSITORY_ADD_READ_ONLY = WorkflowDefinition(
151+
name="git-repository-add-read-only",
152+
type=WorkflowType.INTERNAL,
153+
module="infrahub.git.tasks",
154+
function="add_git_repository_read_only",
155+
branch_support=BranchSupportType.AWARE,
156+
tags=[WorkflowTag.DATABASE_CHANGE],
157+
)
158+
150159
GIT_REPOSITORIES_PULL_READ_ONLY = WorkflowDefinition(
151160
name="git-repository-pull-read-only",
152161
type=WorkflowType.INTERNAL,
@@ -263,6 +272,7 @@
263272
REQUEST_GENERATOR_DEFINITION_RUN,
264273
UPDATE_GRAPHQL_QUERY_GROUP,
265274
GIT_REPOSITORY_ADD,
275+
GIT_REPOSITORY_ADD_READ_ONLY,
266276
PROCESS_COMPUTED_MACRO,
267277
COMPUTED_ATTRIBUTE_SETUP,
268278
UPDATE_COMPUTED_ATTRIBUTE_TRANSFORM,

backend/tests/integration/git/test_readonly_repository.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929

3030
class TestCreateReadOnlyRepository(TestInfrahubApp):
3131
def setup_method(self):
32-
lock_patcher = patch("infrahub.message_bus.operations.git.repository.lock")
32+
lock_patcher = patch("infrahub.git.tasks.lock")
3333
self.mock_infra_lock = lock_patcher.start()
3434
self.mock_infra_lock.registry = AsyncMock(spec=InfrahubLockRegistry)
3535

backend/tests/unit/git/test_git_rpc.py

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,16 @@
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 GitRepositoryAdd, GitRepositoryMerge, GitRepositoryPullReadOnly
13+
from infrahub.git.models import (
14+
GitRepositoryAdd,
15+
GitRepositoryAddReadOnly,
16+
GitRepositoryMerge,
17+
GitRepositoryPullReadOnly,
18+
)
1419
from infrahub.git.repository import InfrahubReadOnlyRepository
15-
from infrahub.git.tasks import add_git_repository, pull_read_only
20+
from infrahub.git.tasks import add_git_repository, add_git_repository_read_only, pull_read_only
1621
from infrahub.lock import InfrahubLockRegistry
1722
from infrahub.message_bus import Meta, messages
18-
from infrahub.message_bus.operations import git
1923
from infrahub.services import InfrahubServices, services
2024
from infrahub.services.adapters.workflow.local import WorkflowLocalExecution
2125
from infrahub.workflows.catalogue import GIT_REPOSITORIES_MERGE
@@ -197,25 +201,26 @@ class TestAddReadOnly:
197201
def setup_method(self):
198202
self.client = AsyncMock(spec=InfrahubClient)
199203
self.git_report = AsyncContextManagerMock()
200-
self.services = InfrahubServices(client=self.client)
201-
self.services.git_report = self.git_report
202204

203-
lock_patcher = patch("infrahub.message_bus.operations.git.repository.lock")
205+
self.original_services = services.service
206+
services.service = InfrahubServices(client=self.client)
207+
services.service.git_report = self.git_report
208+
209+
lock_patcher = patch("infrahub.git.tasks.lock")
204210
self.mock_infra_lock = lock_patcher.start()
205211
self.mock_infra_lock.registry = AsyncMock(spec=InfrahubLockRegistry)
206-
repo_class_patcher = patch(
207-
"infrahub.message_bus.operations.git.repository.InfrahubReadOnlyRepository", spec=InfrahubReadOnlyRepository
208-
)
212+
repo_class_patcher = patch("infrahub.git.tasks.InfrahubReadOnlyRepository", spec=InfrahubReadOnlyRepository)
209213
self.mock_repo_class = repo_class_patcher.start()
210214
self.mock_repo = AsyncMock(spec=InfrahubReadOnlyRepository)
211215
self.mock_repo_class.new.return_value = self.mock_repo
212216

213217
def teardown_method(self):
214218
patch.stopall()
219+
services.service = self.original_services
215220

216221
async def test_git_rpc_add_read_only_success(self, git_upstream_repo_01: dict[str, str]):
217222
repo_id = str(UUIDT())
218-
message = messages.GitRepositoryAddReadOnly(
223+
model = GitRepositoryAddReadOnly(
219224
repository_id=repo_id,
220225
repository_name=git_upstream_repo_01["name"],
221226
location=git_upstream_repo_01["path"],
@@ -224,7 +229,7 @@ async def test_git_rpc_add_read_only_success(self, git_upstream_repo_01: dict[st
224229
internal_status="active",
225230
)
226231

227-
await git.repository.add_read_only(message=message, service=self.services)
232+
await add_git_repository_read_only(model=model)
228233

229234
self.mock_infra_lock.registry.get(name=git_upstream_repo_01["name"], namespace="repository")
230235
self.mock_repo_class.new.assert_awaited_once_with(

0 commit comments

Comments
 (0)