[ Index ]

PHP Cross Reference of Phabricator

title

Body

[close]

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

   1  <?php
   2  
   3  final class DifferentialCreateInlineConduitAPIMethod
   4    extends DifferentialConduitAPIMethod {
   5  
   6    public function getAPIMethodName() {
   7      return 'differential.createinline';
   8    }
   9  
  10    public function getMethodDescription() {
  11      return 'Add an inline comment to a Differential revision.';
  12    }
  13  
  14    public function defineParamTypes() {
  15      return array(
  16        'revisionID' => 'optional revisionid',
  17        'diffID'     => 'optional diffid',
  18        'filePath'   => 'required string',
  19        'isNewFile'  => 'required bool',
  20        'lineNumber' => 'required int',
  21        'lineLength' => 'optional int',
  22        'content'    => 'required string',
  23      );
  24    }
  25  
  26    public function defineReturnType() {
  27      return 'nonempty dict';
  28    }
  29  
  30    public function defineErrorTypes() {
  31      return array(
  32        'ERR-BAD-REVISION' => 'Bad revision ID.',
  33        'ERR-BAD-DIFF'     => 'Bad diff ID, or diff does not belong to revision.',
  34        'ERR-NEED-DIFF'    => 'Neither revision ID nor diff ID was provided.',
  35        'ERR-NEED-FILE'    => 'A file path was not provided.',
  36        'ERR-BAD-FILE'     => "Requested file doesn't exist in this revision.",
  37      );
  38    }
  39  
  40    protected function execute(ConduitAPIRequest $request) {
  41      $rid = $request->getValue('revisionID');
  42      $did = $request->getValue('diffID');
  43  
  44      if ($rid) {
  45        // Given both a revision and a diff, check that they match.
  46        // Given only a revision, find the active diff.
  47        $revision = id(new DifferentialRevisionQuery())
  48          ->setViewer($request->getUser())
  49          ->withIDs(array($rid))
  50          ->executeOne();
  51        if (!$revision) {
  52          throw new ConduitException('ERR-BAD-REVISION');
  53        }
  54  
  55        if (!$did) { // did not!
  56          $diff = $revision->loadActiveDiff();
  57          $did = $diff->getID();
  58        } else { // did too!
  59          $diff = id(new DifferentialDiff())->load($did);
  60          if (!$diff || $diff->getRevisionID() != $rid) {
  61            throw new ConduitException('ERR-BAD-DIFF');
  62          }
  63        }
  64      } else if ($did) {
  65        // Given only a diff, find the parent revision.
  66        $diff = id(new DifferentialDiff())->load($did);
  67        if (!$diff) {
  68          throw new ConduitException('ERR-BAD-DIFF');
  69        }
  70        $rid = $diff->getRevisionID();
  71      } else {
  72        // Given neither, bail.
  73        throw new ConduitException('ERR-NEED-DIFF');
  74      }
  75  
  76      $file = $request->getValue('filePath');
  77      if (!$file) {
  78        throw new ConduitException('ERR-NEED-FILE');
  79      }
  80      $changes = id(new DifferentialChangeset())->loadAllWhere(
  81        'diffID = %d',
  82        $did);
  83      $cid = null;
  84      foreach ($changes as $id => $change) {
  85        if ($file == $change->getFilename()) {
  86          $cid = $id;
  87        }
  88      }
  89      if ($cid == null) {
  90        throw new ConduitException('ERR-BAD-FILE');
  91      }
  92  
  93      $inline = id(new DifferentialInlineComment())
  94        ->setRevisionID($rid)
  95        ->setChangesetID($cid)
  96        ->setAuthorPHID($request->getUser()->getPHID())
  97        ->setContent($request->getValue('content'))
  98        ->setIsNewFile($request->getValue('isNewFile'))
  99        ->setLineNumber($request->getValue('lineNumber'))
 100        ->setLineLength($request->getValue('lineLength', 0))
 101        ->save();
 102  
 103      // Load everything again, just to be safe.
 104      $changeset = id(new DifferentialChangeset())
 105        ->load($inline->getChangesetID());
 106      return $this->buildInlineInfoDictionary($inline, $changeset);
 107    }
 108  
 109  }


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