1
0
Fork 0
mirror of https://we.phorge.it/source/phorge.git synced 2025-01-01 18:30:59 +01:00

Support "Repository's projects" field in Commit and Differential Revision rules

Summary: This also cleans up some code a little bit. Most of the gymnastics are to make sure we call `needProjectPHIDs()` appropriately.

Test Plan: Created new commit and revision rules with this field. Ran commits and revisions through the test console. Field behavior seemed correct.

Reviewers: btrahan

Reviewed By: btrahan

CC: aran, dctrwatson

Differential Revision: https://secure.phabricator.com/D7923
This commit is contained in:
epriestley 2014-01-09 15:56:24 -08:00
parent c020837397
commit efe187d5be
5 changed files with 74 additions and 33 deletions

View file

@ -64,19 +64,24 @@ final class HeraldCommitAdapter extends HeraldAdapter {
if ($object instanceof PhabricatorRepository) { if ($object instanceof PhabricatorRepository) {
return true; return true;
} }
if ($object instanceof PhabricatorProject) {
return true;
}
return false; return false;
} }
public function getTriggerObjectPHIDs() { public function getTriggerObjectPHIDs() {
return array( return array_merge(
$this->repository->getPHID(), array(
$this->getPHID(), $this->repository->getPHID(),
); $this->getPHID(),
),
$this->repository->getProjectPHIDs());
} }
public function explainValidTriggerObjects() { public function explainValidTriggerObjects() {
return pht( return pht(
'This rule can trigger for **repositories**.'); 'This rule can trigger for **repositories** and **projects**.');
} }
public function getFieldNameMap() { public function getFieldNameMap() {
@ -95,6 +100,7 @@ final class HeraldCommitAdapter extends HeraldAdapter {
self::FIELD_COMMITTER, self::FIELD_COMMITTER,
self::FIELD_REVIEWER, self::FIELD_REVIEWER,
self::FIELD_REPOSITORY, self::FIELD_REPOSITORY,
self::FIELD_REPOSITORY_PROJECTS,
self::FIELD_DIFF_FILE, self::FIELD_DIFF_FILE,
self::FIELD_DIFF_CONTENT, self::FIELD_DIFF_CONTENT,
self::FIELD_DIFF_ADDED_CONTENT, self::FIELD_DIFF_ADDED_CONTENT,
@ -177,6 +183,35 @@ final class HeraldCommitAdapter extends HeraldAdapter {
return $object; 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() { public function getPHID() {
return $this->commit->getPHID(); return $this->commit->getPHID();
} }
@ -375,6 +410,8 @@ final class HeraldCommitAdapter extends HeraldAdapter {
return $this->loadAffectedPaths(); return $this->loadAffectedPaths();
case self::FIELD_REPOSITORY: case self::FIELD_REPOSITORY:
return $this->repository->getPHID(); return $this->repository->getPHID();
case self::FIELD_REPOSITORY_PROJECTS:
return $this->repository->getProjectPHIDs();
case self::FIELD_DIFF_CONTENT: case self::FIELD_DIFF_CONTENT:
return $this->getDiffContent('*'); return $this->getDiffContent('*');
case self::FIELD_DIFF_ADDED_CONTENT: case self::FIELD_DIFF_ADDED_CONTENT:

View file

@ -64,6 +64,7 @@ final class HeraldDifferentialRevisionAdapter extends HeraldAdapter {
self::FIELD_REVIEWERS, self::FIELD_REVIEWERS,
self::FIELD_CC, self::FIELD_CC,
self::FIELD_REPOSITORY, self::FIELD_REPOSITORY,
self::FIELD_REPOSITORY_PROJECTS,
self::FIELD_DIFF_FILE, self::FIELD_DIFF_FILE,
self::FIELD_DIFF_CONTENT, self::FIELD_DIFF_CONTENT,
self::FIELD_DIFF_ADDED_CONTENT, self::FIELD_DIFF_ADDED_CONTENT,
@ -152,32 +153,36 @@ final class HeraldDifferentialRevisionAdapter extends HeraldAdapter {
if ($this->repository === null) { if ($this->repository === null) {
$this->repository = false; $this->repository = false;
// TODO: (T603) Implement policy stuff in Herald.
$viewer = PhabricatorUser::getOmnipotentUser(); $viewer = PhabricatorUser::getOmnipotentUser();
$repository_phid = null;
$revision = $this->revision; $revision = $this->revision;
if ($revision->getRepositoryPHID()) { if ($revision->getRepositoryPHID()) {
$repositories = id(new PhabricatorRepositoryQuery()) $repository_phid = $revision->getRepositoryPHID();
} else {
$repository = id(new DifferentialRepositoryLookup())
->setViewer($viewer) ->setViewer($viewer)
->withPHIDs(array($revision->getRepositoryPHID())) ->setDiff($this->diff)
->execute(); ->lookupRepository();
if ($repositories) { if ($repository) {
$this->repository = head($repositories); // We want to get the projects for this repository too, so run a
return $this->repository; // full query for it below.
$repository_phid = $repository->getPHID();
} }
} }
$repository = id(new DifferentialRepositoryLookup()) if ($repository_phid) {
->setViewer($viewer) $repository = id(new PhabricatorRepositoryQuery())
->setDiff($this->diff) ->setViewer($viewer)
->lookupRepository(); ->withPHIDs(array($repository_phid))
if ($repository) { ->needProjectPHIDs(true)
$this->repository = $repository; ->executeOne();
return $this->repository; if ($repository) {
$this->repository = $repository;
}
} }
$repository = false;
} }
return $this->repository; return $this->repository;
} }
@ -345,6 +350,12 @@ final class HeraldDifferentialRevisionAdapter extends HeraldAdapter {
return null; return null;
} }
return $repository->getPHID(); return $repository->getPHID();
case self::FIELD_REPOSITORY_PROJECTS:
$repository = $this->loadRepository();
if (!$repository) {
return null;
}
return $repository->getProjectPHIDs();
case self::FIELD_DIFF_CONTENT: case self::FIELD_DIFF_CONTENT:
return $this->loadContentDictionary(); return $this->loadContentDictionary();
case self::FIELD_DIFF_ADDED_CONTENT: case self::FIELD_DIFF_ADDED_CONTENT:

View file

@ -39,13 +39,8 @@ final class HeraldTestConsoleController extends HeraldController {
$object, $object,
$object->loadActiveDiff()); $object->loadActiveDiff());
} else if ($object instanceof PhabricatorRepositoryCommit) { } else if ($object instanceof PhabricatorRepositoryCommit) {
$data = id(new PhabricatorRepositoryCommitData())->loadOneWhere( $adapter = id(new HeraldCommitAdapter())
'commitID = %d', ->setCommit($object);
$object->getID());
$adapter = HeraldCommitAdapter::newLegacyAdapter(
$object->getRepository(),
$object,
$data);
} else if ($object instanceof ManiphestTask) { } else if ($object instanceof ManiphestTask) {
$adapter = id(new HeraldManiphestTaskAdapter()) $adapter = id(new HeraldManiphestTaskAdapter())
->setTask($object); ->setTask($object);

View file

@ -17,7 +17,7 @@ final class HeraldRule extends HeraldDAO
protected $isDisabled = 0; protected $isDisabled = 0;
protected $triggerObjectPHID; protected $triggerObjectPHID;
protected $configVersion = 26; protected $configVersion = 27;
// phids for which this rule has been applied // phids for which this rule has been applied
private $ruleApplied = self::ATTACHABLE; private $ruleApplied = self::ATTACHABLE;

View file

@ -42,10 +42,8 @@ final class PhabricatorRepositoryCommitHeraldWorker
'or no longer exists.')); 'or no longer exists.'));
} }
$adapter = HeraldCommitAdapter::newLegacyAdapter( $adapter = id(new HeraldCommitAdapter())
$repository, ->setCommit($commit);
$commit,
$data);
$rules = id(new HeraldRuleQuery()) $rules = id(new HeraldRuleQuery())
->setViewer(PhabricatorUser::getOmnipotentUser()) ->setViewer(PhabricatorUser::getOmnipotentUser())