1
0
Fork 0
mirror of https://we.phorge.it/source/phorge.git synced 2025-01-26 22:48:19 +01:00

Make implicit audits by the Owners tool use modern code

Summary:
Ref T10978. This updates audits triggered by Owners to use a modern transaction. Minor changes:

  - After D17264, we no longer need the "AUDIT_NOT_REQUIRED" fake-audits to record package membership. This no longer creates them.
  - This previously saved English-language, untranslatable text strings about audit details onto the audit relationship. I've removed them, per discussion in D17263.

The "Audit Reasons" here are potentially a little more useful than the Herald/Explicit-By-Owner ones were, since the rules are a little more complex, but I'd still like to see evidence that we need them.

In particular, the transaction record now says "Owners added auditors: ...", just like Differential, so the source of the auditors should be clear:

{F2549087}

T11118 (roughly "add several Owners audit modes", despite the title at time of writing) might impact this too. Basically, this is simple and maybe good enough; if it's not quite good enough we can refine it.

Test Plan: Ran `bin/repository reparse --owners <commit>` saw appropriate owners audits trigger.

Reviewers: chad

Reviewed By: chad

Maniphest Tasks: T10978

Differential Revision: https://secure.phabricator.com/D17266
This commit is contained in:
epriestley 2017-01-30 10:14:15 -08:00
parent 5e7a091737
commit 2e9cc5e8e8

View file

@ -28,6 +28,7 @@ final class PhabricatorRepositoryCommitOwnersWorker
private function triggerOwnerAudits( private function triggerOwnerAudits(
PhabricatorRepository $repository, PhabricatorRepository $repository,
PhabricatorRepositoryCommit $commit) { PhabricatorRepositoryCommit $commit) {
$viewer = PhabricatorUser::getOmnipotentUser();
if (!$repository->shouldPublish()) { if (!$repository->shouldPublish()) {
return; return;
@ -48,16 +49,23 @@ final class PhabricatorRepositoryCommitOwnersWorker
return; return;
} }
$data = id(new PhabricatorRepositoryCommitData())->loadOneWhere( $commit = id(new DiffusionCommitQuery())
'commitID = %d', ->setViewer($viewer)
$commit->getID()); ->withPHIDs(array($commit->getPHID()))
$commit->attachCommitData($data); ->needCommitData(true)
->needAuditRequests(true)
->executeOne();
if (!$commit) {
return;
}
$data = $commit->getCommitData();
$author_phid = $data->getCommitDetail('authorPHID'); $author_phid = $data->getCommitDetail('authorPHID');
$revision_id = $data->getCommitDetail('differential.revisionID'); $revision_id = $data->getCommitDetail('differential.revisionID');
if ($revision_id) { if ($revision_id) {
$revision = id(new DifferentialRevisionQuery()) $revision = id(new DifferentialRevisionQuery())
->setViewer(PhabricatorUser::getOmnipotentUser()) ->setViewer($viewer)
->withIDs(array($revision_id)) ->withIDs(array($revision_id))
->needReviewerStatus(true) ->needReviewerStatus(true)
->executeOne(); ->executeOne();
@ -65,13 +73,10 @@ final class PhabricatorRepositoryCommitOwnersWorker
$revision = null; $revision = null;
} }
$requests = id(new PhabricatorRepositoryAuditRequest()) $requests = $commit->getAudits();
->loadAllWhere(
'commitPHID = %s',
$commit->getPHID());
$requests = mpull($requests, null, 'getAuditorPHID'); $requests = mpull($requests, null, 'getAuditorPHID');
$auditor_phids = array();
foreach ($affected_packages as $package) { foreach ($affected_packages as $package) {
$request = idx($requests, $package->getPHID()); $request = idx($requests, $package->getPHID());
if ($request) { if ($request) {
@ -79,61 +84,78 @@ final class PhabricatorRepositoryCommitOwnersWorker
continue; continue;
} }
if ($package->getAuditingEnabled()) { $should_audit = $this->shouldTriggerAudit(
$reasons = $this->checkAuditReasons(
$commit, $commit,
$package, $package,
$author_phid, $author_phid,
$revision); $revision);
if (!$should_audit) {
if ($reasons) { continue;
$audit_status = PhabricatorAuditStatusConstants::AUDIT_REQUIRED;
} else {
$audit_status = PhabricatorAuditStatusConstants::AUDIT_NOT_REQUIRED;
}
} else {
$reasons = array();
$audit_status = PhabricatorAuditStatusConstants::NONE;
} }
$relationship = new PhabricatorRepositoryAuditRequest(); $auditor_phids[] = $package->getPHID();
$relationship->setAuditorPHID($package->getPHID());
$relationship->setCommitPHID($commit->getPHID());
$relationship->setAuditReasons($reasons);
$relationship->setAuditStatus($audit_status);
$relationship->save();
$requests[$package->getPHID()] = $relationship;
} }
$commit->updateAuditStatus($requests); // If none of the packages are triggering audits, we're all done.
$commit->save(); if (!$auditor_phids) {
return;
} }
private function checkAuditReasons( $audit_type = DiffusionCommitAuditorsTransaction::TRANSACTIONTYPE;
$owners_phid = id(new PhabricatorOwnersApplication())
->getPHID();
$content_source = $this->newContentSource();
$xactions = array();
$xactions[] = $commit->getApplicationTransactionTemplate()
->setTransactionType($audit_type)
->setNewValue(
array(
'+' => array_fuse($auditor_phids),
));
$editor = $commit->getApplicationTransactionEditor()
->setActor($viewer)
->setActingAsPHID($owners_phid)
->setContinueOnNoEffect(true)
->setContinueOnMissingFields(true)
->setContentSource($content_source);
$editor->applyTransactions($commit, $xactions);
}
private function shouldTriggerAudit(
PhabricatorRepositoryCommit $commit, PhabricatorRepositoryCommit $commit,
PhabricatorOwnersPackage $package, PhabricatorOwnersPackage $package,
$author_phid, $author_phid,
$revision) { $revision) {
// Don't trigger an audit if auditing isn't enabled for the package.
if (!$package->getAuditingEnabled()) {
return false;
}
// Trigger an audit if we don't recognize the commit's author.
if (!$author_phid) {
return true;
}
$owner_phids = PhabricatorOwnersOwner::loadAffiliatedUserPHIDs( $owner_phids = PhabricatorOwnersOwner::loadAffiliatedUserPHIDs(
array( array(
$package->getID(), $package->getID(),
)); ));
$owner_phids = array_fuse($owner_phids); $owner_phids = array_fuse($owner_phids);
$reasons = array(); // Don't trigger an audit if the author is a package owner.
if (isset($owner_phids[$author_phid])) {
if (!$author_phid) { return false;
$reasons[] = pht('Commit Author Not Recognized');
} else if (isset($owner_phids[$author_phid])) {
return $reasons;
} }
// Trigger an audit of there is no corresponding revision.
if (!$revision) { if (!$revision) {
$reasons[] = pht('No Revision Specified'); return true;
return $reasons;
} }
$accepted_statuses = array( $accepted_statuses = array(
@ -159,11 +181,13 @@ final class PhabricatorRepositoryCommitOwnersWorker
} }
} }
if (!$found_accept) { // Don't trigger an audit if a package owner already reviewed the
$reasons[] = pht('Owners Not Involved'); // revision.
if ($found_accept) {
return false;
} }
return $reasons; return true;
} }
} }