Skip to content

Commit

Permalink
Merge branch 'develop' into changelong-generation
Browse files Browse the repository at this point in the history
  • Loading branch information
jemeza-codegen committed Feb 4, 2025
2 parents f57c00c + 061eec9 commit 68c932d
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 12 deletions.
14 changes: 7 additions & 7 deletions src/codegen/git/clients/git_repo_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,25 +26,25 @@
class GitRepoClient:
"""Wrapper around PyGithub's Remote Repository."""

repo: RepoConfig
repo_config: RepoConfig
github_type: GithubType = GithubType.GithubEnterprise
gh_client: GithubClientType
read_client: Repository
access_scope: GithubScope
__write_client: Repository | None # Will not be initialized if access scope is read-only

def __init__(self, repo_config: RepoConfig, github_type: GithubType = GithubType.GithubEnterprise, access_scope: GithubScope = GithubScope.READ) -> None:
self.repo = repo_config
self.repo_config = repo_config
self.github_type = github_type
self.gh_client = GithubClientFactory.create_from_repo(self.repo, github_type)
self.gh_client = GithubClientFactory.create_from_repo(self.repo_config, github_type)
self.read_client = self._create_client(GithubScope.READ)
self.__write_client = self._create_client(GithubScope.WRITE) if access_scope == GithubScope.WRITE else None
self.access_scope = access_scope

def _create_client(self, github_scope: GithubScope = GithubScope.READ) -> Repository:
client = self.gh_client.get_repo_by_full_name(self.repo.full_name, github_scope=github_scope)
client = self.gh_client.get_repo_by_full_name(self.repo_config.full_name, github_scope=github_scope)
if not client:
msg = f"Repo {self.repo.full_name} not found in {self.github_type.value}!"
msg = f"Repo {self.repo_config.full_name} not found in {self.github_type.value}!"
raise ValueError(msg)
return client

Expand All @@ -61,7 +61,7 @@ def _write_client(self) -> Repository:

@property
def id(self) -> int:
return self.repo.id
return self.repo_config.id

@property
def default_branch(self) -> str:
Expand Down Expand Up @@ -160,7 +160,7 @@ def get_pull_by_branch_and_state(
if not base_branch_name:
base_branch_name = self.default_branch

head_branch_name = f"{self.repo.organization_name}:{head_branch_name}"
head_branch_name = f"{self.repo_config.organization_name}:{head_branch_name}"

# retrieve all pulls ordered by created descending
prs = self.read_client.get_pulls(base=base_branch_name, head=head_branch_name, state=state, sort="created", direction="desc")
Expand Down
6 changes: 3 additions & 3 deletions src/codegen/git/clients/github_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ class GithubClient:
@classmethod
def from_repo_config(cls, repo_config: RepoConfig) -> Self:
gh_wrapper = cls()
gh_wrapper.read_client = gh_wrapper._create_client_for_repo(repo_config, github_scope=GithubScope.READ)
gh_wrapper._write_client = gh_wrapper._create_client_for_repo(repo_config, github_scope=GithubScope.WRITE)
gh_wrapper.read_client = gh_wrapper._create_client_for_repo_config(repo_config, github_scope=GithubScope.READ)
gh_wrapper._write_client = gh_wrapper._create_client_for_repo_config(repo_config, github_scope=GithubScope.WRITE)
return gh_wrapper

@classmethod
Expand All @@ -37,7 +37,7 @@ def from_token(cls, token: str | None = None) -> Self:
gh_wrapper._write_client = Github(token, base_url=cls.base_url)
return gh_wrapper

def _create_client_for_repo(self, repo_config: RepoConfig, github_scope: GithubScope = GithubScope.READ) -> Github:
def _create_client_for_repo_config(self, repo_config: RepoConfig, github_scope: GithubScope = GithubScope.READ) -> Github:
token = get_token_for_repo_config(repo_config=repo_config, github_type=self.type, github_scope=github_scope)
return Github(token, base_url=self.base_url)

Expand Down
4 changes: 2 additions & 2 deletions src/codegen/git/utils/codeowner_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,14 @@ def create_codeowners_parser_for_repo(py_github_repo: GitRepoClient) -> CodeOwne
return codeowners
except Exception as e:
continue
logger.info(f"Failed to create CODEOWNERS parser for repo: {py_github_repo.repo.id}. Returning None.")
logger.info(f"Failed to create CODEOWNERS parser for repo: {py_github_repo.repo_config.id}. Returning None.")
return None


def get_codeowners_for_pull(repo: GitRepoClient, pull: PullRequest) -> list[str]:
codeowners_parser = create_codeowners_parser_for_repo(repo)
if not codeowners_parser:
logger.warning(f"Failed to create codeowners parser for repo: {repo.repo.id}. Returning empty list.")
logger.warning(f"Failed to create codeowners parser for repo: {repo.repo_config.id}. Returning empty list.")
return []
codeowners_for_pull_set = set()
pull_files = pull.get_files()
Expand Down
29 changes: 29 additions & 0 deletions src/codegen/runner/sandbox/ephemeral_server.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import logging
import tempfile

from fastapi import FastAPI

from codegen.runner.models.apis import (
RUN_ON_STRING_ENDPOINT,
GetRunOnStringRequest,
GetRunOnStringResult,
)
from codegen.runner.sandbox.executor import SandboxExecutor
from codegen.sdk.codebase.factory.get_session import get_codebase_session
from codegen.sdk.enums import ProgrammingLanguage
from codegen.shared.compilation.string_to_code import create_execute_function_from_codeblock

logger = logging.getLogger(__name__)
app = FastAPI()


@app.post(RUN_ON_STRING_ENDPOINT)
async def run_on_string(request: GetRunOnStringRequest) -> GetRunOnStringResult:
logger.info(f"====[ run_on_string ]====\n> Codemod source: {request.codemod_source}\n> Input: {request.files}\n> Language: {request.language}\n")
language = ProgrammingLanguage(request.language.upper())
with get_codebase_session(tmpdir=tempfile.mkdtemp(), files=request.files, programming_language=language) as codebase:
executor = SandboxExecutor(codebase)
code_to_exec = create_execute_function_from_codeblock(codeblock=request.codemod_source)
result = await executor.execute(code_to_exec)
logger.info(f"Result: {result}")
return GetRunOnStringResult(result=result)

0 comments on commit 68c932d

Please sign in to comment.