Skip to content

chore: use from_repo + clean-up from_repo #462

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Feb 13, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 6 additions & 9 deletions src/codegen/sdk/core/codebase.py
Original file line number Diff line number Diff line change
Expand Up @@ -176,13 +176,13 @@
main_project = ProjectConfig.from_path(repo_path, programming_language=programming_language)
projects = [main_project]
else:
main_project = projects[0]

Check failure on line 179 in src/codegen/sdk/core/codebase.py

View workflow job for this annotation

GitHub Actions / mypy

error: Value of type "list[ProjectConfig] | None" is not indexable [index]

# Initialize codebase
self._op = main_project.repo_operator
self.viz = VisualizationManager(op=self._op)
self.repo_path = Path(self._op.repo_path)
self.ctx = CodebaseContext(projects, config=config, io=io)

Check failure on line 185 in src/codegen/sdk/core/codebase.py

View workflow job for this annotation

GitHub Actions / mypy

error: Argument 1 to "CodebaseContext" has incompatible type "list[ProjectConfig] | None"; expected "list[ProjectConfig]" [arg-type]
self.console = Console(record=True, soft_wrap=True)

@noapidoc
Expand All @@ -198,7 +198,7 @@
yield "nodes", len(self.ctx.nodes)
yield "edges", len(self.ctx.edges)

__rich_repr__.angular = ANGULAR_STYLE

Check failure on line 201 in src/codegen/sdk/core/codebase.py

View workflow job for this annotation

GitHub Actions / mypy

error: "Callable[[Codebase[TSourceFile, TDirectory, TSymbol, TClass, TFunction, TImport, TGlobalVar, TInterface, TTypeAlias, TParameter, TCodeBlock]], Iterable[Any | tuple[Any] | tuple[str, Any] | tuple[str, Any, Any]]]" has no attribute "angular" [attr-defined]

@property
@deprecated("Please do not use the local repo operator directly")
Expand Down Expand Up @@ -226,8 +226,8 @@

@noapidoc
def _symbols(self, symbol_type: SymbolType | None = None) -> list[TSymbol | TClass | TFunction | TGlobalVar]:
matches: list[Symbol] = self.ctx.get_nodes(NodeType.SYMBOL)

Check failure on line 229 in src/codegen/sdk/core/codebase.py

View workflow job for this annotation

GitHub Actions / mypy

error: Incompatible types in assignment (expression has type "list[Importable[Any]]", variable has type "list[Symbol[Any, Any]]") [assignment]
return [x for x in matches if x.is_top_level and (symbol_type is None or x.symbol_type == symbol_type)]

Check failure on line 230 in src/codegen/sdk/core/codebase.py

View workflow job for this annotation

GitHub Actions / mypy

error: List comprehension has incompatible type List[Symbol[Any, Any]]; expected List[TSymbol | TClass | TFunction | TGlobalVar] [misc]

# =====[ Node Types ]=====
@overload
Expand All @@ -236,7 +236,7 @@
def files(self, *, extensions: Literal["*"]) -> list[File]: ...
@overload
def files(self, *, extensions: None = ...) -> list[TSourceFile]: ...
@proxy_property

Check failure on line 239 in src/codegen/sdk/core/codebase.py

View workflow job for this annotation

GitHub Actions / mypy

error: "cached_property[ProxyProperty[[Codebase[TSourceFile, TDirectory, TSymbol, TClass, TFunction, TImport, TGlobalVar, TInterface, TTypeAlias, TParameter, TCodeBlock], DefaultNamedArg(list[str] | Literal['*'] | None, 'extensions')], list[TSourceFile] | list[File]]]" not callable [operator]
def files(self, *, extensions: list[str] | Literal["*"] | None = None) -> list[TSourceFile] | list[File]:
"""A list property that returns all files in the codebase.

Expand Down Expand Up @@ -266,15 +266,15 @@
return sort_editables(files, alphabetical=True, dedupe=False)

@cached_property
def codeowners(self) -> list["CodeOwner[TSourceFile]"]:

Check failure on line 269 in src/codegen/sdk/core/codebase.py

View workflow job for this annotation

GitHub Actions / mypy

error: "CodeOwner" expects 7 type arguments, but 1 given [type-arg]
"""List all CodeOnwers in the codebase.

