Skip to content
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
17 changes: 16 additions & 1 deletion src/codegen/extensions/tools/github/create_pr.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import uuid
from typing import Any

from github import GithubException

from codegen import Codebase


Expand All @@ -18,12 +20,25 @@
Dict containing PR info, or error information if operation fails
"""
try:
# Check for uncommitted changes and commit them
if len(codebase.get_diff()) == 0:
return {"error": "No changes to create a PR."}

Check warning on line 25 in src/codegen/extensions/tools/github/create_pr.py

View check run for this annotation

Codecov / codecov/patch

src/codegen/extensions/tools/github/create_pr.py#L24-L25

Added lines #L24 - L25 were not covered by tests

# TODO: this is very jank. We should ideally check out the branch before
# making the changes, but it looks like `codebase.checkout` blows away
# all of your changes
codebase.git_commit(".")

Check warning on line 30 in src/codegen/extensions/tools/github/create_pr.py

View check run for this annotation

Codecov / codecov/patch

src/codegen/extensions/tools/github/create_pr.py#L30

Added line #L30 was not covered by tests

# If on default branch, create a new branch
if codebase._op.git_cli.active_branch.name == codebase._op.default_branch:
codebase.checkout(branch=f"{uuid.uuid4()}", create_if_missing=True)

# Create the PR
pr = codebase.create_pr(title=title, body=body)
try:
pr = codebase.create_pr(title=title, body=body)
except GithubException as e:
print(e)
return {"error": "Failed to create PR. Check if the PR already exists."}

Check warning on line 41 in src/codegen/extensions/tools/github/create_pr.py

View check run for this annotation

Codecov / codecov/patch

src/codegen/extensions/tools/github/create_pr.py#L37-L41

Added lines #L37 - L41 were not covered by tests
return {
"status": "success",
"url": pr.html_url,
Expand Down
4 changes: 3 additions & 1 deletion src/codegen/sdk/core/codebase.py
Original file line number Diff line number Diff line change
Expand Up @@ -180,13 +180,13 @@
main_project = ProjectConfig.from_path(repo_path, programming_language=ProgrammingLanguage(language.upper()) if language else None)
projects = [main_project]
else:
main_project = projects[0]

Check failure on line 183 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, progress=progress)

Check failure on line 189 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 @@ -202,7 +202,7 @@
yield "nodes", len(self.ctx.nodes)
yield "edges", len(self.ctx.edges)

__rich_repr__.angular = ANGULAR_STYLE

Check failure on line 205 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 @@ -244,8 +244,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 247 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 248 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 @@ -254,7 +254,7 @@
def files(self, *, extensions: Literal["*"]) -> list[File]: ...
@overload
def files(self, *, extensions: None = ...) -> list[TSourceFile]: ...
@proxy_property

Check failure on line 257 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 @@ -284,15 +284,15 @@
return sort_editables(files, alphabetical=True, dedupe=False)

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

Check failure on line 287 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 293 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 295 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 @@ -304,7 +304,7 @@
Returns:
list[TDirectory]: A list of Directory objects in the codebase.
"""
return list(self.ctx.directories.values())

Check failure on line 307 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 @@ -740,7 +740,7 @@
####################################################################################################################

def git_commit(self, message: str, *, verify: bool = False) -> GitCommit | None:
"""Commits all staged changes to the codebase and git.
"""Stages + commits all changes to the codebase and git.

Args:
message (str): The commit message
Expand All @@ -753,6 +753,8 @@
if self._op.stage_and_commit_all_changes(message, verify):
logger.info(f"Commited repository to {self._op.head_commit} on {self._op.get_active_branch_or_commit()}")
return self._op.head_commit
else:
logger.info("No changes to commit")

Check warning on line 757 in src/codegen/sdk/core/codebase.py

View check run for this annotation

Codecov / codecov/patch

src/codegen/sdk/core/codebase.py#L757

Added line #L757 was not covered by tests
return None

def commit(self, sync_graph: bool = True) -> None:
Expand Down