Newer
Older
<?php
/**
* Copyright Maarch since 2008 under licence GPLv3.
* See LICENCE.txt file at the root folder for more details.
* This file is part of Maarch software.
*
*/
/**
* @brief Version Update Controller
* @author dev@maarch.org
*/
namespace VersionUpdate\controllers;
use Gitlab\Client;
use Group\models\ServiceModel;
use Slim\Http\Request;
use Slim\Http\Response;
use SrcCore\models\CoreConfigModel;
class VersionUpdateController
{
public function get(Request $request, Response $response)
{
if (!ServiceModel::hasService(['id' => 'admin_update_control', 'userId' => $GLOBALS['userId'], 'location' => 'apps', 'type' => 'admin'])) {
return $response->withStatus(403)->withJson(['errors' => 'Service forbidden']);
}
$client = Client::create('https://labs.maarch.org/api/v4/');
try {
$tags = $client->api('tags')->all('12');
} catch (\Exception $e) {
return $response->withJson(['errors' => $e->getMessage()]);
}
$applicationVersion = CoreConfigModel::getApplicationVersion();
return $response->withStatus(400)->withJson(['errors' => "Can't load xml applicationVersion"]);
$currentVersion = $applicationVersion['applicationMinorVersion'];
}
$currentVersionBranch = substr($currentVersion, 0, 5);
$currentVersionBranchYear = substr($currentVersion, 0, 2);
$currentVersionBranchMonth = substr($currentVersion, 3, 2);
$currentVersionTag = substr($currentVersion, 6);
$availableMajorVersions = [];
foreach ($tags as $value) {
if (!preg_match("/^\d{2}\.\d{2}\.\d+$/", $value['name'])) {
continue;
}
$tag = substr($value['name'], 6);
$pos = strpos($value['name'], $currentVersionBranch);
if ($pos === false) {
$year = substr($value['name'], 0, 2);
$month = substr($value['name'], 3, 2);
if (($year == $currentVersionBranchYear && $month > $currentVersionBranchMonth) || $year > $currentVersionBranchYear) {
$availableMajorVersions[] = $value['name'];
}
} else {
if ($tag > $currentVersionTag) {
natcasesort($availableMinorVersions);
natcasesort($availableMajorVersions);
if (empty($availableMinorVersions)) {
$lastAvailableMinorVersion = null;
} else {
$lastAvailableMinorVersion = $availableMinorVersions[0];
}
if (empty($availableMajorVersions)) {
$lastAvailableMajorVersion = null;
} else {
$lastAvailableMajorVersion = $availableMajorVersions[0];
}
$output = [];
$diff = exec('git diff', $output);
$stagedDiff = exec('git diff --staged', $output);
'lastAvailableMinorVersion' => $lastAvailableMinorVersion,
'lastAvailableMajorVersion' => $lastAvailableMajorVersion,
'currentVersion' => $currentVersion,
'canUpdate' => empty($output) && empty($diff) && empty($stagedDiff)
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
public function update(Request $request, Response $response)
{
if (!ServiceModel::hasService(['id' => 'admin_update_control', 'userId' => $GLOBALS['userId'], 'location' => 'apps', 'type' => 'admin'])) {
return $response->withStatus(403)->withJson(['errors' => 'Service forbidden']);
}
$client = Client::create('https://labs.maarch.org/api/v4/');
try {
$tags = $client->api('tags')->all('12');
} catch (\Exception $e) {
return $response->withJson(['errors' => $e->getMessage()]);
}
$applicationVersion = CoreConfigModel::getApplicationVersion();
if (empty($applicationVersion)) {
return $response->withStatus(400)->withJson(['errors' => "Can't load xml applicationVersion"]);
} else {
$currentVersion = $applicationVersion['applicationMinorVersion'];
}
$currentVersionBranch = substr($currentVersion, 0, 5);
$currentVersionTag = substr($currentVersion, 6);
$availableMinorVersions = [];
foreach ($tags as $value) {
if (!preg_match("/^\d{2}\.\d{2}\.\d+$/", $value['name'])) {
continue;
}
$tag = substr($value['name'], 6);
$pos = strpos($value['name'], $currentVersionBranch);
if ($pos !== false && $tag > $currentVersionTag) {
$availableMinorVersions[] = $value['name'];
}
}
if (empty($availableMinorVersions)) {
return $response->withStatus(400)->withJson(['errors' => 'No minor versions available']);
}
natcasesort($availableMinorVersions);
$minorVersion = $availableMinorVersions[0];
$output = [];
$diff = exec('git diff', $output);
$stagedDiff = exec('git diff --staged', $output);
if (!empty($output) || !empty($diff) || !empty($stagedDiff)) {
return $response->withStatus(400)->withJson(['errors' => 'Some files are modified. Can not update application']);
}
$fetch = exec('git fetch');
$checkout = exec("git checkout {$minorVersion}");
return $response->withStatus(204);
}