diff --git a/app/Http/Controllers/VersionController.php b/app/Http/Controllers/VersionController.php index ae62882..cb00494 100644 --- a/app/Http/Controllers/VersionController.php +++ b/app/Http/Controllers/VersionController.php @@ -4,8 +4,8 @@ namespace App\Http\Controllers; use GuzzleHttp\Client; use Illuminate\Http\Request; -use Illuminate\Support\Facades\Cache; -use Illuminate\Support\Facades\Log; +use Illuminate\Support\Facades\{Cache,Log}; +use Illuminate\Support\Str; use App\Models\Site; use App\Models\SiteVersion; @@ -14,8 +14,9 @@ class VersionController extends Controller { private const CACHE_TIME = 86400; // Time to cache version - private const GL_PROJECT = 2; // Gitlab project number - private const GL_URL = 'https://dev.dege.au/api/v4'; // Gitlab URL + private const GH_URL = 'https://api.github.com/repos'; // GitHub 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})$/'; @@ -39,26 +40,26 @@ class VersionController extends Controller case 'dev': $current = Cache::remember('dev',self::CACHE_TIME,function() { $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) { $result = collect(json_decode($response->getBody())); - return $result->first(); + return Str::limit($result->get('sha'),8,NULL); } return NULL; }); 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] - $response = ($matches[4] === $current->short_id) + $response = ($matches[4] === $current) ? ['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 $response = ['action'=>'unable','version'=>'vn.n.n-dev-hhhhhhhh']; @@ -68,7 +69,7 @@ class VersionController extends Controller case 'rel': $current = Cache::remember('rel',self::CACHE_TIME,function() { $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 $response = $client->request('GET',$url,['form_params'=>['ref_name'=>'master','sort'=>'desc']]); @@ -83,7 +84,8 @@ class VersionController extends Controller }); 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] < $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 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 else