Added version to credits and metrics page

This commit is contained in:
Igor Scheller 2019-07-21 02:34:52 +02:00
parent b9cb7d57fd
commit 4582f808f0
15 changed files with 179 additions and 16 deletions

View File

@ -38,7 +38,9 @@ build-image:
<<: *docker_definition
stage: build
script:
- docker build --pull --build-arg NGINX_IMAGE="${TEST_IMAGE}-nginx" -t "${TEST_IMAGE}" -f contrib/Dockerfile .
- apk -q add git
- VERSION="$(git describe --abbrev=0 --tags)-${CI_COMMIT_REF_NAME}+${CI_PIPELINE_ID}.${CI_COMMIT_SHORT_SHA}"
- docker build --pull --build-arg NGINX_IMAGE="${TEST_IMAGE}-nginx" --build-arg VERSION="${VERSION}" -t "${TEST_IMAGE}" -f contrib/Dockerfile .
- docker push "${TEST_IMAGE}"
test:

View File

@ -27,6 +27,7 @@ return [
\Engelsystem\Middleware\SessionHandlerServiceProvider::class,
// Additional services
\Engelsystem\Helpers\VersionServiceProvider::class,
\Engelsystem\Mail\MailerServiceProvider::class,
],

View File

@ -32,6 +32,9 @@ COPY --from=composer /app/composer.lock /app/
RUN find /app/storage/ -type f -not -name .gitignore -exec rm {} \;
RUN rm -f /app/import/* /app/config/config.php
ARG VERSION
RUN if [[ ! -f /app/storage/app/VERSION ]] && [[ ! -z "${VERSION}" ]]; then echo -n "${VERSION}" > /app/storage/app/VERSION; fi
# Build the PHP container
FROM php:7-fpm-alpine
WORKDIR /var/www

View File

@ -15,6 +15,7 @@
<div class="col-md-4">
<h2>Source code</h2>
<p>Version: <i>{{ version }}</i></p>
<p>
The original engelsystem was written by
<a href="https://github.com/cookieBerlin/engelsystem">cookie</a>.

View File

@ -111,6 +111,7 @@ class Application extends Container
$this->instance('path.lang', $this->get('path.resources') . DIRECTORY_SEPARATOR . 'lang');
$this->instance('path.views', $this->get('path.resources') . DIRECTORY_SEPARATOR . 'views');
$this->instance('path.storage', $appPath . DIRECTORY_SEPARATOR . 'storage');
$this->instance('path.storage.app', $this->get('path.storage') . DIRECTORY_SEPARATOR . 'app');
$this->instance('path.cache', $this->get('path.storage') . DIRECTORY_SEPARATOR . 'cache');
$this->instance('path.cache.routes', $this->get('path.cache') . DIRECTORY_SEPARATOR . 'routes.cache.php');
$this->instance('path.cache.views', $this->get('path.cache') . DIRECTORY_SEPARATOR . 'views');

View File

@ -3,6 +3,7 @@
namespace Engelsystem\Controllers;
use Engelsystem\Config\Config;
use Engelsystem\Helpers\Version;
use Engelsystem\Http\Response;
class CreditsController extends BaseController
@ -13,14 +14,19 @@ class CreditsController extends BaseController
/** @var Response */
protected $response;
/** @var Version */
protected $version;
/**
* @param Response $response
* @param Config $config
* @param Version $version
*/
public function __construct(Response $response, Config $config)
public function __construct(Response $response, Config $config, Version $version)
{
$this->config = $config;
$this->response = $response;
$this->version = $version;
}
/**
@ -30,7 +36,10 @@ class CreditsController extends BaseController
{
return $this->response->withView(
'pages/credits.twig',
['credits' => $this->config->get('credits')]
[
'credits' => $this->config->get('credits'),
'version' => $this->version->getVersion(),
]
);
}
}

View File

@ -4,6 +4,7 @@ namespace Engelsystem\Controllers\Metrics;
use Engelsystem\Config\Config;
use Engelsystem\Controllers\BaseController;
use Engelsystem\Helpers\Version;
use Engelsystem\Http\Exceptions\HttpForbidden;
use Engelsystem\Http\Request;
use Engelsystem\Http\Response;
@ -26,25 +27,31 @@ class Controller extends BaseController
/** @var Stats */
protected $stats;
/** @var Version */
protected $version;
/**
* @param Response $response
* @param MetricsEngine $engine
* @param Config $config
* @param Request $request
* @param Stats $stats
* @param Version $version
*/
public function __construct(
Response $response,
MetricsEngine $engine,
Config $config,
Request $request,
Stats $stats
Stats $stats,
Version $version
) {
$this->config = $config;
$this->engine = $engine;
$this->request = $request;
$this->response = $response;
$this->stats = $stats;
$this->version = $version;
}
/**
@ -68,6 +75,18 @@ class Controller extends BaseController
$data = [
$this->config->get('app_name') . ' stats',
'info' => [
'type' => 'gauge',
'help' => 'About the environment',
[
'labels' => [
'os' => PHP_OS_FAMILY,
'php' => implode('.', [PHP_MAJOR_VERSION, PHP_MINOR_VERSION]),
'version' => $this->version->getVersion(),
],
'value' => 1,
],
],
'users' => [
'type' => 'gauge',
['labels' => ['state' => 'incoming'], 'value' => $this->stats->newUsers()],

42
src/Helpers/Version.php Normal file
View File

@ -0,0 +1,42 @@
<?php
namespace Engelsystem\Helpers;
use Engelsystem\Config\Config;
class Version
{
/** @var Config */
protected $config;
/** @vat string */
protected $storage;
/** @var string */
protected $versionFile = 'VERSION';
/**
* @param string $storage
* @param Config $config
*/
public function __construct(string $storage, Config $config)
{
$this->storage = $storage;
$this->config = $config;
}
/**
* @return string
*/
public function getVersion()
{
$file = $this->storage . DIRECTORY_SEPARATOR . $this->versionFile;
$version = 'n/a';
if (file_exists($file)) {
$version = trim(file_get_contents($file));
}
return $this->config->get('version', $version);
}
}

