Skip to content

Commit

Permalink
Improve logging around Git repo cloning (#327)
Browse files Browse the repository at this point in the history
  • Loading branch information
petebachant authored Feb 4, 2025
1 parent 9bf1907 commit b8e43a7
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 7 deletions.
1 change: 0 additions & 1 deletion backend/app/api/routes/projects.py
Original file line number Diff line number Diff line change
Expand Up @@ -718,7 +718,6 @@ def get_project_contents(
min_access_level="read",
)
# Get the repo
# Note this will make the repo our working directory
# TODO: Stop using a TTL and rely on latest commit hash
repo = get_repo(
project=project, user=current_user, session=session, ttl=ttl
Expand Down
27 changes: 21 additions & 6 deletions backend/app/git.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@
import time

import git
from fastapi import HTTPException
from filelock import FileLock, Timeout
from sqlmodel import Session

from app import users
from app.core import logger, ryaml
from app.models import Project, User
from filelock import FileLock, Timeout
from sqlmodel import Session


def get_repo(
Expand Down Expand Up @@ -46,12 +48,14 @@ def get_repo(
shutil.rmtree(repo_dir, ignore_errors=True)
# Clone the repo if it doesn't exist -- it will be in a "repo" dir
if user is not None:
logger.info("Getting access token for Git repo URL")
access_token = users.get_github_token(session=session, user=user)
git_clone_url = (
f"https://x-access-token:{access_token}@"
f"{project.git_repo_url.removeprefix('https://')}.git"
)
else:
logger.info("Using public Git repo URL")
git_clone_url = project.git_repo_url
newly_cloned = False
repo = None
Expand All @@ -60,11 +64,22 @@ def get_repo(
logger.info(f"Git cloning into {repo_dir}")
try:
with lock:
subprocess.call(
["git", "clone", "--depth", "1", git_clone_url, repo_dir]
)
try:
subprocess.check_call(
[
"git",
"clone",
"--depth",
"1",
git_clone_url,
repo_dir,
]
)
except subprocess.CalledProcessError as e:
logger.error(f"Failed to clone repo: {e}")
raise HTTPException(404, "Git repo not found")
# Touch a file so we can compute a TTL
subprocess.call(["touch", updated_fpath])
subprocess.check_call(["touch", updated_fpath])
repo = git.Repo(repo_dir)
if user is not None:
# Run git config so we make commits as this user
Expand Down
7 changes: 7 additions & 0 deletions backend/app/projects.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,13 @@ def get_project(
min_access_level: Literal["read", "write", "admin", "owner"] | None = None,
) -> Project:
"""Fetch a project by owner and name."""
if current_user is None:
user_name = "anonymous"
else:
user_name = current_user.email
logger.info(
f"Fetching project {owner_name}/{project_name} for {user_name}"
)
query = (
select(Project)
.where(Project.owner_account.has(name=owner_name))
Expand Down

0 comments on commit b8e43a7

Please sign in to comment.