Skip to content

Commit 6a8b90b

Browse files
committed
Moved communication with GitHub to webtrees-common library
1 parent dfa2e23 commit 6a8b90b

File tree

3 files changed

+21
-53
lines changed

3 files changed

+21
-53
lines changed

composer.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@
1111
],
1212
"require": {
1313
"bahuma/oauth2-nextcloud": "=2.0.0",
14-
"jefferson49/oauth2-kanidm": ">=1.1.0",
15-
"jefferson49/webtrees-common": ">=1.2.8",
14+
"jefferson49/oauth2-kanidm": ">=1.1.1",
15+
"jefferson49/webtrees-common": ">=1.2.12",
1616
"league/oauth2-client": "=2.8.1",
1717
"league/oauth2-github": "=3.1.1",
1818
"league/oauth2-google": "=4.0.1",

composer.lock

Lines changed: 9 additions & 9 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/OAuth2Client.php

Lines changed: 10 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@
3636
namespace Jefferson49\Webtrees\Module\OAuth2Client;
3737

3838
use Fig\Http\Message\RequestMethodInterface;
39-
use Fig\Http\Message\StatusCodeInterface;
4039
use Fisharebest\Localization\Translation;
4140
use Fisharebest\Webtrees\Auth;
4241
use Fisharebest\Webtrees\FlashMessages;
@@ -64,21 +63,19 @@
6463
use Fisharebest\Webtrees\Validator;
6564
use Fisharebest\Webtrees\Tree;
6665
use Fisharebest\Webtrees\View;
66+
use Jefferson49\Webtrees\Exceptions\GithubCommunicationError;
6767
use Jefferson49\Webtrees\Helpers\Functions;
68+
use Jefferson49\Webtrees\Helpers\GithubService;
6869
use Jefferson49\Webtrees\Internationalization\MoreI18N;
6970
use Jefferson49\Webtrees\Log\CustomModuleLogInterface;
7071
use Jefferson49\Webtrees\Module\OAuth2Client\Factories\AuthorizationProviderFactory;
7172
use Jefferson49\Webtrees\Module\OAuth2Client\LoginWithAuthorizationProviderAction;
7273
use Jefferson49\Webtrees\Module\OAuth2Client\RequestHandlers\RegisterWithProviderAction;
73-
use GuzzleHttp\Client;
7474
use Illuminate\Support\Collection;
75-
use GuzzleHttp\Exception\GuzzleException;
7675
use Psr\Http\Message\ResponseInterface;
7776
use Psr\Http\Message\ServerRequestInterface;
7877
use RuntimeException;
7978

80-
use function substr;
81-
8279

8380
class OAuth2Client extends AbstractModule implements
8481
ModuleCustomInterface,
@@ -105,13 +102,9 @@ class OAuth2Client extends AbstractModule implements
105102
public const REDIRECT_ROUTE = '/OAuth2Client';
106103
public const REGISTER_PROVIDER_ROUTE = '/register-with-provider-action{/tree}';
107104

108-
//Github repository
105+
//Github
109106
public const GITHUB_REPO = 'Jefferson49/webtrees-oauth2-client';
110107

111-
//Github API URL to get the information about the latest releases
112-
public const GITHUB_API_LATEST_VERSION = 'https://api.github.com/repos/'. self::GITHUB_REPO . '/releases/latest';
113-
public const GITHUB_API_TAG_NAME_PREFIX = '"tag_name":"v';
114-
115108
//Author of custom module
116109
public const CUSTOM_AUTHOR = 'Markus Hemprich';
117110

@@ -292,43 +285,18 @@ public function customModuleVersion(): string
292285
*/
293286
public function customModuleLatestVersion(): string
294287
{
295-
// No update URL provided.
296-
if (self::GITHUB_API_LATEST_VERSION === '') {
297-
return $this->customModuleVersion();
298-
}
299288
return Registry::cache()->file()->remember(
300289
$this->name() . '-latest-version',
301290
function (): string {
302-
try {
303-
$client = new Client(
304-
[
305-
'timeout' => 3,
306-
]
307-
);
308-
309-
$response = $client->get(self::GITHUB_API_LATEST_VERSION);
310291

311-
if ($response->getStatusCode() === StatusCodeInterface::STATUS_OK) {
312-
$content = $response->getBody()->getContents();
313-
preg_match_all('/' . self::GITHUB_API_TAG_NAME_PREFIX . '\d+\.\d+\.\d+/', $content, $matches, PREG_OFFSET_CAPTURE);
314-
315-
if(!empty($matches[0]))
316-
{
317-
$version = $matches[0][0][0];
318-
$version = substr($version, strlen(self::GITHUB_API_TAG_NAME_PREFIX));
319-
}
320-
else
321-
{
322-
$version = $this->customModuleVersion();
323-
}
324-
325-
return $version;
326-
}
327-
} catch (GuzzleException $ex) {
328-
// Can't connect to the server?
292+
try {
293+
//Get latest release from GitHub
294+
return GithubService::getLatestReleaseTag(self::GITHUB_REPO);
295+
}
296+
catch (GithubCommunicationError $ex) {
297+
// Can't connect to GitHub?
298+
return $this->customModuleVersion();
329299
}
330-
331-
return $this->customModuleVersion();
332300
},
333301
86400
334302
);

0 commit comments

Comments
 (0)