This commit is contained in:
Slipstream 2025-05-09 17:56:19 -06:00
parent 5300c059c4
commit 1a54a2802b
Signed by: slipstream
GPG Key ID: 13E498CE010AC6FD

View File

@ -24,6 +24,31 @@ log = logging.getLogger(__name__)
router = APIRouter()
api_settings = get_api_settings() # Get loaded API settings
async def get_monitored_repository_by_id_api(request: Request, repo_db_id: int) -> Dict | None:
"""Gets details of a monitored repository by its database ID using the API service's PostgreSQL pool.
This is an alternative to settings_manager.get_monitored_repository_by_id that doesn't rely on the bot instance.
"""
# Try to get the PostgreSQL pool from the FastAPI app state
pg_pool = getattr(request.app.state, "pg_pool", None)
if not pg_pool:
log.warning(f"API service PostgreSQL pool not available for get_monitored_repository_by_id_api (ID {repo_db_id}).")
# Fall back to settings_manager if API pool is not available
return await settings_manager.get_monitored_repository_by_id(repo_db_id)
try:
async with pg_pool.acquire() as conn:
record = await conn.fetchrow(
"SELECT * FROM git_monitored_repositories WHERE id = $1",
repo_db_id
)
log.info(f"Retrieved repository configuration for ID {repo_db_id} using API service PostgreSQL pool")
return dict(record) if record else None
except Exception as e:
log.exception(f"Database error getting monitored repository by ID {repo_db_id} using API service pool: {e}")
# Fall back to settings_manager if there's an error with the API pool
log.info(f"Falling back to settings_manager for repository ID {repo_db_id}")
return await settings_manager.get_monitored_repository_by_id(repo_db_id)
def verify_github_signature(payload_body: bytes, secret_token: str, signature_header: str) -> bool:
"""Verify that the payload was sent from GitHub by validating the signature."""
if not signature_header:
@ -140,7 +165,6 @@ def format_gitlab_embed(payload: Dict[str, Any], repo_url: str) -> discord.Embed
# GitLab commit objects don't directly list added/removed/modified files in the same way GitHub does per commit in a push.
# The overall push event has 'total_commits_count', but individual commit stats are usually fetched separately if needed.
# For simplicity, we'll list files if available, or just the message.
stats_lines = []
# GitLab's commit object in webhook doesn't typically include detailed file stats like GitHub's.
# It might have 'added', 'modified', 'removed' at the top level of the push event for the whole push, not per commit.
# We'll focus on commit message and author for now.
@ -177,7 +201,8 @@ async def webhook_github(
log.info(f"Received GitHub webhook for repo_db_id: {repo_db_id}")
payload_bytes = await request.body()
repo_config = await settings_manager.get_monitored_repository_by_id(repo_db_id)
# Use our new function that uses the API service's PostgreSQL pool
repo_config = await get_monitored_repository_by_id_api(request, repo_db_id)
if not repo_config:
log.error(f"No repository configuration found for repo_db_id: {repo_db_id}")
raise HTTPException(status_code=404, detail="Repository configuration not found.")
@ -254,7 +279,6 @@ async def webhook_github(
# If send_discord_message_via_api is adapted to handle embeds in its 'content' (e.g. by checking if it's a dict with 'embeds' key)
# then the following would be more appropriate:
send_payload = {"embeds": [discord_embed.to_dict()]}
# This requires send_discord_message_via_api to be flexible.
send_payload_dict = {"embeds": [discord_embed.to_dict()]}
@ -281,7 +305,8 @@ async def webhook_gitlab(
log.info(f"Received GitLab webhook for repo_db_id: {repo_db_id}")
payload_bytes = await request.body()
repo_config = await settings_manager.get_monitored_repository_by_id(repo_db_id)
# Use our new function that uses the API service's PostgreSQL pool
repo_config = await get_monitored_repository_by_id_api(request, repo_db_id)
if not repo_config:
log.error(f"No repository configuration found for repo_db_id: {repo_db_id}")
raise HTTPException(status_code=404, detail="Repository configuration not found.")