diff options
author | Egor Tensin <Egor.Tensin@gmail.com> | 2021-10-16 13:05:38 +0300 |
---|---|---|
committer | Egor Tensin <Egor.Tensin@gmail.com> | 2021-10-16 16:44:02 +0300 |
commit | c076718e1cf9943b583ccc3de01940b31fd44211 (patch) | |
tree | e19489c341fa869f79d3842b78ae74c71cfc5384 | |
parent | add dependency to python-gitlab to setup.cfg (diff) | |
download | cgitize-c076718e1cf9943b583ccc3de01940b31fd44211.tar.gz cgitize-c076718e1cf9943b583ccc3de01940b31fd44211.zip |
add Gitlab support
Diffstat (limited to '')
-rw-r--r-- | cgitize/config.py | 22 | ||||
-rw-r--r-- | cgitize/gitlab.py | 41 | ||||
-rw-r--r-- | cgitize/repo.py | 15 | ||||
-rwxr-xr-x | test/integration/example/test.sh | 12 |
4 files changed, 88 insertions, 2 deletions
diff --git a/cgitize/config.py b/cgitize/config.py index 036b855..0e42357 100644 --- a/cgitize/config.py +++ b/cgitize/config.py @@ -9,6 +9,7 @@ import tomli from cgitize.bitbucket import Bitbucket from cgitize.github import GitHub +from cgitize.gitlab import Gitlab from cgitize.repo import Repo @@ -95,6 +96,21 @@ class BitbucketSection(Section): return map(HostedRepo, self.repositories.enum_repositories()) +class GitlabSection(Section): + def __init__(self, *args, **kwargs): + super().__init__(*args, **kwargs) + self.users = UsersSection(self.impl.get('users', {})) + self.repositories = RepositoriesSection(self.impl.get('repositories', {})) + + @property + def access_token(self): + return self._get_config_or_env('access_token', 'CGITIZE_GITLAB_ACCESS_TOKEN') + + @property + def url_auth(self): + return self.access_token + + class UsersSection(Section): def enum_users(self): return self.impl.values() @@ -154,6 +170,7 @@ class Config: self.repositories = RepositoriesSection(self.impl.get('repositories', {})) self.github = GitHubSection(self.impl.get('github', {})) self.bitbucket = BitbucketSection(self.impl.get('bitbucket', {})) + self.gitlab = GitlabSection(self.impl.get('gitlab', {})) def _parse_explicit_repositories(self): for r in self.repositories.enum_repositories(): @@ -179,7 +196,12 @@ class Config: bitbucket = Bitbucket(self.bitbucket.username, self.bitbucket.app_password) return self._parse_hosted_repositories(self.bitbucket, bitbucket) + def _parse_gitlab_repositories(self): + gitlab = Gitlab(self.gitlab.access_token) + return self._parse_hosted_repositories(self.gitlab, gitlab) + def parse_repositories(self): yield from self._parse_explicit_repositories() yield from self._parse_github_repositories() yield from self._parse_bitbucket_repositories() + yield from self._parse_gitlab_repositories() diff --git a/cgitize/gitlab.py b/cgitize/gitlab.py new file mode 100644 index 0000000..77612cb --- /dev/null +++ b/cgitize/gitlab.py @@ -0,0 +1,41 @@ +# Copyright (c) 2021 Egor Tensin <Egor.Tensin@gmail.com> +# This file is part of the "cgitize" project. +# For details, see https://github.com/egor-tensin/cgitize. +# Distributed under the MIT License. + +import logging + +import gitlab + +from cgitize.repo import Repo + + +class Gitlab: + def __init__(self, access_token): + self._impl = gitlab.Gitlab('https://gitlab.com', private_token=access_token) + + def get_repo(self, repo): + try: + return self._impl.projects.get(repo.id) + except gitlab.exceptions.GitlabGetError: + logging.error("Couldn't fetch repository: %s", repo.id) + raise + + def get_user_repos(self, user): + try: + # Strictly speaking, Gitlab supports the /users/:username/projects + # endpoint, which means you shouldn't need to fetch the user first, + # but I don't think python-gitlab 2.10.0 supports that? + users = self._impl.users.list(username=user.name) + if not users: + raise RuntimeError(f"Couldn't find Gitlab user: {user.name}") + assert len(users) == 1 + user = users[0] + return user.projects.list() + except gitlab.exceptions.GitlabGetError: + logging.error("Couldn't fetch user repositories: %s", user.name) + raise + + @staticmethod + def convert_repo(repo, *args, **kwargs): + return Repo.from_gitlab(repo, *args, **kwargs) diff --git a/cgitize/repo.py b/cgitize/repo.py index d926ee9..079801f 100644 --- a/cgitize/repo.py +++ b/cgitize/repo.py @@ -63,6 +63,21 @@ class Repo: return Repo(name, clone_url, owner=owner, desc=desc, homepage=homepage, url_auth=url_auth, subdir=subdir) + @staticmethod + def from_gitlab(src, config, subdir=None): + name = src.name + desc = src.description + homepage = src.web_url + owner = src.namespace['name'] + + https_url = src.http_url_to_repo + ssh_url = src.ssh_url_to_repo + clone_url = ssh_url if config.main.clone_via_ssh else https_url + url_auth = None if config.main.clone_via_ssh else config.gitlab.url_auth + + return Repo(name, clone_url, owner=owner, desc=desc, homepage=homepage, + url_auth=url_auth, subdir=subdir) + def __init__(self, name, clone_url, owner=None, desc=None, homepage=None, url_auth=None, subdir=None): self._name = name diff --git a/test/integration/example/test.sh b/test/integration/example/test.sh index f1f193d..6991697 100755 --- a/test/integration/example/test.sh +++ b/test/integration/example/test.sh @@ -123,13 +123,17 @@ test_ssh() { verify_repos \ lens \ chromiumembedded/cef \ + inkscape \ wireguard/wintun \ github-dir/cgitize-test-repository \ - bitbucket-dir/cgitize-test-repository + bitbucket-dir/cgitize-test-repository \ + gitlab-dir/cgitize-test-repository verify_origin lens 'git@github.com:ekmett/lens.git' verify_origin chromiumembedded/cef 'git@bitbucket.org:chromiumembedded/cef.git' + verify_origin inkscape 'git@gitlab.com:inkscape/inkscape.git' verify_origin github-dir/cgitize-test-repository 'git@github.com:egor-tensin/cgitize-test-repository.git' verify_origin bitbucket-dir/cgitize-test-repository 'git@bitbucket.org:egor-tensin/cgitize-test-repository.git' + verify_origin gitlab-dir/cgitize-test-repository 'git@gitlab.com:egor-tensin/cgitize-test-repository.git' cleanup } @@ -144,13 +148,17 @@ test_https() { verify_repos \ lens \ chromiumembedded/cef \ + inkscape \ wireguard/wintun \ github-dir/cgitize-test-repository \ - bitbucket-dir/cgitize-test-repository + bitbucket-dir/cgitize-test-repository \ + gitlab-dir/cgitize-test-repository verify_origin lens 'https://github.com/ekmett/lens.git' verify_origin chromiumembedded/cef 'https://bitbucket.org/chromiumembedded/cef.git' + verify_origin inkscape 'https://gitlab.com/inkscape/inkscape.git' verify_origin github-dir/cgitize-test-repository 'https://github.com/egor-tensin/cgitize-test-repository.git' verify_origin bitbucket-dir/cgitize-test-repository 'https://bitbucket.org/egor-tensin/cgitize-test-repository.git' + verify_origin gitlab-dir/cgitize-test-repository 'https://gitlab.com/egor-tensin/cgitize-test-repository.git' cleanup } |