PreferredBranchSupport: for directorbranches

fixes #2688
fixes #2689
This commit is contained in:
Thomas Gelf 2022-12-16 14:16:02 +01:00
parent 0db7240c96
commit c643959d06
8 changed files with 92 additions and 25 deletions

View File

@ -18,6 +18,10 @@ This version hasn't been released yet
### Icinga Configuration
* FIX: render Set Services to individual zones where required (#1589, #2356)
### Configuration Branches
* FEATURE: with this release, directorbranches v1.3 supports a "default branch" (#2688)
* FEATURE: users with default branches get warnings in the main branch (#2689)
### Health Check
* FIX: complaint about overdue jobs was not correct (#2680, #2681)

View File

@ -6,6 +6,7 @@ use gipfl\Web\Widget\Hint;
use Icinga\Application\Hook;
use Icinga\Module\Director\Db\Branch\Branch;
use Icinga\Module\Director\Db\Branch\BranchStore;
use Icinga\Module\Director\Db\Branch\PreferredBranchSupport;
use Icinga\Module\Director\Hook\BranchSupportHook;
use ipl\Html\Html;
@ -19,6 +20,12 @@ class BranchesDashboard extends Dashboard
$this->translate('You\'re currently working in a Configuration Branch: %s'),
Branch::requireHook()->linkToBranch($branch, $this->getAuth(), $branch->getName())
)));
} else {
if (($implementation = Branch::optionalHook()) && $implementation instanceof PreferredBranchSupport) {
$this->prepend(Hint::warning(
$this->translate('You\'re currently working in the main Configuration Branch'),
));
}
}
return $this->translate('Prepare your configuration in a safe Environment');

View File

@ -0,0 +1,10 @@
<?php
namespace Icinga\Module\Director\Db\Branch;
use Icinga\Authentication\Auth;
interface PreferredBranchSupport
{
public function hasPreferredBranch(Auth $auth): bool;
}

View File

@ -3,10 +3,10 @@
namespace Icinga\Module\Director\Web\Controller;
use Icinga\Module\Director\Data\Db\DbObjectStore;
use Icinga\Module\Director\Data\Db\DbObjectTypeRegistry;
use Icinga\Module\Director\Db\Branch\Branch;
use Icinga\Module\Director\Db\Branch\BranchStore;
use Icinga\Module\Director\Db\Branch\BranchSupport;
use Icinga\Module\Director\Db\Branch\PreferredBranchSupport;
use Icinga\Module\Director\Objects\IcingaObject;
use Icinga\Module\Director\Web\Widget\NotInBranchedHint;
@ -18,6 +18,9 @@ trait BranchHelper
/** @var BranchStore */
protected $branchStore;
/** @var ?bool */
protected $hasPreferredBranch = null;
/**
* @return false|\Ramsey\Uuid\UuidInterface
*/
@ -73,4 +76,18 @@ trait BranchHelper
return false;
}
protected function hasPreferredBranch()
{
if ($this->hasPreferredBranch === null) {
$implementation = Branch::optionalHook();
if ($implementation instanceof PreferredBranchSupport) {
$this->hasPreferredBranch = $implementation->hasPreferredBranch($this->Auth());
} else {
$this->hasPreferredBranch = false;
}
}
return $this->hasPreferredBranch;
}
}

View File