View File

@ -0,0 +1,15 @@
<?php
namespace Engelsystem\Helpers;
use Engelsystem\Container\ServiceProvider;
class VersionServiceProvider extends ServiceProvider
{
public function register()
{
$this->app->when(Version::class)
->needs('$storage')
->give($this->app->get('path.storage.app'));
}
}

2
storage/app/.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
/*
!.gitignore

View File

@ -4,9 +4,10 @@ namespace Engelsystem\Test\Unit\Controllers;
use Engelsystem\Config\Config;
use Engelsystem\Controllers\CreditsController;
use Engelsystem\Helpers\Version;
use Engelsystem\Http\Response;
use Engelsystem\Test\Unit\TestCase;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
class CreditsControllerTest extends TestCase
{
@ -19,12 +20,17 @@ class CreditsControllerTest extends TestCase
/** @var Response|MockObject $response */
$response = $this->createMock(Response::class);
$config = new Config(['foo' => 'bar', 'credits' => ['lor' => 'em']]);
/** @var Version|MockObject $version */
$version = $this->createMock(Version::class);
$response->expects($this->once())
->method('withView')
->with('pages/credits.twig', ['credits' => ['lor' => 'em']]);
$this->setExpects(
$response,
'withView',
['pages/credits.twig', ['credits' => ['lor' => 'em'], 'version' => '42.1.0-test']]
);
$this->setExpects($version, 'getVersion', [], '42.1.0-test');
$controller = new CreditsController($response, $config);
$controller = new CreditsController($response, $config, $version);
$controller->index();
}
}

View File

