From ead48f217096fb0497ed8ce62f85157ce6ab858f Mon Sep 17 00:00:00 2001 From: Greg Pflaum Date: Sun, 31 Jan 2021 09:34:09 -0500 Subject: [PATCH 1/2] Support GitHub Enterprise Pass base_url parameter to PyGithub when using GitHub Enterprise. --- .config.yml | 3 +++ python-pygithub/lib/__init__.py | 21 ++++++++++++++++----- 2 files changed, 19 insertions(+), 5 deletions(-) diff --git a/.config.yml b/.config.yml index 4002c61..550ebbd 100644 --- a/.config.yml +++ b/.config.yml @@ -4,3 +4,6 @@ orgs: pem: app_id: installation_id: + # If using GitHub Enterprise, set base_url. + # GitHub Enterprise is supported by these examples: python-pygithub + # base_url: https://ghe.example.com/api/v3 diff --git a/python-pygithub/lib/__init__.py b/python-pygithub/lib/__init__.py index 00a6705..8ec3d2a 100644 --- a/python-pygithub/lib/__init__.py +++ b/python-pygithub/lib/__init__.py @@ -15,18 +15,29 @@ def wrapper(self, *args, **kwargs): self.github_client() if not self.gh or self.is_expired() else None return func(self, *args, **kwargs) return wrapper - + def is_expired(self): return datetime.now().timestamp() + 60 >= self.expires_at.timestamp() def token(self): - return GithubIntegration( - self.meta.get("app_id"), self.private_key - ).get_access_token(self.meta.get("installation_id")) + kwargs = { + "integration_id": self.meta.get("app_id"), + "private_key": self.private_key, + } + if self.meta.get("base_url"): + kwargs["base_url"] = self.meta.get("base_url") + integration = GithubIntegration( + **kwargs, + ) + token = integration.get_access_token(self.meta.get("installation_id")) + return token def github_client(self): self._auth = self.token() - self.gh = Github(self._auth.token) + kwargs = {"login_or_token": self._auth.token} + if self.meta.get("base_url"): + kwargs["base_url"] = self.meta.get("base_url") + self.gh = Github(**kwargs) self.expires_at = self._auth.expires_at.replace(tzinfo=timezone.utc) From f8e1eec28e2c40488397a1c1f4dca2d766c63357 Mon Sep 17 00:00:00 2001 From: Greg Pflaum Date: Sun, 31 Jan 2021 09:44:24 -0500 Subject: [PATCH 2/2] ignore Python cache --- python-pygithub/.gitignore | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 python-pygithub/.gitignore diff --git a/python-pygithub/.gitignore b/python-pygithub/.gitignore new file mode 100644 index 0000000..214063e --- /dev/null +++ b/python-pygithub/.gitignore @@ -0,0 +1,2 @@ +# Pytyon 3 bytecode +__pycache__/