Skip to content

Commit 6553ab7

Browse files
committed
Add tag enumerator
1 parent 35bb069 commit 6553ab7

File tree

1 file changed

+57
-0
lines changed

1 file changed

+57
-0
lines changed

src/TagListFetcher.php

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<?php
2+
3+
namespace PHPWatch\PHPCommitBuilder;
4+
5+
use Ayesh\CurlFetcher\CurlFetcher;
6+
7+
class TagListFetcher {
8+
private const API_ENDPOINT_TAG_LIST = 'https://api.github.com/repos/php/php-src/tags';
9+
10+
private const REGEX_TAG_PATTERN = '/^php-\d\.\d\.(?:\d\d?|0(?:alpha\d|beta\d|rc\d|RC\d)?)$/i';
11+
private ?string $apiKey = null;
12+
private CurlFetcher $curlFetcher;
13+
14+
public function __construct(string $apiKey = null) {
15+
$this->apiKey = $apiKey;
16+
$this->curlFetcher = new CurlFetcher();
17+
}
18+
19+
public function getReleaseTags(): array {
20+
$tags = $this->getAllTags();
21+
return array_filter($tags, static function(\stdClass $tag): bool {
22+
return (bool) preg_match(self::REGEX_TAG_PATTERN, $tag->name);
23+
});
24+
}
25+
26+
public function getAllTags(): array {
27+
$params = [
28+
'page' => 1,
29+
'per_page' => 100,
30+
];
31+
32+
$baseUrl = static::API_ENDPOINT_TAG_LIST;
33+
34+
$return = [];
35+
$hardLimits = 50;
36+
37+
do {
38+
$paramsUrl = http_build_query($params);
39+
$url = $baseUrl . '?' .$paramsUrl;
40+
41+
$headers = [];
42+
if ($this->apiKey) {
43+
$headers[] = 'Authorization: Bearer '. $this->apiKey;
44+
}
45+
46+
$data = $this->curlFetcher->getJson($url, $headers);
47+
/** @noinspection SlowArrayOperationsInLoopInspection */
48+
$return = array_merge($return, $data);
49+
50+
$params['page']++;
51+
$hardLimits--;
52+
53+
} while (count($data) >= 100 && $hardLimits > 0);
54+
55+
return $return;
56+
}
57+
}

0 commit comments

Comments
 (0)