-
Notifications
You must be signed in to change notification settings - Fork 52
feat: [CG-10650] codebase.codeowners interface #290
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
clee-codegen
merged 19 commits into
develop
from
clee-cg-10650-for-file-in-codebasecodeowners0
Feb 11, 2025
Merged
Changes from 8 commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
39ef96a
wip: initial implementation of a codeowners interface
clee-codegen d6e2a27
Automated pre-commit update
clee-codegen 7e546c7
add: symbols property to codeowner
clee-codegen 2cf9a36
add: fuller directory API refactor
clee-codegen 1a0925f
Merge remote-tracking branch 'origin/develop' into clee-cg-10650-for-…
clee-codegen 9107c34
fix: move remove to directory specific impl.
clee-codegen 3bf3053
add: unit tests for directory/interface/codeowner
clee-codegen 0ac6601
fix: move cache utils and add unit tests
clee-codegen 1e4ea6a
fix: rename fileinterface to hassymbols and add noapidoc
clee-codegen 6375e19
add: files property test
clee-codegen 28ffbb5
fix: implement uncache system integrated lru_cache
clee-codegen 30f0cce
fix: move py_noapidoc in dec stack
clee-codegen 6e7d197
Automated pre-commit update
clee-codegen fe659e8
fix: use noapidoc instead of py_noapidoc
clee-codegen ceaba3f
Automated pre-commit update
clee-codegen fa302eb
add: docstring codeowner.files
clee-codegen 34756ad
Automated pre-commit update
clee-codegen d55d9e6
add: codeowner.name docstring
clee-codegen 790b457
Merge branch 'develop' into clee-cg-10650-for-file-in-codebasecodeown…
clee-codegen File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
import logging | ||
from collections.abc import Iterable, Iterator | ||
from typing import Callable, Generic, Literal | ||
|
||
from codeowners import CodeOwners as CodeOwnersParser | ||
|
||
from codegen.sdk.core.interfaces.files_interface import FilesInterface, FilesParam, TClass, TFile, TFunction, TGlobalVar, TImport, TImportStatement, TSymbol | ||
from codegen.sdk.core.utils.cache_utils import cached_generator | ||
from codegen.shared.decorators.docs import apidoc | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
@apidoc | ||
class CodeOwner(FilesInterface[TFile, TSymbol, TImportStatement, TGlobalVar, TClass, TFunction, TImport], Generic[TFile, TSymbol, TImportStatement, TGlobalVar, TClass, TFunction, TImport]): | ||
"""CodeOwner is a class that represents a code owner in a codebase. | ||
|
||
It is used to iterate over all files that are owned by a specific owner. | ||
|
||
Attributes: | ||
owner_type: The type of the owner (USERNAME, TEAM, EMAIL). | ||
owner_value: The value of the owner. | ||
files_source: A callable that returns an iterable of all files in the codebase. | ||
""" | ||
|
||
owner_type: Literal["USERNAME", "TEAM", "EMAIL"] | ||
owner_value: str | ||
files_source: Callable[FilesParam, Iterable[TFile]] | ||
|
||
def __init__(self, files_source: Callable[FilesParam, Iterable[TFile]], owner_type: Literal["USERNAME", "TEAM", "EMAIL"], owner_value: str): | ||
self.owner_type = owner_type | ||
self.owner_value = owner_value | ||
self.files_source = files_source | ||
self.files = self.files_generator | ||
Check failure on line 34 in src/codegen/sdk/core/codeowner.py
|
||
|
||
@classmethod | ||
def from_parser(cls, parser: CodeOwnersParser, file_source: Callable[FilesParam, Iterable[TFile]]) -> list["CodeOwner"]: | ||
"""Create a list of CodeOwner objects from a CodeOwnersParser. | ||
|
||
Args: | ||
parser (CodeOwnersParser): The CodeOwnersParser to use. | ||
file_source (Callable[FilesParam, Iterable[TFile]]): A callable that returns an iterable of all files in the codebase. | ||
|
||
Returns: | ||
list[CodeOwner]: A list of CodeOwner objects. | ||
""" | ||
codeowners = [] | ||
for _, _, owners, _, _ in parser.paths: | ||
for owner_label, owner_value in owners: | ||
codeowners.append(CodeOwner(file_source, owner_label, owner_value)) | ||
return codeowners | ||
|
||
@cached_generator(maxsize=16) | ||
Check failure on line 53 in src/codegen/sdk/core/codeowner.py
|
||
def files_generator(self, *args: FilesParam.args, **kwargs: FilesParam.kwargs) -> Iterable[TFile]: | ||
for source_file in self.files_source(*args, **kwargs): | ||
# Filter files by owner value | ||
if self.owner_value in source_file.owners: | ||
yield source_file | ||
|
||
@property | ||
def name(self) -> str: | ||
return self.owner_value | ||
|
||
def __iter__(self) -> Iterator[TFile]: | ||
return iter(self.files_generator()) | ||
|
||
def __repr__(self) -> str: | ||
return f"CodeOwner(owner_type={self.owner_type}, owner_value={self.owner_value})" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
import logging | ||
from collections.abc import Iterator | ||
from itertools import chain | ||
from typing import TYPE_CHECKING, Generic, ParamSpec, TypeVar | ||
|
||
from codegen.sdk.core.utils.cache_utils import cached_generator | ||
from codegen.shared.decorators.docs import py_noapidoc | ||
|
||
if TYPE_CHECKING: | ||
from codegen.sdk.core.assignment import Assignment | ||
from codegen.sdk.core.class_definition import Class | ||
from codegen.sdk.core.file import SourceFile | ||
from codegen.sdk.core.function import Function | ||
from codegen.sdk.core.import_resolution import Import, ImportStatement | ||
from codegen.sdk.core.symbol import Symbol | ||
from codegen.sdk.typescript.class_definition import TSClass | ||
from codegen.sdk.typescript.export import TSExport | ||
from codegen.sdk.typescript.file import TSFile | ||
from codegen.sdk.typescript.function import TSFunction | ||
from codegen.sdk.typescript.import_resolution import TSImport | ||
from codegen.sdk.typescript.statements.import_statement import TSImportStatement | ||
from codegen.sdk.typescript.symbol import TSSymbol | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
TFile = TypeVar("TFile", bound="SourceFile") | ||
TSymbol = TypeVar("TSymbol", bound="Symbol") | ||
TImportStatement = TypeVar("TImportStatement", bound="ImportStatement") | ||
TGlobalVar = TypeVar("TGlobalVar", bound="Assignment") | ||
TClass = TypeVar("TClass", bound="Class") | ||
TFunction = TypeVar("TFunction", bound="Function") | ||
TImport = TypeVar("TImport", bound="Import") | ||
FilesParam = ParamSpec("FilesParam") | ||
|
||
TSGlobalVar = TypeVar("TSGlobalVar", bound="Assignment") | ||
|
||
|
||
class FilesInterface(Generic[TFile, TSymbol, TImportStatement, TGlobalVar, TClass, TFunction, TImport]): | ||
clee-codegen marked this conversation as resolved.
Show resolved
Hide resolved
|
||
"""Abstract interface for files in a codebase. | ||
|
||
Abstract interface for files in a codebase. | ||
""" | ||
|
||
@cached_generator() | ||
def files_generator(self, *args: FilesParam.args, **kwargs: FilesParam.kwargs) -> Iterator[TFile]: | ||
clee-codegen marked this conversation as resolved.
Show resolved
Hide resolved
|
||
msg = "This method should be implemented by the subclass" | ||
raise NotImplementedError(msg) | ||
|
||
@property | ||
def symbols(self) -> list[TSymbol]: | ||
"""Get a recursive list of all symbols in files container.""" | ||
return list(chain.from_iterable(f.symbols for f in self.files_generator())) | ||
|
||
@property | ||
def import_statements(self) -> list[TImportStatement]: | ||
"""Get a recursive list of all import statements in files container.""" | ||
return list(chain.from_iterable(f.import_statements for f in self.files_generator())) | ||
|
||
@property | ||
def global_vars(self) -> list[TGlobalVar]: | ||
"""Get a recursive list of all global variables in files container.""" | ||
return list(chain.from_iterable(f.global_vars for f in self.files_generator())) | ||
|
||
@property | ||
def classes(self) -> list[TClass]: | ||
"""Get a recursive list of all classes in files container.""" | ||
return list(chain.from_iterable(f.classes for f in self.files_generator())) | ||
|
||
@property | ||
def functions(self) -> list[TFunction]: | ||
"""Get a recursive list of all functions in files container.""" | ||
return list(chain.from_iterable(f.functions for f in self.files_generator())) | ||
|
||
@property | ||
@py_noapidoc | ||
def exports(self) -> "list[TSExport]": | ||
"""Get a recursive list of all exports in files container.""" | ||
return list(chain.from_iterable(f.exports for f in self.files_generator())) | ||
|
||
@property | ||
def imports(self) -> list[TImport]: | ||
"""Get a recursive list of all imports in files container.""" | ||
return list(chain.from_iterable(f.imports for f in self.files_generator())) | ||
|
||
def get_symbol(self, name: str) -> TSymbol | None: | ||
"""Get a symbol by name in files container.""" | ||
return next((s for s in self.symbols if s.name == name), None) | ||
|
||
def get_import_statement(self, name: str) -> TImportStatement | None: | ||
"""Get an import statement by name in files container.""" | ||
return next((s for s in self.import_statements if s.name == name), None) | ||
|
||
def get_global_var(self, name: str) -> TGlobalVar | None: | ||
"""Get a global variable by name in files container.""" | ||
return next((s for s in self.global_vars if s.name == name), None) | ||
|
||
def get_class(self, name: str) -> TClass | None: | ||
"""Get a class by name in files container.""" | ||
return next((s for s in self.classes if s.name == name), None) | ||
|
||
def get_function(self, name: str) -> TFunction | None: | ||
"""Get a function by name in files container.""" | ||
return next((s for s in self.functions if s.name == name), None) | ||
|
||
@py_noapidoc | ||
def get_export(self: "FilesInterface[TSFile, TSSymbol, TSImportStatement, TSGlobalVar, TSClass, TSFunction, TSImport]", name: str) -> "TSExport | None": | ||
"""Get an export by name in files container (supports only typescript).""" | ||
return next((s for s in self.exports if s.name == name), None) | ||
|
||
def get_import(self, name: str) -> TImport | None: | ||
"""Get an import by name in files container.""" | ||
return next((s for s in self.imports if s.name == name), None) |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.