feature: parse git remote url with giturlparse

This commit is contained in:
Jonathan Teran
2026-07-31 12:22:14 -03:00
parent 8f1a60cc06
commit f4adb431db
4 changed files with 38 additions and 21 deletions
+22 -17
View File
@@ -1,6 +1,7 @@
import giturlparse
from abc import ABC, abstractmethod
from git_flow import SUPPORTED_REMOTE_APIS, GitFlowError
from git_flow import SUPPORTED_REMOTE_URL_SCHEMAS, GitFlowError
from git_flow.git import Git
@@ -11,25 +12,29 @@ class RemoteAPI(ABC):
@staticmethod
def parse(remote: str):
"""Soportamos 3 tipos de URL: http, https y ssh. Si no se especifica el esquema, asumimos ssh:
Ejemplos:
- http://gitea/user/repo
- https://gitea.com/user/repo.git
- git@bitbucket.org:user/repo
- git.mygiteainstance.com:user/repo.git
- ssh://git@git.mygiteainstance.com:1234/user/repo.git
"""
url = Git("remote", "get-url", remote).firstline()
if url.startswith("git@"):
at_pos = url.index("@")
colon_pos = url.index(":")
schema = "ssh://"
host = url[at_pos+1:colon_pos]
repository = url[colon_pos+1:]
elif url.startswith("http://") or url.startswith("https://"):
start_pos = url.index("://") + 3
uri_start = url.index("/", start_pos)
schema = url[:start_pos]
host = url[start_pos:uri_start]
repository = url[uri_start+1:]
else:
parsed_url = giturlparse.parse(url) # Validamos que la URL sea válida
if not parsed_url.valid:
raise GitFlowError(f"La URL del remoto {remote} es inválida: {url}")
repository = repository.removesuffix(".git")
return [schema, host, repository]
if not parsed_url.protocol in SUPPORTED_REMOTE_URL_SCHEMAS:
raise GitFlowError(f"La URL del remoto {remote} no tiene un esquema soportado: {url}")
# Cuando el host es seteado por pipeline puede ser "http://gitea/OWNER/REPO", en ese caso
# la libreria parsea OWNER="" y REPO="OWNER/REPO"
repository = "/".join(filter(None, [parsed_url.owner, parsed_url.repo])).removeprefix("/").removesuffix(".git")
return [parsed_url.protocol + "://", parsed_url.host, repository]
@abstractmethod
def create_pull_request(
@@ -38,7 +43,7 @@ class RemoteAPI(ABC):
destination: str,
title: str | None = None,
description: str | None = None,
close_source_branch: bool = True
close_source_branch: bool = True,
) -> str:
pass