Differential D16846 Diff 40568 src/applications/differential/conduit/DifferentialParseCommitMessageConduitAPIMethod.php
Changeset View
Changeset View
Standalone View
Standalone View
src/applications/differential/conduit/DifferentialParseCommitMessageConduitAPIMethod.php
| Show All 23 Lines | protected function defineReturnType() { | ||||
| return 'nonempty dict'; | return 'nonempty dict'; | ||||
| } | } | ||||
| protected function execute(ConduitAPIRequest $request) { | protected function execute(ConduitAPIRequest $request) { | ||||
| $viewer = $request->getUser(); | $viewer = $request->getUser(); | ||||
| $corpus = $request->getValue('corpus'); | $corpus = $request->getValue('corpus'); | ||||
| $is_partial = $request->getValue('partial'); | $is_partial = $request->getValue('partial'); | ||||
| $revision = new DifferentialRevision(); | |||||
| $field_list = PhabricatorCustomField::getObjectFields( | $field_list = PhabricatorCustomField::getObjectFields( | ||||
| $revision, | new DifferentialRevision(), | ||||
| DifferentialCustomField::ROLE_COMMITMESSAGE); | DifferentialCustomField::ROLE_COMMITMESSAGE); | ||||
| $field_list->setViewer($viewer); | $field_list->setViewer($viewer); | ||||
| $field_map = mpull($field_list->getFields(), null, 'getFieldKeyForConduit'); | $field_map = mpull($field_list, null, 'getFieldKeyForConduit'); | ||||
| $this->errors = array(); | |||||
| $label_map = $this->buildLabelMap($field_list); | $corpus_map = $this->parseCommitMessage($corpus); | ||||
| $corpus_map = $this->parseCommitMessage($corpus, $label_map); | |||||
| $values = array(); | $values = array(); | ||||
| foreach ($corpus_map as $field_key => $text_value) { | foreach ($corpus_map as $field_key => $text_value) { | ||||
| $field = idx($field_map, $field_key); | $field = idx($field_map, $field_key); | ||||
| if (!$field) { | if (!$field) { | ||||
| throw new Exception( | throw new Exception( | ||||
| pht( | pht( | ||||
| Show All 37 Lines | return array( | ||||
| 'fields' => $values, | 'fields' => $values, | ||||
| 'revisionIDFieldInfo' => array( | 'revisionIDFieldInfo' => array( | ||||
| 'value' => $revision_id_value, | 'value' => $revision_id_value, | ||||
| 'validDomain' => $revision_id_valid_domain, | 'validDomain' => $revision_id_valid_domain, | ||||
| ), | ), | ||||
| ); | ); | ||||
| } | } | ||||
| private function buildLabelMap(PhabricatorCustomFieldList $field_list) { | private function parseCommitMessage($corpus) { | ||||
| $label_map = array(); | $viewer = $this->getViewer(); | ||||
| $parser = DifferentialCommitMessageParser::newStandardParser($viewer); | |||||
| foreach ($field_list->getFields() as $key => $field) { | |||||
| $labels = $field->getCommitMessageLabels(); | |||||
| $key = $field->getFieldKeyForConduit(); | |||||
| foreach ($labels as $label) { | |||||
| $normal_label = DifferentialCommitMessageParser::normalizeFieldLabel( | |||||
| $label); | |||||
| if (!empty($label_map[$normal_label])) { | |||||
| throw new Exception( | |||||
| pht( | |||||
| 'Field label "%s" is parsed by two custom fields: "%s" and '. | |||||
| '"%s". Each label must be parsed by only one field.', | |||||
| $label, | |||||
| $key, | |||||
| $label_map[$normal_label])); | |||||
| } | |||||
| $label_map[$normal_label] = $key; | |||||
| } | |||||
| } | |||||
| return $label_map; | |||||
| } | |||||
| private function parseCommitMessage($corpus, array $label_map) { | |||||
| $key_title = id(new DifferentialTitleField())->getFieldKeyForConduit(); | |||||
| $key_summary = id(new DifferentialSummaryField())->getFieldKeyForConduit(); | |||||
| $parser = id(new DifferentialCommitMessageParser()) | |||||
| ->setLabelMap($label_map) | |||||
| ->setTitleKey($key_title) | |||||
| ->setSummaryKey($key_summary); | |||||
| $result = $parser->parseCorpus($corpus); | $result = $parser->parseCorpus($corpus); | ||||
| $this->errors = array(); | |||||
| foreach ($parser->getErrors() as $error) { | foreach ($parser->getErrors() as $error) { | ||||
| $this->errors[] = $error; | $this->errors[] = $error; | ||||
| } | } | ||||
| return $result; | return $result; | ||||
| } | } | ||||
| } | } | ||||