@ -6,6 +6,7 @@ use Engelsystem\Config\Config;
use Engelsystem\Controllers\Metrics\Controller;
use Engelsystem\Controllers\Metrics\MetricsEngine;
use Engelsystem\Controllers\Metrics\Stats;
use Engelsystem\Helpers\Version;
use Engelsystem\Http\Exceptions\HttpForbidden;
use Engelsystem\Http\Request;
use Engelsystem\Http\Response;
@ -28,7 +29,8 @@ class ControllerTest extends TestCase
/** @var MetricsEngine|MockObject $engine */
/** @var Stats|MockObject $stats */
/** @var Config $config */
list($response, $request, $engine, $stats, $config) = $this->getMocks();
/** @var Version|MockObject $version */
list($response, $request, $engine, $stats, $config, $version) = $this->getMocks();
$request->server = new ServerBag();
$request->server->set('REQUEST_TIME_FLOAT', 0.0123456789);
@ -37,6 +39,7 @@ class ControllerTest extends TestCase
->method('get')
->willReturnCallback(function ($path, $data) use ($response) {
$this->assertEquals('/metrics', $path);
$this->assertArrayHasKey('info', $data);
$this->assertArrayHasKey('users', $data);
$this->assertArrayHasKey('licenses', $data);
$this->assertArrayHasKey('users_working', $data);
@ -122,7 +125,9 @@ class ControllerTest extends TestCase
'XL' => 'X Large',
]);
$controller = new Controller($response, $engine, $config, $request, $stats);
$this->setExpects($version, 'getVersion', [], '0.42.42');
$controller = new Controller($response, $engine, $config, $request, $stats, $version);
$controller->metrics();
}
@ -137,7 +142,8 @@ class ControllerTest extends TestCase
/** @var MetricsEngine|MockObject $engine */
/** @var Stats|MockObject $stats */
/** @var Config $config */
list($response, $request, $engine, $stats, $config) = $this->getMocks();
/** @var Version|MockObject $version */
list($response, $request, $engine, $stats, $config, $version) = $this->getMocks();
$response->expects($this->once())
->method('withHeader')
@ -168,7 +174,7 @@ class ControllerTest extends TestCase
$this->setExpects($stats, 'arrivedUsers', null, 10, $this->exactly(2));
$this->setExpects($stats, 'currentlyWorkingUsers', null, 5);
$controller = new Controller($response, $engine, $config, $request, $stats);
$controller = new Controller($response, $engine, $config, $request, $stats, $version);
$controller->stats();
}
@ -182,7 +188,8 @@ class ControllerTest extends TestCase
/** @var MetricsEngine|MockObject $engine */
/** @var Stats|MockObject $stats */
/** @var Config $config */
list($response, $request, $engine, $stats, $config) = $this->getMocks();
/** @var Version|MockObject $version */
list($response, $request, $engine, $stats, $config, $version) = $this->getMocks();
$request->expects($this->once())
->method('get')
@ -191,7 +198,7 @@ class ControllerTest extends TestCase
$config->set('api_key', 'fooBar!');
$controller = new Controller($response, $engine, $config, $request, $stats);
$controller = new Controller($response, $engine, $config, $request, $stats, $version);
$this->expectException(HttpForbidden::class);
$this->expectExceptionMessage(json_encode(['error' => 'The api_key is invalid']));
@ -212,7 +219,8 @@ class ControllerTest extends TestCase
/** @var Stats|MockObject $stats */
$stats = $this->createMock(Stats::class);
$config = new Config();
$version = $this->createMock(Version::class);
return [$response, $request, $engine, $stats, $config];
return [$response, $request, $engine, $stats, $config, $version];
}
}

View File

@ -0,0 +1 @@
0.42.0-testing

View File

@ -0,0 +1,25 @@
<?php
namespace Engelsystem\Test\Unit\Helpers;
use Engelsystem\Application;
use Engelsystem\Helpers\Version;
use Engelsystem\Helpers\VersionServiceProvider;
use Engelsystem\Test\Unit\ServiceProviderTest;
class VersionServiceProviderTest extends ServiceProviderTest
{
/**
* @covers \Engelsystem\Helpers\VersionServiceProvider::register
*/
public function testRegister()
{
$app = new Application();
$app->instance('path.storage.app', '/tmp');
$serviceProvider = new VersionServiceProvider($app);
$serviceProvider->register();
$this->assertArrayHasKey(Version::class, $app->contextual);
}
}

View File

@ -0,0 +1,28 @@
<?php
namespace Engelsystem\Test\Unit\Helpers;
use Engelsystem\Config\Config;
use Engelsystem\Helpers\Version;
use Engelsystem\Test\Unit\ServiceProviderTest;
class VersionTest extends ServiceProviderTest
{
/**
* @covers \Engelsystem\Helpers\Version::__construct
* @covers \Engelsystem\Helpers\Version::getVersion
*/
public function testGetVersion()
{
$config = new Config();
$version = new Version(__DIR__ . '/Stub', $config);
$this->assertEquals('n/a', $version->getVersion());
$version = new Version(__DIR__ . '/Stub/files', $config);
$this->assertEquals('0.42.0-testing', $version->getVersion());
$config->set('version', '1.2.3-dev');
$this->assertEquals('1.2.3-dev', $version->getVersion());
}
}