diff --git a/src/applications/differential/conduit/DifferentialParseCommitMessageConduitAPIMethod.php b/src/applications/differential/conduit/DifferentialParseCommitMessageConduitAPIMethod.php --- a/src/applications/differential/conduit/DifferentialParseCommitMessageConduitAPIMethod.php +++ b/src/applications/differential/conduit/DifferentialParseCommitMessageConduitAPIMethod.php @@ -29,18 +29,13 @@ $corpus = $request->getValue('corpus'); $is_partial = $request->getValue('partial'); - $revision = new DifferentialRevision(); - $field_list = PhabricatorCustomField::getObjectFields( - $revision, + new DifferentialRevision(), DifferentialCustomField::ROLE_COMMITMESSAGE); $field_list->setViewer($viewer); - $field_map = mpull($field_list->getFields(), null, 'getFieldKeyForConduit'); - - $this->errors = array(); + $field_map = mpull($field_list, null, 'getFieldKeyForConduit'); - $label_map = $this->buildLabelMap($field_list); - $corpus_map = $this->parseCommitMessage($corpus, $label_map); + $corpus_map = $this->parseCommitMessage($corpus); $values = array(); foreach ($corpus_map as $field_key => $text_value) { @@ -94,44 +89,12 @@ ); } - private function buildLabelMap(PhabricatorCustomFieldList $field_list) { - $label_map = array(); - - 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); - + 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; } diff --git a/src/applications/differential/customfield/DifferentialCoreCustomField.php b/src/applications/differential/customfield/DifferentialCoreCustomField.php --- a/src/applications/differential/customfield/DifferentialCoreCustomField.php +++ b/src/applications/differential/customfield/DifferentialCoreCustomField.php @@ -10,6 +10,7 @@ private $value; private $fieldError; + private $fieldParser; abstract protected function readValueFromRevision( DifferentialRevision $revision); @@ -60,6 +61,32 @@ $error->setIsMissingFieldError(true); $errors[] = $error; $this->setFieldError(pht('Required')); + continue; + } + } + + if (is_string($value)) { + $parser = $this->getFieldParser(); + $result = $parser->parseCorpus($value); + + unset($result['__title__']); + unset($result['__summary__']); + + if ($result) { + $error = new PhabricatorApplicationTransactionValidationError( + $type, + pht('Invalid'), + pht( + 'The value you have entered in "%s" can not be parsed '. + 'unambiguously when rendered in a commit message. Edit the '. + 'message so that keywords like "Summary:" and "Test Plan:" do '. + 'not appear at the beginning of lines. Parsed keys: %s.', + $this->getFieldName(), + implode(', ', array_keys($result))), + $xaction); + $errors[] = $error; + $this->setFieldError(pht('Invalid')); + continue; } } } @@ -67,6 +94,22 @@ return $errors; } + private function getFieldParser() { + if (!$this->fieldParser) { + $viewer = $this->getViewer(); + $parser = DifferentialCommitMessageParser::newStandardParser($viewer); + + // Set custom title and summary keys so we can detect the presence of + // "Summary:" in, e.g., a test plan. + $parser->setTitleKey('__title__'); + $parser->setSummaryKey('__summary__'); + + $this->fieldParser = $parser; + } + + return $this->fieldParser; + } + public function canDisableField() { return false; } diff --git a/src/applications/differential/parser/DifferentialCommitMessageParser.php b/src/applications/differential/parser/DifferentialCommitMessageParser.php --- a/src/applications/differential/parser/DifferentialCommitMessageParser.php +++ b/src/applications/differential/parser/DifferentialCommitMessageParser.php @@ -27,6 +27,45 @@ private $errors; + public static function newStandardParser(PhabricatorUser $viewer) { + + $key_title = id(new DifferentialTitleField())->getFieldKeyForConduit(); + $key_summary = id(new DifferentialSummaryField())->getFieldKeyForConduit(); + + $field_list = PhabricatorCustomField::getObjectFields( + new DifferentialRevision(), + DifferentialCustomField::ROLE_COMMITMESSAGE); + $field_list->setViewer($viewer); + + $label_map = array(); + + foreach ($field_list->getFields() as $field) { + $labels = $field->getCommitMessageLabels(); + $key = $field->getFieldKeyForConduit(); + + foreach ($labels as $label) { + $normal_label = self::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 id(new self()) + ->setLabelMap($label_map) + ->setTitleKey($key_title) + ->setSummaryKey($key_summary); + } + + /* -( Configuring the Parser )--------------------------------------------- */