new: [API] update command got new branch parameter

- instruct the update process to be prepended by a checkout of a given branch
- passed via a URL parameter (/servers/update/branch:develop)
OR
- passed via a JSON object ({"branch": "develop"})
pull/6898/head
iglocska 2021-01-20 14:21:31 +01:00
parent 6f8359a8c8
commit 8ee0555798
No known key found for this signature in database
GPG Key ID: BEA224F1FEF113AC
2 changed files with 25 additions and 4 deletions

View File

@ -1823,9 +1823,19 @@ class ServersController extends AppController
$result = $this->Server->checkoutMain();
}
public function update()
public function update($branch = false)
{
if ($this->request->is('post')) {
$branch = false;
$filterData = array(
'request' => $this->request,
'named_params' => $this->params['named'],
'paramArray' => ['branch'],
'ordered_url_params' => @compact($paramArray),
'additional_delimiters' => PHP_EOL
);
$exception = false;
$settings = $this->_harvestParameters($filterData, $exception);
$status = $this->Server->getCurrentGitStatus();
$raw = array();
if (empty($status['branch'])) { // do not try to update if you are not on branch
@ -1833,7 +1843,7 @@ class ServersController extends AppController
$raw[] = $msg;
$update = $msg;
} else {
$update = $this->Server->update($status, $raw);
$update = $this->Server->update($status, $raw, $settings);
}
if ($this->_isRest()) {
return $this->RestResponse->viewData(array('results' => $raw), $this->response->type());

View File

@ -3924,7 +3924,7 @@ class Server extends AppModel
return implode('\n', $result);
}
public function update(array $status, &$raw = array())
public function update(array $status, &$raw = [], array $settings = [])
{
$final = '';
$workingDirectoryPrefix = 'cd $(git rev-parse --show-toplevel) && ';
@ -3943,7 +3943,18 @@ class Server extends AppModel
);
$final .= implode("\n", $output) . "\n\n";
}
$command1 = $workingDirectoryPrefix . 'git pull origin ' . $status['branch'] . ' 2>&1';
if (!empty($settings['branch'])) {
$branchname = preg_match('/$[a-z0-9\_]+/i', $settings['branch']);
$checkout_command = $workingDirectoryPrefix . 'git checkout ' . escapeshellarg($branchname) . ' 2>&1';
exec($checkout_command, $output, $returnCode);
$raw[] = array(
'input' => $command1,
'output' => $output,
'status' => $returnCode,
);
$status = $this->getCurrentGitStatus();
}
$command1 = $workingDirectoryPrefix . 'git pull origin ' . escapeshellarg($status['branch']) . ' 2>&1';
$command2 = $workingDirectoryPrefix . 'git submodule update --init --recursive 2>&1';
$final .= $command1 . "\n\n";
$returnCode = false;