refactor: extract focused clients from GiteaClient (Slices 1-6)
- Create gitea/issues_client.py with IssuesClient class (9 methods) - Create gitea/prs_client.py with PullRequestsClient class (17 methods) - Create gitea/files_client.py with FilesClient class (4 methods) - Create gitea/notifications_client.py with NotificationsClient class (2 methods) - Create gitea/repos_client.py with ReposClient class (2 methods) - Create gitea/__init__.py to export all client classes - Remove delegation methods from GiteaClient (now ~70 lines) - Update all callers to use sub-clients (client.issues, client.prs, etc.) - Update test files to mock sub-client attributes GiteaClient is now a facade that provides access to focused sub-clients: - repos: Repository operations (ReposClient) - issues: Issue operations (IssuesClient) - prs: Pull request operations (PullRequestsClient) - files: File and git ref operations (FilesClient) - notifications: Notification operations (NotificationsClient) Refs: #godclass-refactor
This commit is contained in:
+64
-21
@@ -21,7 +21,7 @@ class WorkspaceManager:
|
||||
def _configure_repo_user(self, repo_path: Path) -> None:
|
||||
try:
|
||||
client = GiteaClient()
|
||||
user = client.get_authenticated_user()
|
||||
user = client.repos.get_authenticated_user()
|
||||
if not user or not user.login:
|
||||
raise RuntimeError("No authenticated user found.")
|
||||
username: str = user.login
|
||||
@@ -32,19 +32,29 @@ class WorkspaceManager:
|
||||
|
||||
# Configure extraHeader locally for the repo
|
||||
subprocess.run(
|
||||
["git", "-C", str(repo_path), "config", "http.extraHeader", f"Authorization: Basic {auth_b64}"],
|
||||
check=True, capture_output=True
|
||||
[
|
||||
"git",
|
||||
"-C",
|
||||
str(repo_path),
|
||||
"config",
|
||||
"http.extraHeader",
|
||||
f"Authorization: Basic {auth_b64}",
|
||||
],
|
||||
check=True,
|
||||
capture_output=True,
|
||||
)
|
||||
|
||||
name: str = user.full_name or user.login
|
||||
email: str = user.email or f"{user.login}@noreply.gitea"
|
||||
subprocess.run(
|
||||
["git", "-C", str(repo_path), "config", "user.name", name],
|
||||
check=True, capture_output=True
|
||||
check=True,
|
||||
capture_output=True,
|
||||
)
|
||||
subprocess.run(
|
||||
["git", "-C", str(repo_path), "config", "user.email", email],
|
||||
check=True, capture_output=True
|
||||
check=True,
|
||||
capture_output=True,
|
||||
)
|
||||
except Exception as e:
|
||||
logger.error(f"Error configuring local git user: {e}")
|
||||
@@ -64,59 +74,84 @@ class WorkspaceManager:
|
||||
auth_url = self._get_authenticated_url(repo_full_name)
|
||||
subprocess.run(
|
||||
["git", "-C", str(repo_path), "remote", "set-url", "origin", auth_url],
|
||||
check=True, capture_output=True,
|
||||
check=True,
|
||||
capture_output=True,
|
||||
)
|
||||
self._configure_repo_user(repo_path)
|
||||
|
||||
# Check for any uncommitted changes or untracked files
|
||||
status_res = subprocess.run(
|
||||
["git", "-C", str(repo_path), "status", "--porcelain"],
|
||||
check=True, capture_output=True, text=True
|
||||
check=True,
|
||||
capture_output=True,
|
||||
text=True,
|
||||
)
|
||||
if status_res.stdout.strip():
|
||||
logger.info(f"Uncommitted changes detected in {repo_path}. Stashing before sanitization.")
|
||||
logger.info(
|
||||
f"Uncommitted changes detected in {repo_path}. Stashing before sanitization."
|
||||
)
|
||||
subprocess.run(
|
||||
["git", "-C", str(repo_path), "stash", "push", "-u", "-m", "Auto-backup before agent sanitization"],
|
||||
check=True, capture_output=True
|
||||
[
|
||||
"git",
|
||||
"-C",
|
||||
str(repo_path),
|
||||
"stash",
|
||||
"push",
|
||||
"-u",
|
||||
"-m",
|
||||
"Auto-backup before agent sanitization",
|
||||
],
|
||||
check=True,
|
||||
capture_output=True,
|
||||
)
|
||||
|
||||
subprocess.run(
|
||||
["git", "-C", str(repo_path), "reset", "--hard", "HEAD"],
|
||||
check=True, capture_output=True,
|
||||
check=True,
|
||||
capture_output=True,
|
||||
)
|
||||
subprocess.run(
|
||||
["git", "-C", str(repo_path), "clean", "-fdx"],
|
||||
check=True, capture_output=True,
|
||||
check=True,
|
||||
capture_output=True,
|
||||
)
|
||||
try:
|
||||
subprocess.run(
|
||||
["git", "-C", str(repo_path), "checkout", "main"],
|
||||
check=True, capture_output=True,
|
||||
check=True,
|
||||
capture_output=True,
|
||||
)
|
||||
except subprocess.CalledProcessError:
|
||||
subprocess.run(
|
||||
["git", "-C", str(repo_path), "checkout", "master"],
|
||||
check=True, capture_output=True,
|
||||
check=True,
|
||||
capture_output=True,
|
||||
)
|
||||
try:
|
||||
subprocess.run(
|
||||
["git", "-C", str(repo_path), "pull", "origin", "main"],
|
||||
check=True, capture_output=True,
|
||||
check=True,
|
||||
capture_output=True,
|
||||
)
|
||||
except subprocess.CalledProcessError:
|
||||
subprocess.run(
|
||||
["git", "-C", str(repo_path), "pull", "origin", "master"],
|
||||
check=True, capture_output=True,
|
||||
check=True,
|
||||
capture_output=True,
|
||||
)
|
||||
except Exception as e:
|
||||
logger.error(f"Error during sanitization: {e}", exc_info=True)
|
||||
raise RuntimeError(f"Failed to sanitize repository {repo_full_name} at {repo_path}: {e}") from e
|
||||
raise RuntimeError(
|
||||
f"Failed to sanitize repository {repo_full_name} at {repo_path}: {e}"
|
||||
) from e
|
||||
|
||||
def clone_repo(self, repo_full_name: str, clone_url: str | None = None) -> Path:
|
||||
repo_path: Path = self.get_repo_path(repo_full_name)
|
||||
if repo_path.exists():
|
||||
if not (repo_path / ".git").exists():
|
||||
new_path: Path = repo_path.parent / f"{repo_full_name.replace('/', '_')}_old"
|
||||
new_path: Path = (
|
||||
repo_path.parent / f"{repo_full_name.replace('/', '_')}_old"
|
||||
)
|
||||
if new_path.exists():
|
||||
shutil.rmtree(new_path)
|
||||
repo_path.rename(new_path)
|
||||
@@ -126,7 +161,7 @@ class WorkspaceManager:
|
||||
auth_url = self._get_authenticated_url(repo_full_name)
|
||||
|
||||
client = GiteaClient()
|
||||
user = client.get_authenticated_user()
|
||||
user = client.repos.get_authenticated_user()
|
||||
if not user or not user.login:
|
||||
raise RuntimeError("No authenticated user found.")
|
||||
username: str = user.login
|
||||
@@ -135,8 +170,16 @@ class WorkspaceManager:
|
||||
auth_bytes: bytes = auth_str.encode("utf-8")
|
||||
auth_b64: str = base64.b64encode(auth_bytes).decode("utf-8")
|
||||
subprocess.run(
|
||||
["git", "clone", "-c", f"http.extraHeader=Authorization: Basic {auth_b64}", auth_url, str(repo_path)],
|
||||
check=True, capture_output=True
|
||||
[
|
||||
"git",
|
||||
"clone",
|
||||
"-c",
|
||||
f"http.extraHeader=Authorization: Basic {auth_b64}",
|
||||
auth_url,
|
||||
str(repo_path),
|
||||
],
|
||||
check=True,
|
||||
capture_output=True,
|
||||
)
|
||||
self._configure_repo_user(repo_path)
|
||||
return repo_path
|
||||
|
||||
Reference in New Issue
Block a user