Skip to content

An open-source, self-hostable PaaS alternative to Vercel, Heroku & Netlify that lets you easily deploy static sites, databases, full-stack applications and 360+ one-click services on your own servers.

refactor: rename sync function and improve error handling

Andras Bacsai23a3b4701184
diff --git a/app/Console/Commands/SyncBunny.php b/app/Console/Commands/SyncBunny.php
index b0cd247150..1a76b33d10 100644
--- a/app/Console/Commands/SyncBunny.php
+++ b/app/Console/Commands/SyncBunny.php
@@ -26,9 +26,9 @@ class SyncBunny extends Command
protected $description = 'Sync files to BunnyCDN';
/**
- * Fetch GitHub releases and sync to CDN
+ * Fetch GitHub releases and sync to GitHub repository
*/
- private function syncGitHubReleases($parent_dir, $bunny_cdn_storage_name, $bunny_cdn_path, $bunny_cdn)
+ private function syncReleasesToGitHubRepo(): bool
{
$this->info('Fetching releases from GitHub...');
try {
@@ -37,33 +37,95 @@ class SyncBunny extends Command
'per_page' => 30, // Fetch more releases for better changelog
]);
- if ($response->successful()) {
- $releases = $response->json();
+ if (! $response->successful()) {
+ $this->error('Failed to fetch releases from GitHub: '.$response->status());
- // Save releases to a temporary file
- $releases_file = "$parent_dir/releases.json";
- file_put_contents($releases_file, json_encode($releases, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES));
+ return false;
+ }
- // Upload to CDN
- Http::pool(fn (Pool $pool) => [
- $pool->storage(fileName: $releases_file)->put("/$bunny_cdn_storage_name/$bunny_cdn_path/releases.json"),
- $pool->purge("$bunny_cdn/coolify/releases.json"),
- ]);
+ $releases = $response->json();
+ $timestamp = time();
+ $tmpDir = sys_get_temp_dir().'/coolify-cdn-'.$timestamp;
+ $branchName = 'update-releases-'.$timestamp;
- // Clean up temporary file
- unlink($releases_file);
+ // Clone the repository
+ $this->info('Cloning coolify-cdn repository...');
+ exec("gh repo clone coollabsio/coolify-cdn $tmpDir 2>&1", $output, $returnCode);
+ if ($returnCode !== 0) {
+ $this->error('Failed to clone repository: '.implode("\n", $output));
- $this->info('releases.json uploaded & purged...');
- $this->info('Total releases synced: '.count($releases));
+ return false;
+ }
- return true;
- } else {
- $this->error('Failed to fetch releases from GitHub: '.$response->status());
+ // Create feature branch
+ $this->info('Creating feature branch...');
+ exec("cd $tmpDir && git checkout -b $branchName 2>&1", $output, $returnCode);
+ if ($returnCode !== 0) {
+ $this->error('Failed to create branch: '.implode("\n", $output));
+ exec("rm -rf $tmpDir");
return false;
}
+
+ // Write releases.json
+ $this->info('Writing releases.json...');
+ $releasesPath = "$tmpDir/json/releases.json";
+ file_put_contents($releasesPath, json_encode($releases, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES));
+
+ // Stage and commit
+ $this->info('Committing changes...');
+ exec("cd $tmpDir && git add json/releases.json 2>&1", $output, $returnCode);
+ if ($returnCode !== 0) {
+ $this->error('Failed to stage changes: '.implode("\n", $output));
+ exec("rm -rf $tmpDir");
+
+ return false;
+ }
+
+ $commitMessage = 'Update releases.json with latest releases - '.date('Y-m-d H:i:s');
+ exec("cd $tmpDir && git commit -m '$commitMessage' 2>&1", $output, $returnCode);
+ if ($returnCode !== 0) {
+ $this->error('Failed to commit changes: '.implode("\n", $output));
+ exec("rm -rf $tmpDir");
+
+ return false;
+ }
+
+ // Push to remote
+ $this->info('Pushing branch to remote...');
+ exec("cd $tmpDir && git push origin $branchName 2>&1", $output, $returnCode);
+ if ($returnCode !== 0) {
+ $this->error('Failed to push branch: '.implode("\n", $output));
+ exec("rm -rf $tmpDir");
+
+ return false;
+ }
+
+ // Create pull request
+ $this->info('Creating pull request...');
+ $prTitle = 'Update releases.json - '.date('Y-m-d H:i:s');
+ $prBody = 'Automated update of releases.json with latest '.count($releases).' releases from GitHub API';
+ $prCommand = "gh pr create --repo coollabsio/coolify-cdn --title '$prTitle' --body '$prBody' --base main --head $branchName 2>&1";
+ exec($prCommand, $output, $returnCode);
+
+ // Clean up
+ exec("rm -rf $tmpDir");
+
+ if ($returnCode !== 0) {
+ $this->error('Failed to create PR: '.implode("\n", $output));
+
+ return false;
+ }
+
+ $this->info('Pull request created successfully!');
+ if (! empty($output)) {
+ $this->info('PR Output: '.implode("\n", $output));
+ }
+ $this->info('Total releases synced: '.count($releases));
+
+ return true;
} catch (\Throwable $e) {
- $this->error('Error fetching releases: '.$e->getMessage());
+ $this->error('Error syncing releases: '.$e->getMessage());
return false;
}
@@ -174,11 +236,7 @@ class SyncBunny extends Command
return;
}
- // First sync GitHub releases
- $this->info('Syncing GitHub releases first...');
- $this->syncGitHubReleases($parent_dir, $bunny_cdn_storage_name, $bunny_cdn_path, $bunny_cdn);
-
- // Then sync versions.json
+ // Sync versions.json to BunnyCDN
Http::pool(fn (Pool $pool) => [
$pool->storage(fileName: $versions_location)->put("/$bunny_cdn_storage_name/$bunny_cdn_path/$versions"),
$pool->purge("$bunny_cdn/$bunny_cdn_path/$versions"),
@@ -187,14 +245,14 @@ class SyncBunny extends Command
return;
} elseif ($only_github_releases) {
- $this->info('About to sync GitHub releases to BunnyCDN.');
+ $this->info('About to sync GitHub releases to GitHub repository.');
$confirmed = confirm('Are you sure you want to sync GitHub releases?');
if (! $confirmed) {
return;
}
- // Use the reusable function
- $this->syncGitHubReleases($parent_dir, $bunny_cdn_storage_name, $bunny_cdn_path, $bunny_cdn);
+ // Sync releases to GitHub repository
+ $this->syncReleasesToGitHubRepo();
return;
}