Skip to content

test: add codebase.create_pr tests #422

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 8 commits into from
Feb 11, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
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
3 changes: 1 addition & 2 deletions src/codegen/git/repo_operator/local_repo_operator.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
from codegen.git.schemas.repo_config import RepoConfig
from codegen.git.utils.clone_url import url_to_github
from codegen.git.utils.file_utils import create_files
from codegen.shared.configs.config import config

logger = logging.getLogger(__name__)

Expand All @@ -41,7 +40,7 @@
github_api_key: str | None = None,
bot_commit: bool = False,
) -> None:
self._github_api_key = github_api_key or config.secrets.github_token
self._github_api_key = github_api_key
self._remote_git_repo = None
super().__init__(repo_config, bot_commit)
os.makedirs(self.repo_path, exist_ok=True)
Expand All @@ -55,7 +54,7 @@
def remote_git_repo(self) -> Repository:
if self._remote_git_repo is None:
if not self._github_api_key:
return None

Check failure on line 57 in src/codegen/git/repo_operator/local_repo_operator.py

View workflow job for this annotation

GitHub Actions / mypy

error: Incompatible return value type (got "None", expected "Repository") [return-value]

if not (base_url := self.base_url):
msg = "Could not determine GitHub URL from remotes"
Expand All @@ -72,8 +71,8 @@
repo = parts[-3]

github = Github(self._github_api_key)
self._remote_git_repo = github.get_repo(f"{owner}/{repo}")

Check failure on line 74 in src/codegen/git/repo_operator/local_repo_operator.py

View workflow job for this annotation

GitHub Actions / mypy

error: Incompatible types in assignment (expression has type "Repository", variable has type "GitRepoClient | None") [assignment]
return self._remote_git_repo

Check failure on line 75 in src/codegen/git/repo_operator/local_repo_operator.py

View workflow job for this annotation

GitHub Actions / mypy

error: Incompatible return value type (got "GitRepoClient | None", expected "Repository") [return-value]

####################################################################################################################
# CLASS METHODS
Expand Down Expand Up @@ -165,11 +164,11 @@
####################################################################################################################

@property
def codeowners_parser(self) -> CodeOwnersParser | None:

Check failure on line 167 in src/codegen/git/repo_operator/local_repo_operator.py

View workflow job for this annotation

GitHub Actions / mypy

error: Signature of "codeowners_parser" incompatible with supertype "RepoOperator" [override]
return None

@cached_property
def base_url(self) -> str | None:

Check failure on line 171 in src/codegen/git/repo_operator/local_repo_operator.py

View workflow job for this annotation

GitHub Actions / mypy

error: Missing return statement [return]

Check failure on line 171 in src/codegen/git/repo_operator/local_repo_operator.py

View workflow job for this annotation

GitHub Actions / mypy

error: Signature of "base_url" incompatible with supertype "RepoOperator" [override]
if remote := next(iter(self.git_cli.remotes), None):
return url_to_github(remote.url, self.get_active_branch_or_commit())

Expand Down
37 changes: 37 additions & 0 deletions tests/integration/codegen/git/codebase/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
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.config import config


@pytest.fixture
def repo_config(tmpdir):
repo_config = RepoConfig(
name="Kevin-s-Adventure-Game",
full_name="codegen-sh/Kevin-s-Adventure-Game",
organization_name="codegen-sh",
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, github_api_key=config.secrets.github_token)
yield op


@pytest.fixture
def codebase(op: LocalRepoOperator):
project_config = ProjectConfig(repo_operator=op)
codebase = Codebase(projects=[project_config])

Check failure on line 36 in tests/integration/codegen/git/codebase/conftest.py

View workflow job for this annotation

GitHub Actions / mypy

error: Need type annotation for "codebase" [var-annotated]
yield codebase
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import uuid

import pytest

from codegen.sdk.core.codebase import Codebase


def test_codebase_create_pr_active_branch(codebase: Codebase):
head = f"test-create-pr-{uuid.uuid4()}"
codebase.checkout(branch=head, create_if_missing=True)
codebase.files[0].remove()

Check failure on line 11 in tests/integration/codegen/git/codebase/test_codebase_create_pr.py

View workflow job for this annotation

GitHub Actions / mypy

error: Value of type overloaded function is not indexable [index]
codebase.commit()
pr = codebase.create_pr(title="test-create-pr title", body="test-create-pr body")
assert pr.title == "test-create-pr title"
assert pr.body == "test-create-pr body"
assert pr.draft is False
assert pr.state == "open"
assert pr.head.ref == head
assert pr.base.ref == "main"


def test_codebase_create_pr_detached_head(codebase: Codebase):
codebase.checkout(commit=codebase._op.git_cli.head.commit) # move to detached head state
with pytest.raises(ValueError) as exc_info:
codebase.create_pr(title="test-create-pr title", body="test-create-pr body")
assert "Cannot make a PR from a detached HEAD" in str(exc_info.value)
4 changes: 2 additions & 2 deletions tests/integration/codegen/git/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from codegen.git.schemas.repo_config import RepoConfig


@pytest.fixture(autouse=True)
@pytest.fixture()
def mock_config():
"""Mock Config instance to prevent actual environment variable access during tests."""
mock_config = MagicMock()
Expand All @@ -14,7 +14,7 @@ def mock_config():
yield mock_config


@pytest.fixture(autouse=True)
@pytest.fixture()
def repo_config(tmpdir):
repo_config = RepoConfig(
name="Kevin-s-Adventure-Game",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,7 @@

@pytest.fixture
def op(repo_config, request):
op = RemoteRepoOperator(
repo_config,
shallow=request.param if hasattr(request, "param") else True,
bot_commit=False,
)
op = RemoteRepoOperator(repo_config, shallow=request.param if hasattr(request, "param") else True, bot_commit=False)
yield op


Expand Down
2 changes: 1 addition & 1 deletion tests/integration/codegen/runner/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
return port


@pytest.fixture(autouse=True)
@pytest.fixture()
def repo_config(tmpdir) -> Generator[RepoConfig, None, None]:
yield RepoConfig(
name="Kevin-s-Adventure-Game",
Expand All @@ -40,11 +40,11 @@

@pytest.fixture
def git_repo_client(repo_config: RepoConfig) -> Generator[GitRepoClient, None, None]:
yield GitRepoClient(repo_config=repo_config, access_token=config.secrets.github_token)

Check failure on line 43 in tests/integration/codegen/runner/conftest.py

View workflow job for this annotation

GitHub Actions / mypy

error: Argument "access_token" to "GitRepoClient" has incompatible type "str | None"; expected "str" [arg-type]


@pytest.fixture
def sandbox_client(repo_config: RepoConfig, get_free_port) -> Generator[SandboxClient, None, None]:
sb_client = SandboxClient(repo_config=repo_config, port=get_free_port, git_access_token=config.secrets.github_token)
sb_client.runner = Mock()

Check failure on line 49 in tests/integration/codegen/runner/conftest.py

View workflow job for this annotation

GitHub Actions / mypy

error: "SandboxClient" has no attribute "runner" [attr-defined]
yield sb_client