Skip to content

Commit 9e3baac

Browse files
author
epriestley
committed
Restore old "author can not be a reviewer" rule to Transactions
Summary: This is a bit messy, but not tooo bad: - In general, stop the author from being added as a reviewer. - In the specific case that "self accept" is enabled, allow it. This is easier than trying to special case it. - When commandeering, we make the author a reviewer and make the actor the author, but these happen after validation. At validation time, it looks like we're making the author a reviewer. Just special case this. - Provide a slightly nicer message when trying to add yourself from `arc`. We hit the Transactions message anyway, but it's not formatted as cleanly. - Don't try to add the author via Herald. Test Plan: - Edited a revision with author = reviewer, got stopped. - Commandeered revision. - Updated from `arc`. - Updated in general. - Fired a "add author as reviewer" Herald rule without things breaking. Reviewers: btrahan Reviewed By: btrahan Subscribers: aran, epriestley Differential Revision: https://secure.phabricator.com/D8496
1 parent 95285ae commit 9e3baac

File tree

3 files changed

+75
-0
lines changed

3 files changed

+75
-0
lines changed

src/applications/differential/customfield/DifferentialReviewersField.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,4 +177,19 @@ public function renderCommitMessageValue(array $handles) {
177177
return $this->renderObjectList($handles);
178178
}
179179

180+
public function validateCommitMessageValue($value) {
181+
$author_phid = $this->getObject()->getAuthorPHID();
182+
183+
$config_self_accept_key = 'differential.allow-self-accept';
184+
$allow_self_accept = PhabricatorEnv::getEnvConfig($config_self_accept_key);
185+
186+
foreach ($value as $phid) {
187+
if (($phid == $author_phid) && !$allow_self_accept) {
188+
throw new DifferentialFieldValidationException(
189+
pht('The author of a revision can not be a reviewer.'));
190+
}
191+
}
192+
}
193+
194+
180195
}

src/applications/differential/editor/DifferentialTransactionEditor.php

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -395,10 +395,15 @@ protected function expandTransaction(
395395
);
396396
}
397397

398+
// NOTE: We're setting setIsCommandeerSideEffect() on this because
399+
// normally you can't add a revision's author as a reviewer, but
400+
// this action swaps them after validation executes.
401+
398402
$results[] = id(new DifferentialTransaction())
399403
->setTransactionType($type_edge)
400404
->setMetadataValue('edge:type', $edge_reviewer)
401405
->setIgnoreOnNoEffect(true)
406+
->setIsCommandeerSideEffect(true)
402407
->setNewValue($edits);
403408

404409
break;
@@ -625,8 +630,45 @@ protected function validateTransaction(
625630

626631
$errors = parent::validateTransaction($object, $type, $xactions);
627632

633+
$config_self_accept_key = 'differential.allow-self-accept';
634+
$allow_self_accept = PhabricatorEnv::getEnvConfig($config_self_accept_key);
635+
628636
foreach ($xactions as $xaction) {
629637
switch ($type) {
638+
case PhabricatorTransactions::TYPE_EDGE:
639+
switch ($xaction->getMetadataValue('edge:type')) {
640+
case PhabricatorEdgeConfig::TYPE_DREV_HAS_REVIEWER:
641+
642+
// Prevent the author from becoming a reviewer.
643+
644+
// NOTE: This is pretty gross, but this restriction is unusual.
645+
// If we end up with too much more of this, we should try to clean
646+
// this up -- maybe by moving validation to after transactions
647+
// are adjusted (so we can just examine the final value) or adding
648+
// a second phase there?
649+
650+
$author_phid = $object->getAuthorPHID();
651+
$new = $xaction->getNewValue();
652+
653+
$add = idx($new, '+', array());
654+
$eq = idx($new, '=', array());
655+
$phids = array_keys($add + $eq);
656+
657+
foreach ($phids as $phid) {
658+
if (($phid == $author_phid) &&
659+
!$allow_self_accept &&
660+
!$xaction->getIsCommandeerSideEffect()) {
661+
$errors[] =
662+
new PhabricatorApplicationTransactionValidationError(
663+
$type,
664+
pht('Invalid'),
665+
pht('The author of a revision can not be a reviewer.'),
666+
$xaction);
667+
}
668+
}
669+
break;
670+
}
671+
break;
630672
case DifferentialTransaction::TYPE_UPDATE:
631673
$diff = $this->loadDiff($xaction->getNewValue());
632674
if (!$diff) {
@@ -1340,6 +1382,12 @@ protected function didApplyHeraldRules(
13401382
$value = array();
13411383
foreach ($reviewers as $status => $phids) {
13421384
foreach ($phids as $phid) {
1385+
if ($phid == $object->getAuthorPHID()) {
1386+
// Don't try to add the revision's author as a reviewer, since this
1387+
// isn't valid and doesn't make sense.
1388+
continue;
1389+
}
1390+
13431391
$value['+'][$phid] = array(
13441392
'data' => array(
13451393
'status' => $status,

src/applications/differential/storage/DifferentialTransaction.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,18 @@
22

33
final class DifferentialTransaction extends PhabricatorApplicationTransaction {
44

5+
private $isCommandeerSideEffect;
6+
7+
8+
public function setIsCommandeerSideEffect($is_side_effect) {
9+
$this->isCommandeerSideEffect = $is_side_effect;
10+
return $this;
11+
}
12+
13+
public function getIsCommandeerSideEffect() {
14+
return $this->isCommandeerSideEffect;
15+
}
16+
517
const TYPE_INLINE = 'differential:inline';
618
const TYPE_UPDATE = 'differential:update';
719
const TYPE_ACTION = 'differential:action';

0 commit comments

Comments
 (0)