From 5ffe1fb9f036fbd81b5e1b1fa191bd7560aa9f5c Mon Sep 17 00:00:00 2001 From: Egor Tensin Date: Wed, 23 Aug 2023 10:13:59 +0200 Subject: fetch private GitHub user repositories This is in line with how it works for Bitbucket & GitLab. --- .github/workflows/ci.yml | 2 ++ cgitize/config.py | 22 +++++++++++++++++++--- cgitize/github.py | 10 ++++++++-- examples/cgitize.toml | 11 +++++------ test/integration/example/test.sh | 8 ++++++-- 5 files changed, 40 insertions(+), 13 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index baa7633..d78f815 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -15,6 +15,7 @@ jobs: python-version: ['3.7', '3.8', '3.9', '3.10', '3.11'] name: 'Test / ${{ matrix.python-version }}' env: + CGITIZE_GITHUB_USERNAME: cgitize-test CGITIZE_GITHUB_TOKEN: '${{ secrets.CGITIZE_GITHUB_TOKEN }}' CGITIZE_BITBUCKET_USERNAME: cgitize-test CGITIZE_BITBUCKET_TOKEN: '${{ secrets.CGITIZE_BITBUCKET_TOKEN }}' @@ -51,6 +52,7 @@ jobs: runs-on: ubuntu-latest name: 'Test / example config' env: + CGITIZE_GITHUB_USERNAME: cgitize-test CGITIZE_GITHUB_TOKEN: '${{ secrets.CGITIZE_GITHUB_TOKEN }}' CGITIZE_BITBUCKET_USERNAME: cgitize-test CGITIZE_BITBUCKET_TOKEN: '${{ secrets.CGITIZE_BITBUCKET_TOKEN }}' diff --git a/cgitize/config.py b/cgitize/config.py index af7c195..d4e660a 100644 --- a/cgitize/config.py +++ b/cgitize/config.py @@ -88,6 +88,10 @@ class GitHubSection(ServiceSection): def token(self): return self._get_config_or_env('token', 'CGITIZE_GITHUB_TOKEN') + @property + def username(self): + return self._get_config_or_env('username', 'CGITIZE_GITHUB_USERNAME') + @property def url_auth(self): return self.token @@ -103,7 +107,11 @@ class GitHubSection(ServiceSection): yield repo def connect_to_service(self): - return GitHub(self.token) + username = self.username + token = self.token + if (username is None) != (token is None): + raise RuntimeError('please set either both the GitHub username & token, or neither') + return GitHub(username, token) def two_part_url_auth(username, password): @@ -126,7 +134,11 @@ class BitbucketSection(ServiceSection): return two_part_url_auth(self.username, self.token) def connect_to_service(self): - return Bitbucket(self.username, self.token) + username = self.username + token = self.token + if (username is None) != (token is None): + raise RuntimeError('please set either both the Bitbucket username & token, or neither') + return Bitbucket(username, token) class GitLabSection(ServiceSection): @@ -143,7 +155,11 @@ class GitLabSection(ServiceSection): return two_part_url_auth(self.username, self.token) def connect_to_service(self): - return GitLab(self.token) + username = self.username + token = self.token + if (username is None) != (token is None): + raise RuntimeError('please set either both the GitLab username & token, or neither') + return GitLab(token) class UsersSection(Section): diff --git a/cgitize/github.py b/cgitize/github.py index b3dcc19..9948a33 100644 --- a/cgitize/github.py +++ b/cgitize/github.py @@ -11,7 +11,8 @@ from cgitize.repo import Repo class GitHub: - def __init__(self, token): + def __init__(self, username, token): + self._username = username self._impl = Github(token) def get_repo(self, repo): @@ -23,7 +24,12 @@ class GitHub: def get_user_repos(self, user): try: - return self._impl.get_user(user.name).get_repos() + if user.name == self._username: + # To get private repositories, get_user() must be called + # without arguments: + return self._impl.get_user().get_repos(affiliation='owner') + else: + return self._impl.get_user(user.name).get_repos() except GithubException: logging.error("Couldn't fetch user repositories: %s", user.name) raise diff --git a/examples/cgitize.toml b/examples/cgitize.toml index 6f3193e..059e8ec 100644 --- a/examples/cgitize.toml +++ b/examples/cgitize.toml @@ -22,6 +22,9 @@ owner = "Your Name" # request rate limit), use "access tokens". Set in the config or using the # CGITIZE_GITHUB_TOKEN environment variable. #token = "XXX" +# Set the username that owns the token here or using the environment variable +# CGITIZE_GITHUB_USERNAME. +#username = "your-username" [github.users.me] name = "cgitize-test" @@ -55,9 +58,7 @@ dir = "python" # Access tokens are called "app passwords" on Bitbucket. # The environment variable is CGITIZE_BITBUCKET_TOKEN. #token = "XXX" -# Contrary to GitHub, Bitbucket doesn't associate app passwords with a -# username, so you also need that (CGITIZE_BITBUCKET_USERNAME is the -# environment variable). +# The environment variable is CGITIZE_BITBUCKET_USERNAME. #username = "your-username" [bitbucket.users.me] @@ -80,9 +81,7 @@ id = "berkeleylab/upc-runtime" [gitlab] # The environment variable is CGITIZE_GITLAB_TOKEN. #token = "XXX" -# Similar to Bitbucket, GitLab doesn't associate access tokens with a username -# (for HTTP clones), so you also need that (CGITIZE_GITLAB_USERNAME is the -# environment variable). +# The environment variable is CGITIZE_GITLAB_USERNAME. #username = "your-username" [gitlab.users.me] diff --git a/test/integration/example/test.sh b/test/integration/example/test.sh index 901fd01..7568936 100755 --- a/test/integration/example/test.sh +++ b/test/integration/example/test.sh @@ -136,6 +136,7 @@ test_ssh() { wireguard/wintun \ goauthentik-dir/authentik \ github-dir/public \ + github-dir/private \ bitbucket-dir/public \ bitbucket-dir/private \ gitlab-dir/public \ @@ -146,7 +147,8 @@ test_ssh() { verify_origin graphviz 'git@gitlab.com:graphviz/graphviz.git' verify_origin goauthentik-dir/authentik 'git@github.com:goauthentik/authentik.git' - verify_origin github-dir/public 'git@github.com:cgitize-test/public.git' + verify_origin github-dir/public 'git@github.com:cgitize-test/public.git' + verify_origin github-dir/private 'git@github.com:cgitize-test/private.git' verify_origin bitbucket-dir/public 'git@bitbucket.org:cgitize-test-workspace/public.git' verify_origin bitbucket-dir/private 'git@bitbucket.org:cgitize-test-workspace/private.git' verify_origin gitlab-dir/public 'git@gitlab.com:cgitize-test/public.git' @@ -172,6 +174,7 @@ test_https() { wireguard/wintun \ goauthentik-dir/authentik \ github-dir/public \ + github-dir/private \ bitbucket-dir/public \ bitbucket-dir/private \ gitlab-dir/public \ @@ -182,7 +185,8 @@ test_https() { verify_origin graphviz 'https://gitlab.com/graphviz/graphviz.git' verify_origin goauthentik-dir/authentik 'https://github.com/goauthentik/authentik.git' - verify_origin github-dir/public 'https://github.com/cgitize-test/public.git' + verify_origin github-dir/public 'https://github.com/cgitize-test/public.git' + verify_origin github-dir/private 'https://github.com/cgitize-test/private.git' verify_origin bitbucket-dir/public 'https://bitbucket.org/cgitize-test-workspace/public.git' verify_origin bitbucket-dir/private 'https://bitbucket.org/cgitize-test-workspace/private.git' verify_origin gitlab-dir/public 'https://gitlab.com/cgitize-test/public.git' -- cgit v1.2.3