-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGitHubRefsCache.php
More file actions
127 lines (107 loc) · 3.6 KB
/
Copy pathGitHubRefsCache.php
File metadata and controls
127 lines (107 loc) · 3.6 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
115
116
117
118
119
120
121
122
123
124
125
126
127
<?php
declare(strict_types=1);
/**
* @return array<int, array{name: string, commit: string}>|null
*/
function normalizeGitHubRepositoryRefsCacheValue(mixed $refs): ?array
{
if (!is_array($refs)) {
return null;
}
$normalized = [];
foreach ($refs as $ref) {
if (!is_array($ref)) {
return null;
}
$name = $ref['name'] ?? null;
$commit = $ref['commit'] ?? null;
if ((!is_string($name) && !is_int($name)) || (!is_string($commit) && !is_int($commit))) {
return null;
}
$nameNormalized = trim((string) $name);
$commitNormalized = trim((string) $commit);
if ('' === $nameNormalized || '' === $commitNormalized) {
return null;
}
$normalized[] = [
'name' => $nameNormalized,
'commit' => $commitNormalized,
];
}
return $normalized;
}
function buildGitHubRepositoryRefsCacheFilePath(string $cacheDir, string $repo): string
{
return rtrim($cacheDir, '/').'/github-repository-refs-'.sha1($repo).'.php';
}
/**
* @return array{tags: array<int, array{name: string, commit: string}>, branches: array<int, array{name: string, commit: string}>}|null
*/
function readGitHubRepositoryRefsCache(string $cacheFile, int $ttl = 600): ?array
{
if (!is_file($cacheFile)) {
return null;
}
$modifiedAt = filemtime($cacheFile);
if (false === $modifiedAt || $modifiedAt < time() - $ttl) {
return null;
}
$cached = require $cacheFile;
if (!is_array($cached)) {
return null;
}
$tags = normalizeGitHubRepositoryRefsCacheValue($cached['tags'] ?? null);
$branches = normalizeGitHubRepositoryRefsCacheValue($cached['branches'] ?? null);
if (null === $tags || null === $branches) {
return null;
}
return [
'tags' => $tags,
'branches' => $branches,
];
}
/**
* @param array{tags: array<int, array{name: string, commit: string}>, branches: array<int, array{name: string, commit: string}>} $refs
*/
function writeGitHubRepositoryRefsCache(string $cacheFile, array $refs): void
{
$directory = dirname($cacheFile);
if (file_exists($directory) && !is_dir($directory)) {
throw new RuntimeException('GitHub cache directory cannot be created: '.$directory);
}
if (!\Oak\Engine\Installer\createDirectoryTree($directory, 0o755)) {
throw new RuntimeException('GitHub cache directory cannot be created: '.$directory);
}
$content = "<?php\n\ndeclare(strict_types=1);\n\nreturn ".var_export($refs, true).";\n";
if (false === @file_put_contents($cacheFile, $content, LOCK_EX)) {
throw new RuntimeException('GitHub cache file cannot be written: '.$cacheFile);
}
}
/**
* @return array{tags: array<int, array{name: string, commit: string}>, branches: array<int, array{name: string, commit: string}>}
*/
function getCachedGitHubRepositoryRefs(GitHubClient $client, string $repo, string $cacheDir, int $ttl = 600): array
{
$cacheFile = buildGitHubRepositoryRefsCacheFilePath($cacheDir, $repo);
$cached = readGitHubRepositoryRefsCache($cacheFile, $ttl);
if (null !== $cached) {
return $cached;
}
try {
$refs = [
'tags' => $client->getTags($repo),
'branches' => $client->getBranches($repo),
];
} catch (RuntimeException) {
return [
'tags' => [],
'branches' => [],
];
}
try {
writeGitHubRepositoryRefsCache($cacheFile, $refs);
} catch (RuntimeException $exception) {
error_log($exception->getMessage());
}
return $refs;
}