This repository was archived by the owner on Apr 12, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClientHelper.php
More file actions
114 lines (99 loc) · 3.68 KB
/
Copy pathClientHelper.php
File metadata and controls
114 lines (99 loc) · 3.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
<?php
/**
* Copyright Zikula - https://ziku.la/
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Zikula\Module\CoreManagerModule\Helper;
use Cache\Adapter\Filesystem\FilesystemCachePool;
use Github\Client as GitHubClient;
use Github\HttpClient\Message\ResponseMediator;
use Github\ResultPager;
use League\Flysystem\Adapter\Local;
use League\Flysystem\Filesystem;
use Zikula\ExtensionsModule\Api\ApiInterface\VariableApiInterface;
class ClientHelper
{
/**
* @var VariableApiInterface
*/
private $variableApi;
/**
* @var string
*/
private $cacheDir;
/**
* ClientHelper constructor.
* @param VariableApiInterface $variableApi
* @param string $cacheDir
*/
public function __construct(VariableApiInterface $variableApi, $cacheDir)
{
$this->variableApi = $variableApi;
$this->cacheDir = $cacheDir;
}
/**
* Get an instance of the GitHub Client, authenticated with the admin's authentication token.
*
* @param bool $fallBackToNonAuthenticatedClient Whether or not to fall back to a non-authenticated client if
* authentication fails, default true.
*
* @return GitHubClient|bool The authenticated GitHub client, or false if $fallBackToNonAuthenticatedClient
* is false and the client could not be authenticated.
*/
public function getGitHubClient($fallBackToNonAuthenticatedClient = true)
{
$filesystemAdapter = new Local($this->cacheDir . 'el/github-api');
$filesystem = new Filesystem($filesystemAdapter);
$pool = new FilesystemCachePool($filesystem);
$client = new GitHubClient();
$client->addCache($pool);
$token = $this->variableApi->get('ZikulaCoreManagerModule', 'github_token', null);
if (!empty($token)) {
$client->authenticate($token, null, GitHubClient::AUTH_ACCESS_TOKEN);
try {
$client->getHttpClient()->get('rate_limit');
} catch (\RuntimeException $exception) {
// Authentication failed!
if ($fallBackToNonAuthenticatedClient) {
// Replace client with one not using authentication.
$client = new GitHubClient();
$client->addCache($pool);
} else {
die('Error: ' . $exception->getMessage());
$client = false;
}
}
}
return $client;
}
/**
* Determines if the GitHub client has push access to a specifc repository.
*
* @param $client
*
* @return bool
*/
public function hasGitHubClientPushAccess($client)
{
if (!($client instanceof GitHubClient)) {
return false;
}
$repo = $this->variableApi->get('ZikulaCoreManagerModule', 'github_core_repo');
if (empty($repo)) {
return false;
}
try {
// One can only show collaborators if one has push access to an organization
ResponseMediator::getContent($client->getHttpClient()->get('repos/' . $repo . "/collaborators"));
return true;
} catch (\Github\Exception\RuntimeException $e) { }
// See if maybe we only are a member of a non-organization repository
$paginator = new ResultPager($client);
$reposWhereMember = $paginator->fetchAll($client->api('me'), 'repositories', ['member']);
return in_array($repo, array_map(function ($repoWhereMember) {
return $repoWhereMember['full_name'];
}, $reposWhereMember));
}
}