from abc import ABC, abstractmethod from git_flow import SUPPORTED_REMOTE_APIS, GitFlowError from git_flow.git import Git class RemoteAPI(ABC): def __init__(self, repository: str, token: str) -> None: self.repository = repository self.token = token @staticmethod def parse(remote: str): 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: raise GitFlowError(f"La URL del remoto {remote} es inválida: {url}") repository = repository.removesuffix(".git") return [schema, host, repository] @abstractmethod def create_pull_request( self, source: str, destination: str, title: str | None = None, description: str | None = None, close_source_branch: bool = True ) -> str: pass @abstractmethod def create_tag(self, tag: str, commit: str) -> str: pass @abstractmethod def get_endpoint(self, resource: str) -> str: pass def get_headers(self): return { "Accept": "application/json", "Content-Type": "application/json", "Authorization": "Bearer " + self.token, }