Skip to content

Commit 6ef6801

Browse files
committed
hide token and removed stale spec files
1 parent bc7020c commit 6ef6801

1 file changed

Lines changed: 56 additions & 5 deletions

File tree

fetch-openapi-specs.php

Lines changed: 56 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,58 @@
1717
exit(1);
1818
}
1919

20+
function buildRequestUrl(string $baseUrl, array $params): string
21+
{
22+
return $baseUrl . '/index.php?' . http_build_query($params);
23+
}
24+
25+
function describeRequest(string $baseUrl, array $params): string
26+
{
27+
$descriptionParams = $params;
28+
unset($descriptionParams['token_auth']);
29+
30+
return buildRequestUrl($baseUrl, $descriptionParams);
31+
}
32+
33+
/**
34+
* @return list<string>
35+
*/
36+
function findExistingSpecFiles(string $targetDirectory): array
37+
{
38+
$files = glob($targetDirectory . '/*_openapi_spec_v*.json');
39+
if ($files === false) {
40+
throw new RuntimeException("Failed to list existing OpenAPI spec files in $targetDirectory");
41+
}
42+
43+
return array_values($files);
44+
}
45+
46+
function removeStaleSpecFiles(string $targetDirectory, array $expectedPaths): void
47+
{
48+
$expectedPaths = array_fill_keys($expectedPaths, true);
49+
50+
foreach (findExistingSpecFiles($targetDirectory) as $existingPath) {
51+
if (isset($expectedPaths[$existingPath])) {
52+
continue;
53+
}
54+
55+
if (!unlink($existingPath)) {
56+
throw new RuntimeException("Failed to remove stale file: $existingPath");
57+
}
58+
}
59+
}
60+
2061
function fetchJson(string $baseUrl, string $tokenAuth, array $params): array
2162
{
2263
$params['module'] = 'API';
2364
$params['format'] = 'JSON';
2465
$params['token_auth'] = $tokenAuth;
2566

26-
$url = $baseUrl . '/index.php?' . http_build_query($params);
67+
$url = buildRequestUrl($baseUrl, $params);
68+
$requestDescription = describeRequest($baseUrl, $params);
2769
$ch = curl_init($url);
2870
if ($ch === false) {
29-
throw new RuntimeException("Failed to initialize curl for $url");
71+
throw new RuntimeException("Failed to initialize curl for $requestDescription");
3072
}
3173

3274
curl_setopt_array($ch, [
@@ -40,19 +82,19 @@ function fetchJson(string $baseUrl, string $tokenAuth, array $params): array
4082
if ($response === false) {
4183
$error = curl_error($ch);
4284
curl_close($ch);
43-
throw new RuntimeException("Request failed for $url: $error");
85+
throw new RuntimeException("Request failed for $requestDescription: $error");
4486
}
4587

4688
$statusCode = curl_getinfo($ch, CURLINFO_RESPONSE_CODE);
4789
curl_close($ch);
4890

4991
if ($statusCode < 200 || $statusCode >= 300) {
50-
throw new RuntimeException("Request failed for $url with HTTP status $statusCode");
92+
throw new RuntimeException("Request failed for $requestDescription with HTTP status $statusCode");
5193
}
5294

5395
$decoded = json_decode($response, true);
5496
if (!is_array($decoded)) {
55-
throw new RuntimeException("Invalid JSON response: $url");
97+
throw new RuntimeException("Invalid JSON response for $requestDescription");
5698
}
5799

58100
if (isset($decoded['result']) && $decoded['result'] === 'error') {
@@ -73,6 +115,7 @@ function fetchJson(string $baseUrl, string $tokenAuth, array $params): array
73115
}
74116

75117
$written = 0;
118+
$expectedPaths = [];
76119

77120
foreach ($plugins as $plugin) {
78121
if (!is_string($plugin) || $plugin === '') {
@@ -86,6 +129,7 @@ function fetchJson(string $baseUrl, string $tokenAuth, array $params): array
86129
]);
87130

88131
$path = sprintf('%s/%s_openapi_spec_v%s.json', $targetDirectory, $plugin, $version);
132+
$expectedPaths[] = $path;
89133
$json = json_encode($spec, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES);
90134
if ($json === false) {
91135
throw new RuntimeException("Failed to encode JSON for plugin $plugin");
@@ -114,4 +158,11 @@ function fetchJson(string $baseUrl, string $tokenAuth, array $params): array
114158
}
115159
}
116160

161+
try {
162+
removeStaleSpecFiles($targetDirectory, $expectedPaths);
163+
} catch (Throwable $e) {
164+
fwrite(STDERR, "Failed to clean up stale spec files: {$e->getMessage()}\n");
165+
exit(1);
166+
}
167+
117168
echo "Wrote $written spec file(s) to $targetDirectory\n";

0 commit comments

Comments
 (0)