예제 #1
0
    def test_gitlab_instance_url(self):
        # https
        repos = (
            "[email protected]:KDE/kaidan.git",
            "ssh://[email protected]/KDE/kaidan.git"
            "https://invent.kde.org/KDE/kaidan",
            "git://invent.kde.org/KDE/kaidan",
        )

        for repo in repos:
            url = Utils.gitlab_instance_url(repo)
            self.assertEqual(url, "https://invent.kde.org")

        # http
        url = Utils.gitlab_instance_url("http://invent.kde.org/KDE/kaidan")
        self.assertEqual(url, "http://invent.kde.org")
예제 #2
0
    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)