chg: [internal] Use GitTool for remote version fetching

pull/7959/head
Jakub Onderka 2021-11-14 09:34:29 +01:00
parent db3183ae54
commit e41a8a785e
2 changed files with 59 additions and 16 deletions

40
app/Lib/Tools/GitTool.php Normal file
View File

@ -0,0 +1,40 @@
<?php
class GitTool
{
/**
* @param HttpSocketExtended $HttpSocket
* @return array
* @throws HttpSocketHttpException
* @throws HttpSocketJsonException
*/
public static function getRemoveVersion(HttpSocketExtended $HttpSocket)
{
$url = 'https://api.github.com/repos/MISP/MISP/tags?per_page=10';
$response = $HttpSocket->get($url);
if (!$response->isOk()) {
throw new HttpSocketHttpException($response, $url);
}
return $response->json();
}
/**
* @param HttpSocketExtended $HttpSocket
* @return string
* @throws HttpSocketHttpException
* @throws HttpSocketJsonException
*/
public static function getRemoteCommit(HttpSocketExtended $HttpSocket)
{
$url = 'https://api.github.com/repos/MISP/MISP/commits?per_page=1';
$response = $HttpSocket->get($url);
if (!$response->isOk()) {
throw new HttpSocketHttpException($response, $url);
}
$data = $response->json();
if (!isset($data[0]['sha'])) {
throw new Exception("Response do not contains requested data.");
}
return $data[0]['sha'];
}
}

View File

@ -4,6 +4,7 @@ App::uses('GpgTool', 'Tools');
App::uses('ServerSyncTool', 'Tools');
App::uses('SystemSetting', 'Model');
App::uses('EncryptedValue', 'Tools');
App::uses('GitTool', 'Tools');
/**
* @property-read array $serverSettings
@ -3817,37 +3818,39 @@ class Server extends AppModel
/**
* Fetch latest MISP version from GitHub
* @return array|false
* @throws HttpSocketJsonException
* @throws JsonException
*/
public function checkRemoteVersion()
{
$HttpSocket = $this->setupHttpSocket(null, null, 3);
try {
$response = $HttpSocket->get('https://api.github.com/repos/MISP/MISP/tags?per_page=10');
$json_decoded_tags = GitTool::getRemoveVersion($HttpSocket);
} catch (Exception $e) {
return false;
}
if ($response->isOK()) {
$json_decoded_tags = $response->json();
// find the latest version tag in the v[major].[minor].[hotfix] format
foreach ($json_decoded_tags as $tag)
if (preg_match('/^v[0-9]+\.[0-9]+\.[0-9]+$/', $tag['name'])) {
return $this->checkVersion($tag['name']);
}
// find the latest version tag in the v[major].[minor].[hotfix] format
foreach ($json_decoded_tags as $tag) {
if (preg_match('/^v[0-9]+\.[0-9]+\.[0-9]+$/', $tag['name'])) {
return $this->checkVersion($tag['name']);
}
}
return false;
}
public function getCurrentGitStatus()
{
$latestCommit = exec('timeout 3 git ls-remote https://github.com/MISP/MISP | head -1 | sed "s/HEAD//"');
$HttpSocket = $this->setupHttpSocket(null, null, 3);
try {
$latestCommit = GitTool::getRemoteCommit($HttpSocket);
} catch (Exception $e) {
$latestCommit = false;
}
$status = array();
$status['commit'] = $this->checkMIPSCommit();
$status['branch'] = $this->getCurrentBranch();
$status['latestCommit'] = $latestCommit;
return $status;
return [
'commit' => $this->checkMIPSCommit(),
'branch' => $this->getCurrentBranch(),
'latestCommit' => $latestCommit,
];
}
public function getCurrentBranch()