Now query github for current version of PLA

This commit is contained in:
Deon George 2024-01-10 16:09:42 +11:00
parent 2e74189da6
commit 9026462b54

View File

@ -4,8 +4,8 @@ namespace App\Http\Controllers;
use GuzzleHttp\Client; use GuzzleHttp\Client;
use Illuminate\Http\Request; use Illuminate\Http\Request;
use Illuminate\Support\Facades\Cache; use Illuminate\Support\Facades\{Cache,Log};
use Illuminate\Support\Facades\Log; use Illuminate\Support\Str;
use App\Models\Site; use App\Models\Site;
use App\Models\SiteVersion; use App\Models\SiteVersion;
@ -14,8 +14,9 @@ class VersionController extends Controller
{ {
private const CACHE_TIME = 86400; // Time to cache version private const CACHE_TIME = 86400; // Time to cache version
private const GL_PROJECT = 2; // Gitlab project number private const GH_URL = 'https://api.github.com/repos'; // GitHub URL
private const GL_URL = 'https://dev.dege.au/api/v4'; // Gitlab URL private const GH_PROJECT = 'leenooks/phpldapadmin';
private const GH_TREE = 'master';
private 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})$/';
@ -39,26 +40,26 @@ class VersionController extends Controller
case 'dev': case 'dev':
$current = Cache::remember('dev',self::CACHE_TIME,function() { $current = Cache::remember('dev',self::CACHE_TIME,function() {
$client = new Client; $client = new Client;
$url = sprintf('%s/projects/%d/repository/commits',self::GL_URL,self::GL_PROJECT); $url = sprintf('%s/%s/commits/%s',self::GH_URL,self::GH_PROJECT,self::GH_TREE);
$response = $client->request('GET',$url,['form_params'=>['ref_name'=>'BRANCH-2.0','order'=>'default']]); $response = $client->request('GET',$url,['form_params'=>['per_page'=>'1']]);
if ($response->getStatusCode() === 200) { if ($response->getStatusCode() === 200) {
$result = collect(json_decode($response->getBody())); $result = collect(json_decode($response->getBody()));
return $result->first(); return Str::limit($result->get('sha'),8,NULL);
} }
return NULL; return NULL;
}); });
if ($current) { if ($current) {
$repository = sprintf('v%s-rel-%s',$matches[1],$current->short_id); $repository = sprintf('v%s-rel-%s',$matches[1],$current);
// Find the tag associated with version $matches[1] and see if it is more recent than $matches[4] // Find the tag associated with version $matches[1] and see if it is more recent than $matches[4]
$response = ($matches[4] === $current->short_id) $response = ($matches[4] === $current)
? ['action'=>'current','version'=>$repository] ? ['action'=>'current','version'=>$repository]
: ['action'=>'upgrade','version'=>sprintf('v%s-%s-%s',$matches[1],$matches[3],$current->short_id)]; : ['action'=>'upgrade','version'=>sprintf('v%s-%s-%s',$matches[1],$matches[3],$current)];
} else } else
$response = ['action'=>'unable','version'=>'vn.n.n-dev-hhhhhhhh']; $response = ['action'=>'unable','version'=>'vn.n.n-dev-hhhhhhhh'];
@ -68,7 +69,7 @@ class VersionController extends Controller
case 'rel': case 'rel':
$current = Cache::remember('rel',self::CACHE_TIME,function() { $current = Cache::remember('rel',self::CACHE_TIME,function() {
$client = new Client; $client = new Client;
$url = sprintf('%s/projects/%d/repository/tags',self::GL_URL,self::GL_PROJECT); $url = sprintf('%s/%s/tags',self::GH_URL,self::GH_PROJECT);
// Find the tag associated with version $matches[1] and see if there is a more recent version number // Find the tag associated with version $matches[1] and see if there is a more recent version number
$response = $client->request('GET',$url,['form_params'=>['ref_name'=>'master','sort'=>'desc']]); $response = $client->request('GET',$url,['form_params'=>['ref_name'=>'master','sort'=>'desc']]);
@ -83,7 +84,8 @@ class VersionController extends Controller
}); });
if ($current) { if ($current) {
$repository = sprintf('v%s-rel-%s',$current->name,$current->commit->short_id); $sha = Str::limit($current->commit->sha,8,NULL);
$repository = sprintf('v%s-rel-%s',$current->name,$sha);
// If $matches[1] is smaller, "upgrade available" // If $matches[1] is smaller, "upgrade available"
if ($matches[1] < $current->name) if ($matches[1] < $current->name)
@ -91,7 +93,7 @@ class VersionController extends Controller
// If $matches[1] is the same, validate that $matches[4] is current and the same and if not, error // If $matches[1] is the same, validate that $matches[4] is current and the same and if not, error
elseif ($matches[1] === $current->name) elseif ($matches[1] === $current->name)
$response = ($matches[4] === $current->commit->short_id) ? ['action'=>'current','version'=>$repository] : ['action'=>'mismatch','version'=>$repository]; $response = ($matches[4] === $sha) ? ['action'=>'current','version'=>$repository] : ['action'=>'mismatch','version'=>$repository];
// if $matches[1] is higher, abort // if $matches[1] is higher, abort
else else