-
-
Notifications
You must be signed in to change notification settings - Fork 78
/
GitHubApiCli.php
191 lines (172 loc) · 4.74 KB
/
GitHubApiCli.php
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
<?php
namespace DevShop\Component\GitHubApiCli;
use Github\AuthMethod;
use Github\Client as GitHubApiClient;
class GitHubApiCli
{
protected $apiToken;
/**
* @var GitHubApiClient
*/
public $apiClient;
/**
* @var string The "version" of the GitHub API to use.
*/
private $apiVersion;
/**
* GitHubApiCli constructor
*
* @param $apiToken String GitHub API Token. If not passed, will look for
* GTIHUB_TOKEN environment variable.
*
* @param $apiVersion String GitHub API Version. If a feature requires an
* additional "Accept" header like "application/vnd.github.flash-preview+json",
* Specify the string between "github." and "+json".
*/
public function __construct($apiToken = NULL, $apiVersion = 'flash-preview')
{
if (!$apiToken && getenv('GITHUB_TOKEN')) {
$this->apiToken = getenv('GITHUB_TOKEN');
}
else {
$this->apiToken = $apiToken;
}
$this->apiVersion = $apiVersion;
// Setup GitHub API client
$this->apiClient = new GitHubApiClient();
// @TODO: Allow password auth?
// Only attempt authentication if there is a token.
if (!empty($this->getToken())) {
$this->apiClient->authenticate($this->getToken(), null, AuthMethod::ACCESS_TOKEN);
}
// Skip SSL verification if ENV var is found.
if (getenv('DEVSHOP_GITHUB_API_IGNORE_SSL')) {
$this->apiClient->getHttpClient()->client->setDefaultOption('verify', false);
}
// Set options or headers from CLI or config options.
// @see Client::options
// Using pattern from HttpClient::clearHeaders()
// $this->apiClient->setHeaders([
// 'Accept' => sprintf('application/vnd.github.%s+json', $this->apiVersion),
// ]);
}
/**
*
*/
public function getToken() {
return $this->apiToken;
}
/**
* @TODO: Should this just extend GitHub\Client?
*
* @param $name
*/
public function api($name) {
return $this->apiClient->api($name);
}
/**
* Return all available APIs.
*
* This has to be a list of strings because the GitHub\Client::api method uses
* a long "switch" statement to map API strings to classes.
*
* @return array List of available APIs.
*/
public function getApis()
{
$apis[] = 'me';
$apis[] = 'deployment';
$apis[] = 'enterprise';
$apis[] = 'git';
$apis[] = 'git_data';
$apis[] = 'gist';
$apis[] = 'issue';
$apis[] = 'markdown';
$apis[] = 'notification';
$apis[] = 'organization';
$apis[] = 'pull_request';
$apis[] = 'rate_limit';
$apis[] = 'repo';
$apis[] = 'search';
$apis[] = 'team';
$apis[] = 'user';
$apis[] = 'authorization';
$apis[] = 'meta';
return $apis;
}
/**
* Return all available APIs.
*
* This has to be a list of strings because the GitHub\Client::api method uses
* a long "switch" statement to map API strings to classes.
*
* @return array List of available APIs.
*/
public function getApisWithAliases()
{
$apis[] = 'me';
$apis[] = 'current_user';
$apis[] = 'currentUser';
$apis[] = 'deployment';
$apis[] = 'deployments';
$apis[] = 'ent';
$apis[] = 'enterprise';
$apis[] = 'git';
$apis[] = 'git_data';
$apis[] = 'gitData';
$apis[] = 'gist';
$apis[] = 'gists';
$apis[] = 'issue';
$apis[] = 'issues';
$apis[] = 'markdown';
$apis[] = 'notification';
$apis[] = 'notifications';
$apis[] = 'organization';
$apis[] = 'organizations';
$apis[] = 'pr';
$apis[] = 'pullRequest';
$apis[] = 'pull_request';
$apis[] = 'pullRequests';
$apis[] = 'pull_requests';
$apis[] = 'rateLimit';
$apis[] = 'rate_limit';
$apis[] = 'repo';
$apis[] = 'repos';
$apis[] = 'repository';
$apis[] = 'repositories';
$apis[] = 'search';
$apis[] = 'team';
$apis[] = 'teams';
$apis[] = 'user';
$apis[] = 'users';
$apis[] = 'authorization';
$apis[] = 'authorizations';
$apis[] = 'meta';
return $apis;
}
/**
* Return all available = methods for the chosen API.
*
* @param $apiName
*/
public function getApiMethods($apiName) {
return $this->getPublicMethods($this->api($apiName));
}
/**
* Return all public methods that are not magic methods.
*
* @param $classNameOrInstance
*/
public function getPublicMethods($classNameOrInstance) {
// Ignore special functions, such as __construct and __call, which
// can never be commands.
$commandMethodNames = array_filter(
get_class_methods($classNameOrInstance) ?: [],
function ($m) use ($classNameOrInstance) {
$reflectionMethod = new \ReflectionMethod($classNameOrInstance, $m);
return !$reflectionMethod->isStatic() && !preg_match('#^_#', $m);
}
);
return $commandMethodNames;
}
}