From 5923915ecf4de5de1a86ba54235f953f20fcb80a Mon Sep 17 00:00:00 2001 From: vsc46128 vscuser Date: Fri, 20 Oct 2023 13:47:22 +0200 Subject: [PATCH 1/8] Upgrade bot to be compatible with PyGithub --- connections/github.py | 4 ++-- requirements.txt | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/connections/github.py b/connections/github.py index 20271c66..3b04f2f7 100644 --- a/connections/github.py +++ b/connections/github.py @@ -11,7 +11,7 @@ # # Standard library imports -import datetime +from datetime import datetime, timezone import time # Third party imports (anything installed into the local Python environment) @@ -101,7 +101,7 @@ 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): + if not _gh or (_token and datetime.now(timezone.utc) > _token.expires_at): _gh = connect() return _gh diff --git a/requirements.txt b/requirements.txt index 5545325e..288e4caa 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,4 +1,4 @@ -PyGithub +PyGithub>=2.1.1 Waitress cryptography PyGHee>=0.0.3 From ab67a19300e27a10dabeba1eae9ecc56f3787b3f Mon Sep 17 00:00:00 2001 From: vsc46128 vscuser Date: Mon, 23 Oct 2023 14:23:16 +0200 Subject: [PATCH 2/8] update import --- connections/github.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/connections/github.py b/connections/github.py index 3b04f2f7..ee7c5e34 100644 --- a/connections/github.py +++ b/connections/github.py @@ -101,7 +101,11 @@ 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.now(timezone.utc) > _token.expires_at): + + # Check if PyGithub version is <1.56 + if + + if not _gh or (_token and datetime.datetime.now(datetimezone.utc) > _token.expires_at): _gh = connect() return _gh From e3373475c8529593931f0bed2766eb18cf30c6fb Mon Sep 17 00:00:00 2001 From: vsc46128 vscuser Date: Mon, 23 Oct 2023 14:43:32 +0200 Subject: [PATCH 3/8] add fix that is compatible with python 3.6 and PyGithub < 1.56 --- connections/github.py | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/connections/github.py b/connections/github.py index ee7c5e34..9770d915 100644 --- a/connections/github.py +++ b/connections/github.py @@ -15,7 +15,7 @@ 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 @@ -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: @@ -84,7 +84,7 @@ def connect(): Returns: Instance of Github """ - return Github(get_token().token) + return github.Github(get_token().token) def get_instance(): @@ -102,11 +102,13 @@ def get_instance(): # TODO Possibly renew token already if expiry date is soon, not only # after it has expired. - # Check if PyGithub version is <1.56 - if - - if not _gh or (_token and datetime.datetime.now(datetimezone.utc) > _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() return _gh From 5bc4c1bb7fbef355bafb1eb8fecda3109fe09681 Mon Sep 17 00:00:00 2001 From: vsc46128 vscuser Date: Mon, 23 Oct 2023 14:46:23 +0200 Subject: [PATCH 4/8] update requirements --- requirements.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/requirements.txt b/requirements.txt index 288e4caa..5545325e 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,4 +1,4 @@ -PyGithub>=2.1.1 +PyGithub Waitress cryptography PyGHee>=0.0.3 From cc76b84ee3ec6be09cb1de3de159d499af99020c Mon Sep 17 00:00:00 2001 From: vsc46128 vscuser Date: Mon, 23 Oct 2023 15:35:43 +0200 Subject: [PATCH 5/8] add missing ')' --- connections/github.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/connections/github.py b/connections/github.py index 9770d915..9cc6b4e4 100644 --- a/connections/github.py +++ b/connections/github.py @@ -103,10 +103,10 @@ def get_instance(): # after it has expired. # Check if PyGithub version is < 1.56 - if hasattr(github, 'GithubRetry' is False: + 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: + elif hasattr(github, 'GithubRetry') is True: if not _gh or (_token and datetime.now(timezone.utc) > _token.expires_at): _gh = connect() return _gh From 1a02a1bfb24f9c9e236b9494c05a24279148c10f Mon Sep 17 00:00:00 2001 From: vsc46128 vscuser Date: Tue, 24 Oct 2023 09:09:23 +0200 Subject: [PATCH 6/8] implemment suggestions https://github.com/EESSI/eessi-bot-software-layer/pull/224#discussion_r1369024804 --- connections/github.py | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/connections/github.py b/connections/github.py index 9cc6b4e4..7fb6d565 100644 --- a/connections/github.py +++ b/connections/github.py @@ -103,12 +103,15 @@ def get_instance(): # after it has expired. # 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() + 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 From e9d9327498de4ea0371e034c3b055d4952e00ab5 Mon Sep 17 00:00:00 2001 From: vsc46128 vscuser Date: Tue, 24 Oct 2023 09:11:02 +0200 Subject: [PATCH 7/8] remove trailing whitespace --- connections/github.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/connections/github.py b/connections/github.py index 7fb6d565..ad0224e7 100644 --- a/connections/github.py +++ b/connections/github.py @@ -110,7 +110,7 @@ def get_instance(): # Pygithub 1.x time_now = datetime.utcnow() - if not _gh or (_token and time_now > _token.expires_at): + if not _gh or (_token and time_now > _token.expires_at): _gh = connect() return _gh From 21e024979a6970f326603dffe6cba4b027fac1cc Mon Sep 17 00:00:00 2001 From: vsc46128 vscuser Date: Tue, 24 Oct 2023 09:14:18 +0200 Subject: [PATCH 8/8] remove trailing whitespace --- connections/github.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/connections/github.py b/connections/github.py index ad0224e7..2204fe89 100644 --- a/connections/github.py +++ b/connections/github.py @@ -110,7 +110,7 @@ def get_instance(): # Pygithub 1.x time_now = datetime.utcnow() - if not _gh or (_token and time_now > _token.expires_at): + if not _gh or (_token and time_now > _token.expires_at): _gh = connect() return _gh