open-swe/agent/utils/repo.py
Brace Sproul 46d7ed9d43
feat: extract repo parsing into shared util, add linear comment repo override (#1103)
* feat: extract repo parsing into shared util and add linear comment repo override

Moves the repo extraction regex logic (repo:, repo , GitHub URL) into
agent/utils/repo.py so it can be reused. Updates the Linear webhook
handler to check the comment body for a custom repo first, falling back
to the team/project mapping when none is specified.

* chore: document repo extraction util and default org configuration

* feat: add generic DEFAULT_REPO_OWNER/DEFAULT_REPO_NAME env vars replacing Slack-only defaults

* chore: remove deprecated SLACK_REPO_OWNER/NAME env vars from docs

---------

Co-authored-by: open-swe[bot] <open-swe@users.noreply.github.com>
2026-03-20 13:34:00 -07:00

42 lines
1.3 KiB
Python

"""Utilities for extracting repository configuration from text."""
from __future__ import annotations
import os
import re
_DEFAULT_REPO_OWNER = os.environ.get("DEFAULT_REPO_OWNER", "langchain-ai")
def extract_repo_from_text(text: str, default_owner: str | None = None) -> dict[str, str] | None:
"""Extract owner/name repo config from text containing repo: syntax or GitHub URLs.
Checks for explicit ``repo:owner/name`` or ``repo owner/name`` first, then
falls back to GitHub URL extraction.
Returns:
A dict with ``owner`` and ``name`` keys, or ``None`` if no repo found.
"""
if default_owner is None:
default_owner = _DEFAULT_REPO_OWNER
owner: str | None = None
name: str | None = None
if "repo:" in text or "repo " in text:
match = re.search(r"repo[: ]([a-zA-Z0-9_.\-/]+)", text)
if match:
value = match.group(1).rstrip("/")
if "/" in value:
owner, name = value.split("/", 1)
else:
owner = default_owner
name = value
if not owner or not name:
github_match = re.search(r"github\.com/([a-zA-Z0-9_.-]+/[a-zA-Z0-9_.-]+)", text)
if github_match:
owner, name = github_match.group(1).split("/", 1)
if owner and name:
return {"owner": owner, "name": name}
return None