From 353f9be1ff8b308eb5779f5793cbc5d88b9a5e91 Mon Sep 17 00:00:00 2001 From: Egor Tensin Date: Tue, 8 Mar 2022 11:52:44 +0500 Subject: support mirroring all of org's repositories --- cgitize/config.py | 31 ++++++++++++++++++++++++++++--- cgitize/github.py | 7 +++++++ examples/cgitize.toml | 9 ++++++++- test/integration/example/test.sh | 3 +++ 4 files changed, 46 insertions(+), 4 deletions(-) diff --git a/cgitize/config.py b/cgitize/config.py index ce139ea..925dee7 100644 --- a/cgitize/config.py +++ b/cgitize/config.py @@ -68,7 +68,6 @@ class ServiceSection(Section, ABC): r = HostedRepo(r) yield api.convert_repo(api.get_repo(r), cfg, r.dir) for u in self.users.enum_users(): - u = User(u) for r in api.get_user_repos(u): r = api.convert_repo(r, cfg, u.dir) if r.name in u.skip: @@ -81,6 +80,10 @@ class ServiceSection(Section, ABC): class GitHubSection(ServiceSection): + def __init__(self, *args, **kwargs): + super().__init__(*args, **kwargs) + self.orgs = OrgsSection(self.impl.get('organizations', {})) + @property def access_token(self): return self._get_config_or_env('access_token', 'CGITIZE_GITHUB_ACCESS_TOKEN') @@ -89,6 +92,16 @@ class GitHubSection(ServiceSection): def url_auth(self): return self.access_token + def enum_repositories(self, cfg): + yield from super().enum_repositories(cfg) + api = self.connect_to_service() + for org in self.orgs.enum_orgs(): + for repo in api.get_org_repos(org): + repo = api.convert_repo(repo, cfg, org.dir) + if repo.name in org.skip: + continue + yield repo + def connect_to_service(self): return GitHub(self.access_token) @@ -135,7 +148,12 @@ class GitLabSection(ServiceSection): class UsersSection(Section): def enum_users(self): - return self.impl.values() + return map(User, self.impl.values()) + + +class OrgsSection(Section): + def enum_orgs(self): + return map(Org, self.impl.values()) class RepositoriesSection(Section): @@ -146,7 +164,7 @@ class RepositoriesSection(Section): class User: def __init__(self, impl): if 'name' not in impl: - raise ValueError("every user must have 'name'") + raise ValueError("every user must have a 'name'") self._impl = impl @property @@ -162,6 +180,13 @@ class User: return self._impl.get('skip', []) +class Org(User): + def __init__(self, impl): + if 'name' not in impl: + raise ValueError("every organization must have a 'name'") + self._impl = impl + + class HostedRepo: def __init__(self, impl): if 'id' not in impl: diff --git a/cgitize/github.py b/cgitize/github.py index 761b2b7..5bbf8d8 100644 --- a/cgitize/github.py +++ b/cgitize/github.py @@ -28,6 +28,13 @@ class GitHub: logging.error("Couldn't fetch user repositories: %s", user.name) raise + def get_org_repos(self, org): + try: + return self._impl.get_organization(org.name).get_repos() + except GithubException: + logging.error("Couldn't fetch organization repositories: %s", org.name) + raise + @staticmethod def convert_repo(repo, *args, **kwargs): return Repo.from_github(repo, *args, **kwargs) diff --git a/examples/cgitize.toml b/examples/cgitize.toml index ce48a2c..95317b1 100644 --- a/examples/cgitize.toml +++ b/examples/cgitize.toml @@ -29,7 +29,14 @@ name = "egor-tensin" # subdirectory to put that user's repositories to. dir = "github-dir" # Repositories to skip: -#skip = ["repo1", "repo2"] +skip = ["chruby", "SafeInt"] + +[github.organizations.browserify] +# For GitHub, you can also specify organizations. The settings are similar to +# those for users. "browserify" is a random organization. +name = "browserify" +dir = "browserify-dir" +skip = ["awesome-browserify", "discuss"] # Some random repositories hosted on GitHub: [github.repositories.lens] diff --git a/test/integration/example/test.sh b/test/integration/example/test.sh index 846cda4..6e38729 100755 --- a/test/integration/example/test.sh +++ b/test/integration/example/test.sh @@ -132,12 +132,14 @@ test_ssh() { chromiumembedded/cef \ inkscape \ wireguard/wintun \ + browserify-dir/browserify \ github-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 browserify-dir/browserify 'git@github.com:browserify/browserify.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' @@ -164,6 +166,7 @@ test_https() { 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 browserify-dir/browserify 'https://github.com/browserify/browserify.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' -- cgit v1.2.3