Format source · python/memory.python.org@629ce1f · GitHub
Skip to content

Commit 629ce1f

Browse files
committed
Format source
1 parent c3ab628 commit 629ce1f

50 files changed

Lines changed: 1200 additions & 798 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

backend/app/admin_auth.py

Lines changed: 13 additions & 11 deletions

backend/app/auth.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,9 @@ async def get_current_token(
4848
if not auth_token:
4949
# Only log token length and first/last 4 characters for security
5050
masked_token = f"{token[:4]}...{token[-4:]}" if len(token) > 8 else "***"
51-
logger.warning(f"Authentication failed: Invalid token {masked_token} (length: {len(token)})")
51+
logger.warning(
52+
f"Authentication failed: Invalid token {masked_token} (length: {len(token)})"
53+
)
5254
raise authentication_failed("Invalid or expired token")
5355

5456
# Set user context for logging
@@ -58,4 +60,4 @@ async def get_current_token(
5860
await crud.update_token_last_used(db, token)
5961

6062
logger.info(f"Authentication successful for token: {auth_token.name}")
61-
return auth_token
63+
return auth_token

backend/app/config.py

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,17 @@ class Settings(BaseSettings):
2626
database_pool_recycle: int = 3600
2727
database_echo: bool = False
2828

29-
# CORS Configuration
29+
# CORS Configuration
3030
cors_origins: str = "" # Comma-separated string, will be parsed to list
3131
cors_allow_credentials: bool = True
3232
cors_allow_methods: List[str] = ["GET", "POST", "PUT", "DELETE", "OPTIONS"]
33-
cors_allow_headers: List[str] = ["Authorization", "Content-Type", "Accept", "Origin", "X-Requested-With"]
33+
cors_allow_headers: List[str] = [
34+
"Authorization",
35+
"Content-Type",
36+
"Accept",
37+
"Origin",
38+
"X-Requested-With",
39+
]
3440

3541
# Pagination Configuration
3642
default_page_size: int = 100
@@ -41,18 +47,20 @@ class Settings(BaseSettings):
4147
# Authentication Configuration
4248
token_length: int = 32 # in bytes, results in 64 hex characters
4349
token_cleanup_days: int = 90
44-
50+
4551
# GitHub OAuth Configuration
4652
github_client_id: str = ""
4753
github_client_secret: str = ""
4854
oauth_redirect_uri: str = "http://localhost:3000/admin/auth/callback"
4955
oauth_state_secret: str = "your-secret-key-change-me"
50-
56+
5157
# Admin authorization via GitHub usernames
5258
admin_initial_username: str = "" # Initial admin username (e.g., "pablogsal")
5359
# Legacy team-based auth (deprecated)
54-
admin_github_org: str = "" # GitHub organization name (e.g., "python")
55-
admin_github_teams: str = "" # Comma-separated list of team slugs (e.g., "memory-python-org")
60+
admin_github_org: str = "" # GitHub organization name (e.g., "python")
61+
admin_github_teams: str = (
62+
"" # Comma-separated list of team slugs (e.g., "memory-python-org")
63+
)
5664

5765
# Performance Configuration
5866
top_functions_limit: int = 10
@@ -78,14 +86,18 @@ def cors_origins_list(self) -> List[str]:
7886
"""Parse CORS origins string into a list."""
7987
if not self.cors_origins.strip():
8088
return []
81-
return [origin.strip() for origin in self.cors_origins.split(",") if origin.strip()]
82-
89+
return [
90+
origin.strip() for origin in self.cors_origins.split(",") if origin.strip()
91+
]
92+
8393
@property
8494
def admin_github_teams_list(self) -> List[str]:
8595
"""Parse admin GitHub teams string into a list."""
8696
if not self.admin_github_teams.strip():
8797
return []
88-
return [team.strip() for team in self.admin_github_teams.split(",") if team.strip()]
98+
return [
99+
team.strip() for team in self.admin_github_teams.split(",") if team.strip()
100+
]
89101

90102

91103
@lru_cache()

backend/app/crud.py

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ async def create_commit(
9999
timestamp = commit.timestamp
100100
if timestamp.tzinfo is not None:
101101
timestamp = timestamp.replace(tzinfo=None)
102-
102+
103103
db_commit = models.Commit(
104104
sha=commit.sha,
105105
timestamp=timestamp,
@@ -118,8 +118,7 @@ async def create_commit(
118118
async def get_binaries(db: AsyncSession) -> List[models.Binary]:
119119
result = await db.execute(
120120
select(models.Binary).order_by(
121-
models.Binary.display_order.asc(),
122-
models.Binary.name.asc()
121+
models.Binary.display_order.asc(), models.Binary.name.asc()
123122
)
124123
)
125124
return result.scalars().all()
@@ -217,7 +216,7 @@ async def get_runs_with_commits(
217216

218217
if commit_sha:
219218
# Use prefix matching (starts with) for commit SHA
220-
query = query.where(models.Run.commit_sha.ilike(f'{commit_sha}%'))
219+
query = query.where(models.Run.commit_sha.ilike(f"{commit_sha}%"))
221220
if binary_id:
222221
query = query.where(models.Run.binary_id == binary_id)
223222
if environment_id:
@@ -239,7 +238,7 @@ async def count_runs(
239238

240239
if commit_sha:
241240
# Use prefix matching (starts with) for commit SHA
242-
query = query.where(models.Run.commit_sha.ilike(f'{commit_sha}%'))
241+
query = query.where(models.Run.commit_sha.ilike(f"{commit_sha}%"))
243242
if binary_id:
244243
query = query.where(models.Run.binary_id == binary_id)
245244
if environment_id:
@@ -254,7 +253,7 @@ async def create_run(db: AsyncSession, run: schemas.RunCreate) -> models.Run:
254253
timestamp = run.timestamp
255254
if timestamp.tzinfo is not None:
256255
timestamp = timestamp.replace(tzinfo=None)
257-
256+
258257
db_run = models.Run(
259258
run_id=run.run_id,
260259
commit_sha=run.commit_sha,
@@ -467,9 +466,7 @@ async def create_auth_token(
467466
db: AsyncSession, token: str, name: str, description: str = None
468467
) -> models.AuthToken:
469468
"""Create a new auth token."""
470-
db_token = models.AuthToken(
471-
token=token, name=name, description=description
472-
)
469+
db_token = models.AuthToken(token=token, name=name, description=description)
473470
db.add(db_token)
474471
await db.commit()
475472
await db.refresh(db_token)
@@ -512,30 +509,34 @@ async def deactivate_auth_token(db: AsyncSession, token_id: int) -> bool:
512509
async def get_admin_users(db: AsyncSession) -> List[models.AdminUser]:
513510
"""Get all admin users."""
514511
result = await db.execute(
515-
select(models.AdminUser).where(models.AdminUser.is_active == True).order_by(models.AdminUser.added_at)
512+
select(models.AdminUser)
513+
.where(models.AdminUser.is_active == True)
514+
.order_by(models.AdminUser.added_at)
516515
)
517516
return result.scalars().all()
518517

519518

520-
async def get_admin_user_by_username(db: AsyncSession, username: str) -> Optional[models.AdminUser]:
519+
async def get_admin_user_by_username(
520+
db: AsyncSession, username: str
521+
) -> Optional[models.AdminUser]:
521522
"""Get admin user by GitHub username."""
522523
result = await db.execute(
523524
select(models.AdminUser).where(
524525
and_(
525526
models.AdminUser.github_username == username,
526-
models.AdminUser.is_active == True
527+
models.AdminUser.is_active == True,
527528
)
528529
)
529530
)
530531
return result.scalars().first()
531532

532533

533-
async def create_admin_user(db: AsyncSession, username: str, added_by: str, notes: Optional[str] = None) -> models.AdminUser:
534+
async def create_admin_user(
535+
db: AsyncSession, username: str, added_by: str, notes: Optional[str] = None
536+
) -> models.AdminUser:
534537
"""Create a new admin user."""
535538
admin_user = models.AdminUser(
536-
github_username=username,
537-
added_by=added_by,
538-
notes=notes
539+
github_username=username, added_by=added_by, notes=notes
539540
)
540541
db.add(admin_user)
541542
await db.commit()
@@ -566,10 +567,12 @@ async def ensure_initial_admin(db: AsyncSession, username: str) -> None:
566567
"""Ensure the initial admin user exists."""
567568
if not username:
568569
return
569-
570+
570571
existing = await get_admin_user_by_username(db, username)
571572
if not existing:
572573
logger.info(f"Creating initial admin user: {username}")
573-
await create_admin_user(db, username, "system", "Initial admin from environment variable")
574+
await create_admin_user(
575+
db, username, "system", "Initial admin from environment variable"
576+
)
574577
else:
575578
logger.info(f"Initial admin user already exists: {username}")

backend/app/database.py

Lines changed: 7 additions & 4 deletions

0 commit comments

Comments
 (0)