Changeset View
Changeset View
Standalone View
Standalone View
src/applications/herald/controller/HeraldTestConsoleController.php
<?php | <?php | ||||
final class HeraldTestConsoleController extends HeraldController { | final class HeraldTestConsoleController extends HeraldController { | ||||
public function handleRequest(AphrontRequest $request) { | private $testObject; | ||||
$viewer = $request->getViewer(); | private $testAdapter; | ||||
$object_name = trim($request->getStr('object_name')); | |||||
$e_name = true; | public function setTestObject($test_object) { | ||||
$errors = array(); | $this->testObject = $test_object; | ||||
if ($request->isFormPost()) { | return $this; | ||||
if (!$object_name) { | |||||
$e_name = pht('Required'); | |||||
$errors[] = pht('An object name is required.'); | |||||
} | } | ||||
if (!$errors) { | public function getTestObject() { | ||||
$object = id(new PhabricatorObjectQuery()) | return $this->testObject; | ||||
->setViewer($viewer) | } | ||||
->withNames(array($object_name)) | |||||
->executeOne(); | |||||
if (!$object) { | public function setTestAdapter(HeraldAdapter $test_adapter) { | ||||
$e_name = pht('Invalid'); | $this->testAdapter = $test_adapter; | ||||
$errors[] = pht('No object exists with that name.'); | return $this; | ||||
} | } | ||||
if (!$errors) { | public function getTestAdapter() { | ||||
return $this->testAdapter; | |||||
} | |||||
// TODO: Let the adapters claim objects instead. | public function handleRequest(AphrontRequest $request) { | ||||
$viewer = $request->getViewer(); | |||||
if ($object instanceof DifferentialRevision) { | $response = $this->loadTestObject($request); | ||||
$adapter = HeraldDifferentialRevisionAdapter::newLegacyAdapter( | if ($response) { | ||||
$object, | return $response; | ||||
$object->loadActiveDiff()); | } | ||||
} else if ($object instanceof PhabricatorRepositoryCommit) { | |||||
$adapter = id(new HeraldCommitAdapter()) | $response = $this->loadAdapter($request); | ||||
->setCommit($object); | if ($response) { | ||||
} else if ($object instanceof ManiphestTask) { | return $response; | ||||
$adapter = id(new HeraldManiphestTaskAdapter()) | |||||
->setTask($object); | |||||
} else if ($object instanceof PholioMock) { | |||||
$adapter = id(new HeraldPholioMockAdapter()) | |||||
->setMock($object); | |||||
} else if ($object instanceof PhrictionDocument) { | |||||
$adapter = id(new PhrictionDocumentHeraldAdapter()) | |||||
->setDocument($object); | |||||
} else if ($object instanceof PonderQuestion) { | |||||
$adapter = id(new HeraldPonderQuestionAdapter()) | |||||
->setQuestion($object); | |||||
} else if ($object instanceof PhabricatorMetaMTAMail) { | |||||
$adapter = id(new PhabricatorMailOutboundMailHeraldAdapter()) | |||||
->setObject($object); | |||||
} else { | |||||
throw new Exception(pht('Can not build adapter for object!')); | |||||
} | } | ||||
$object = $this->getTestObject(); | |||||
$adapter = $this->getTestAdapter(); | |||||
$adapter->setIsNewObject(false); | $adapter->setIsNewObject(false); | ||||
$rules = id(new HeraldRuleQuery()) | $rules = id(new HeraldRuleQuery()) | ||||
->setViewer($viewer) | ->setViewer($viewer) | ||||
->withContentTypes(array($adapter->getAdapterContentType())) | ->withContentTypes(array($adapter->getAdapterContentType())) | ||||
->withDisabled(false) | ->withDisabled(false) | ||||
->needConditionsAndActions(true) | ->needConditionsAndActions(true) | ||||
->needAppliedToPHIDs(array($object->getPHID())) | ->needAppliedToPHIDs(array($object->getPHID())) | ||||
->needValidateAuthors(true) | ->needValidateAuthors(true) | ||||
->execute(); | ->execute(); | ||||
$engine = id(new HeraldEngine()) | $engine = id(new HeraldEngine()) | ||||
->setDryRun(true); | ->setDryRun(true); | ||||
$effects = $engine->applyRules($rules, $adapter); | $effects = $engine->applyRules($rules, $adapter); | ||||
$engine->applyEffects($effects, $adapter, $rules); | $engine->applyEffects($effects, $adapter, $rules); | ||||
$xscript = $engine->getTranscript(); | $xscript = $engine->getTranscript(); | ||||
return id(new AphrontRedirectResponse()) | return id(new AphrontRedirectResponse()) | ||||
->setURI('/herald/transcript/'.$xscript->getID().'/'); | ->setURI('/herald/transcript/'.$xscript->getID().'/'); | ||||
} | } | ||||
private function loadTestObject(AphrontRequest $request) { | |||||
$viewer = $this->getViewer(); | |||||
$e_name = true; | |||||
$v_name = null; | |||||
$errors = array(); | |||||
if ($request->isFormPost()) { | |||||
$v_name = trim($request->getStr('object_name')); | |||||
if (!$v_name) { | |||||
$e_name = pht('Required'); | |||||
$errors[] = pht('An object name is required.'); | |||||
} | |||||
if (!$errors) { | |||||
$object = id(new PhabricatorObjectQuery()) | |||||
->setViewer($viewer) | |||||
->withNames(array($v_name)) | |||||
->executeOne(); | |||||
if (!$object) { | |||||
$e_name = pht('Invalid'); | |||||
$errors[] = pht('No object exists with that name.'); | |||||
} | |||||
} | |||||
if (!$errors) { | |||||
$this->setTestObject($object); | |||||
return null; | |||||
} | } | ||||
} | } | ||||
$form = id(new AphrontFormView()) | $form = id(new AphrontFormView()) | ||||
->setUser($viewer) | ->setUser($viewer) | ||||
->appendRemarkupInstructions( | ->appendRemarkupInstructions( | ||||
pht( | pht( | ||||
'Enter an object to test rules for, like a Diffusion commit (e.g., '. | 'Enter an object to test rules for, like a Diffusion commit (e.g., '. | ||||
'`rX123`) or a Differential revision (e.g., `D123`). You will be '. | '`rX123`) or a Differential revision (e.g., `D123`). You will be '. | ||||
'shown the results of a dry run on the object.')) | 'shown the results of a dry run on the object.')) | ||||
->appendChild( | ->appendChild( | ||||
id(new AphrontFormTextControl()) | id(new AphrontFormTextControl()) | ||||
->setLabel(pht('Object Name')) | ->setLabel(pht('Object Name')) | ||||
->setName('object_name') | ->setName('object_name') | ||||
->setError($e_name) | ->setError($e_name) | ||||
->setValue($object_name)) | ->setValue($v_name)) | ||||
->appendChild( | ->appendChild( | ||||
id(new AphrontFormSubmitControl()) | id(new AphrontFormSubmitControl()) | ||||
->setValue(pht('Test Rules'))); | ->setValue(pht('Continue'))); | ||||
return $this->buildTestConsoleResponse($form, $errors); | |||||
} | |||||
private function loadAdapter(AphrontRequest $request) { | |||||
$viewer = $this->getViewer(); | |||||
$object = $this->getTestObject(); | |||||
$adapter_key = $request->getStr('adapter'); | |||||
$adapters = HeraldAdapter::getAllAdapters(); | |||||
$can_select = array(); | |||||
$display_adapters = array(); | |||||
foreach ($adapters as $key => $adapter) { | |||||
if (!$adapter->isTestAdapterForObject($object)) { | |||||
continue; | |||||
} | |||||
if (!$adapter->isAvailableToUser($viewer)) { | |||||
continue; | |||||
} | |||||
$display_adapters[$key] = $adapter; | |||||
if ($adapter->canCreateTestAdapterForObject($object)) { | |||||
$can_select[$key] = $adapter; | |||||
} | |||||
} | |||||
if ($request->isFormPost() && $adapter_key) { | |||||
if (isset($can_select[$adapter_key])) { | |||||
$adapter = $can_select[$adapter_key]->newTestAdapter($object); | |||||
$this->setTestAdapter($adapter); | |||||
return null; | |||||
} | |||||
} | |||||
$form = id(new AphrontFormView()) | |||||
->addHiddenInput('object_name', $request->getStr('object_name')) | |||||
->setViewer($viewer); | |||||
$cancel_uri = $this->getApplicationURI(); | |||||
if (!$display_adapters) { | |||||
$form | |||||
->appendRemarkupInstructions( | |||||
pht('//There are no available Herald events for this object.//')) | |||||
->appendControl( | |||||
id(new AphrontFormSubmitControl()) | |||||
->addCancelButton($cancel_uri)); | |||||
} else { | |||||
$adapter_control = id(new AphrontFormRadioButtonControl()) | |||||
->setLabel(pht('Event')) | |||||
->setName('adapter') | |||||
->setValue(head_key($can_select)); | |||||
foreach ($display_adapters as $adapter_key => $adapter) { | |||||
$is_disabled = empty($can_select[$adapter_key]); | |||||
$adapter_control->addButton( | |||||
$adapter_key, | |||||
$adapter->getAdapterContentName(), | |||||
$adapter->getAdapterTestDescription(), | |||||
null, | |||||
$is_disabled); | |||||
} | |||||
$form | |||||
->appendControl($adapter_control) | |||||
->appendControl( | |||||
id(new AphrontFormSubmitControl()) | |||||
->setValue(pht('Run Test'))); | |||||
} | |||||
return $this->buildTestConsoleResponse($form, array()); | |||||
} | |||||
private function buildTestConsoleResponse($form, array $errors) { | |||||
$box = id(new PHUIObjectBoxView()) | $box = id(new PHUIObjectBoxView()) | ||||
->setFormErrors($errors) | ->setFormErrors($errors) | ||||
->setForm($form); | ->setForm($form); | ||||
$crumbs = id($this->buildApplicationCrumbs()) | $crumbs = id($this->buildApplicationCrumbs()) | ||||
->addTextCrumb(pht('Test Console')) | ->addTextCrumb(pht('Test Console')) | ||||
->setBorder(true); | ->setBorder(true); | ||||
$title = pht('Test Console'); | $title = pht('Test Console'); | ||||
$header = id(new PHUIHeaderView()) | $header = id(new PHUIHeaderView()) | ||||
->setHeader($title) | ->setHeader($title) | ||||
->setHeaderIcon('fa-desktop'); | ->setHeaderIcon('fa-desktop'); | ||||
$view = id(new PHUITwoColumnView()) | $view = id(new PHUITwoColumnView()) | ||||
->setHeader($header) | ->setHeader($header) | ||||
->setFooter($box); | ->setFooter($box); | ||||
return $this->newPage() | return $this->newPage() | ||||
->setTitle($title) | ->setTitle($title) | ||||
->setCrumbs($crumbs) | ->setCrumbs($crumbs) | ||||
->appendChild( | ->appendChild($view); | ||||
array( | |||||
$view, | |||||
)); | |||||
} | } | ||||
} | } |