[ Index ]

PHP Cross Reference of Phabricator

title

Body

[close]

/src/applications/differential/conduit/ -> DifferentialParseCommitMessageConduitAPIMethod.php (source)

   1  <?php
   2  
   3  final class DifferentialParseCommitMessageConduitAPIMethod
   4    extends DifferentialConduitAPIMethod {
   5  
   6    private $errors;
   7  
   8    public function getAPIMethodName() {
   9      return 'differential.parsecommitmessage';
  10    }
  11  
  12    public function getMethodDescription() {
  13      return pht('Parse commit messages for Differential fields.');
  14    }
  15  
  16    public function defineParamTypes() {
  17      return array(
  18        'corpus'  => 'required string',
  19        'partial' => 'optional bool',
  20      );
  21    }
  22  
  23    public function defineReturnType() {
  24      return 'nonempty dict';
  25    }
  26  
  27    public function defineErrorTypes() {
  28      return array();
  29    }
  30  
  31    protected function execute(ConduitAPIRequest $request) {
  32      $viewer = $request->getUser();
  33      $corpus = $request->getValue('corpus');
  34      $is_partial = $request->getValue('partial');
  35  
  36      $revision = new DifferentialRevision();
  37  
  38      $field_list = PhabricatorCustomField::getObjectFields(
  39        $revision,
  40        DifferentialCustomField::ROLE_COMMITMESSAGE);
  41      $field_list->setViewer($viewer);
  42      $field_map = mpull($field_list->getFields(), null, 'getFieldKeyForConduit');
  43  
  44      $this->errors = array();
  45  
  46      $label_map = $this->buildLabelMap($field_list);
  47      $corpus_map = $this->parseCommitMessage($corpus, $label_map);
  48  
  49      $values = array();
  50      foreach ($corpus_map as $field_key => $text_value) {
  51        $field = idx($field_map, $field_key);
  52  
  53        if (!$field) {
  54          throw new Exception(
  55            pht(
  56              'Parser emitted text value for field key "%s", but no such '.
  57              'field exists.',
  58              $field_key));
  59        }
  60  
  61        try {
  62          $values[$field_key] = $field->parseValueFromCommitMessage($text_value);
  63        } catch (DifferentialFieldParseException $ex) {
  64          $this->errors[] = pht(
  65            'Error parsing field "%s": %s',
  66            $field->renderCommitMessageLabel(),
  67            $ex->getMessage());
  68        }
  69      }
  70  
  71      if (!$is_partial) {
  72        foreach ($field_map as $key => $field) {
  73          try {
  74            $field->validateCommitMessageValue(idx($values, $key));
  75          } catch (DifferentialFieldValidationException $ex) {
  76            $this->errors[] = pht(
  77              'Invalid or missing field "%s": %s',
  78              $field->renderCommitMessageLabel(),
  79              $ex->getMessage());
  80          }
  81        }
  82      }
  83  
  84      // grab some extra information about the Differential Revision: field...
  85      $revision_id_field = new DifferentialRevisionIDField();
  86      $revision_id_value = idx(
  87        $corpus_map,
  88        $revision_id_field->getFieldKeyForConduit());
  89      $revision_id_valid_domain = PhabricatorEnv::getProductionURI('');
  90  
  91      return array(
  92        'errors' => $this->errors,
  93        'fields' => $values,
  94        'revisionIDFieldInfo' => array(
  95          'value' => $revision_id_value,
  96          'validDomain' => $revision_id_valid_domain,
  97        ),
  98      );
  99    }
 100  
 101    private function buildLabelMap(PhabricatorCustomFieldList $field_list) {
 102      $label_map = array();
 103  
 104      foreach ($field_list->getFields() as $key => $field) {
 105        $labels = $field->getCommitMessageLabels();
 106        $key = $field->getFieldKeyForConduit();
 107  
 108        foreach ($labels as $label) {
 109          $normal_label = DifferentialCommitMessageParser::normalizeFieldLabel(
 110            $label);
 111          if (!empty($label_map[$normal_label])) {
 112            throw new Exception(
 113              pht(
 114                'Field label "%s" is parsed by two custom fields: "%s" and '.
 115                '"%s". Each label must be parsed by only one field.',
 116                $label,
 117                $key,
 118                $label_map[$normal_label]));
 119          }
 120          $label_map[$normal_label] = $key;
 121        }
 122      }
 123  
 124      return $label_map;
 125    }
 126  
 127  
 128    private function parseCommitMessage($corpus, array $label_map) {
 129      $key_title = id(new DifferentialTitleField())->getFieldKeyForConduit();
 130      $key_summary = id(new DifferentialSummaryField())->getFieldKeyForConduit();
 131  
 132      $parser = id(new DifferentialCommitMessageParser())
 133        ->setLabelMap($label_map)
 134        ->setTitleKey($key_title)
 135        ->setSummaryKey($key_summary);
 136  
 137      $result = $parser->parseCorpus($corpus);
 138  
 139      foreach ($parser->getErrors() as $error) {
 140        $this->errors[] = $error;
 141      }
 142  
 143      return $result;
 144    }
 145  
 146  }


Generated: Sun Nov 30 09:20:46 2014 Cross-referenced by PHPXref 0.7.1