Differential D17055 Diff 41037 src/applications/differential/conduit/DifferentialParseCommitMessageConduitAPIMethod.php
Changeset View
Changeset View
Standalone View
Standalone View
src/applications/differential/conduit/DifferentialParseCommitMessageConduitAPIMethod.php
| <?php | <?php | ||||
| final class DifferentialParseCommitMessageConduitAPIMethod | final class DifferentialParseCommitMessageConduitAPIMethod | ||||
| extends DifferentialConduitAPIMethod { | extends DifferentialConduitAPIMethod { | ||||
| private $errors; | |||||
| public function getAPIMethodName() { | public function getAPIMethodName() { | ||||
| return 'differential.parsecommitmessage'; | return 'differential.parsecommitmessage'; | ||||
| } | } | ||||
| public function getMethodDescription() { | public function getMethodDescription() { | ||||
| return pht('Parse commit messages for Differential fields.'); | return pht('Parse commit messages for Differential fields.'); | ||||
| } | } | ||||
| protected function defineParamTypes() { | protected function defineParamTypes() { | ||||
| return array( | return array( | ||||
| 'corpus' => 'required string', | 'corpus' => 'required string', | ||||
| 'partial' => 'optional bool', | 'partial' => 'optional bool', | ||||
| ); | ); | ||||
| } | } | ||||
| protected function defineReturnType() { | protected function defineReturnType() { | ||||
| return 'nonempty dict'; | return 'nonempty dict'; | ||||
| } | } | ||||
| protected function execute(ConduitAPIRequest $request) { | protected function execute(ConduitAPIRequest $request) { | ||||
| $viewer = $request->getUser(); | $viewer = $this->getViewer(); | ||||
| $corpus = $request->getValue('corpus'); | |||||
| $is_partial = $request->getValue('partial'); | |||||
| $field_list = PhabricatorCustomField::getObjectFields( | $parser = DifferentialCommitMessageParser::newStandardParser($viewer); | ||||
| new DifferentialRevision(), | |||||
| DifferentialCustomField::ROLE_COMMITMESSAGE); | |||||
| $field_list->setViewer($viewer); | |||||
| $field_map = mpull($field_list->getFields(), null, 'getFieldKeyForConduit'); | |||||
| $corpus_map = $this->parseCommitMessage($corpus); | |||||
| $values = array(); | |||||
| foreach ($corpus_map as $field_key => $text_value) { | |||||
| $field = idx($field_map, $field_key); | |||||
| if (!$field) { | |||||
| throw new Exception( | |||||
| pht( | |||||
| 'Parser emitted text value for field key "%s", but no such '. | |||||
| 'field exists.', | |||||
| $field_key)); | |||||
| } | |||||
| try { | |||||
| $values[$field_key] = $field->parseValueFromCommitMessage($text_value); | |||||
| } catch (DifferentialFieldParseException $ex) { | |||||
| $this->errors[] = pht( | |||||
| 'Error parsing field "%s": %s', | |||||
| $field->renderCommitMessageLabel(), | |||||
| $ex->getMessage()); | |||||
| } | |||||
| } | |||||
| if (!$is_partial) { | $is_partial = $request->getValue('partial'); | ||||
| foreach ($field_map as $key => $field) { | if ($is_partial) { | ||||
| try { | $parser->setRaiseMissingFieldErrors(false); | ||||
| $field->validateCommitMessageValue(idx($values, $key)); | |||||
| } catch (DifferentialFieldValidationException $ex) { | |||||
| $this->errors[] = pht( | |||||
| 'Invalid or missing field "%s": %s', | |||||
| $field->renderCommitMessageLabel(), | |||||
| $ex->getMessage()); | |||||
| } | |||||
| } | |||||
| } | } | ||||
| $corpus = $request->getValue('corpus'); | |||||
| $field_map = $parser->parseFields($corpus); | |||||
| $errors = $parser->getErrors(); | |||||
| // grab some extra information about the Differential Revision: field... | // grab some extra information about the Differential Revision: field... | ||||
| $revision_id_field = new DifferentialRevisionIDField(); | $revision_id_field = new DifferentialRevisionIDField(); | ||||
| $revision_id_value = idx( | $revision_id_value = idx( | ||||
| $corpus_map, | $field_map, | ||||
| $revision_id_field->getFieldKeyForConduit()); | $revision_id_field->getFieldKeyForConduit()); | ||||
| $revision_id_valid_domain = PhabricatorEnv::getProductionURI(''); | $revision_id_valid_domain = PhabricatorEnv::getProductionURI(''); | ||||
| return array( | return array( | ||||
| 'errors' => $this->errors, | 'errors' => $errors, | ||||
| 'fields' => $values, | 'fields' => $field_map, | ||||
| 'revisionIDFieldInfo' => array( | 'revisionIDFieldInfo' => array( | ||||
| 'value' => $revision_id_value, | 'value' => $revision_id_value, | ||||
| 'validDomain' => $revision_id_valid_domain, | 'validDomain' => $revision_id_valid_domain, | ||||
| ), | ), | ||||
| ); | ); | ||||
| } | } | ||||
| private function parseCommitMessage($corpus) { | |||||
| $viewer = $this->getViewer(); | |||||
| $parser = DifferentialCommitMessageParser::newStandardParser($viewer); | |||||
| $result = $parser->parseCorpus($corpus); | |||||
| $this->errors = array(); | |||||
| foreach ($parser->getErrors() as $error) { | |||||
| $this->errors[] = $error; | |||||
| } | |||||
| return $result; | |||||
| } | |||||
| } | } | ||||