def test_str_id_for_url(self): url: str = "ssh://[email protected]/KDE/kaidan.git" str_id: str = Utils.str_id_for_url(url) self.assertEqual(str_id, "KDE/kaidan") # This is not a valid repository name for KDE invent, though url = "ssh://[email protected]/KDE/kaidan%.git" str_id = Utils.str_id_for_url(url) self.assertEqual(str_id, "KDE/kaidan%")
def fork(self) -> None: """ Try to create a fork of the remote repository. If the fork already exists, no new fork will be created. """ if "fork" in self._local_repo.remotes: # Fork already exists fork_str_id: str = Utils.str_id_for_url( self._local_repo.remotes.fork.url) # Try to retrieve the remote project object, if it doesn't exist on the server, # go on with the logic to create a new fork. try: self.__remote_fork = self._connection.projects.get(fork_str_id) return except GitlabGetError: pass try: self.__remote_fork = self._remote_project.forks.create({}) # WORKAROUND: the return of create() is unreliable, # and sometimes doesn't allow to create merge requests, # so request a fresh project object. self.__remote_fork = self._connection.projects.get( self.__remote_fork.id) self._local_repo.create_remote( "fork", url=self.__remote_fork.ssh_url_to_repo) except GitlabCreateError: Utils.log( LogType.INFO, "Fork exists, but no fork remote exists locally, trying to guess the url", ) # Detect ssh url url = Utils.ssh_url_from_http(self._connection.user.web_url + "/" + self._remote_project.path) self._local_repo.create_remote("fork", url=url) str_id: str = Utils.str_id_for_url( self._local_repo.remotes.fork.url) self.__remote_fork = self._connection.projects.get(str_id)
def __init__(self) -> None: self._local_repo = Utils.get_cwd_repo() self.__config = Config() try: origin = self._local_repo.remote(name="origin") except ValueError: Utils.log(LogType.ERROR, "No origin remote exists") sys.exit(1) repository: str = next(origin.urls) if repository.startswith("http"): Utils.log( LogType.INFO, "Found http remote, if you want to switch this " + "repository to ssh, run `git lab rewrite-remote " + "origin`", ) print() gitlab_url = Utils.gitlab_instance_url(repository) gitlab_hostname: Optional[str] = urlparse(gitlab_url).hostname if not gitlab_hostname: Utils.log(LogType.ERROR, "Failed to detect GitLab hostname") sys.exit(1) auth_token: Optional[str] = self.__config.token(gitlab_hostname) if not auth_token: Utils.log(LogType.ERROR, "No authentication token found. ") print( "Please create a token with the api and write_repository scopes on {}/-/{}." .format(gitlab_url, "profile/personal_access_tokens")) print('Afterwards use "git lab login --host {} --token t0k3n"'. format(gitlab_hostname)) sys.exit(1) self.__login(gitlab_url, auth_token) if not self._connection: Utils.log(LogType.ERROR, "Failed to connect to GitLab") sys.exit(1) try: self._remote_project = self._connection.projects.get( Utils.str_id_for_url(repository)) except (GitlabHttpError, GitlabGetError): Utils.log( LogType.ERROR, "The repository could not be found on the GitLab instance.", ) print( "If the repository was recently moved, please update the origin remote using git." ) sys.exit(1)
def test_id_url_ending_on_slash(self): url: str = "https://invent.kde.org/network/kaidan/" str_id: str = Utils.str_id_for_url(url) self.assertEqual(str_id, "network/kaidan")