1 | <?php |
||
2 | |||
3 | declare(strict_types=1); |
||
4 | |||
5 | namespace XoopsModules\Wggithub\Github; |
||
6 | |||
7 | /* |
||
8 | * You may not change or alter any portion of this comment or credits |
||
9 | * of supporting developers from this source code or any supporting source code |
||
10 | * which is considered copyrighted (c) material of the original comment or credit authors. |
||
11 | * |
||
12 | * This program is distributed in the hope that it will be useful, |
||
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
||
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
||
15 | */ |
||
16 | /** |
||
17 | * @copyright XOOPS Project https://xoops.org/ |
||
18 | * @license GNU GPL 2 or later (http://www.gnu.org/licenses/gpl-2.0.html) |
||
19 | * @since |
||
20 | * @author Goffy - XOOPS Development Team |
||
21 | */ |
||
22 | |||
23 | use XoopsModules\Wggithub; |
||
24 | use XoopsModules\Wggithub\{ |
||
25 | Helper, |
||
26 | Constants, |
||
27 | Github |
||
28 | }; |
||
29 | |||
30 | /** |
||
31 | * Class GithubClient |
||
32 | */ |
||
33 | class GithubClient extends Api |
||
34 | { |
||
35 | /** |
||
36 | * @var string |
||
37 | */ |
||
38 | public const BASE_URL = 'https://api.github.com/'; |
||
39 | |||
40 | /** |
||
41 | * @var string |
||
42 | */ |
||
43 | //public $userAuth = 'myusername'; |
||
44 | |||
45 | /** |
||
46 | * @var string |
||
47 | */ |
||
48 | public $tokenAuth = 'mytoken'; |
||
49 | |||
50 | /** |
||
51 | * @var object |
||
52 | */ |
||
53 | private $helper = null; |
||
54 | |||
55 | /** |
||
56 | * Constructor |
||
57 | * |
||
58 | * @param null |
||
59 | */ |
||
60 | public function __construct() |
||
61 | { |
||
62 | $this->helper = \XoopsModules\Wggithub\Helper::getInstance(); |
||
63 | $this->getSetting(); |
||
64 | } |
||
65 | |||
66 | /** |
||
67 | * @static function &getInstance |
||
68 | * |
||
69 | * @param null |
||
70 | * @return GitHubClient of Api |
||
71 | */ |
||
72 | public static function getInstance() |
||
73 | { |
||
74 | static $instance = false; |
||
75 | if (!$instance) { |
||
76 | $instance = new self(); |
||
77 | } |
||
78 | |||
79 | return $instance; |
||
80 | } |
||
81 | |||
82 | public function testApi1($url) { |
||
83 | $api = new Github\Api; |
||
84 | $response = $api->get(static::BASE_URL . $url); |
||
85 | $data = $api->decode($response); |
||
86 | |||
87 | return $data; |
||
88 | } |
||
89 | |||
90 | public function testApi2($url) { |
||
91 | $api = new Github\Api; |
||
92 | |||
93 | $token = new Github\OAuth\Token('{myKey}', 'bearer', ['repo', 'user', 'public_repo']); |
||
94 | $api->setToken($token); |
||
95 | $response = $api->get(static::BASE_URL . $url); |
||
96 | |||
97 | $data = $api->decode($response); |
||
98 | |||
99 | /* |
||
100 | $api = new Github\Api; |
||
101 | |||
102 | $request = $api->createRequest('GET', $url, [], [], ''); |
||
103 | $response = $api->request($request); |
||
104 | $data = (array)$api->decode($response); |
||
105 | */ |
||
106 | |||
107 | return $data; |
||
108 | } |
||
109 | |||
110 | /** |
||
111 | * Get repositories of given user |
||
112 | * |
||
113 | * @param $username |
||
114 | * @param int $per_page |
||
115 | * @param int $page |
||
116 | * @return array|bool |
||
117 | */ |
||
118 | public function getUserRepositories($username, $per_page = 100, $page = 1) |
||
119 | { |
||
120 | $url = static::BASE_URL . 'users/' . \rawurlencode($username) . '/repos?per_page=' . $per_page . '&page=' . $page; |
||
121 | |||
122 | return $this->_get($url); |
||
123 | } |
||
124 | |||
125 | /** |
||
126 | * Get repositories of given organisation |
||
127 | * |
||
128 | * @param $org |
||
129 | * @param int $per_page |
||
130 | * @param int $page |
||
131 | * @return array|bool |
||
132 | */ |
||
133 | public function getOrgRepositories($org, $per_page = 100, $page = 1) |
||
134 | { |
||
135 | $url = static::BASE_URL . 'orgs/' . \rawurlencode($org) . '/repos?per_page=' . $per_page . '&page=' . $page; |
||
136 | |||
137 | return $this->_get($url); |
||
138 | } |
||
139 | |||
140 | /** |
||
141 | * Get the readme content for a repository by its username and repository name. |
||
142 | * |
||
143 | * @param string $username the user who owns the repository |
||
144 | * @param string $repository the name of the repository |
||
145 | * @return array|bool |
||
146 | */ |
||
147 | public function getReadme($username, $repository) |
||
148 | { |
||
149 | $url = static::BASE_URL . 'repos/' . \rawurlencode($username) . '/' . \rawurlencode($repository) . '/readme'; |
||
150 | |||
151 | return $this->_get($url, true); |
||
152 | } |
||
153 | |||
154 | /** |
||
155 | * Get all releases |
||
156 | * |
||
157 | * @param string $username the user who owns the repository |
||
158 | * @param string $repository the name of the repository |
||
159 | * @return array|bool |
||
160 | */ |
||
161 | public function getReleases($username, $repository) |
||
162 | { |
||
163 | $url = static::BASE_URL . 'repos/' . $username . '/' . $repository . '/releases'; |
||
164 | |||
165 | return $this->_get($url, true); |
||
166 | } |
||
167 | |||
168 | /** |
||
169 | * Get latest release |
||
170 | * |
||
171 | * @param string $username the user who owns the repository |
||
172 | * @param string $repository the name of the repository |
||
173 | * @param bool $prerelease |
||
174 | * @return array|bool |
||
175 | */ |
||
176 | public function getLatestRelease($username, $repository, $prerelease = false) |
||
177 | { |
||
178 | //function currently not used |
||
179 | if ($prerelease) { |
||
180 | $url = static::BASE_URL . 'repos/' . $username . '/' . $repository . '/releases'; |
||
181 | } else { |
||
182 | $url = static::BASE_URL . 'repos/' . $username . '/' . $repository . '/releases/latest'; |
||
183 | } |
||
184 | $result = $this->_get($url); |
||
185 | |||
186 | if ($prerelease) { |
||
187 | if (\is_array($result)) { |
||
188 | return $result[0]; |
||
189 | } else { |
||
190 | return []; |
||
191 | } |
||
192 | } |
||
193 | |||
194 | return $result; |
||
195 | } |
||
196 | |||
197 | /** |
||
198 | * Get content of repository |
||
199 | * |
||
200 | * @param $username |
||
201 | * @param $repository |
||
202 | * @return array|bool |
||
203 | */ |
||
204 | public function getRepositoryContent($username, $repository) |
||
205 | { |
||
206 | $url = static::BASE_URL . 'repos/'.rawurlencode($username).'/'.rawurlencode($repository).'/contents'; |
||
207 | |||
208 | return $this->_get($url); |
||
209 | } |
||
210 | |||
211 | /** |
||
212 | * Get github content |
||
213 | * |
||
214 | * @param $url |
||
215 | * @param bool $skipError |
||
216 | * @return array|bool |
||
217 | */ |
||
218 | public function _get($url, $skipError = false) |
||
219 | { |
||
220 | $error = false; |
||
221 | $errMsg = ''; |
||
222 | |||
223 | $logsHandler = $this->helper->getHandler('Logs'); |
||
224 | $logsHandler->updateTableLogs(Constants::LOG_TYPE_REQUEST, $url, 'START'); |
||
225 | $api = new Github\Api; |
||
226 | $token = new Github\OAuth\Token($this->tokenAuth, 'bearer', ['repo', 'user', 'public_repo']); |
||
227 | $api->setToken($token); |
||
228 | $response = $api->get($url); |
||
229 | $code = $response->getCode(); |
||
230 | if (\in_array($code, [200, 201], true)) { |
||
231 | $logsHandler->updateTableLogs(Constants::LOG_TYPE_REQUEST, $url, 'OK'); |
||
232 | } else { |
||
233 | if ($skipError) { |
||
234 | $logsHandler->updateTableLogs(Constants::LOG_TYPE_ERROR, $errMsg, 'Skipped'); |
||
235 | return false; |
||
236 | } else { |
||
237 | $error = true; |
||
238 | $errMsg = $response->getContent(); |
||
239 | $logsHandler->updateTableLogs(Constants::LOG_TYPE_ERROR, $errMsg, 'ERROR ' . $code); |
||
240 | } |
||
241 | } |
||
242 | if ($error) { |
||
243 | //catch common errors |
||
244 | switch ($code) { |
||
245 | case 401: |
||
246 | $message = \_MA_WGGITHUB_READGH_ERROR_API_401; |
||
247 | break; |
||
248 | case 403: |
||
249 | /*if (\strpos($errMsg, 'API rate limit exceeded') > 0) {$GLOBALS['xoopsTpl']->assign('apiexceed', true);}*/ |
||
250 | $message = \_MA_WGGITHUB_READGH_ERROR_API_403; |
||
251 | break; |
||
252 | case 404: |
||
253 | $message = \_MA_WGGITHUB_READGH_ERROR_API_404; |
||
254 | break; |
||
255 | case 405: |
||
256 | $message = \_MA_WGGITHUB_READGH_ERROR_API_405; |
||
257 | break; |
||
258 | case 0: |
||
259 | default: |
||
260 | $message = \_MA_WGGITHUB_READGH_ERROR_API . '(' .$code . ' - ' . $errMsg . ')'; |
||
261 | break; |
||
262 | } |
||
263 | redirect_header('index.php?op=api_error&message='. $message . '&url='. $url, 5, $message); |
||
264 | //throw new \RuntimeException('"' . $message . '"'); |
||
265 | } else { |
||
266 | $data = (array)$api->decode($response); |
||
267 | } |
||
268 | |||
269 | return $data; |
||
270 | } |
||
271 | |||
272 | /** |
||
273 | * Execute update of repositories and all related tables |
||
274 | * @param string $dirName |
||
275 | * @return bool |
||
276 | */ |
||
277 | public function executeUpdate($dirName = '') |
||
278 | { |
||
279 | $helper = Helper::getInstance(); |
||
280 | $directoriesHandler = $helper->getHandler('Directories'); |
||
281 | $repositoriesHandler = $helper->getHandler('Repositories'); |
||
282 | $releasesHandler = $helper->getHandler('Releases'); |
||
283 | $readmesHandler = $helper->getHandler('Readmes'); |
||
284 | $logsHandler = $helper->getHandler('Logs'); |
||
285 | |||
286 | $logsHandler->updateTableLogs(Constants::LOG_TYPE_UPDATE_START, '', 'OK'); |
||
287 | $crDirectories = new \CriteriaCompo(); |
||
288 | if ('' !== $dirName) { |
||
289 | $crDirectories->add(new \Criteria('dir_name', $dirName)); |
||
290 | } else { |
||
291 | $crDirectories->add(new \Criteria('dir_autoupdate', 1)); |
||
292 | } |
||
293 | $crDirectories->add(new \Criteria('dir_online', 1)); |
||
294 | $directoriesAll = $directoriesHandler->getAll($crDirectories); |
||
295 | // Get All Directories |
||
296 | $directories = []; |
||
297 | foreach (\array_keys($directoriesAll) as $i) { |
||
298 | $directories[$i] = $directoriesAll[$i]->getValuesDirectories(); |
||
299 | $dirName = $directoriesAll[$i]->getVar('dir_name'); |
||
300 | $dirContent = $directoriesAll[$i]->getVar('dir_content'); |
||
301 | $repos = []; |
||
302 | for ($j = 1; $j <= 9; $j++) { |
||
303 | $repos[$j] = []; |
||
304 | if (Constants::DIRECTORY_TYPE_ORG == $directoriesAll[$i]->getVar('dir_type')) { |
||
305 | $repos = $this->getOrgRepositories($dirName, 100, $j); |
||
306 | } else { |
||
307 | $repos = $this->getUserRepositories($dirName, 100, $j); |
||
308 | } |
||
309 | if (false === $repos) { |
||
310 | return false; |
||
311 | break 1; |
||
0 ignored issues
–
show
|
|||
312 | } |
||
313 | if (\count($repos) > 0) { |
||
314 | $repositoriesHandler->updateTableRepositories($dirName, $repos, true, $dirContent); |
||
315 | } else { |
||
316 | break 1; |
||
317 | } |
||
318 | if (\count($repos) < 100) { |
||
319 | break 1; |
||
320 | } |
||
321 | } |
||
322 | } |
||
323 | unset($directories); |
||
324 | |||
325 | $releasesHandler->updateRepoReleases(); |
||
326 | $readmesHandler->updateRepoReadme(); |
||
327 | |||
328 | $logsHandler->updateTableLogs(Constants::LOG_TYPE_UPDATE_END, '', 'OK'); |
||
329 | |||
330 | return true; |
||
331 | } |
||
332 | |||
333 | /** |
||
334 | * Get primary setting |
||
335 | * |
||
336 | * @return bool|array |
||
337 | */ |
||
338 | private function getSetting() |
||
339 | { |
||
340 | $settingsHandler = $this->helper->getHandler('Settings'); |
||
341 | $setting = $settingsHandler->getPrimarySetting(); |
||
342 | |||
343 | if (0 == \count($setting)) { |
||
344 | \redirect_header(\XOOPS_URL . '/index.php', 3, \_AM_WGGITHUB_THEREARENT_SETTINGS); |
||
345 | } |
||
346 | //$this->userAuth = $setting['user']; |
||
347 | $this->tokenAuth = $setting['token']; |
||
348 | |||
349 | return true; |
||
350 | } |
||
351 | } |
||
352 |
The
break
statement is not necessary if it is preceded for example by areturn
statement:If you would like to keep this construct to be consistent with other
case
statements, you can safely mark this issue as a false-positive.