Skip to content

Commit 41218bb

Browse files
committed
chore: merge RepoConfig and RepositoryConfig
1 parent f64f455 commit 41218bb

File tree

27 files changed

+138
-212
lines changed

27 files changed

+138
-212
lines changed

src/codegen/cli/auth/session.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
from codegen.cli.git.repo import get_git_repo
99
from codegen.cli.rich.codeblocks import format_command
10-
from codegen.configs.constants import CODEGEN_DIR_NAME, ENV_FILENAME
10+
from codegen.configs.constants import CODEGEN_DIR_NAME
1111
from codegen.configs.session_manager import session_manager
1212
from codegen.configs.user_config import UserConfig
1313
from codegen.git.repo_operator.local_git_repo import LocalGitRepo
@@ -30,7 +30,7 @@ def __init__(self, repo_path: Path, git_token: str | None = None) -> None:
3030
self.repo_path = repo_path
3131
self.local_git = LocalGitRepo(repo_path=repo_path)
3232
self.codegen_dir = repo_path / CODEGEN_DIR_NAME
33-
self.config = UserConfig(env_filepath=repo_path / ENV_FILENAME)
33+
self.config = UserConfig(root_path=repo_path)
3434
self.config.secrets.github_token = git_token or self.config.secrets.github_token
3535
self.existing = session_manager.get_session(repo_path) is not None
3636

src/codegen/cli/commands/config/main.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import rich_click as click
55
from rich.table import Table
66

7-
from codegen.configs.constants import ENV_FILENAME, GLOBAL_ENV_FILE
7+
from codegen.configs.constants import ENV_FILENAME, GLOBAL_CONFIG_DIR
88
from codegen.configs.user_config import UserConfig
99
from codegen.shared.path import get_git_root_path
1010

@@ -117,8 +117,8 @@ def set_command(key: str, value: str):
117117

118118
def _get_user_config() -> UserConfig:
119119
if (project_root := get_git_root_path()) is None:
120-
env_filepath = GLOBAL_ENV_FILE
120+
root_path = GLOBAL_CONFIG_DIR
121121
else:
122-
env_filepath = project_root / ENV_FILENAME
122+
root_path = project_root
123123

124-
return UserConfig(env_filepath)
124+
return UserConfig(root_path)

src/codegen/cli/commands/run/run_local.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66

