Index: src/applications/herald/adapter/HeraldCommitAdapter.php =================================================================== --- src/applications/herald/adapter/HeraldCommitAdapter.php +++ src/applications/herald/adapter/HeraldCommitAdapter.php @@ -64,19 +64,24 @@ if ($object instanceof PhabricatorRepository) { return true; } + if ($object instanceof PhabricatorProject) { + return true; + } return false; } public function getTriggerObjectPHIDs() { - return array( - $this->repository->getPHID(), - $this->getPHID(), - ); + return array_merge( + array( + $this->repository->getPHID(), + $this->getPHID(), + ), + $this->repository->getProjectPHIDs()); } public function explainValidTriggerObjects() { return pht( - 'This rule can trigger for **repositories**.'); + 'This rule can trigger for **repositories** and **projects**.'); } public function getFieldNameMap() { @@ -95,6 +100,7 @@ self::FIELD_COMMITTER, self::FIELD_REVIEWER, self::FIELD_REPOSITORY, + self::FIELD_REPOSITORY_PROJECTS, self::FIELD_DIFF_FILE, self::FIELD_DIFF_CONTENT, self::FIELD_DIFF_ADDED_CONTENT, @@ -177,6 +183,35 @@ return $object; } + public function setCommit(PhabricatorRepositoryCommit $commit) { + $viewer = PhabricatorUser::getOmnipotentUser(); + + $repository = id(new PhabricatorRepositoryQuery()) + ->setViewer($viewer) + ->withIDs(array($commit->getRepositoryID())) + ->needProjectPHIDs(true) + ->executeOne(); + if (!$repository) { + throw new Exception(pht('Unable to load repository!')); + } + + $data = id(new PhabricatorRepositoryCommitData())->loadOneWhere( + 'commitID = %d', + $commit->getID()); + if (!$data) { + throw new Exception(pht('Unable to load commit data!')); + } + + $this->commit = clone $commit; + $this->commit->attachRepository($repository); + $this->commit->attachCommitData($data); + + $this->repository = $repository; + $this->commitData = $data; + + return $this; + } + public function getPHID() { return $this->commit->getPHID(); } @@ -375,6 +410,8 @@ return $this->loadAffectedPaths(); case self::FIELD_REPOSITORY: return $this->repository->getPHID(); + case self::FIELD_REPOSITORY_PROJECTS: + return $this->repository->getProjectPHIDs(); case self::FIELD_DIFF_CONTENT: return $this->getDiffContent('*'); case self::FIELD_DIFF_ADDED_CONTENT: Index: src/applications/herald/adapter/HeraldDifferentialRevisionAdapter.php =================================================================== --- src/applications/herald/adapter/HeraldDifferentialRevisionAdapter.php +++ src/applications/herald/adapter/HeraldDifferentialRevisionAdapter.php @@ -64,6 +64,7 @@ self::FIELD_REVIEWERS, self::FIELD_CC, self::FIELD_REPOSITORY, + self::FIELD_REPOSITORY_PROJECTS, self::FIELD_DIFF_FILE, self::FIELD_DIFF_CONTENT, self::FIELD_DIFF_ADDED_CONTENT, @@ -152,32 +153,36 @@ if ($this->repository === null) { $this->repository = false; - // TODO: (T603) Implement policy stuff in Herald. $viewer = PhabricatorUser::getOmnipotentUser(); + $repository_phid = null; $revision = $this->revision; if ($revision->getRepositoryPHID()) { - $repositories = id(new PhabricatorRepositoryQuery()) + $repository_phid = $revision->getRepositoryPHID(); + } else { + $repository = id(new DifferentialRepositoryLookup()) ->setViewer($viewer) - ->withPHIDs(array($revision->getRepositoryPHID())) - ->execute(); - if ($repositories) { - $this->repository = head($repositories); - return $this->repository; + ->setDiff($this->diff) + ->lookupRepository(); + if ($repository) { + // We want to get the projects for this repository too, so run a + // full query for it below. + $repository_phid = $repository->getPHID(); } } - $repository = id(new DifferentialRepositoryLookup()) - ->setViewer($viewer) - ->setDiff($this->diff) - ->lookupRepository(); - if ($repository) { - $this->repository = $repository; - return $this->repository; + if ($repository_phid) { + $repository = id(new PhabricatorRepositoryQuery()) + ->setViewer($viewer) + ->withPHIDs(array($repository_phid)) + ->needProjectPHIDs(true) + ->executeOne(); + if ($repository) { + $this->repository = $repository; + } } - - $repository = false; } + return $this->repository; } @@ -345,6 +350,12 @@ return null; } return $repository->getPHID(); + case self::FIELD_REPOSITORY_PROJECTS: + $repository = $this->loadRepository(); + if (!$repository) { + return null; + } + return $repository->getProjectPHIDs(); case self::FIELD_DIFF_CONTENT: return $this->loadContentDictionary(); case self::FIELD_DIFF_ADDED_CONTENT: Index: src/applications/herald/controller/HeraldTestConsoleController.php =================================================================== --- src/applications/herald/controller/HeraldTestConsoleController.php +++ src/applications/herald/controller/HeraldTestConsoleController.php @@ -39,13 +39,8 @@ $object, $object->loadActiveDiff()); } else if ($object instanceof PhabricatorRepositoryCommit) { - $data = id(new PhabricatorRepositoryCommitData())->loadOneWhere( - 'commitID = %d', - $object->getID()); - $adapter = HeraldCommitAdapter::newLegacyAdapter( - $object->getRepository(), - $object, - $data); + $adapter = id(new HeraldCommitAdapter()) + ->setCommit($object); } else if ($object instanceof ManiphestTask) { $adapter = id(new HeraldManiphestTaskAdapter()) ->setTask($object); Index: src/applications/herald/storage/HeraldRule.php =================================================================== --- src/applications/herald/storage/HeraldRule.php +++ src/applications/herald/storage/HeraldRule.php @@ -17,7 +17,7 @@ protected $isDisabled = 0; protected $triggerObjectPHID; - protected $configVersion = 26; + protected $configVersion = 27; // phids for which this rule has been applied private $ruleApplied = self::ATTACHABLE; Index: src/applications/repository/worker/PhabricatorRepositoryCommitHeraldWorker.php =================================================================== --- src/applications/repository/worker/PhabricatorRepositoryCommitHeraldWorker.php +++ src/applications/repository/worker/PhabricatorRepositoryCommitHeraldWorker.php @@ -42,10 +42,8 @@ 'or no longer exists.')); } - $adapter = HeraldCommitAdapter::newLegacyAdapter( - $repository, - $commit, - $data); + $adapter = id(new HeraldCommitAdapter()) + ->setCommit($commit); $rules = id(new HeraldRuleQuery()) ->setViewer(PhabricatorUser::getOmnipotentUser())