Differential D8600 Diff 20391 src/applications/harbormaster/controller/HarbormasterStepEditController.php
Changeset View
Changeset View
Standalone View
Standalone View
src/applications/harbormaster/controller/HarbormasterStepEditController.php
Show All 21 Lines | $step = id(new HarbormasterBuildStepQuery()) | ||||
->executeOne(); | ->executeOne(); | ||||
if (!$step) { | if (!$step) { | ||||
return new Aphront404Response(); | return new Aphront404Response(); | ||||
} | } | ||||
$plan = $step->getBuildPlan(); | $plan = $step->getBuildPlan(); | ||||
$implementation = $step->getStepImplementation(); | $implementation = $step->getStepImplementation(); | ||||
$implementation->validateSettingDefinitions(); | |||||
$settings = $implementation->getSettings(); | $field_list = PhabricatorCustomField::getObjectFields( | ||||
$step, | |||||
PhabricatorCustomField::ROLE_EDIT); | |||||
$field_list | |||||
->setViewer($viewer) | |||||
->readFieldsFromStorage($step); | |||||
$errors = array(); | $errors = array(); | ||||
$validation_exception = null; | |||||
if ($request->isFormPost()) { | if ($request->isFormPost()) { | ||||
foreach ($implementation->getSettingDefinitions() as $name => $opt) { | $xactions = $field_list->buildFieldTransactionsFromRequest( | ||||
$readable_name = $this->getReadableName($name, $opt); | new HarbormasterBuildStepTransaction(), | ||||
$value = $this->getValueFromRequest($request, $name, $opt['type']); | $request); | ||||
// TODO: This won't catch any validation issues unless the field | $editor = id(new HarbormasterBuildStepEditor()) | ||||
// is missing completely. How should we check if the user is | ->setActor($viewer) | ||||
// required to enter an integer? | ->setContinueOnNoEffect(true) | ||||
if ($value === null) { | ->setContentSourceFromRequest($request); | ||||
$errors[] = $readable_name.' is not valid.'; | |||||
} else { | |||||
$step->setDetail($name, $value); | |||||
} | |||||
} | |||||
if (!$errors) { | try { | ||||
$step->save(); | $editor->applyTransactions($step, $xactions); | ||||
return id(new AphrontRedirectResponse()) | return id(new AphrontRedirectResponse()) | ||||
->setURI($this->getApplicationURI('plan/'.$plan->getID().'/')); | ->setURI($this->getApplicationURI('plan/'.$plan->getID().'/')); | ||||
} catch (PhabricatorApplicationTransactionValidationException $ex) { | |||||
$validation_exception = $ex; | |||||
} | } | ||||
} | } | ||||
$form = id(new AphrontFormView()) | $form = id(new AphrontFormView()) | ||||
->setUser($viewer); | ->setUser($viewer); | ||||
// We need to render out all of the fields for the settings that | $field_list->appendFieldsToForm($form); | ||||
// the implementation has. | |||||
foreach ($implementation->getSettingDefinitions() as $name => $opt) { | |||||
if ($request->isFormPost()) { | |||||
$value = $this->getValueFromRequest($request, $name, $opt['type']); | |||||
} else { | |||||
$value = $settings[$name]; | |||||
} | |||||
switch ($opt['type']) { | |||||
case BuildStepImplementation::SETTING_TYPE_STRING: | |||||
case BuildStepImplementation::SETTING_TYPE_INTEGER: | |||||
$control = id(new AphrontFormTextControl()) | |||||
->setLabel($this->getReadableName($name, $opt)) | |||||
->setName($name) | |||||
->setValue($value); | |||||
break; | |||||
case BuildStepImplementation::SETTING_TYPE_BOOLEAN: | |||||
$control = id(new AphrontFormCheckboxControl()) | |||||
->setLabel($this->getReadableName($name, $opt)) | |||||
->setName($name) | |||||
->setValue($value); | |||||
break; | |||||
case BuildStepImplementation::SETTING_TYPE_ARTIFACT: | |||||
$filter = $opt['artifact_type']; | |||||
$available_artifacts = | |||||
BuildStepImplementation::loadAvailableArtifacts( | |||||
$plan, | |||||
$step, | |||||
$filter); | |||||
$options = array(); | |||||
foreach ($available_artifacts as $key => $type) { | |||||
$options[$key] = $key; | |||||
} | |||||
$control = id(new AphrontFormSelectControl()) | |||||
->setLabel($this->getReadableName($name, $opt)) | |||||
->setName($name) | |||||
->setValue($value) | |||||
->setOptions($options); | |||||
break; | |||||
default: | |||||
throw new Exception("Unable to render field with unknown type."); | |||||
} | |||||
if (isset($opt['description'])) { | |||||
$control->setCaption($opt['description']); | |||||
} | |||||
$form->appendChild($control); | |||||
} | |||||
$form->appendChild( | $form->appendChild( | ||||
id(new AphrontFormSubmitControl()) | id(new AphrontFormSubmitControl()) | ||||
->setValue(pht('Save Build Step')) | ->setValue(pht('Save Build Step')) | ||||
->addCancelButton( | ->addCancelButton( | ||||
$this->getApplicationURI('plan/'.$plan->getID().'/'))); | $this->getApplicationURI('plan/'.$plan->getID().'/'))); | ||||
$box = id(new PHUIObjectBoxView()) | $box = id(new PHUIObjectBoxView()) | ||||
->setHeaderText('Edit Step: '.$implementation->getName()) | ->setHeaderText('Edit Step: '.$implementation->getName()) | ||||
->setValidationException(null) | ->setValidationException($validation_exception) | ||||
->setForm($form); | ->setForm($form); | ||||
$crumbs = $this->buildApplicationCrumbs(); | $crumbs = $this->buildApplicationCrumbs(); | ||||
$id = $plan->getID(); | $id = $plan->getID(); | ||||
$crumbs->addTextCrumb( | $crumbs->addTextCrumb( | ||||
pht("Plan %d", $id), | pht("Plan %d", $id), | ||||
$this->getApplicationURI("plan/{$id}/")); | $this->getApplicationURI("plan/{$id}/")); | ||||
$crumbs->addTextCrumb(pht('Edit Step')); | $crumbs->addTextCrumb(pht('Edit Step')); | ||||
$variables = $this->renderBuildVariablesTable(); | $variables = $this->renderBuildVariablesTable(); | ||||
$xactions = id(new HarbormasterBuildStepTransactionQuery()) | |||||
->setViewer($viewer) | |||||
->withObjectPHIDs(array($step->getPHID())) | |||||
->execute(); | |||||
$xaction_view = id(new PhabricatorApplicationTransactionView()) | |||||
->setUser($viewer) | |||||
->setObjectPHID($step->getPHID()) | |||||
->setTransactions($xactions) | |||||
->setShouldTerminate(true); | |||||
return $this->buildApplicationPage( | return $this->buildApplicationPage( | ||||
array( | array( | ||||
$crumbs, | $crumbs, | ||||
$box, | $box, | ||||
$variables, | $variables, | ||||
$xaction_view, | |||||
), | ), | ||||
array( | array( | ||||
'title' => $implementation->getName(), | 'title' => $implementation->getName(), | ||||
'device' => true, | 'device' => true, | ||||
)); | )); | ||||
} | } | ||||
public function getReadableName($name, $opt) { | |||||
$readable_name = $name; | |||||
if (isset($opt['name'])) { | |||||
$readable_name = $opt['name']; | |||||
} | |||||
return $readable_name; | |||||
} | |||||
public function getValueFromRequest(AphrontRequest $request, $name, $type) { | |||||
switch ($type) { | |||||
case BuildStepImplementation::SETTING_TYPE_STRING: | |||||
case BuildStepImplementation::SETTING_TYPE_ARTIFACT: | |||||
return $request->getStr($name); | |||||
break; | |||||
case BuildStepImplementation::SETTING_TYPE_INTEGER: | |||||
return $request->getInt($name); | |||||
break; | |||||
case BuildStepImplementation::SETTING_TYPE_BOOLEAN: | |||||
return $request->getBool($name); | |||||
break; | |||||
default: | |||||
throw new Exception("Unsupported setting type '".$type."'."); | |||||
} | |||||
} | |||||
private function renderBuildVariablesTable() { | private function renderBuildVariablesTable() { | ||||
$viewer = $this->getRequest()->getUser(); | $viewer = $this->getRequest()->getUser(); | ||||
$variables = HarbormasterBuild::getAvailableBuildVariables(); | $variables = HarbormasterBuild::getAvailableBuildVariables(); | ||||
ksort($variables); | ksort($variables); | ||||
$rows = array(); | $rows = array(); | ||||
$rows[] = pht( | $rows[] = pht( | ||||
Show All 20 Lines |