extracted a function, updated it, and using it in

pull_requests
This commit is contained in:
Karma Riuk
2025-03-14 14:38:24 +01:00
parent fd82ff5128
commit 4544922165
3 changed files with 77 additions and 39 deletions

View File

@ -1,9 +1,9 @@
import os, sys, logging, subprocess
from datetime import datetime
from github.Commit import Commit
from github.PaginatedList import PaginatedList
from github.PullRequestComment import PullRequestComment
from tqdm import tqdm
import logging
def move_github_logging_to_file():
github_logger = logging.getLogger("github")
@ -77,3 +77,68 @@ def has_only_1_comment(commits: PaginatedList[Commit], comments: PaginatedList[P
continue
if verbose: print(f"n_before: {n_before}, n_after: {n_after}")
return n_before >= 1 and n_after >= 1
def is_already_repo_cloned(repos_dir: str, repo_name: str) -> bool:
"""
Checks if the repository is cloned locally and if its remote URL matches the expected GitHub repository URL.
Parameters:
repos_dir (str): The directory where repositories are stored.
repo_name (str): The name of the repository.
Returns:
bool: True if the repository is correctly cloned, False otherwise.
"""
path = os.path.join(repos_dir, repo_name)
if not os.path.exists(path) or not os.path.isdir(path):
return False
try:
result = subprocess.run(
["git", "-C", path, "remote", "-v"],
capture_output=True,
text=True,
check=True
)
remote_urls = result.stdout.splitlines()
expected_url = f"https://github.com/{repo_name}"
return any(expected_url in url for url in remote_urls)
except subprocess.CalledProcessError:
return False
def clone(repo: str, dest: str, updates: dict = {}, force: bool = False, verbose: bool = False) -> None:
"""
Clones a GitHub repository into a local directory.
Args:
repo (str): The GitHub repository to clone, in the format "owner/repo".
dest (str): The directory to clone the repository into.
updates (dict, optional): A dictionary to store updates about the cloning process.
force (bool): Whether to force the cloning process, even if the repository already exists.
verbose (bool): Whether to print verbose output.
"""
local_repo_path = os.path.join(dest, repo)
if not force and is_already_repo_cloned(dest, repo):
# if verbose: print(f"Skipping {repo}, already exists")
updates["cloned_successfully"] = "Already exists"
return
if verbose: print(f"Cloning {repo}")
proc = subprocess.run(
["git", "clone", "--depth", "1", f"https://github.com/{repo}", local_repo_path],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE
)
if proc.returncode != 0:
updates["cloned_successfully"] = False
print(f"Failed to clone {repo}", file=sys.stderr)
print(f"Error message was:", file=sys.stderr)
error_msg = proc.stderr.decode()
print(error_msg, file=sys.stderr)
updates["error_msg"] = error_msg
else:
updates["cloned_successfully"] = True