Skip to content

fix: remove secrets prefix #595

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 3 commits into from
Feb 21, 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
2 changes: 1 addition & 1 deletion .github/workflows/unit-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ jobs:
timeout-minutes: 5
env:
GITHUB_WORKSPACE: $GITHUB_WORKSPACE
SECRETS_GITHUB_TOKEN: ${{ secrets.GHA_PAT }}
GITHUB_TOKEN: ${{ secrets.GHA_PAT }}
run: |
uv run pytest \
-n auto \
Expand Down
2 changes: 1 addition & 1 deletion src/codegen/cli/auth/session.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
self.repo_path = repo_path
self.local_git = LocalGitRepo(repo_path=repo_path)
self.codegen_dir = repo_path / CODEGEN_DIR_NAME
self.config = UserConfig(env_filepath=self.codegen_dir / ENV_FILENAME)
self.config = UserConfig(env_filepath=repo_path / ENV_FILENAME)
self.config.secrets.github_token = git_token or self.config.secrets.github_token
self.existing = session_manager.get_session(repo_path) is not None

Expand Down Expand Up @@ -77,11 +77,11 @@

try:
if git_token is not None:
Github(login_or_token=git_token).get_repo(self.local_git.full_name)

Check failure on line 80 in src/codegen/cli/auth/session.py

View workflow job for this annotation

GitHub Actions / mypy

error: Argument 1 to "get_repo" of "Github" has incompatible type "str | None"; expected "int | str" [arg-type]
except BadCredentialsException:
rich.print(format_command(f"\n[bold red]Error:[/bold red] Invalid GitHub token={git_token} for repo={self.local_git.full_name}"))
rich.print("[white]Please provide a valid GitHub token for this repository.[/white]")
raise click.Abort()

def __str__(self) -> str:
return f"CodegenSession(user={self.config.repository.user_name}, repo={self.config.repository.repo_name})"

Check failure on line 87 in src/codegen/cli/auth/session.py

View workflow job for this annotation

GitHub Actions / mypy

error: "RepositoryConfig" has no attribute "repo_name" [attr-defined]
44 changes: 34 additions & 10 deletions src/codegen/cli/commands/config/main.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import logging
from itertools import groupby

import rich
import rich_click as click
from rich.table import Table

from codegen.shared.configs.constants import CODEGEN_DIR_NAME, ENV_FILENAME, GLOBAL_ENV_FILE
from codegen.shared.configs.constants import ENV_FILENAME, GLOBAL_ENV_FILE
from codegen.shared.configs.session_manager import session_manager
from codegen.shared.configs.user_config import UserConfig

Expand Down Expand Up @@ -43,14 +42,39 @@ def flatten_dict(data: dict, prefix: str = "") -> dict:
table.add_column("Key", style="cyan", no_wrap=True)
table.add_column("Value", style="magenta")

# Group by prefix (before underscore)
def get_prefix(item):
return item[0].split("_")[0]
# Group items by prefix
codebase_items = []
repository_items = []
other_items = []

for key, value in sorted_items:
prefix = key.split("_")[0].lower()
if prefix == "codebase":
codebase_items.append((key, value))
elif prefix == "repository":
repository_items.append((key, value))
else:
other_items.append((key, value))

# Add codebase section
if codebase_items:
table.add_section()
table.add_row("[bold yellow]Codebase[/bold yellow]", "")
for key, value in codebase_items:
table.add_row(f" {key}", str(value))

# Add repository section
if repository_items:
table.add_section()
table.add_row("[bold yellow]Repository[/bold yellow]", "")
for key, value in repository_items:
table.add_row(f" {key}", str(value))

for prefix, group in groupby(sorted_items, key=get_prefix):
# Add other section
if other_items:
table.add_section()
table.add_row(f"[bold yellow]{prefix.title()}[/bold yellow]", "")
for key, value in group:
table.add_row("[bold yellow]Other[/bold yellow]", "")
for key, value in other_items:
table.add_row(f" {key}", str(value))

rich.print(table)
Expand Down Expand Up @@ -91,13 +115,13 @@ def set_command(key: str, value: str, is_global: bool):
rich.print(f"[red]{e}[/red]")
return

rich.print(f"[green]Successfully set {key}=[magenta]{value}[/magenta] and saved to .env[/green]")
rich.print(f"[green]Successfully set {key}=[magenta]{value}[/magenta] and saved to {ENV_FILENAME}[/green]")


