From 4e1e4ee1e9c0bca573e71b447aedcb2cd019d819 Mon Sep 17 00:00:00 2001
From: Alexis Saettler <alexis@saettler.org>
Date: Wed, 11 May 2022 09:15:51 +0200
Subject: [PATCH] fix: skip version check if current version is empty (#6137)

---
 app/Console/Commands/PingVersionServer.php | 25 ++++++++++++++++++----
 1 file changed, 21 insertions(+), 4 deletions(-)

diff --git a/app/Console/Commands/PingVersionServer.php b/app/Console/Commands/PingVersionServer.php
index cc770755c96..8944854d14d 100644
--- a/app/Console/Commands/PingVersionServer.php
+++ b/app/Console/Commands/PingVersionServer.php
@@ -43,7 +43,7 @@ public function handle()
         }
 
         if (! $this->confirmToProceed('Checking version deactivated', function () {
-            return $this->getLaravel()->environment() == 'production';
+            return $this->getLaravel()->environment() === 'production';
         })) {
             return false;
         }
@@ -51,6 +51,12 @@ public function handle()
         $instance = Instance::first();
         $instance->current_version = config('monica.app_version');
 
+        if ($instance->current_version == '') {
+            Log::warning('Current instance version is not set, skipping version check.');
+
+            return;
+        }
+
         // Query version.monicahq.com
         try {
             $this->log('Call url: '.config('monica.weekly_ping_server_url'));
@@ -72,10 +78,10 @@ public function handle()
         $json = $response->json();
 
         $this->log('instance version: '.$instance->current_version);
-        $this->log('current version: '.$json['latest_version']);
+        $currentVersion = $this->getVersion($instance->current_version);
 
-        $latestVersion = new Version($json['latest_version']);
-        $currentVersion = new Version($instance->current_version);
+        $this->log('current version: '.$json['latest_version']);
+        $latestVersion = $this->getVersion($json['latest_version']);
 
         if ($latestVersion > $currentVersion) {
             $instance->latest_version = $json['latest_version'];
@@ -93,4 +99,15 @@ public function log($string)
     {
         $this->info($string, OutputInterface::VERBOSITY_VERBOSE);
     }
+
+    private function getVersion(string $version): ?Version
+    {
+        try {
+            return new Version($version);
+        } catch (\Exception $e) {
+            $this->error("Error parsing version '$version': ".$e->getMessage());
+        }
+
+        return null;
+    }
 }