Differential D17055 Diff 41037 src/applications/differential/parser/DifferentialCommitMessageParser.php
Changeset View
Changeset View
Standalone View
Standalone View
src/applications/differential/parser/DifferentialCommitMessageParser.php
| Show All 15 Lines | |||||
| * | * | ||||
| * @task config Configuring the Parser | * @task config Configuring the Parser | ||||
| * @task parse Parsing Messages | * @task parse Parsing Messages | ||||
| * @task support Support Methods | * @task support Support Methods | ||||
| * @task internal Internals | * @task internal Internals | ||||
| */ | */ | ||||
| final class DifferentialCommitMessageParser extends Phobject { | final class DifferentialCommitMessageParser extends Phobject { | ||||
| private $viewer; | |||||
| private $labelMap; | private $labelMap; | ||||
| private $titleKey; | private $titleKey; | ||||
| private $summaryKey; | private $summaryKey; | ||||
| private $errors; | private $errors; | ||||
| private $raiseMissingFieldErrors = true; | |||||
| public static function newStandardParser(PhabricatorUser $viewer) { | public static function newStandardParser(PhabricatorUser $viewer) { | ||||
| $key_title = id(new DifferentialTitleField())->getFieldKeyForConduit(); | $key_title = id(new DifferentialTitleField())->getFieldKeyForConduit(); | ||||
| $key_summary = id(new DifferentialSummaryField())->getFieldKeyForConduit(); | $key_summary = id(new DifferentialSummaryField())->getFieldKeyForConduit(); | ||||
| $field_list = PhabricatorCustomField::getObjectFields( | $field_list = PhabricatorCustomField::getObjectFields( | ||||
| new DifferentialRevision(), | new DifferentialRevision(), | ||||
| Show All 18 Lines | foreach ($field_list->getFields() as $field) { | ||||
| $key, | $key, | ||||
| $label_map[$normal_label])); | $label_map[$normal_label])); | ||||
| } | } | ||||
| $label_map[$normal_label] = $key; | $label_map[$normal_label] = $key; | ||||
| } | } | ||||
| } | } | ||||
| return id(new self()) | return id(new self()) | ||||
| ->setViewer($viewer) | |||||
| ->setLabelMap($label_map) | ->setLabelMap($label_map) | ||||
| ->setTitleKey($key_title) | ->setTitleKey($key_title) | ||||
| ->setSummaryKey($key_summary); | ->setSummaryKey($key_summary); | ||||
| } | } | ||||
| /* -( Configuring the Parser )--------------------------------------------- */ | /* -( Configuring the Parser )--------------------------------------------- */ | ||||
| /** | /** | ||||
| * @task config | * @task config | ||||
| */ | */ | ||||
| public function setViewer(PhabricatorUser $viewer) { | |||||
| $this->viewer = $viewer; | |||||
| return $this; | |||||
| } | |||||
| /** | |||||
| * @task config | |||||
| */ | |||||
| public function getViewer() { | |||||
| return $this->viewer; | |||||
| } | |||||
| /** | |||||
| * @task config | |||||
| */ | |||||
| public function setRaiseMissingFieldErrors($raise) { | |||||
| $this->raiseMissingFieldErrors = $raise; | |||||
| return $this; | |||||
| } | |||||
| /** | |||||
| * @task config | |||||
| */ | |||||
| public function getRaiseMissingFieldErrors() { | |||||
| return $this->raiseMissingFieldErrors; | |||||
| } | |||||
| /** | |||||
| * @task config | |||||
| */ | |||||
| public function setLabelMap(array $label_map) { | public function setLabelMap(array $label_map) { | ||||
| $this->labelMap = $label_map; | $this->labelMap = $label_map; | ||||
| return $this; | return $this; | ||||
| } | } | ||||
| /** | /** | ||||
| * @task config | * @task config | ||||
| ▲ Show 20 Lines • Show All 130 Lines • ▼ Show 20 Lines | public function parseCorpus($corpus) { | ||||
| return $fields; | return $fields; | ||||
| } | } | ||||
| /** | /** | ||||
| * @task parse | * @task parse | ||||
| */ | */ | ||||
| public function parseFields($corpus) { | |||||
| $viewer = $this->getViewer(); | |||||
| $text_map = $this->parseCorpus($corpus); | |||||
| $field_list = PhabricatorCustomField::getObjectFields( | |||||
| new DifferentialRevision(), | |||||
| DifferentialCustomField::ROLE_COMMITMESSAGE); | |||||
| $field_list->setViewer($viewer); | |||||
| $field_map = $field_list->getFields(); | |||||
| $field_map = mpull($field_map, null, 'getFieldKeyForConduit'); | |||||
| $result_map = array(); | |||||
| foreach ($text_map as $field_key => $text_value) { | |||||
| $field = idx($field_map, $field_key); | |||||
| if (!$field) { | |||||
| // This is a strict error, since we only parse fields which we have | |||||
| // been told are valid. The caller probably handed us an invalid label | |||||
| // map. | |||||
| throw new Exception( | |||||
| pht( | |||||
| 'Parser emitted a field with key "%s", but no corresponding '. | |||||
| 'field definition exists.', | |||||
| $field_key)); | |||||
| } | |||||
| try { | |||||
| $result = $field->parseValueFromCommitMessage($text_value); | |||||
| $result_map[$field_key] = $result; | |||||
| } catch (DifferentialFieldParseException $ex) { | |||||
| $this->errors[] = pht( | |||||
| 'Error parsing field "%s": %s', | |||||
| $field->renderCommitMessageLabel(), | |||||
| $ex->getMessage()); | |||||
| } | |||||
| } | |||||
| if ($this->getRaiseMissingFieldErrors()) { | |||||
| foreach ($field_map as $key => $field) { | |||||
| try { | |||||
| $field->validateCommitMessageValue(idx($result_map, $key)); | |||||
| } catch (DifferentialFieldValidationException $ex) { | |||||
| $this->errors[] = pht( | |||||
| 'Invalid or missing field "%s": %s', | |||||
| $field->renderCommitMessageLabel(), | |||||
| $ex->getMessage()); | |||||
| } | |||||
| } | |||||
| } | |||||
| return $result_map; | |||||
| } | |||||
| /** | |||||
| * @task parse | |||||
| */ | |||||
| public function getErrors() { | public function getErrors() { | ||||
| return $this->errors; | return $this->errors; | ||||
| } | } | ||||
| /* -( Support Methods )---------------------------------------------------- */ | /* -( Support Methods )---------------------------------------------------- */ | ||||
| Show All 27 Lines | |||||