Import Versions with Github

This changes the import script to fetch the versions from Github instead of using Gitea.
This commit is contained in:
Refringe 2025-01-28 22:41:59 -05:00
parent bcd813cc18
commit 6cd98a617e
Signed by: Refringe
SSH Key Fingerprint: SHA256:t865XsQpfTeqPRBMN2G6+N8wlDjkgUCZF3WGW6O9N/k
2 changed files with 41 additions and 14 deletions

View File

@ -726,29 +726,57 @@ class ImportHubDataJob implements ShouldBeUnique, ShouldQueue
} }
/** /**
* Import the SPT versions from the Hub database to the local database. * Import the SPT versions from the public GitHub repo to the local database.
*
* @throws Exception
*/ */
protected function importSptVersions(): void protected function importSptVersions(): void
{ {
$domain = config('services.gitea.domain'); $url = 'https://api.github.com/repos/sp-tarkov/build/releases';
$token = config('services.gitea.token'); $token = config('services.github.token');
if (empty($domain) || empty($token)) { $ch = curl_init();
return; curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
'User-Agent: The Forge (forge.sp-tarkov.com)',
'Authorization: token '.$token,
]);
$response = curl_exec($ch);
if (curl_errno($ch)) {
throw new Exception('cURL Error: '.curl_error($ch));
} }
$url = "{$domain}/api/v1/repos/SPT/Stable-releases/releases?draft=false&pre-release=false&token={$token}"; curl_close($ch);
$response = json_decode(file_get_contents($url), true); $response = (array) json_decode($response, true);
if (json_last_error() !== JSON_ERROR_NONE) { if (json_last_error() !== JSON_ERROR_NONE) {
throw new Exception('JSON Decode Error: '.json_last_error_msg()); throw new Exception('JSON Decode Error: '.json_last_error_msg());
} }
if (empty($response)) { if (empty($response)) {
throw new Exception('No version data found in the API response.'); throw new Exception('No version data found in the GitHub API response.');
} }
// Filter out drafts and pre-releases.
$response = array_filter($response, function ($release) {
return ! $release['draft'] && ! $release['prerelease'];
});
if (empty($response)) {
throw new Exception('No finalized versions found after filtering drafts and pre-releases.');
}
// Ensure that each of the tag_name values has any 'v' prefix trimmed.
$response = array_map(function ($release) {
$release['tag_name'] = Str::of($release['tag_name'])->ltrim('v')->toString();
return $release;
}, $response);
$latestVersion = $this->getLatestVersion($response); $latestVersion = $this->getLatestVersion($response);
$insertData = []; $insertData = [];
@ -757,8 +785,8 @@ class ImportHubDataJob implements ShouldBeUnique, ShouldQueue
'version' => $version['tag_name'], 'version' => $version['tag_name'],
'link' => $version['html_url'], 'link' => $version['html_url'],
'color_class' => $this->detectVersionColor($version['tag_name'], $latestVersion), 'color_class' => $this->detectVersionColor($version['tag_name'], $latestVersion),
'created_at' => Carbon::parse($version['created_at'], 'UTC'), 'created_at' => Carbon::parse($version['published_at'], 'UTC'),
'updated_at' => Carbon::parse($version['created_at'], 'UTC'), 'updated_at' => Carbon::parse($version['published_at'], 'UTC'),
]; ];
} }
@ -771,7 +799,7 @@ class ImportHubDataJob implements ShouldBeUnique, ShouldQueue
'updated_at' => Carbon::now('UTC'), 'updated_at' => Carbon::now('UTC'),
]; ];
// Upsert won't work here. Do it manually. :( // Manually update or create
foreach ($insertData as $data) { foreach ($insertData as $data) {
$existingVersion = SptVersion::where('version', $data['version'])->first(); $existingVersion = SptVersion::where('version', $data['version'])->first();
if ($existingVersion) { if ($existingVersion) {

View File

@ -35,9 +35,8 @@ return [
], ],
], ],
'gitea' => [ 'github' => [
'domain' => env('GITEA_DOMAIN', ''), 'token' => env('GITHUB_TOKEN', ''),
'token' => env('GITEA_TOKEN', ''),
], ],
'discord' => [ 'discord' => [