Skip to content

Commit

Permalink
PreferredBranchSupport: for directorbranches
Browse files Browse the repository at this point in the history
fixes #2688
fixes #2689
  • Loading branch information
Thomas-Gelf committed Dec 16, 2022
1 parent 0db7240 commit c643959
Show file tree
Hide file tree
Showing 8 changed files with 92 additions and 25 deletions.
4 changes: 4 additions & 0 deletions doc/82-Changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
7 changes: 7 additions & 0 deletions library/Director/Dashboard/BranchesDashboard.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
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;

Expand All @@ -19,6 +20,12 @@ public function getTitle()
$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');
Expand Down
10 changes: 10 additions & 0 deletions library/Director/Db/Branch/PreferredBranchSupport.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace Icinga\Module\Director\Db\Branch;

use Icinga\Authentication\Auth;

interface PreferredBranchSupport
{
public function hasPreferredBranch(Auth $auth): bool;
}
19 changes: 18 additions & 1 deletion library/Director/Web/Controller/BranchHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -18,6 +18,9 @@ trait BranchHelper
/** @var BranchStore */
protected $branchStore;

/** @var ?bool */
protected $hasPreferredBranch = null;

/**
* @return false|\Ramsey\Uuid\UuidInterface
*/
Expand Down Expand Up @@ -73,4 +76,18 @@ protected function showNotInBranch($subject)

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;
}
}
20 changes: 16 additions & 4 deletions library/Director/Web/Controller/ObjectController.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
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;
Expand Down Expand Up @@ -151,8 +152,11 @@ public function addAction()
$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();
Expand Down Expand Up @@ -558,8 +562,16 @@ protected function loadSpecificObject($tableName, $key, $showHint = false)
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;
Expand Down
2 changes: 1 addition & 1 deletion library/Director/Web/Controller/ObjectsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ public function indexAction()
->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');
Expand Down
48 changes: 30 additions & 18 deletions library/Director/Web/Widget/BranchedObjectHint.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
7 changes: 6 additions & 1 deletion library/Director/Web/Widget/BranchedObjectsHint.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down

0 comments on commit c643959

Please sign in to comment.