Skip to content

Commit e0231ad

Browse files
committed
Introduce envvar support for info
1 parent 5aab82c commit e0231ad

15 files changed

Lines changed: 408 additions & 99 deletions

.github/workflows/code_coverage.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ jobs:
1313
php-version: 8.2
1414
coverage: pcov
1515

16+
# Implicitly runs `composer tests` in post update cmd. So PHPCS, MD and other static analysis is executed at
17+
# this point.
1618
- run: composer install --no-progress
1719

1820
- run: vendor/bin/phpunit --coverage-clover build/logs/clover.xml

composer.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
},
1515
"require-dev": {
1616
"php-parallel-lint/php-parallel-lint": "^1.3",
17-
"phpmd/phpmd": "^2.6",
17+
"phpmd/phpmd": "^2.13",
1818
"matthiasnoback/symfony-config-test": "^4.3",
1919
"phpdocumentor/reflection-docblock": "^5.2",
2020
"phpunit/php-token-stream": "^3.1.3|^4.0.4",
@@ -37,11 +37,11 @@
3737
"scripts": {
3838
"tests": {
3939
"docheader": "vendor/bin/docheader check src/",
40-
"phpcmd": "vendor/bin/phpmd src text phpmd.xml",
4140
"phpcs": "vendor/bin/phpcs src --report=full --standard=phpcs.xml --extensions=php --warning-severity=0",
4241
"phpcpd": "vendor/bin/phpcpd src --exclude=src/Tests/*",
43-
"phpunit": "vendor/bin/phpunit --coverage-text"
44-
},
42+
"phpunit": "vendor/bin/phpunit --coverage-text",
43+
"phpmd": "vendor/bin/phpmd src text phpmd.xml --exclude 'src/Tests/*'"
44+
},
4545
"post-update-cmd": [
4646
"@tests"
4747
]

phpmd.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@
3030
<rule ref="rulesets/naming.xml">
3131
<exclude name="ShortVariable" />
3232
<exclude name="LongVariable" />
33+
<exclude name="LongClassName" />
34+
<exclude name="ShortClassName" />
3335
</rule>
3436

3537
</ruleset>

src/Controller/HealthController.php

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,9 @@
3030
*/
3131
class HealthController extends AbstractController
3232
{
33-
/**
34-
* @var HealthCheckChain
35-
*/
36-
private $healthChecker;
37-
38-
public function __construct(HealthCheckChain $healthChecker)
39-
{
40-
$this->healthChecker = $healthChecker;
33+
public function __construct(
34+
private readonly HealthCheckChain $healthChecker
35+
) {
4136
}
4237

4338
public function __invoke(): JsonResponse

src/Controller/InfoController.php

Lines changed: 15 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
namespace OpenConext\MonitorBundle\Controller;
2020

21+
use OpenConext\MonitorBundle\Value\BuildInformationFactory;
2122
use OpenConext\MonitorBundle\Value\BuildPathFactory;
2223
use OpenConext\MonitorBundle\Value\Information;
2324
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
@@ -41,34 +42,16 @@
4142
*/
4243
class InfoController extends AbstractController
4344
{
44-
/**
45-
* @var string
46-
*/
47-
private $buildPath;
48-
49-
/**
50-
* @var string
51-
*/
52-
private $environment;
53-
54-
/**
55-
* @var bool
56-
*/
57-
private $debuggerEnabled;
58-
59-
/**
60-
* @var array
61-
*/
62-
private $systemInfo = [];
45+
private array $systemInfo = [];
6346

6447
public function __construct(
65-
$buildPath,
66-
$environment,
67-
$debuggerEnabled
48+
private readonly string $buildPath,
49+
private readonly string $environment,
50+
private readonly bool $debuggerEnabled,
51+
private readonly ?string $version,
52+
private readonly ?string $revision,
53+
private readonly ?string $commitDate,
6854
) {
69-
$this->buildPath = $buildPath;
70-
$this->environment = $environment;
71-
$this->debuggerEnabled = $debuggerEnabled;
7255

7356
if (function_exists('opcache_get_status')) {
7457
$this->systemInfo['opcache'] = opcache_get_status(false);
@@ -77,8 +60,14 @@ public function __construct(
7760

7861
public function __invoke(): JsonResponse
7962
{
63+
$buildInformation = BuildInformationFactory::build(
64+
$this->version,
65+
$this->revision,
66+
$this->commitDate,
67+
$this->buildPath
68+
);
8069
$info = Information::buildFrom(
81-
BuildPathFactory::buildFrom($this->buildPath),
70+
$buildInformation,
8271
$this->environment,
8372
$this->debuggerEnabled,
8473
$this->systemInfo

src/DependencyInjection/Compiler/HealthCheckPass.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public function process(ContainerBuilder $container)
3838

3939
$definition = $container->findDefinition('openconext.monitor.health_check_chain');
4040

41-
// find all service IDs with the app.mail_transport tag
41+
// find all service IDs with the openconext.monitor.health_check tag
4242
$taggedServices = $container->findTaggedServiceIds('openconext.monitor.health_check');
4343

4444
foreach ($taggedServices as $id => $tags) {

src/Resources/config/services.yml

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,17 @@ services:
55
- '~'
66
- '%kernel.environment%'
77
- '%kernel.debug%'
8-
public: true
8+
- '%env(default::string:OPENCONEXT_APP_VERSION)%'
9+
- '%env(default::string:OPENCONEXT_GIT_SHA)%'
10+
- '%env(default::string:OPENCONEXT_COMMIT_DATE)%'
11+
912
autowire: true
1013
tags: ['controller.service_arguments', 'container.service_subscriber']
1114

1215
openconext.monitor.controller.health:
1316
class: OpenConext\MonitorBundle\Controller\HealthController
1417
arguments:
1518
- '@openconext.monitor.health_check_chain'
16-
public: true
1719
autowire: true
1820
tags: ['controller.service_arguments', 'container.service_subscriber']
1921

@@ -30,4 +32,4 @@ services:
3032
calls:
3133
- [ setEntityManager, ['@?doctrine.orm.entity_manager']]
3234
tags:
33-
- { name: openconext.monitor.health_check }
35+
- { name: openconext.monitor.health_check }
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<?php
2+
3+
/**
4+
* Copyright 2024 SURFnet B.V.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
namespace OpenConext\MonitorBundle\Tests\Value;
20+
21+
use OpenConext\MonitorBundle\Value\BuildEnvVars;
22+
use OpenConext\MonitorBundle\Value\BuildInformationFactory;
23+
use OpenConext\MonitorBundle\Value\BuildPath;
24+
use PHPUnit\Framework\TestCase;
25+
26+
class BuildInformationFactoryTest extends TestCase
27+
{
28+
public function test_builds_based_on_env_vars()
29+
{
30+
$buildInformation = BuildInformationFactory::build(
31+
'1.0.0',
32+
'e97bbfc39653cb5541d453e704c64a7fab4f5b1',
33+
'1900-01-01',
34+
'My-Application-da39a3ee5e6b4b0d3255bfef95601890afd80709'
35+
);
36+
$this->assertInstanceOf(BuildEnvVars::class, $buildInformation);
37+
}
38+
public function test_builds_based_on_env_vars_commit_date_optional()
39+
{
40+
$buildInformation = BuildInformationFactory::build(
41+
'1.0.0',
42+
'e97bbfc39653cb5541d453e704c64a7fab4f5b1',
43+
null,
44+
'My-Application-da39a3ee5e6b4b0d3255bfef95601890afd80709'
45+
);
46+
$this->assertInstanceOf(BuildEnvVars::class, $buildInformation);
47+
}
48+
public function test_builds_on_path()
49+
{
50+
$buildInformation = BuildInformationFactory::build(
51+
null,
52+
null,
53+
null,
54+
'My-Application-da39a3ee5e6b4b0d3255bfef95601890afd80709'
55+
);
56+
$this->assertInstanceOf(BuildPath::class, $buildInformation);
57+
}
58+
}
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
<?php
2+
3+
/**
4+
* Copyright 2024 SURFnet B.V.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
namespace OpenConext\MonitorBundle\Tests\Value;
20+
21+
use OpenConext\MonitorBundle\Value\BuildEnvVars;
22+
use OpenConext\MonitorBundle\Value\BuildEnvVarsFactory;
23+
use OpenConext\MonitorBundle\Value\BuildInformation;
24+
use PHPUnit\Framework\TestCase;
25+
26+
class EnvVarFactoryTest extends TestCase
27+
{
28+
public function test_happy_flow()
29+
{
30+
$expectedSemverVersion = '1.1.0';
31+
$expectedRevision = 'e3f5a1f0';
32+
$expectedDate = '2024-01-01';
33+
$envVarVO = BuildEnvVarsFactory::buildFrom($expectedSemverVersion, $expectedRevision, $expectedDate);
34+
$this->assertInstanceOf(BuildInformation::class, $envVarVO);
35+
$this->assertInstanceOf(BuildEnvVars::class, $envVarVO);
36+
$this->assertTrue($envVarVO->hasRevision() && $envVarVO->hasVersion() && $envVarVO->hasCommitDate());
37+
$this->assertEquals($expectedSemverVersion, $envVarVO->getVersion());
38+
$this->assertEquals($expectedRevision, $envVarVO->getRevision());
39+
$this->assertEquals($expectedDate, $envVarVO->getCommitDate());
40+
}
41+
42+
/**
43+
* @dataProvider buildValidVariants
44+
*/
45+
public function test_happy_flow_variants(
46+
string $expectedSemverVersion,
47+
string $expectedRevision,
48+
?string $expectedDate = null,
49+
) {
50+
$envVarVO = BuildEnvVarsFactory::buildFrom($expectedSemverVersion, $expectedRevision, $expectedDate);
51+
$this->assertInstanceOf(BuildInformation::class, $envVarVO);
52+
$this->assertInstanceOf(BuildEnvVars::class, $envVarVO);
53+
$this->assertEquals($expectedSemverVersion, $envVarVO->getVersion());
54+
$this->assertEquals($expectedRevision, $envVarVO->getRevision());
55+
$this->assertEquals($expectedDate, $envVarVO->getCommitDate());
56+
}
57+
58+
public function buildValidVariants()
59+
{
60+
return [
61+
'all set' => ['1.1.0', 'e97bbfc3', '2000-01-01'],
62+
'all set, long date' => ['1.1.0', 'e97bbfc3', '2000-01-01 00:00:00'],
63+
'no date' => ['1.1.0', 'e97bbfc3'],
64+
'full commit hash' => ['1.1.0', 'e97bbfc39653cb5541d453e704c64a7fab4f5b1'],
65+
'non-semver hash' => ['beta-1', 'e97bbfc39653cb5541d453e704c64a7fab4f5b1'],
66+
];
67+
}
68+
69+
}

src/Value/BuildEnvVars.php

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
<?php
2+
3+
/**
4+
* Copyright 2024 SURFnet B.V.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
namespace OpenConext\MonitorBundle\Value;
20+
21+
/**
22+
* Representation of the env vars that describe the build details
23+
*
24+
* By default, the following env vars are evaluated:
25+
*
26+
* OPENCONEXT_APP_VERSION | maps to $version | required
27+
* OPENCONEXT_GIT_SHA | maps to $revision | required
28+
* OPENCONEXT_COMMIT_DATE | maps to $commitDate | optional
29+
*
30+
* This VO is created by its factory who actually reads the env-vars
31+
*/
32+
class BuildEnvVars implements BuildInformation
33+
{
34+
public function __construct(
35+
private readonly string $version,
36+
private readonly string $revision,
37+
private readonly ?string $commitDate,
38+
) {
39+
}
40+
41+
public function getVersion(): string
42+
{
43+
return $this->version;
44+
}
45+
46+
public function getRevision(): string
47+
{
48+
return $this->revision;
49+
}
50+
51+
public function getCommitDate(): ?string
52+
{
53+
return $this->commitDate;
54+
}
55+
56+
public function hasRevision(): bool
57+
{
58+
return true;
59+
}
60+
61+
public function getPath(): string
62+
{
63+
return '';
64+
}
65+
66+
public function hasVersion(): bool
67+
{
68+
return true;
69+
}
70+
71+
public function hasPath(): bool
72+
{
73+
return false;
74+
}
75+
76+
public function hasCommitDate(): bool
77+
{
78+
return !is_null($this->commitDate);
79+
}
80+
81+
public function jsonSerialize(): mixed
82+
{
83+
$data = [
84+
'version' => $this->version,
85+
'revision' => $this->revision,
86+
];
87+
if ($this->hasCommitDate()) {
88+
$data['commitDate'] = $this->commitDate;
89+
}
90+
91+
return $data;
92+
}
93+
}

0 commit comments

Comments
 (0)