Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Upgrade bot to be compatible with PyGithub v2.1.1 #224

Merged
merged 8 commits into from
Nov 9, 2023
Merged
Changes from 5 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
18 changes: 12 additions & 6 deletions connections/github.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@
#

# Standard library imports
import datetime
from datetime import datetime, timezone
import time

# Third party imports (anything installed into the local Python environment)
from github import Github, GithubIntegration
import github

# Local application imports (anything from EESSI/eessi-bot-software-layer)
from tools import config, logging
Expand Down Expand Up @@ -57,7 +57,7 @@ def get_token():
# If the config keys are not set, get_access_token will raise a NotImplementedError
# Returning NoneType token will stop the connection in get_instance
try:
github_integration = GithubIntegration(app_id, private_key)
github_integration = github.GithubIntegration(app_id, private_key)
_token = github_integration.get_access_token(installation_id)
break
except NotImplementedError as err:
Expand All @@ -84,7 +84,7 @@ def connect():
Returns:
Instance of Github
"""
return Github(get_token().token)
return github.Github(get_token().token)


def get_instance():
Expand All @@ -101,8 +101,14 @@ def get_instance():
global _gh, _token
# TODO Possibly renew token already if expiry date is soon, not only
# after it has expired.
if not _gh or (_token and datetime.datetime.utcnow() > _token.expires_at):
_gh = connect()

# Check if PyGithub version is < 1.56
if hasattr(github, 'GithubRetry') is False:
if not _gh or (_token and datetime.utcnow() > _token.expires_at):
_gh = connect()
elif hasattr(github, 'GithubRetry') is True:
if not _gh or (_token and datetime.now(timezone.utc) > _token.expires_at):
_gh = connect()
Copy link
Contributor

@boegel boegel Oct 23, 2023

Choose a reason for hiding this comment

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

if ... is False is kind of silly, and positive logic is easier to follow, and there's a lot of copy-pasting, so I would change this to:

    # PyGithub 2.x expects a datetime object with timezone info,
    # PyGithub 1.x requires one without timezone info;
    # see also https://github.com/PyGithub/PyGithub/pull/2565
    if hasattr(github, 'GithubRetry'):
        # PyGithub 2.x
        time_now = datetime.now(timezone.utc)
    else:
        # PyGithub 1.x
        time_now = datetime.utcnow()

    if not _gh or (_token and time_now > _token.expires_at):
        _gh = connect()

return _gh


Expand Down