77
from codegen.cli.auth.session import CodegenSession
88
from codegen.cli.utils.function_finder import DecoratedFunction
9+
from codegen.configs.models.repository import RepositoryConfig
910
from codegen.git.repo_operator.repo_operator import RepoOperator
10-
from codegen.git.schemas.repo_config import RepoConfig
1111
from codegen.git.utils.language import determine_project_language
1212
from codegen.sdk.codebase.config import ProjectConfig
1313
from codegen.sdk.core.codebase import Codebase
@@ -30,7 +30,7 @@ def parse_codebase(
3030
codebase = Codebase(
3131
projects=[
3232
ProjectConfig(
33-
repo_operator=RepoOperator(repo_config=RepoConfig.from_repo_path(repo_path=repo_path)),
33+
repo_operator=RepoOperator(repo_config=RepositoryConfig.from_path(path=repo_path)),
3434
subdirectories=subdirectories,
3535
programming_language=language or determine_project_language(repo_path),
3636
)

src/codegen/cli/commands/start/main.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@
1010

1111
from codegen.cli.commands.start.docker_container import DockerContainer
1212
from codegen.cli.commands.start.docker_fleet import CODEGEN_RUNNER_IMAGE
13+
from codegen.configs.models.repository import RepositoryConfig
1314
from codegen.configs.models.secrets import SecretsConfig
1415
from codegen.git.repo_operator.local_git_repo import LocalGitRepo
15-
from codegen.git.schemas.repo_config import RepoConfig
1616
from codegen.shared.network.port import get_free_port
1717

1818
_default_host = "0.0.0.0"
@@ -26,7 +26,7 @@
2626
def start_command(port: int | None, detached: bool = False, skip_build: bool = False, force: bool = False) -> None:
2727
"""Starts a local codegen server"""
2828
repo_path = Path.cwd().resolve()
29-
repo_config = RepoConfig.from_repo_path(str(repo_path))
29+
repo_config = LocalGitRepo(repo_path=repo_path).get_repo_config()
3030
if (container := DockerContainer.get(repo_config.name)) is not None:
3131
if force:
3232
rich.print(f"[yellow]Removing existing runner {repo_config.name} to force restart[/yellow]")
@@ -50,7 +50,7 @@ def start_command(port: int | None, detached: bool = False, skip_build: bool = F
5050
raise click.Abort()
5151

5252

53-
def _handle_existing_container(repo_config: RepoConfig, container: DockerContainer) -> None:
53+
def _handle_existing_container(repo_config: RepositoryConfig, container: DockerContainer) -> None:
5454
if container.is_running():
5555
rich.print(
5656
Panel(
@@ -122,20 +122,20 @@ def _get_platform() -> str:
122122
return "linux/amd64"
123123

124124

125-
def _run_docker_container(repo_config: RepoConfig, port: int, detached: bool) -> None:
125+
def _run_docker_container(repo_config: RepositoryConfig, port: int, detached: bool) -> None:
126126
rich.print("[bold blue]Starting Docker container...[/bold blue]")
127127
container_repo_path = f"/app/git/{repo_config.name}"
128128
name_args = ["--name", f"{repo_config.name}"]
129129
envvars = {
130-
"REPOSITORY_LANGUAGE": repo_config.language.value,
131-
"REPOSITORY_OWNER": LocalGitRepo(repo_config.repo_path).owner,
130+
"REPOSITORY_LANGUAGE": repo_config.language,
131+
"REPOSITORY_OWNER": LocalGitRepo(repo_config.path).owner,
132132
"REPOSITORY_PATH": container_repo_path,
133-
"GITHUB_TOKEN": SecretsConfig().github_token,
133+
"GITHUB_TOKEN": SecretsConfig(root_path=repo_config.path).github_token,
134134
"PYTHONUNBUFFERED": "1", # Ensure Python output is unbuffered
135135
"CODEBASE_SYNC_ENABLED": "True",
136136
}
137137
envvars_args = [arg for k, v in envvars.items() for arg in ("--env", f"{k}={v}")]
138-
mount_args = ["-v", f"{repo_config.repo_path}:{container_repo_path}"]
138+
mount_args = ["-v", f"{repo_config.path}:{container_repo_path}"]
139139
entry_point = f"uv run --frozen uvicorn codegen.runner.servers.local_daemon:app --host {_default_host} --port {port}"
140140
port_args = ["-p", f"{port}:{port}"]
141141
detached_args = ["-d"] if detached else []

src/codegen/cli/mcp/resources/system_prompt.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1417,15 +1417,14 @@ def baz():
14171417
```python
14181418
from codegen import Codebase
14191419
from codegen.git.repo_operator.repo_operator import RepoOperator
1420-
from codegen.git.schemas.repo_config import RepoConfig
14211420
from codegen.sdk.codebase.config import ProjectConfig
14221421
from codegen.shared.enums.programming_language import ProgrammingLanguage
14231422
14241423
codebase = Codebase(
14251424
projects = [
14261425
ProjectConfig(
14271426
repo_operator=RepoOperator(
1428-
repo_config=RepoConfig(name="codegen-sdk"),
1427+
repo_path="/tmp/codegen-sdk",
14291428
bot_commit=True
14301429
),
14311430
programming_language=ProgrammingLanguage.TYPESCRIPT,

src/codegen/configs/models/base_config.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ class BaseConfig(BaseSettings, ABC):
1616

1717
model_config = SettingsConfigDict(extra="ignore", case_sensitive=False)
1818

19-
def __init__(self, prefix: str, env_filepath: Path | None = None, *args, **kwargs) -> None:
19+
def __init__(self, prefix: str, root_path: Path | None = None, *args, **kwargs) -> None:
20+
env_filepath = root_path / ENV_FILENAME if root_path else None
2021
if env_filepath is None:
2122
root_path = get_git_root_path()
2223
if root_path is not None:

src/codegen/configs/models/repository.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import os
2+
from typing import Self
23

34
from codegen.configs.models.base_config import BaseConfig
45

@@ -13,16 +14,14 @@ class RepositoryConfig(BaseConfig):
1314
language: str | None = None
1415
user_name: str | None = None
1516
user_email: str | None = None
17+
subdirectories: list[str] | None = None
1618

1719
def __init__(self, prefix: str = "REPOSITORY", *args, **kwargs) -> None:
1820
super().__init__(prefix=prefix, *args, **kwargs)
1921

20-
def _initialize(
21-
self,
22-
) -> None:
23-
"""Initialize the repository config"""
24-
if self.path is None:
25-
self.path = os.getcwd()
22+
@classmethod
23+
def from_path(cls, path: str) -> Self:
24+
return cls(root_path=path, path=path)
2625

2726
@property
2827
def base_dir(self) -> str:

src/codegen/configs/user_config.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
from pydantic import Field
55

6+
from codegen.configs.constants import ENV_FILENAME
67
from codegen.configs.models.codebase import CodebaseConfig
78
from codegen.configs.models.repository import RepositoryConfig
89
from codegen.configs.models.secrets import SecretsConfig
@@ -14,11 +15,11 @@ class UserConfig:
1415
codebase: CodebaseConfig = Field(default_factory=CodebaseConfig)
1516
secrets: SecretsConfig = Field(default_factory=SecretsConfig)
1617

17-
def __init__(self, env_filepath: Path) -> None:
18-
self.env_filepath = env_filepath
19-
self.secrets = SecretsConfig(env_filepath=env_filepath)
20-
self.repository = RepositoryConfig(env_filepath=env_filepath)
21-
self.codebase = CodebaseConfig(env_filepath=env_filepath)
18+
def __init__(self, root_path: Path) -> None:
19+
self.env_filepath = root_path / ENV_FILENAME
20+
self.secrets = SecretsConfig(root_path=root_path)
21+
self.repository = RepositoryConfig(root_path=root_path)
22+
self.codebase = CodebaseConfig(root_path=root_path)
2223

2324
def save(self) -> None:
2425
"""Save configuration to the config file."""

src/codegen/extensions/events/modal/base.py

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
from codegen.extensions.events.codegen_app import CodegenApp
99
from codegen.extensions.events.modal.request_util import fastapi_request_adapter
1010
from codegen.git.clients.git_repo_client import GitRepoClient
11-
from codegen.git.schemas.repo_config import RepoConfig
1211

1312
logging.basicConfig(level=logging.INFO, force=True)
1413
logger = logging.getLogger(__name__)
@@ -36,17 +35,11 @@ def get_event_handler_cls(self) -> modal.Cls:
3635
raise NotImplementedError(msg)
3736

3837
async def handle_event(self, org: str, repo: str, provider: Literal["slack", "github", "linear"], request: Request):
39-
repo_config = RepoConfig(
40-
name=repo,
41-
full_name=f"{org}/{repo}",
42-
)
43-
4438
repo_snapshotdict = modal.Dict.from_name(self.snapshot_index_id, {}, create_if_missing=True)
45-
4639
last_snapshot_commit = repo_snapshotdict.get(f"{org}/{repo}", None)
4740

4841
if last_snapshot_commit is None:
49-
git_client = GitRepoClient(repo_config=repo_config, access_token=os.environ["GITHUB_ACCESS_TOKEN"])
42+
git_client = GitRepoClient(repo_full_name=f"{org}/{repo}", access_token=os.environ["GITHUB_ACCESS_TOKEN"])
5043
branch = git_client.get_branch_safe(git_client.default_branch)
5144
last_snapshot_commit = branch.commit.sha if branch and branch.commit else None
5245

@@ -76,15 +69,8 @@ def refresh_repository_snapshots(self, snapshot_index_id: str):
7669
try:
7770
# Parse the repository full name to get org and repo
7871
org, repo = repo_full_name.split("/")
79-
80-
# Create a RepoConfig for the repository
81-
repo_config = RepoConfig(
82-
name=repo,
83-
full_name=repo_full_name,
84-
)
85-
8672
# Initialize the GitRepoClient to fetch the latest commit
87-
git_client = GitRepoClient(repo_config=repo_config, access_token=os.environ["GITHUB_ACCESS_TOKEN"])
73+
git_client = GitRepoClient(repo_full_name=repo_full_name, access_token=os.environ["GITHUB_ACCESS_TOKEN"])
8874

8975
# Get the default branch and its latest commit
9076
branch = git_client.get_branch_safe(git_client.default_branch)

src/codegen/git/clients/git_repo_client.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616

1717
from codegen.configs.models.secrets import SecretsConfig
1818
from codegen.git.clients.github_client import GithubClient
19-
from codegen.git.schemas.repo_config import RepoConfig
2019
from codegen.git.utils.format import format_comparison
2120
from codegen.shared.logging.get_logger import get_logger
2221

@@ -26,22 +25,22 @@
2625
class GitRepoClient:
2726
"""Wrapper around PyGithub's Remote Repository."""
2827

29-
repo_config: RepoConfig
28+
repo_full_name: str
3029
gh_client: GithubClient
3130
_repo: Repository
3231

33-
def __init__(self, repo_config: RepoConfig, access_token: str | None = None) -> None:
34-
self.repo_config = repo_config
32+
def __init__(self, repo_full_name: str, access_token: str | None = None) -> None:
33+
self.repo_full_name = repo_full_name
3534
self.gh_client = self._create_github_client(token=access_token or SecretsConfig().github_token)
3635
self._repo = self._create_client()
3736

3837
def _create_github_client(self, token: str) -> GithubClient:
3938
return GithubClient(token=token)
4039

4140
def _create_client(self) -> Repository:
42-
client = self.gh_client.get_repo_by_full_name(self.repo_config.full_name)
41+
client = self.gh_client.get_repo_by_full_name(self.repo_full_name)
4342
if not client:
44-
msg = f"Repo {self.repo_config.full_name} not found!"
43+
msg = f"Repo {self.repo_full_name} not found!"
4544
raise ValueError(msg)
4645
return client
4746

0 commit comments

Comments
 (0)