Skip to content

Commit 44b957f

Browse files
solababsdummyuser
authored andcommitted
IFC-1977: Add git config for user name and email, set config on repo
This PR creates new git configurations that are then set on the repository. The created configurations are - [x] `user_name` - The user name of the git user - [x] `user_email` - The email of the git user
1 parent b3d1222 commit 44b957f

File tree

3 files changed

+37
-0
lines changed

3 files changed

+37
-0
lines changed

backend/infrahub/config.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
from pydantic import (
1515
AliasChoices,
1616
BaseModel,
17+
EmailStr,
1718
Field,
1819
PrivateAttr,
1920
ValidationError,
@@ -453,6 +454,8 @@ class GitSettings(BaseSettings):
453454
"Note: other branches created with sync with git will be imported also"
454455
),
455456
)
457+
user_name: str | None = Field(default=None, description="User name of the git user")
458+
user_email: EmailStr | None = Field(default=None, description="Email of the git user")
456459

457460
@model_validator(mode="after")
458461
def validate_sync_branch_names(self) -> Self:

backend/infrahub/git/base.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -407,6 +407,7 @@ async def create_locally(
407407
except GitCommandError as exc:
408408
await self._raise_enriched_error(error=exc, branch_name=checkout_ref or self.default_branch)
409409

410+
self.create_repo_config(repo)
410411
self.has_origin = True
411412

412413
# Create a worktree for the commit in the default branch
@@ -418,6 +419,13 @@ async def create_locally(
418419

419420
return True
420421

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+
421429
def has_worktree(self, identifier: str) -> bool:
422430
"""Return True if a worktree with a given identifier already exist."""
423431

backend/tests/unit/git/test_git_repository.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
from infrahub_sdk.uuidt import UUIDT
1313
from pytest_httpx._httpx_mock import HTTPXMock
1414

15+
from infrahub import config
1516
from infrahub.core.branch import Branch
1617
from infrahub.core.constants import InfrahubKind
1718
from infrahub.core.registry import registry
@@ -1104,3 +1105,28 @@ async def test_get_filtered_remote_branches__no_import_sync_branch_names(git_rep
11041105
repo = git_repo_01
11051106
filtered_remote_branches = await repo.get_filtered_remote_branches()
11061107
assert sorted(filtered_remote_branches.keys()) == ["branch01", "branch02", "clean-branch", "main"]
1108+
1109+
1110+
async def test_new_repo_has_config(git_upstream_repo_01: dict[str, str | Path], git_repos_dir: Path):
1111+
config.SETTINGS.git.user_email = "[email protected]"
1112+
config.SETTINGS.git.user_name = "Test User"
1113+
1114+
repo = await InfrahubRepository.new(
1115+
id=UUIDT.new(),
1116+
name=git_upstream_repo_01["name"],
1117+
location=str(git_upstream_repo_01["path"]),
1118+
client=InfrahubClient(config=Config(requester=dummy_async_request)),
1119+
)
1120+
1121+
# Check if all the directories are present
1122+
assert repo.directory_root.is_dir()
1123+
assert repo.directory_branches.is_dir()
1124+
assert repo.directory_commits.is_dir()
1125+
assert repo.directory_temp.is_dir()
1126+
1127+
with repo.get_git_repo_main().config_reader() as git_config:
1128+
assert git_config.get_value("user", "name") == "Test User"
1129+
assert git_config.get_value("user", "email") == "[email protected]"
1130+
1131+
config.SETTINGS.git.user_email = None
1132+
config.SETTINGS.git.user_name = None

0 commit comments

Comments
 (0)