Skip to content

chore: [CG-10794] default to Github token if available #457

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 4 commits into from
Feb 13, 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: 2 additions & 1 deletion src/codegen/git/repo_operator/repo_operator.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
from codegen.git.schemas.enums import CheckoutResult, FetchResult
from codegen.git.schemas.repo_config import RepoConfig
from codegen.git.utils.remote_progress import CustomRemoteProgress
from codegen.shared.configs.session_configs import config
from codegen.shared.performance.stopwatch_utils import stopwatch
from codegen.shared.performance.time_utils import humanize_duration

Expand Down Expand Up @@ -46,7 +47,7 @@
) -> None:
assert repo_config is not None
self.repo_config = repo_config
self.access_token = access_token
self.access_token = access_token or config.secrets.github_token
self.base_dir = repo_config.base_dir
self.bot_commit = bot_commit

Expand All @@ -65,7 +66,7 @@
@property
def remote_git_repo(self) -> GitRepoClient:
if not self._remote_git_repo:
self._remote_git_repo = GitRepoClient(self.repo_config, access_token=self.access_token)

Check failure on line 69 in src/codegen/git/repo_operator/repo_operator.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]
return self._remote_git_repo

@property
Expand Down Expand Up @@ -111,7 +112,7 @@
email_level = None
levels = ["system", "global", "user", "repository"]
for level in levels:
with git_cli.config_reader(level) as reader:

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

View workflow job for this annotation

GitHub Actions / mypy

error: Argument 1 to "config_reader" of "Repo" has incompatible type "str"; expected "Literal['system', 'global', 'user', 'repository'] | None" [arg-type]
if reader.has_option("user", "name") and not username:
username = reader.get("user", "name")
user_level = level
Expand Down Expand Up @@ -472,7 +473,7 @@
return content
except UnicodeDecodeError:
print(f"Warning: Unable to decode file {file_path}. Skipping.")
return None

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

View workflow job for this annotation

GitHub Actions / mypy

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

def write_file(self, relpath: str, content: str) -> None:
"""Writes file content to disk"""
Expand Down Expand Up @@ -517,7 +518,7 @@

# Iterate through files and yield contents
for rel_filepath in filepaths:
rel_filepath: str

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

View workflow job for this annotation

GitHub Actions / mypy

error: Name "rel_filepath" already defined on line 520 [no-redef]
filepath = os.path.join(self.repo_path, rel_filepath)

# Filter by subdirectory (includes full filenames)
Expand Down Expand Up @@ -548,7 +549,7 @@
list_files = []

for rel_filepath in self.git_cli.git.ls_files().split("\n"):
rel_filepath: str

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

View workflow job for this annotation

GitHub Actions / mypy

error: Name "rel_filepath" already defined on line 551 [no-redef]
if subdirs and not any(d in rel_filepath for d in subdirs):
continue
if extensions is None or any(rel_filepath.endswith(e) for e in extensions):
Expand All @@ -572,7 +573,7 @@

def get_modified_files_in_last_n_days(self, days: int = 1) -> tuple[list[str], list[str]]:
"""Returns a list of files modified and deleted in the last n days"""
modified_files = []

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

View workflow job for this annotation

GitHub Actions / mypy

error: Need type annotation for "modified_files" (hint: "modified_files: list[<type>] = ...") [var-annotated]
deleted_files = []
allowed_extensions = [".py"]

Expand All @@ -588,9 +589,9 @@
if file in modified_files:
modified_files.remove(file)
else:
if file not in modified_files and file[-3:] in allowed_extensions:

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

View workflow job for this annotation

GitHub Actions / mypy

error: Value of type "str | PathLike[str]" is not indexable [index]
modified_files.append(file)
return modified_files, deleted_files

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

View workflow job for this annotation

GitHub Actions / mypy

error: Incompatible return value type (got "tuple[list[str | PathLike[str]], list[str | PathLike[str]]]", expected "tuple[list[str], list[str]]") [return-value]

@abstractmethod
def base_url(self) -> str | None: ...
Expand All @@ -607,7 +608,7 @@

def get_pr_data(self, pr_number: int) -> dict:
"""Returns the data associated with a PR"""
return self.remote_git_repo.get_pr_data(pr_number)

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

View workflow job for this annotation

GitHub Actions / mypy

error: "GitRepoClient" has no attribute "get_pr_data" [attr-defined]

def create_pr_comment(self, pr_number: int, body: str) -> None:
"""Create a general comment on a pull request.
Expand Down Expand Up @@ -650,7 +651,7 @@
body=body,
commit=commit,
path=path,
line=line,

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

View workflow job for this annotation

GitHub Actions / mypy

error: Argument "line" to "create_review_comment" of "GitRepoClient" has incompatible type "int | None"; expected "int | _NotSetType" [arg-type]
side=side,
start_line=start_line,
)
19 changes: 18 additions & 1 deletion src/codegen/sdk/core/codebase.py
Original file line number Diff line number Diff line change
Expand Up @@ -898,7 +898,24 @@ def restore_stashed_changes(self):
####################################################################################################################

def create_pr(self, title: str, body: str) -> PullRequest:
"""Creates a PR from the current branch."""
"""Creates a pull request from the current branch to the repository's default branch.

This method will:
1. Stage and commit any pending changes with the PR title as the commit message
2. Push the current branch to the remote repository
3. Create a pull request targeting the default branch

Args:
title (str): The title for the pull request
body (str): The description/body text for the pull request

Returns:
PullRequest: The created GitHub pull request object

Raises:
ValueError: If attempting to create a PR while in a detached HEAD state
ValueError: If the current branch is the default branch
"""
if self._op.git_cli.head.is_detached:
msg = "Cannot make a PR from a detached HEAD"
raise ValueError(msg)
Expand Down
2 changes: 1 addition & 1 deletion tests/integration/codegen/git/codebase/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ def repo_config(tmpdir):
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, access_token=config.secrets.github_token)
op = LocalRepoOperator(repo_config=repo_config)
yield op


Expand Down
Loading