def _get_user_config(is_global: bool) -> UserConfig:
if is_global or (active_session_path := session_manager.get_active_session()) is None:
env_filepath = GLOBAL_ENV_FILE
else:
env_filepath = active_session_path / CODEGEN_DIR_NAME / ENV_FILENAME
env_filepath = active_session_path / ENV_FILENAME

return UserConfig(env_filepath)
1 change: 0 additions & 1 deletion src/codegen/cli/commands/init/render.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
def get_success_message(codegen_dir: Path, docs_dir: Path, examples_dir: Path) -> str:
"""Get the success message to display after initialization."""
return """📁 .codegen configuration folder created:
[dim].env[/dim] Project configuration
[dim]codemods/[/dim] Your codemod implementations
[dim].venv/[/dim] Python virtual environment (gitignored)
[dim]codegen-system-prompt.txt[/dim] AI system prompt (gitignored)"""
1 change: 0 additions & 1 deletion src/codegen/cli/mcp/resources/system_prompt.py
Original file line number Diff line number Diff line change
Expand Up @@ -524,7 +524,6 @@ def baz():
```bash
.codegen/
├── .venv/ # Python virtual environment (gitignored)
├── .env # Project configuration
├── codemods/ # Your codemod implementations
├── jupyter/ # Jupyter notebooks for exploration
└── codegen-system-prompt.txt # AI system prompt
Expand Down
1 change: 0 additions & 1 deletion src/codegen/cli/workspace/initialize_workspace.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@
CODEMODS_DIR.mkdir(parents=True, exist_ok=True)

# Initialize virtual environment
status_obj.update(f" {'Creating' if isinstance(status, str) else 'Checking'} virtual environment...")

Check failure on line 53 in src/codegen/cli/workspace/initialize_workspace.py

View workflow job for this annotation

GitHub Actions / mypy

error: Item "None" of "Status | None" has no attribute "update" [union-attr]
venv = VenvManager(session.codegen_dir)
if not venv.is_initialized():
venv.create_venv()
Expand All @@ -64,7 +64,7 @@
except Exception as e:
rich.print(f"[yellow]Warning: Could not download system prompt: {e}[/yellow]")

status_obj.update(f" {'Updating' if isinstance(status, Status) else status} .gitignore...")

Check failure on line 67 in src/codegen/cli/workspace/initialize_workspace.py

View workflow job for this annotation

GitHub Actions / mypy

error: Item "None" of "Status | None" has no attribute "update" [union-attr]
modify_gitignore(CODEGEN_FOLDER)

# Create notebook template
Expand All @@ -72,14 +72,14 @@

# Only fetch docs and examples if requested and session is provided
if fetch_docs and session:
status_obj.update("Fetching latest docs & examples...")

Check failure on line 75 in src/codegen/cli/workspace/initialize_workspace.py

View workflow job for this annotation

GitHub Actions / mypy

error: Item "None" of "Status | None" has no attribute "update" [union-attr]
shutil.rmtree(DOCS_FOLDER, ignore_errors=True)
shutil.rmtree(EXAMPLES_FOLDER, ignore_errors=True)

DOCS_FOLDER.mkdir(parents=True, exist_ok=True)
EXAMPLES_FOLDER.mkdir(parents=True, exist_ok=True)

response = RestAPI(get_current_token()).get_docs()

Check failure on line 82 in src/codegen/cli/workspace/initialize_workspace.py

View workflow job for this annotation

GitHub Actions / mypy

error: Incompatible types in assignment (expression has type "DocsResponse", variable has type "Response") [assignment]

Check failure on line 82 in src/codegen/cli/workspace/initialize_workspace.py

View workflow job for this annotation

GitHub Actions / mypy

error: Argument 1 to "RestAPI" has incompatible type "str | None"; expected "str" [arg-type]
populate_api_docs(DOCS_FOLDER, response.docs, status_obj)
populate_examples(session, EXAMPLES_FOLDER, response.examples, status_obj)

Expand All @@ -105,7 +105,6 @@
"prompts/",
"jupyter/",
".venv/", # Add venv to gitignore
".env",
"codegen-system-prompt.txt",
"",
"# Python cache files",
Expand Down
10 changes: 5 additions & 5 deletions src/codegen/shared/configs/models/base_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from pydantic_settings import BaseSettings, SettingsConfigDict

from codegen.shared.configs.constants import ENV_FILENAME, GLOBAL_ENV_FILE
from codegen.shared.configs.session_manager import session_codegen_dir
from codegen.shared.configs.session_manager import session_root


class BaseConfig(BaseSettings, ABC):
Expand All @@ -18,9 +18,9 @@ class BaseConfig(BaseSettings, ABC):

def __init__(self, prefix: str, env_filepath: Path | None = None, *args, **kwargs) -> None:
if env_filepath is None:
codegen_dir = session_codegen_dir
if codegen_dir is not None:
env_filepath = codegen_dir / ENV_FILENAME
root_path = session_root
if root_path is not None:
env_filepath = root_path / ENV_FILENAME

# Only include env files that exist
env_filepaths = []
Expand All @@ -29,7 +29,7 @@ def __init__(self, prefix: str, env_filepath: Path | None = None, *args, **kwarg
if env_filepath and env_filepath.exists() and env_filepath != GLOBAL_ENV_FILE:
env_filepaths.append(env_filepath)

self.model_config["env_prefix"] = f"{prefix.upper()}_"
self.model_config["env_prefix"] = f"{prefix.upper()}_" if len(prefix) > 0 else ""
self.model_config["env_file"] = env_filepaths

super().__init__(*args, **kwargs)
Expand Down
3 changes: 2 additions & 1 deletion src/codegen/shared/configs/models/secrets.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,12 @@
Falls back to .env file for missing values.
"""

def __init__(self, prefix: str = "SECRETS", *args, **kwargs) -> None:
def __init__(self, prefix: str = "", *args, **kwargs) -> None:
super().__init__(prefix=prefix, *args, **kwargs)

Check failure on line 12 in src/codegen/shared/configs/models/secrets.py

View workflow job for this annotation

GitHub Actions / mypy

error: "__init__" of "BaseConfig" gets multiple values for keyword argument "prefix" [misc]

github_token: str | None = None
openai_api_key: str | None = None
linear_api_key: str | None = None


DefaultSecrets = SecretsConfig()
10 changes: 5 additions & 5 deletions src/codegen/shared/configs/session_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
with open(SESSION_FILE, "w") as f:
json.dump(self.__dict__(), f)

def __dict__(self) -> dict:

Check failure on line 51 in src/codegen/shared/configs/session_manager.py

View workflow job for this annotation

GitHub Actions / mypy

error: Signature of "__dict__" incompatible with supertype "object" [override]
return {
"active_session_path": self.active_session_path,
"sessions": self.sessions,
Expand All @@ -58,14 +58,14 @@
active = self.active_session_path or "None"
sessions_str = "\n ".join(self.sessions) if self.sessions else "None"

return f"GlobalConfig:\n Active Session: {active}\n Sessions:\n {sessions_str}\n Global Session:\n {self.session_config}"

Check failure on line 61 in src/codegen/shared/configs/session_manager.py

View workflow job for this annotation

GitHub Actions / mypy

error: "SessionManager" has no attribute "session_config" [attr-defined]


def _get_codegen_dir() -> Path | None:
def _get_project_root() -> Path | None:
"""Get the active codegen directory."""
active_session = session_manager.get_active_session()
if active_session:
return active_session / CODEGEN_DIR_NAME
return active_session

try:
path = Path.cwd().resolve()
Expand All @@ -78,9 +78,9 @@
git_path = path / ".git"

if codegen_path.exists():
return codegen_path
return path
if git_path.exists():
return codegen_path
return path

parent = path.parent.resolve()
if parent == path: # We've reached the root directory
Expand All @@ -91,4 +91,4 @@


session_manager = SessionManager()
session_codegen_dir = _get_codegen_dir()
session_root = _get_project_root()
4 changes: 2 additions & 2 deletions src/codegen/shared/configs/user_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,10 @@ def set(self, full_key: str, value: str) -> None:
match f"{prefix}_":
case self.repository.env_prefix:
self.repository.set(self.env_filepath, key, value)
case self.secrets.env_prefix:
self.secrets.set(self.env_filepath, key, value)
case self.codebase.env_prefix:
self.codebase.set(self.env_filepath, key, value)
case _:
self.secrets.set(self.env_filepath, full_key, value)

def __str__(self) -> str:
"""Return a pretty-printed string representation of the config."""
Expand Down