gitea.py 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. """Gitea backend — uses the Git Data API inherited from GitHubBackend."""
  2. import re
  3. from backend.app.services.git_providers.github import GitHubBackend
  4. class GiteaBackend(GitHubBackend):
  5. """Backend for Gitea instances.
  6. Gitea's Git Data API (/api/v1/repos/{owner}/{repo}/git/...) is compatible
  7. with GitHub's, so push_files, _create_branch_and_push, and _create_initial_commit
  8. are inherited unchanged. Only the API base URL and Accept header differ.
  9. """
  10. def parse_repo_url(self, url: str) -> tuple[str, str]:
  11. """Return (owner, repo) — accepts both https:// and http:// for self-hosted instances."""
  12. if not url or len(url) > 500:
  13. raise ValueError("Invalid Git URL: URL too long or empty")
  14. match = re.match(
  15. r"https?://[\w.\-]+(:\d+)?/([\w.\-]{1,100})/([\w.\-]{1,100})(?:\.git)?/?$",
  16. url,
  17. )
  18. if match:
  19. return match.group(2), match.group(3).removesuffix(".git")
  20. match = re.match(
  21. r"git@[\w.\-]+:([\w.\-]{1,100})/([\w.\-]{1,100})(?:\.git)?$",
  22. url,
  23. )
  24. if match:
  25. return match.group(1), match.group(2).removesuffix(".git")
  26. raise ValueError(f"Cannot parse repository URL: {url}")
  27. def get_api_base(self, repo_url: str) -> str:
  28. """Derive API base from the repository URL's scheme and host."""
  29. match = re.match(r"(https?://[\w.\-]+(:\d+)?)/", repo_url)
  30. if match:
  31. return f"{match.group(1)}/api/v1"
  32. raise ValueError(f"Cannot derive API base from URL: {repo_url}")
  33. def get_headers(self, token: str) -> dict:
  34. headers = super().get_headers(token)
  35. headers["Accept"] = "application/json"
  36. return headers