Skip to content

Commit 58fc689

Browse files
authored
Merge pull request #4814 from opsmill/lgu-migrate-git-repo-pull-read-only
Migrate GitRepositoryPullReadOnly to prefect
2 parents 895563e + 9a933c6 commit 58fc689

File tree

10 files changed

+89
-129
lines changed

10 files changed

+89
-129
lines changed

backend/infrahub/git/models.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,3 +33,14 @@ class RequestArtifactGenerate(BaseModel):
3333
query: str = Field(..., description="The name of the query to use when collecting data")
3434
timeout: int = Field(..., description="Timeout for requests used to generate this artifact")
3535
variables: dict = Field(..., description="Input variables when generating the artifact")
36+
37+
38+
class GitRepositoryPullReadOnly(BaseModel):
39+
"""Update a read-only repository to the latest commit for its ref"""
40+
41+
location: str = Field(..., description="The external URL of the repository")
42+
repository_id: str = Field(..., description="The unique ID of the Repository")
43+
repository_name: str = Field(..., description="The name of the repository")
44+
ref: Optional[str] = Field(None, description="Ref to track on the external repository")
45+
commit: Optional[str] = Field(None, description="Specific commit to pull")
46+
infrahub_branch_name: str = Field(..., description="Infrahub branch on which to sync the remote repository")

backend/infrahub/git/tasks.py

Lines changed: 43 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@
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 RequestArtifactDefinitionGenerate, RequestArtifactGenerate
16-
from .repository import InfrahubRepository, get_initialized_repo
15+
from .models import GitRepositoryPullReadOnly, RequestArtifactDefinitionGenerate, RequestArtifactGenerate
16+
from .repository import InfrahubReadOnlyRepository, InfrahubRepository, get_initialized_repo
1717

1818
log = get_logger()
1919

