Skip to content

Commit 2740532

Browse files
committed
move setting git global config to async worker
1 parent 75062c1 commit 2740532

File tree

6 files changed

+68
-36
lines changed

6 files changed

+68
-36
lines changed

backend/infrahub/git/base.py

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
from pydantic import BaseModel, ConfigDict, Field
1717
from pydantic import ValidationError as PydanticValidationError
1818

19-
from infrahub import config
2019
from infrahub.core.branch import Branch
2120
from infrahub.core.constants import InfrahubKind, RepositoryOperationalStatus, RepositorySyncStatus
2221
from infrahub.core.registry import registry
@@ -407,7 +406,6 @@ async def create_locally(
407406
except GitCommandError as exc:
408407
await self._raise_enriched_error(error=exc, branch_name=checkout_ref or self.default_branch)
409408

410-
self.create_repo_config(repo)
411409
self.has_origin = True
412410

413411
# Create a worktree for the commit in the default branch
@@ -419,13 +417,6 @@ async def create_locally(
419417

420418
return True
421419

422-
@staticmethod
423-
def create_repo_config(repo: Repo) -> None:
424-
if config.SETTINGS.git.user_name and config.SETTINGS.git.user_email:
425-
with repo.config_writer() as git_config:
426-
git_config.set_value("user", "name", config.SETTINGS.git.user_name)
427-
git_config.set_value("user", "email", config.SETTINGS.git.user_email)
428-
429420
def has_worktree(self, identifier: str) -> bool:
430421
"""Return True if a worktree with a given identifier already exist."""
431422

backend/infrahub/workers/infrahub_async.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import asyncio
12
import logging
23
import os
34
from typing import Any
@@ -122,6 +123,7 @@ async def setup(
122123
)
123124

124125
set_component_type(component_type=self.component_type)
126+
await self.set_git_global_config()
125127
await self._init_services(client=client)
126128

127129
if not registry.schema_has_been_initialized():
@@ -204,3 +206,30 @@ async def _init_services(self, client: InfrahubClient) -> None:
204206
)
205207

206208
self.service = service
209+
210+
async def set_git_global_config(self) -> None:
211+
if config.SETTINGS.git.user_name:
212+
proc_name = await asyncio.create_subprocess_exec(
213+
"git",
214+
"config",
215+
"--global",
216+
"user.name",
217+
config.SETTINGS.git.user_name,
218+
stdout=asyncio.subprocess.PIPE,
219+
stderr=asyncio.subprocess.PIPE,
220+
)
221+
await proc_name.wait()
222+
self._logger.info("Git user name set")
223+
224+
if config.SETTINGS.git.user_email:
225+
proc_email = await asyncio.create_subprocess_exec(
226+
"git",
227+
"config",
228+
"--global",
229+
"user.email",
230+
config.SETTINGS.git.user_email,
231+
stdout=asyncio.subprocess.PIPE,
232+
stderr=asyncio.subprocess.PIPE,
233+
)
234+
await proc_email.wait()
235+
self._logger.info("Git user email set")

backend/tests/integration/conftest.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,3 +142,14 @@ def prefect_test_fixture() -> Generator:
142142
def prefect_test(prefect_test_fixture) -> Generator:
143143
with disable_run_logger():
144144
yield
145+
146+
147+
@pytest.fixture
148+
def git_user_config():
149+
initial_user_name = config.SETTINGS.git.user_name
150+
initial_user_email = config.SETTINGS.git.user_email
151+
config.SETTINGS.git.user_email = "[email protected]"
152+
config.SETTINGS.git.user_name = "Test User"
153+
yield
154+
config.SETTINGS.git.user_email = initial_user_email
155+
config.SETTINGS.git.user_name = initial_user_name

backend/tests/integration/workers/test_infrahubasync.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import asyncio
12
from typing import TYPE_CHECKING
23

34
import pytest
@@ -107,3 +108,30 @@ async def test_broken_flow(
107108
await flow_after.state.result(raise_on_failure=True)
108109

109110
assert "validation error for DummyOutput" in str(exc.value)
111+
112+
async def test_worker_has_set_git_user_config(self, client, work_pool, git_user_config):
113+
worker = InfrahubWorkerAsync(work_pool_name=work_pool.name)
114+
await worker.setup(client=client, metric_port=0)
115+
proc_name = await asyncio.create_subprocess_exec(
116+
"git",
117+
"config",
118+
"--global",
119+
"user.name",
120+
stdout=asyncio.subprocess.PIPE,
121+
stderr=asyncio.subprocess.PIPE,
122+
)
123+
stdout_name, _ = await proc_name.communicate()
124+
user_name = stdout_name.decode().strip()
125+
assert user_name == "Test User"
126+
127+
proc_email = await asyncio.create_subprocess_exec(
128+
"git",
129+
"config",
130+
"--global",
131+
"user.email",
132+
stdout=asyncio.subprocess.PIPE,
133+
stderr=asyncio.subprocess.PIPE,
134+
)
135+
stdout_email, _ = await proc_email.communicate()
136+
user_email = stdout_email.decode().strip()
137+
assert user_email == "[email protected]"

backend/tests/unit/git/conftest.py

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1371,11 +1371,3 @@ async def mock_create_branch_git_repo_01(db: InfrahubDatabase, default_branch: B
13711371
@pytest.fixture
13721372
async def mock_create_branch_git_repo_03(db: InfrahubDatabase, default_branch: Branch) -> None:
13731373
await create_branch(branch_name="branch01", db=db)
1374-
1375-
1376-
def git_user_config():
1377-
config.SETTINGS.git.user_email = "[email protected]"
1378-
config.SETTINGS.git.user_name = "Test User"
1379-
yield
1380-
config.SETTINGS.git.user_email = None
1381-
config.SETTINGS.git.user_name = None

backend/tests/unit/git/test_git_repository.py

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1104,22 +1104,3 @@ async def test_get_filtered_remote_branches__no_import_sync_branch_names(git_rep
11041104
repo = git_repo_01
11051105
filtered_remote_branches = await repo.get_filtered_remote_branches()
11061106
assert sorted(filtered_remote_branches.keys()) == ["branch01", "branch02", "clean-branch", "main"]
1107-
1108-
1109-
async def test_new_repo_has_config(git_upstream_repo_01: dict[str, str | Path], git_repos_dir: Path, git_user_config):
1110-
repo = await InfrahubRepository.new(
1111-
id=UUIDT.new(),
1112-
name=git_upstream_repo_01["name"],
1113-
location=str(git_upstream_repo_01["path"]),
1114-
client=InfrahubClient(config=Config(requester=dummy_async_request)),
1115-
)
1116-
1117-
# Check if all the directories are present
1118-
assert repo.directory_root.is_dir()
1119-
assert repo.directory_branches.is_dir()
1120-
assert repo.directory_commits.is_dir()
1121-
assert repo.directory_temp.is_dir()
1122-
1123-
with repo.get_git_repo_main().config_reader() as git_config:
1124-
assert git_config.get_value("user", "name") == "Test User"
1125-
assert git_config.get_value("user", "email") == "[email protected]"

0 commit comments

Comments
 (0)