From fb4f535b79537823c99552dc3b453a1cf3a5f37b Mon Sep 17 00:00:00 2001 From: Yemaneberhan-Lemma Date: Mon, 28 Oct 2024 23:24:47 +0300 Subject: [PATCH 1/3] Fixing Github contribution error --- .../custom/ct_github/src/GithubQuery.php | 37 +++++++++++++++++-- 1 file changed, 33 insertions(+), 4 deletions(-) diff --git a/web/modules/custom/ct_github/src/GithubQuery.php b/web/modules/custom/ct_github/src/GithubQuery.php index 2e7f3d37..177a97c9 100644 --- a/web/modules/custom/ct_github/src/GithubQuery.php +++ b/web/modules/custom/ct_github/src/GithubQuery.php @@ -8,6 +8,7 @@ use Drupal\Core\Config\ConfigFactory; use Github\AuthMethod; use Github\Client; +use Drupal\Core\Logger\LoggerChannelFactoryInterface; /** * Github Query Class. @@ -31,7 +32,7 @@ class GithubQuery { * @var \Drupal\Core\Cache\CacheBackendInterface */ protected $cache; - + protected $logger; /** * Set authentication token to access GitHub API. * @@ -40,13 +41,14 @@ class GithubQuery { * @param \Drupal\Core\Cache\CacheBackendInterface $cacheBackend * The injected cache backend service. */ - public function __construct(ConfigFactory $config_factory, CacheBackendInterface $cacheBackend) { + public function __construct(ConfigFactory $config_factory, CacheBackendInterface $cacheBackend, LoggerChannelFactoryInterface $loggerFactory) { $config = $config_factory->get('ct_github.settings'); $token = $config->get('github_auth_token'); $client = new Client(); $client->authenticate($token, NULL, AuthMethod::ACCESS_TOKEN); $this->client = $client; $this->cache = $cacheBackend; + $this->logger = $loggerFactory->get('ct_github'); // Assigning the logger channel directly here } /** @@ -131,7 +133,34 @@ public function isUserValid(string $username): bool { */ public function getUserContributions(string $username) { $query = $this->getQuery($username); - return $this->client->api('graphql')->execute($query); - } + $maxRetries = 5; + $retryCount = 0; + $waitTime = 1; // Initial wait time in seconds for backoff + + while ($retryCount < $maxRetries) { + try { + return $this->client->api('graphql')->execute($query); + } catch (Github\Exception\RuntimeException $e) { + // Check if the error is due to bad credentials + if ($e->getMessage() === 'Bad credentials') { + $this->logger->error('GitHub API error: Bad credentials. Please check the credentials.'); + return NULL; + } + + // Handle other errors with backoff + $retryCount++; + $this->logger->warning("GitHub API request failed. Retry $retryCount/$maxRetries. Error: " . $e->getMessage()); + // Stop retries if max retries reached + if ($retryCount >= $maxRetries) { + $this->logger->error("GitHub API request failed after $maxRetries attempts."); + return NULL; + } + + // Wait before retrying with exponential backoff + sleep($waitTime); + $waitTime *= 2; // Double the wait time for exponential backoff + } + } + } } From a70f7acc346f21fc41063251bc4ec5c4d1091bf9 Mon Sep 17 00:00:00 2001 From: Yemaneberhan-Lemma Date: Tue, 29 Oct 2024 11:28:32 +0300 Subject: [PATCH 2/3] Added new contruct argument in module services.yml --- web/modules/custom/ct_github/ct_github.services.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/web/modules/custom/ct_github/ct_github.services.yml b/web/modules/custom/ct_github/ct_github.services.yml index c0006939..55ef9268 100644 --- a/web/modules/custom/ct_github/ct_github.services.yml +++ b/web/modules/custom/ct_github/ct_github.services.yml @@ -1,7 +1,7 @@ services: ct_github.query: class: Drupal\ct_github\GithubQuery - arguments: ['@config.factory', '@cache.data'] + arguments: ['@config.factory', '@cache.data', '@logger.factory'] ct_github.loggerChannel: parent: logger.channel_base arguments: ['ct_github'] From ea35a7e05e07aad86dde7455dfca3dbdf6912468 Mon Sep 17 00:00:00 2001 From: Yemaneberhan-Lemma Date: Sun, 10 Nov 2024 22:46:33 +0300 Subject: [PATCH 3/3] Made fixes to Github token related errors --- web/modules/custom/ct_github/src/GithubQuery.php | 10 ++++++++-- web/modules/custom/ct_github/src/GithubRetriever.php | 7 ++++++- .../ct_manager/src/Plugin/QueueWorker/ProcessUsers.php | 6 ++++++ 3 files changed, 20 insertions(+), 3 deletions(-) diff --git a/web/modules/custom/ct_github/src/GithubQuery.php b/web/modules/custom/ct_github/src/GithubQuery.php index 177a97c9..c094cee9 100644 --- a/web/modules/custom/ct_github/src/GithubQuery.php +++ b/web/modules/custom/ct_github/src/GithubQuery.php @@ -44,8 +44,8 @@ class GithubQuery { public function __construct(ConfigFactory $config_factory, CacheBackendInterface $cacheBackend, LoggerChannelFactoryInterface $loggerFactory) { $config = $config_factory->get('ct_github.settings'); $token = $config->get('github_auth_token'); - $client = new Client(); - $client->authenticate($token, NULL, AuthMethod::ACCESS_TOKEN); + $client = (strlen($token) === 40) ? (new Client())->authenticate($token, NULL, AuthMethod::ACCESS_TOKEN) : NULL; + $this->client = $client; $this->cache = $cacheBackend; $this->logger = $loggerFactory->get('ct_github'); // Assigning the logger channel directly here @@ -115,6 +115,9 @@ public function getQuery(string $username): string { */ public function isUserValid(string $username): bool { $cid = 'ct_github:user:' . $username; + if ($this->client === NULL) { + return FALSE; + } $cache = $this->cache->get($cid); if ($cache) { return $cache->data == 'valid'; @@ -132,6 +135,9 @@ public function isUserValid(string $username): bool { * API request to get user contributions. */ public function getUserContributions(string $username) { + if ($this->client === NULL) { + return NULL; + } $query = $this->getQuery($username); $maxRetries = 5; $retryCount = 0; diff --git a/web/modules/custom/ct_github/src/GithubRetriever.php b/web/modules/custom/ct_github/src/GithubRetriever.php index af237e14..94898ed9 100644 --- a/web/modules/custom/ct_github/src/GithubRetriever.php +++ b/web/modules/custom/ct_github/src/GithubRetriever.php @@ -65,10 +65,12 @@ protected function getUserContributions($username) { */ public function getIssues() { $userContributions = $this->getUserContributions($this->username); + + if($userContributions){ $issues = array_map(function ($issue) { return new Issue($issue['title'], $issue['url']); }, $userContributions['data']['user']['issues']['nodes']); - return $issues; + return $issues;} } /** @@ -78,6 +80,9 @@ public function getCodeContributions() { $userContributions = $this->getUserContributions($this->username); $codeContribution = []; // Get all commits associated with the user and set the title accodingly. + if(!$userContributions){ + return NULL; + } foreach ($userContributions['data']['user']['pullRequests']['nodes'] as $data) { foreach ($data['commits']['nodes'] as $node) { if ($node['commit']['authoredByCommitter']) { diff --git a/web/modules/custom/ct_manager/src/Plugin/QueueWorker/ProcessUsers.php b/web/modules/custom/ct_manager/src/Plugin/QueueWorker/ProcessUsers.php index 321bd449..3c93491b 100644 --- a/web/modules/custom/ct_manager/src/Plugin/QueueWorker/ProcessUsers.php +++ b/web/modules/custom/ct_manager/src/Plugin/QueueWorker/ProcessUsers.php @@ -90,6 +90,9 @@ public function processItem($data) { /** @var \Drupal\ct_manager\ContributionSourceInterface $plugin_instance */ $plugin_instance = $this->pluginManager->createInstance($plugin_id); if (!($plugin_instance->isUserValid($user))) { + if ($plugin_id === "github") { + return; + } $this->logger->error('@plugin username for @username is invalid.', ['@plugin' => $plugin_id, '@username' => $user->getAccountName()]); return; } @@ -99,6 +102,9 @@ public function processItem($data) { $this->logger->notice('Saving @plugin issues for @user', ['@plugin' => $plugin_id, '@user' => $user->getAccountName()]); /** @var \Drupal\ct_manager\Data\Issue $issue */ + if ($issues) { + return NULL; + } foreach ($issues as $issue) { if ($this->contribStorage->getNodeForIssue($issue->getUrl())) { $this->logger->notice('Skipping issue @issue, and all after it.', ['@issue' => $issue->getUrl()]);