@@ -234,3 +234,44 @@ async def generate_request_artifact_definition(model: RequestArtifactDefinitionG
234234
await service.workflow.submit_workflow(
235235
workflow=REQUEST_ARTIFACT_GENERATE, parameters={"model": request_artifact_generate_model}
236236
)
237+
238+
239+
@flow(name="git-repository-pull-read-only")
240+
async def pull_read_only(model: GitRepositoryPullReadOnly) -> None:
241+
service = services.service
242+
if not model.ref and not model.commit:
243+
log.warning(
244+
"No commit or ref in GitRepositoryPullReadOnly message",
245+
name=model.repository_name,
246+
repository_id=model.repository_id,
247+
)
248+
return
249+
async with service.git_report(related_node=model.repository_id, title="Pulling read-only repository") as git_report:
250+
async with lock.registry.get(name=model.repository_name, namespace="repository"):
251+
init_failed = False
252+
try:
253+
repo = await InfrahubReadOnlyRepository.init(
254+
id=model.repository_id,
255+
name=model.repository_name,
256+
location=model.location,
257+
client=service.client,
258+
ref=model.ref,
259+
infrahub_branch_name=model.infrahub_branch_name,
260+
task_report=git_report,
261+
)
262+
except RepositoryError:
263+
init_failed = True
264+
265+
if init_failed:
266+
repo = await InfrahubReadOnlyRepository.new(
267+
id=model.repository_id,
268+
name=model.repository_name,
269+
location=model.location,
270+
client=service.client,
271+
ref=model.ref,
272+
infrahub_branch_name=model.infrahub_branch_name,
273+
task_report=git_report,
274+
)
275+
276+
await repo.import_objects_from_files(infrahub_branch_name=model.infrahub_branch_name, commit=model.commit)
277+
await repo.sync_from_remote(commit=model.commit)

backend/infrahub/graphql/mutations/repository.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,12 @@
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 GitRepositoryPullReadOnly
1314
from infrahub.graphql.types.common import IdentifierInput
1415
from infrahub.log import get_logger
1516
from infrahub.message_bus import messages
1617
from infrahub.message_bus.messages.git_repository_connectivity import GitRepositoryConnectivityResponse
18+
from infrahub.workflows.catalogue import GIT_REPOSITORIES_PULL_READ_ONLY
1719

1820
from .main import InfrahubMutationMixin, InfrahubMutationOptions
1921

@@ -166,7 +168,7 @@ async def mutate_update(
166168
ref=data.ref.value if data.ref else None,
167169
)
168170

169-
message = messages.GitRepositoryPullReadOnly(
171+
model = GitRepositoryPullReadOnly(
170172
repository_id=obj.id,
171173
repository_name=obj.name.value,
172174
location=obj.location.value,
@@ -175,7 +177,9 @@ async def mutate_update(
175177
infrahub_branch_name=branch.name,
176178
)
177179
if context.service:
178-
await context.service.send(message=message)
180+
await context.service.workflow.submit_workflow(
181+
workflow=GIT_REPOSITORIES_PULL_READ_ONLY, parameters={"model": model}
182+
)
179183
return obj, result
180184

181185

backend/infrahub/message_bus/messages/__init__.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
from .git_repository_importobjects import GitRepositoryImportObjects
2121
from .git_repository_merge import GitRepositoryMerge
2222
from .git_repository_read_only_add import GitRepositoryAddReadOnly
23-
from .git_repository_read_only_pull import GitRepositoryPullReadOnly
2423
from .proposed_change.request_proposedchange_dataintegrity import RequestProposedChangeDataIntegrity
2524
from .proposed_change.request_proposedchange_refreshartifacts import RequestProposedChangeRefreshArtifacts
2625
from .proposed_change.request_proposedchange_repositorychecks import RequestProposedChangeRepositoryChecks
@@ -65,7 +64,6 @@
6564
"git.repository.merge": GitRepositoryMerge,
6665
"git.repository.add_read_only": GitRepositoryAddReadOnly,
6766
"git.repository.import_objects": GitRepositoryImportObjects,
68-
"git.repository.pull_read_only": GitRepositoryPullReadOnly,
6967
"schema.migration.path": SchemaMigrationPath,
7068
"schema.validator.path": SchemaValidatorPath,
7169
"refresh.registry.branches": RefreshRegistryBranches,

backend/infrahub/message_bus/messages/git_repository_read_only_pull.py

Lines changed: 0 additions & 18 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
@@ -39,7 +39,6 @@
3939
"git.repository.add_read_only": git.repository.add_read_only,
4040
"git.repository.connectivity": git.repository.connectivity,
4141
"git.repository.import_objects": git.repository.import_objects,
42-
"git.repository.pull_read_only": git.repository.pull_read_only,
4342
"git.repository.merge": git.repository.merge,
4443
"refresh.registry.branches": refresh.registry.branches,
4544
"refresh.registry.rebased_branch": refresh.registry.rebased_branch,

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

Lines changed: 0 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -104,57 +104,6 @@ async def import_objects(message: messages.GitRepositoryImportObjects, service:
104104
await repo.import_objects_from_files(infrahub_branch_name=message.infrahub_branch_name, commit=message.commit)
105105

106106

107-
@flow(name="git-repository-pull-read-only")
108-
async def pull_read_only(message: messages.GitRepositoryPullReadOnly, service: InfrahubServices) -> None:
109-
if not message.ref and not message.commit:
110-
log.warning(
111-
"No commit or ref in GitRepositoryPullReadOnly message",
112-
name=message.repository_name,
113-
repository_id=message.repository_id,
114-
)
115-
return
116-
log.info(
117-
"Pulling read-only repository",
118-
repository=message.repository_name,
119-
location=message.location,
120-
ref=message.ref,
121-
commit=message.commit,
122-
)
123-
async with service.git_report(
124-
related_node=message.repository_id, title="Pulling read-only repository"
125-
) as git_report:
126-
async with lock.registry.get(name=message.repository_name, namespace="repository"):
127-
init_failed = False
128-
try:
129-
repo = await InfrahubReadOnlyRepository.init(
130-
id=message.repository_id,
131-
name=message.repository_name,
132-
location=message.location,
133-
client=service.client,
134-
ref=message.ref,
135-
infrahub_branch_name=message.infrahub_branch_name,
136-
task_report=git_report,
137-
)
138-
except RepositoryError:
139-
init_failed = True
140-
141-
if init_failed:
142-
repo = await InfrahubReadOnlyRepository.new(
143-
id=message.repository_id,
144-
name=message.repository_name,
145-
location=message.location,
146-
client=service.client,
147-
ref=message.ref,
148-
infrahub_branch_name=message.infrahub_branch_name,
149-
task_report=git_report,
150-
)
151-
152-
await repo.import_objects_from_files(
153-
infrahub_branch_name=message.infrahub_branch_name, commit=message.commit
154-
)
155-
await repo.sync_from_remote(commit=message.commit)
156-
157-
158107
@flow(name="git-repository-merge")
159108
async def merge(message: messages.GitRepositoryMerge, service: InfrahubServices) -> None:
160109
log.info(

backend/infrahub/workflows/catalogue.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,16 @@
129129
branch_support=BranchSupportType.AWARE,
130130
tags=[WorkflowTag.DATABASE_CHANGE],
131131
)
132+
133+
GIT_REPOSITORIES_PULL_READ_ONLY = WorkflowDefinition(
134+
name="git-repository-pull-read-only",
135+
type=WorkflowType.INTERNAL,
136+
module="infrahub.git.tasks",
137+
function="pull_read_only",
138+
branch_support=BranchSupportType.AWARE,
139+
tags=[WorkflowTag.DATABASE_CHANGE],
140+
)
141+
132142
BRANCH_REBASE = WorkflowDefinition(
133143
name="branch-rebase",
134144
type=WorkflowType.INTERNAL,
@@ -167,5 +177,6 @@
167177
REQUEST_GENERATOR_RUN,
168178
REQUEST_DIFF_UPDATE,
169179
REQUEST_DIFF_REFRESH,
180+
GIT_REPOSITORIES_PULL_READ_ONLY,
170181
TRIGGER_GENERATOR_DEFINITION_RUN,
171182
]

backend/tests/unit/git/test_git_rpc.py

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,14 @@
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
1314
from infrahub.git.repository import InfrahubReadOnlyRepository
15+
from infrahub.git.tasks import pull_read_only
1416
from infrahub.lock import InfrahubLockRegistry
1517
from infrahub.message_bus import Meta, messages
1618
from infrahub.message_bus.operations import git
17-
from infrahub.services import InfrahubServices
19+
from infrahub.services import InfrahubServices, services
20+
from infrahub.services.adapters.workflow.local import WorkflowLocalExecution
1821
from tests.helpers.test_client import dummy_async_request
1922

2023
# pylint: disable=redefined-outer-name
@@ -230,16 +233,18 @@ class TestPullReadOnly:
230233
def setup_method(self):
231234
self.client = AsyncMock(spec=InfrahubClient)
232235
self.git_report = AsyncContextManagerMock()
233-
self.services = InfrahubServices(client=self.client)
234-
self.services.git_report = self.git_report
236+
self.original_services = services.service
237+
services.service = InfrahubServices(client=self.client, workflow=WorkflowLocalExecution())
238+
services.service.git_report = self.git_report
239+
235240
self.commit = str(UUIDT())
236241
self.infrahub_branch_name = "read-only-branch"
237242
self.repo_id = str(UUIDT())
238243
self.location = "/some/directory/over/here"
239244
self.repo_name = "dont-update-this-dude"
240245
self.ref = "stable-branch"
241246

242-
self.message = messages.GitRepositoryPullReadOnly(
247+
self.model = GitRepositoryPullReadOnly(
243248
location=self.location,
244249
repository_id=self.repo_id,
245250
repository_name=self.repo_name,
@@ -248,31 +253,30 @@ def setup_method(self):
248253
infrahub_branch_name=self.infrahub_branch_name,
249254
)
250255

251-
lock_patcher = patch("infrahub.message_bus.operations.git.repository.lock")
256+
lock_patcher = patch("infrahub.git.tasks.lock")
252257
self.mock_infra_lock = lock_patcher.start()
253-
self.mock_infra_lock.registry = AsyncMock(spec=InfrahubLockRegistry)
254-
repo_class_patcher = patch(
255-
"infrahub.message_bus.operations.git.repository.InfrahubReadOnlyRepository", spec=InfrahubReadOnlyRepository
256-
)
258+
self.mock_infra_lock.registry = AsyncMock(spec=InfrahubLockRegistry) # TODO fix mock?
259+
repo_class_patcher = patch("infrahub.git.tasks.InfrahubReadOnlyRepository", spec=InfrahubReadOnlyRepository)
257260
self.mock_repo_class = repo_class_patcher.start()
258261
self.mock_repo = AsyncMock(spec=InfrahubReadOnlyRepository)
259262
self.mock_repo_class.new.return_value = self.mock_repo
260263
self.mock_repo_class.init.return_value = self.mock_repo
261264

262265
def teardown_method(self):
263266
patch.stopall()
267+
services.service = self.original_services
264268

265269
async def test_improper_message(self):
266-
self.message.ref = None
267-
self.message.commit = None
270+
self.model.ref = None
271+
self.model.commit = None
268272

269-
await git.repository.pull_read_only(message=self.message, service=self.services)
273+
await pull_read_only(model=self.model)
270274

271275
self.mock_repo_class.new.assert_not_awaited()
272276
self.mock_repo_class.init.assert_not_awaited()
273277

274278
async def test_existing_repository(self):
275-
await git.repository.pull_read_only(message=self.message, service=self.services)
279+
await pull_read_only(model=self.model)
276280

277281
self.mock_infra_lock.registry.get(name=self.repo_name, namespace="repository")
278282
self.mock_repo_class.init.assert_awaited_once_with(
@@ -292,7 +296,7 @@ async def test_existing_repository(self):
292296
async def test_new_repository(self):
293297
self.mock_repo_class.init.side_effect = RepositoryError(self.repo_name, "it is broken")
294298

295-
await git.repository.pull_read_only(message=self.message, service=self.services)
299+
await pull_read_only(model=self.model)
296300

297301
self.mock_infra_lock.registry.get(name=self.repo_name, namespace="repository")
298302
self.mock_repo_class.init.assert_awaited_once_with(

docs/docs/reference/message-bus-events.mdx

Lines changed: 0 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -448,25 +448,6 @@ For more detailed explanations on how to use these events within Infrahub, see t
448448
| **commit** | Specific commit to pull | string | None |
449449
| **infrahub_branch_name** | Infrahub branch on which to sync the remote repository | string | None |
450450
<!-- vale on -->
451-
<!-- vale off -->
452-
#### Event git.repository.pull_read_only
453-
<!-- vale on -->
454-
455-
**Description**: Update a read-only repository to the latest commit for its ref
456-
457-
**Priority**: 3
458-
459-
<!-- vale off -->
460-
| Key | Description | Type | Default Value |
461-
|-----|-------------|------|---------------|
462-
| **meta** | Meta properties for the message | N/A | None |
463-
| **location** | The external URL of the repository | string | None |
464-
| **repository_id** | The unique ID of the Repository | string | None |
465-
| **repository_name** | The name of the repository | string | None |
466-
| **ref** | Ref to track on the external repository | N/A | None |
467-
| **commit** | Specific commit to pull | N/A | None |
468-
| **infrahub_branch_name** | Infrahub branch on which to sync the remote repository | string | None |
469-
<!-- vale on -->
470451

471452

472453
<!-- vale off -->
@@ -1356,26 +1337,6 @@ For more detailed explanations on how to use these events within Infrahub, see t
13561337
| **commit** | Specific commit to pull | string | None |
13571338
| **infrahub_branch_name** | Infrahub branch on which to sync the remote repository | string | None |
13581339
<!-- vale on -->
1359-
<!-- vale off -->
1360-
#### Event git.repository.pull_read_only
1361-
<!-- vale on -->
1362-
1363-
**Description**: Update a read-only repository to the latest commit for its ref
1364-
1365-
**Priority**: 3
1366-
1367-
1368-
<!-- vale off -->
1369-
| Key | Description | Type | Default Value |
1370-
|-----|-------------|------|---------------|
1371-
| **meta** | Meta properties for the message | N/A | None |
1372-
| **location** | The external URL of the repository | string | None |
1373-
| **repository_id** | The unique ID of the Repository | string | None |
1374-
| **repository_name** | The name of the repository | string | None |
1375-
| **ref** | Ref to track on the external repository | N/A | None |
1376-
| **commit** | Specific commit to pull | N/A | None |
1377-
| **infrahub_branch_name** | Infrahub branch on which to sync the remote repository | string | None |
1378-
<!-- vale on -->
13791340

13801341

13811342
<!-- vale off -->

0 commit comments

Comments
 (0)