Change the response output format

This commit is contained in:
Deon George 2023-03-03 15:22:52 +11:00
parent 042889bcd5
commit ae113662db

View File

@ -12,12 +12,12 @@ use App\Models\SiteVersion;
class VersionController extends Controller
{
const CACHE_TIME = 86400; // Time to cache version
private const CACHE_TIME = 86400; // Time to cache version
const GL_PROJECT = 2; // Gitlab project number
const GL_URL = 'https://dev.dege.au/api/v4'; // Gitlab URL
private const GL_PROJECT = 2; // Gitlab project number
private const GL_URL = 'https://dev.dege.au/api/v4'; // Gitlab URL
const VERSION_REGEX = '/^v([0-9]+\.[0-9]+\.[0-9]+(\.[0-9]+)?)-([a-z]{3})-([a-z0-9]{8})$/';
private const VERSION_REGEX = '/^v([0-9]+\.[0-9]+\.[0-9]+(\.[0-9]+)?)-([a-z]{3})-([a-z0-9]{8})$/';
public function main(Request $request,string $version=NULL) {
// Our version is in the format of either:
@ -56,10 +56,12 @@ class VersionController extends Controller
$repository = sprintf('v%s-rel-%s',$matches[1],$current->short_id);
// Find the tag associated with version $matches[1] and see if it is more recent than $matches[4]
$response = ($matches[4] === $current->short_id) ? ['current'=>$repository] : ['upgrade'=>sprintf('v%s-%s-%s',$matches[1],$matches[3],$current->short_id)];
$response = ($matches[4] === $current->short_id)
? ['action'=>'current','version'=>$repository]
: ['action'=>'upgrade','version'=>sprintf('v%s-%s-%s',$matches[1],$matches[3],$current->short_id)];
} else
$response = ['unknown'=>'vn.n.n-dev-hhhhhhhh'];
$response = ['action'=>'unknown','version'=>'vn.n.n-dev-hhhhhhhh'];
break;
@ -85,18 +87,18 @@ class VersionController extends Controller
// If $matches[1] is smaller, "upgrade available"
if ($matches[1] < $current->name)
$response = ['upgrade'=>$repository];
$response = ['action'=>'upgrade','version'=>$repository];
// If $matches[1] is the same, validate that $matches[4] is current and the same and if not, error
elseif ($matches[1] === $current->name)
$response = ($matches[4] === $current->commit->short_id) ? ['current'=>$repository] : ['mismatch'=>$repository];
$response = ($matches[4] === $current->commit->short_id) ? ['action'=>'current','version'=>$repository] : ['action'=>'mismatch','version'=>$repository];
// if $matches[1] is higher, abort
else
$response = ['unknown'=>$repository];
$response = ['action'=>'unknown','version'=>$repository];
} else
$response = ['unknown'=>'vn.n.n-rel-hhhhhhhh'];
$response = ['action'=>'unknown','version'=>'vn.n.n-rel-hhhhhhhh'];
break;
}
@ -108,7 +110,7 @@ class VersionController extends Controller
}
// Return the current version
return ['unknown'=>'vn.n.n-xxxx-hhhhhhhh'];
return ['action'=>'unknown','version'=>'vn.n.n-xxxx-hhhhhhhh'];
}
public function getUserIpAddr(): string