Commit 3cb3aeeb authored by Daniel Stone's avatar Daniel Stone Committed by Ana Rute Mendes

LOCAL/POLICY: Maniphest: Auto-assign purchasing tasks to approver

Enforce a local policy, that on purchasing tasks, the approver will be
auto-assigned if there is no assignee.

The 'correct' fix would probably be a Herald rule, if it were actually
possible to implement. An EditEngine extension might work as well, but
this was easier.
parent d057f7f0
......@@ -374,7 +374,8 @@ final class ManiphestTransactionEditor
$any_assign = false;
foreach ($xactions as $xaction) {
if ($xaction->getTransactionType() ==
ManiphestTaskOwnerTransaction::TRANSACTIONTYPE) {
ManiphestTaskOwnerTransaction::TRANSACTIONTYPE &&
!empty($xaction->getNewValue())) {
$any_assign = true;
break;
}
......@@ -423,6 +424,85 @@ final class ManiphestTransactionEditor
}
}
// CCU: If this is a purchasing type, then try to assign the task to the
// assignee, if empty. It's not overlay pleasant code, since we need
// to iterate the transactions, rather than interrogate an object.
if ($this->getIsNewObject() && !$any_assign) {
$subtype = null;
$approver = null;
$purchasing_project = null;
foreach ($xactions as $xaction) {
switch ($xaction->getTransactionType()) {
case PhabricatorTransactions::TYPE_SUBTYPE:
$subtype = $xaction->getNewValue();
break;
case PhabricatorTransactions::TYPE_CUSTOMFIELD:
if ($xaction->getMetadataValue('customfield:key') == 'std:maniphest:purchasing:approver') {
foreach ($xaction->getRequiredHandlePHIDs() as $p) {
if (!empty($p)) {
$approver = $p;
break;
}
}
}
if ($xaction->getMetadataValue('customfield:key') == 'std:maniphest:purchasing:project') {
foreach ($xaction->getRequiredHandlePHIDs() as $p) {
if (!empty($p)) {
$purchasing_project = $p;
break;
}
}
}
break;
}
}
if ($subtype == 'purchasing') {
if ($approver == null && $purchasing_project !== null) {
$p_proj = id(new PhabricatorProjectQuery())
->setViewer($this->getActor())
->withPHIDs(array($purchasing_project))
->executeOne();
if ($p_proj) {
$fields = PhabricatorCustomField::getObjectFields($p_proj,
PhabricatorCustomField::ROLE_VIEW);
$fields->readFieldsFromStorage($p_proj);
$fields->setViewer($this->getActor());
foreach ($fields->getFields() as $field) {
if ($field->getFieldKey() == 'std:project:ccu:purchasing-approver') {
$phids = $field->getRequiredHandlePHIDsForPropertyView();
foreach ($phids as $k => $a) {
if ($a) {
$approver = $a;
}
}
}
}
}
if ($approver !== null) {
$results[] = id(new ManiphestTransaction())
->setTransactionType(PhabricatorTransactions::TYPE_CUSTOMFIELD)
->attachObject($object)
->attachViewer($this->getActor())
->setMetadataValue('customfield:key', 'std:maniphest:purchasing:approver')
->setNewValue(array($approver));
}
}
if ($approver !== null) {
$results[] = id(new ManiphestTransaction())
->setTransactionType(ManiphestTaskOwnerTransaction::TRANSACTIONTYPE)
->setNewValue($approver);
}
}
}
return $results;
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment