11from pathlib import Path
22
3- from pygit2 .repository import Repository
3+ import click
4+ import rich
5+ from github import BadCredentialsException
6+ from github .MainClass import Github
47
58from codegen .cli .git .repo import get_git_repo
9+ from codegen .cli .rich .codeblocks import format_command
10+ from codegen .git .repo_operator .local_git_repo import LocalGitRepo
611from codegen .shared .configs .constants import CODEGEN_DIR_NAME , CONFIG_FILENAME
712from codegen .shared .configs .models .session import SessionConfig
813from codegen .shared .configs .session_configs import global_config , load_session_config
1116class CodegenSession :
1217 """Represents an authenticated codegen session with user and repository context"""
1318
14- repo_path : Path # TODO: rename to root_path
19+ repo_path : Path
20+ local_git : LocalGitRepo
1521 codegen_dir : Path
1622 config : SessionConfig
1723 existing : bool
1824
19- def __init__ (self , repo_path : Path ):
25+ def __init__ (self , repo_path : Path , git_token : str | None = None ) -> None :
26+ if not repo_path .exists () or get_git_repo (repo_path ) is None :
27+ rich .print (f"\n [bold red]Error:[/bold red] Path to git repo does not exist at { self .repo_path } " )
28+ raise click .Abort ()
29+
2030 self .repo_path = repo_path
31+ self .local_git = LocalGitRepo (repo_path = repo_path )
2132 self .codegen_dir = repo_path / CODEGEN_DIR_NAME
22- self .existing = global_config .get_session (repo_path ) is not None
2333 self .config = load_session_config (self .codegen_dir / CONFIG_FILENAME )
34+ self .config .secrets .github_token = git_token or self .config .secrets .github_token
35+ self .existing = global_config .get_session (repo_path ) is not None
36+
37+ self ._initialize ()
2438 global_config .set_active_session (repo_path )
2539
2640 @classmethod
@@ -31,19 +45,52 @@ def from_active_session(cls) -> "CodegenSession | None":
3145
3246 return cls (active_session )
3347
34- def is_valid (self ) -> bool :
35- """Validates that the session configuration is correct"""
36- # TODO: also make sure all the expected prompt, jupyter, codemods are present
37- # TODO: make sure there is still a git instance here.
38- return self .repo_path .exists () and self .codegen_dir .exists () and Path (self .config .file_path ).exists ()
39-
40- @property
41- def git_repo (self ) -> Repository :
42- git_repo = get_git_repo (Path .cwd ())
43- if not git_repo :
44- msg = "No git repository found"
45- raise ValueError (msg )
46- return git_repo
48+ def _initialize (self ) -> None :
49+ """Initialize the codegen session"""
50+ self ._validate ()
51+
52+ self .config .repository .repo_path = self .config .repository .repo_path or str (self .local_git .repo_path )
53+ self .config .repository .repo_name = self .config .repository .repo_name or self .local_git .name
54+ self .config .repository .full_name = self .config .repository .full_name or self .local_git .full_name
55+ self .config .repository .user_name = self .config .repository .user_name or self .local_git .user_name
56+ self .config .repository .user_email = self .config .repository .user_email or self .local_git .user_email
57+ self .config .repository .language = self .config .repository .language or self .local_git .get_language (access_token = self .config .secrets .github_token ).upper ()
58+ self .config .save ()
59+
60+ def _validate (self ) -> None :
61+ """Validates that the session configuration is correct, otherwise raises an error"""
62+ if not self .codegen_dir .exists ():
63+ rich .print (f"\n [bold red]Error:[/bold red] Codegen folder is missing at { self .codegen_dir } " )
64+ raise click .Abort ()
65+
66+ if not Path (self .config .file_path ).exists ():
67+ rich .print (f"\n [bold red]Error:[/bold red] Missing config.toml at { self .codegen_dir } " )
68+ rich .print ("[white]Please remove the codegen folder and reinitialize.[/white]" )
69+ rich .print (format_command (f"rm -rf { self .codegen_dir } && codegen init" ))
70+ raise click .Abort ()
71+
72+ git_token = self .config .secrets .github_token
73+ if git_token is None :
74+ rich .print ("\n [bold yellow]Warning:[/bold yellow] GitHub token not found" )
75+ rich .print ("To enable full functionality, please set your GitHub token:" )
76+ rich .print (format_command ("export CODEGEN_SECRETS__GITHUB_TOKEN=<your-token>" ))
77+ rich .print ("Or pass in as a parameter:" )
78+ rich .print (format_command ("codegen init --token <your-token>" ))
79+ raise click .Abort ()
80+
81+ if self .local_git .origin_remote is None :
82+ rich .print ("\n [bold red]Error:[/bold red] No remote found for repository" )
83+ rich .print ("[white]Please add a remote to the repository.[/white]" )
84+ rich .print ("\n [dim]To add a remote to the repository:[/dim]" )
85+ rich .print (format_command ("git remote add origin <your-repo-url>" ))
86+ raise click .Abort ()
87+
88+ try :
89+ Github (login_or_token = git_token ).get_repo (self .local_git .full_name )
90+ except BadCredentialsException :
91+ rich .print (format_command (f"\n [bold red]Error:[/bold red] Invalid GitHub token={ git_token } for repo={ self .local_git .full_name } " ))
92+ rich .print ("[white]Please provide a valid GitHub token for this repository.[/white]" )
93+ raise click .Abort ()
4794
4895 def __str__ (self ) -> str :
4996 return f"CodegenSession(user={ self .config .repository .user_name } , repo={ self .config .repository .repo_name } )"
0 commit comments