Returns:
list[CodeOwners]: A list of CodeOwners objects in the codebase.
"""
if self.G.codeowners_parser is None:

Check failure on line 275 in src/codegen/sdk/core/codebase.py

View workflow job for this annotation

GitHub Actions / mypy

error: "Codebase[TSourceFile, TDirectory, TSymbol, TClass, TFunction, TImport, TGlobalVar, TInterface, TTypeAlias, TParameter, TCodeBlock]" has no attribute "G" [attr-defined]
return []
return CodeOwner.from_parser(self.G.codeowners_parser, lambda *args, **kwargs: self.files(*args, **kwargs))

Check failure on line 277 in src/codegen/sdk/core/codebase.py

View workflow job for this annotation

GitHub Actions / mypy

error: "Codebase[TSourceFile, TDirectory, TSymbol, TClass, TFunction, TImport, TGlobalVar, TInterface, TTypeAlias, TParameter, TCodeBlock]" has no attribute "G" [attr-defined]

@property
def directories(self) -> list[TDirectory]:
Expand All @@ -286,7 +286,7 @@
Returns:
list[TDirectory]: A list of Directory objects in the codebase.
"""
return list(self.ctx.directories.values())

Check failure on line 289 in src/codegen/sdk/core/codebase.py

View workflow job for this annotation

GitHub Actions / mypy

error: Argument 1 to "list" has incompatible type "dict_values[Path, Directory[Any, Any, Any, Any, Any, Any, Any]]"; expected "Iterable[TDirectory]" [arg-type]

@property
def imports(self) -> list[TImport]:
Expand Down Expand Up @@ -1219,11 +1219,10 @@
@classmethod
def from_repo(
cls,
repo_name: str,
repo_full_name: str,
*,
tmp_dir: str | None = None,
tmp_dir: str | None = "/tmp/codegen",
commit: str | None = None,
shallow: bool = True,
programming_language: ProgrammingLanguage | None = None,
config: CodebaseConfig = DefaultConfig,
) -> "Codebase":
Expand All @@ -1240,23 +1239,21 @@
Returns:
Codebase: A Codebase instance initialized with the cloned repository
"""
logger.info(f"Fetching codebase for {repo_name}")
logger.info(f"Fetching codebase for {repo_full_name}")

# Parse repo name
if "/" not in repo_name:
if "/" not in repo_full_name:
msg = "repo_name must be in format 'owner/repo'"
raise ValueError(msg)
owner, repo = repo_name.split("/")
owner, repo = repo_full_name.split("/")

# Setup temp directory
if tmp_dir is None:
tmp_dir = "/tmp/codegen"
os.makedirs(tmp_dir, exist_ok=True)
logger.info(f"Using directory: {tmp_dir}")

# Setup repo path and URL
repo_path = os.path.join(tmp_dir, repo)
repo_url = f"https://github.com/{repo_name}.git"
repo_url = f"https://github.com/{repo_full_name}.git"
logger.info(f"Will clone {repo_url} to {repo_path}")

try:
Expand Down
31 changes: 4 additions & 27 deletions tests/integration/codegen/git/codebase/conftest.py
Original file line number Diff line number Diff line change
@@ -1,36 +1,13 @@
import os

import pytest
from git import Repo as GitRepo

from codegen.git.repo_operator.local_repo_operator import LocalRepoOperator
from codegen.git.schemas.repo_config import RepoConfig
from codegen.git.utils.clone_url import get_authenticated_clone_url_for_repo_config
from codegen.sdk.codebase.config import ProjectConfig
from codegen.sdk.core.codebase import Codebase
from codegen.shared.configs.session_configs import config
from codegen.shared.enums.programming_language import ProgrammingLanguage


@pytest.fixture
def repo_config(tmpdir):
repo_config = RepoConfig(
name="Kevin-s-Adventure-Game",
full_name="codegen-sh/Kevin-s-Adventure-Game",
base_dir=str(tmpdir),
)
yield repo_config


@pytest.fixture
def op(repo_config):
os.chdir(repo_config.base_dir)
GitRepo.clone_from(url=get_authenticated_clone_url_for_repo_config(repo_config, token=config.secrets.github_token), to_path=os.path.join(repo_config.base_dir, repo_config.name), depth=1)
op = LocalRepoOperator(repo_config=repo_config)
yield op


@pytest.fixture
def codebase(op: LocalRepoOperator):
project_config = ProjectConfig(repo_operator=op)
codebase = Codebase(projects=[project_config])
def codebase(tmpdir):
os.chdir(tmpdir)
codebase = Codebase.from_repo(repo_full_name="codegen-sh/Kevin-s-Adventure-Game", tmp_dir=tmpdir, programming_language=ProgrammingLanguage.PYTHON)
yield codebase
Loading