@ -10,6 +10,7 @@ use Icinga\Exception\ProgrammingError;
use Icinga\Module\Director\Data\Db\DbObjectTypeRegistry;
use Icinga\Module\Director\Db\Branch\Branch;
use Icinga\Module\Director\Db\Branch\BranchedObject;
use Icinga\Module\Director\Db\Branch\BranchSupport;
use Icinga\Module\Director\Db\Branch\UuidLookup;
use Icinga\Module\Director\Deployment\DeploymentInfo;
use Icinga\Module\Director\DirectorObject\Automation\ExportInterface;
@ -151,8 +152,11 @@ abstract class ObjectController extends ActionController
$this->addObject();
}
$branch = $this->getBranch();
if ($branch->isBranch() && ! $this->getRequest()->isApiRequest()) {
$this->content()->add(new BranchedObjectHint($branch, $this->Auth()));
if (! $this->getRequest()->isApiRequest()) {
$hasPreferred = $this->hasPreferredBranch();
if ($branch->isBranch() || $hasPreferred) {
$this->content()->add(new BranchedObjectHint($branch, $this->Auth(), null, $hasPreferred));
}
}
$form->handleRequest();
@ -558,8 +562,16 @@ abstract class ObjectController extends ActionController
if (! $this->allowsObject($object)) {
throw new NotFoundError('No such object available');
}
if ($showHint && $branch->isBranch() && $object->isObject() && ! $this->getRequest()->isApiRequest()) {
$this->content()->add(new BranchedObjectHint($branch, $this->Auth(), $branchedObject));
if ($showHint) {
$hasPreferredBranch = $this->hasPreferredBranch();
if (($hasPreferredBranch || $branch->isBranch())
&& $object->isObject()
&& ! $this->getRequest()->isApiRequest()
) {
$this->content()->add(
new BranchedObjectHint($branch, $this->Auth(), $branchedObject, $hasPreferredBranch)
);
}
}
return $object;

View File

@ -124,7 +124,7 @@ abstract class ObjectsController extends ActionController
->addTitle($this->translate(ucfirst($this->getPluralType())))
->actions(new ObjectsActionBar($this->getBaseObjectUrl(), $this->url()));
$this->content()->add(new BranchedObjectsHint($this->getBranch(), $this->Auth()));
$this->content()->add(new BranchedObjectsHint($this->getBranch(), $this->Auth(), $this->hasPreferredBranch()));
if ($type === 'command' && $this->params->get('type') === 'external_object') {
$this->tabs()->activate('external');

View File

@ -15,33 +15,45 @@ class BranchedObjectHint extends HtmlDocument
{
use TranslationHelper;
public function __construct(Branch $branch, Auth $auth, BranchedObject $object = null)
public function __construct(Branch $branch, Auth $auth, BranchedObject $object = null, $hasPreferredBranch = false)
{
if (! $branch->isBranch()) {
return;
}
$hook = Branch::requireHook();
$name = $branch->getName();
if (substr($name, 0, 1) === '/') {
$label = $this->translate('this configuration branch');
if ($hasPreferredBranch) {
$main = true;
$hintMethod = 'warning';
$link = $this->translate('the main configuration branch');
$deployHint = ' ' . $this->translate('This will be part of the next deployment');
} else {
return;
}
} else {
$label = $name;
$main = false;
$hintMethod = 'info';
$deployHint = ' ' . $this->translate('This will not be part of any deployment, unless being merged');
$hook = Branch::requireHook();
$name = $branch->getName();
if (substr($name, 0, 1) === '/') {
$label = $this->translate('this configuration branch');
} else {
$label = $name;
}
$link = $hook->linkToBranch($branch, $auth, $label);
}
$link = $hook->linkToBranch($branch, $auth, $label);
if ($object === null) {
$this->add(Hint::info(Html::sprintf($this->translate(
'This object will be created in %s. It will not be part of any deployment'
. ' unless being merged'
), $link)));
$this->add(Hint::$hintMethod(Html::sprintf($this->translate(
'This object will be created in %s.'
) . $deployHint, $link)));
return;
}
if (! $object->hasBeenTouchedByBranch()) {
$this->add(Hint::info(Html::sprintf($this->translate(
'Your changes will be stored in %s. The\'ll not be part of any deployment'
. ' unless being merged'
), $link)));
$this->add(Hint::$hintMethod(Html::sprintf($this->translate(
'Your changes are going to be stored in %s.'
) . $deployHint, $link)));
return;
}
if ($main) {
return;
}

View File

@ -13,9 +13,14 @@ class BranchedObjectsHint extends HtmlDocument
{
use TranslationHelper;
public function __construct(Branch $branch, Auth $auth)
public function __construct(Branch $branch, Auth $auth, $hasPreferredBranch = false)
{
if (! $branch->isBranch()) {
if ($hasPreferredBranch) {
$this->add(Hint::warning($this->translate(
"You're currently in the master branch, your changes will make part of the next Deployment"
)));
}
return;
}
$hook = Branch::requireHook();