Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,6 @@ orgs:
pem: <path_to_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
2 changes: 2 additions & 0 deletions python-pygithub/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Pytyon 3 bytecode
__pycache__/
21 changes: 16 additions & 5 deletions python-pygithub/lib/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Comment on lines +37 to +40
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Alternatively, could do it like this:

Suggested change
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)
if self.meta.get("base_url"):
self.gh = Github(login_or_token=self._auth.token, base_url=self.meta.get("base_url")
else:
self.gh = Github(login_or_token=self._auth.token)

And similarly for the call above to GithubIntegration().

self.expires_at = self._auth.expires_at.replace(tzinfo=timezone.utc)


Expand Down