[ Index ]

PHP Cross Reference of Phabricator

title

Body

[close]

/src/applications/releeph/conduit/ -> ReleephRequestConduitAPIMethod.php (source)

   1  <?php
   2  
   3  final class ReleephRequestConduitAPIMethod extends ReleephConduitAPIMethod {
   4  
   5    public function getAPIMethodName() {
   6      return 'releeph.request';
   7    }
   8  
   9    public function getMethodDescription() {
  10      return 'Request a commit or diff to be picked to a branch.';
  11    }
  12  
  13    public function defineParamTypes() {
  14      return array(
  15        'branchPHID'  => 'required string',
  16        'things'      => 'required list<string>',
  17        'fields'      => 'dict<string, string>',
  18      );
  19    }
  20  
  21    public function defineReturnType() {
  22      return 'dict<string, wild>';
  23    }
  24  
  25    public function defineErrorTypes() {
  26      return array(
  27        'ERR_BRANCH'      => 'Unknown Releeph branch.',
  28        'ERR_FIELD_PARSE' => 'Unable to parse a Releeph field.',
  29      );
  30    }
  31  
  32    protected function execute(ConduitAPIRequest $request) {
  33      $user = $request->getUser();
  34  
  35      $viewer_handle = id(new PhabricatorHandleQuery())
  36        ->setViewer($user)
  37        ->withPHIDs(array($user->getPHID()))
  38        ->executeOne();
  39  
  40      $branch_phid = $request->getValue('branchPHID');
  41      $releeph_branch = id(new ReleephBranchQuery())
  42        ->setViewer($user)
  43        ->withPHIDs(array($branch_phid))
  44        ->executeOne();
  45  
  46      if (!$releeph_branch) {
  47        throw id(new ConduitException('ERR_BRANCH'))->setErrorDescription(
  48          "No ReleephBranch found with PHID {$branch_phid}!");
  49      }
  50  
  51      $releeph_project = $releeph_branch->getProduct();
  52  
  53      // Find the requested commit identifiers
  54      $requested_commits = array();
  55      $requested_object_phids = array();
  56      $things = $request->getValue('things');
  57      $finder = id(new ReleephCommitFinder())
  58        ->setUser($user)
  59        ->setReleephProject($releeph_project);
  60      foreach ($things as $thing) {
  61        try {
  62          $requested_commits[$thing] = $finder->fromPartial($thing);
  63          $object_phid = $finder->getRequestedObjectPHID();
  64          if (!$object_phid) {
  65            $object_phid = $requested_commits[$thing]->getPHID();
  66          }
  67          $requested_object_phids[$thing] = $object_phid;
  68        } catch (ReleephCommitFinderException $ex) {
  69          throw id(new ConduitException('ERR_NO_MATCHES'))
  70            ->setErrorDescription($ex->getMessage());
  71        }
  72      }
  73      $requested_commit_phids = mpull($requested_commits, 'getPHID');
  74  
  75      // Find any existing requests that clash on the commit id, for this branch
  76      $existing_releeph_requests = id(new ReleephRequest())->loadAllWhere(
  77        'requestCommitPHID IN (%Ls) AND branchID = %d',
  78        $requested_commit_phids,
  79        $releeph_branch->getID());
  80      $existing_releeph_requests = mpull(
  81        $existing_releeph_requests,
  82        null,
  83        'getRequestCommitPHID');
  84  
  85      $selector = $releeph_project->getReleephFieldSelector();
  86      $fields = $selector->getFieldSpecifications();
  87      foreach ($fields as $field) {
  88        $field
  89          ->setReleephProject($releeph_project)
  90          ->setReleephBranch($releeph_branch);
  91      }
  92  
  93      $results = array();
  94      $handles = id(new PhabricatorHandleQuery())
  95        ->setViewer($user)
  96        ->withPHIDs($requested_commit_phids)
  97        ->execute();
  98      foreach ($requested_commits as $thing => $commit) {
  99        $phid = $commit->getPHID();
 100        $name = id($handles[$phid])->getName();
 101  
 102        $releeph_request = null;
 103  
 104        $existing_releeph_request = idx($existing_releeph_requests, $phid);
 105        if ($existing_releeph_request) {
 106          $releeph_request = $existing_releeph_request;
 107        } else {
 108          $releeph_request = id(new ReleephRequest())
 109            ->setRequestUserPHID($user->getPHID())
 110            ->setBranchID($releeph_branch->getID())
 111            ->setInBranch(0)
 112            ->setRequestedObjectPHID($requested_object_phids[$thing]);
 113  
 114          $xactions = array();
 115  
 116          $xactions[] = id(new ReleephRequestTransaction())
 117            ->setTransactionType(ReleephRequestTransaction::TYPE_REQUEST)
 118            ->setNewValue($commit->getPHID());
 119  
 120          $xactions[] = id(new ReleephRequestTransaction())
 121            ->setTransactionType(ReleephRequestTransaction::TYPE_USER_INTENT)
 122            ->setMetadataValue('userPHID', $user->getPHID())
 123            ->setMetadataValue(
 124              'isAuthoritative',
 125              $releeph_project->isAuthoritative($user))
 126            ->setNewValue(ReleephRequest::INTENT_WANT);
 127  
 128          foreach ($fields as $field) {
 129            if (!$field->isEditable()) {
 130              continue;
 131            }
 132            $field->setReleephRequest($releeph_request);
 133            try {
 134              $field->setValueFromConduitAPIRequest($request);
 135            } catch (ReleephFieldParseException $ex) {
 136              throw id(new ConduitException('ERR_FIELD_PARSE'))
 137                ->setErrorDescription($ex->getMessage());
 138            }
 139          }
 140  
 141          $editor = id(new ReleephRequestTransactionalEditor())
 142            ->setActor($user)
 143            ->setContinueOnNoEffect(true)
 144            ->setContentSource(
 145              PhabricatorContentSource::newForSource(
 146                PhabricatorContentSource::SOURCE_CONDUIT,
 147                array()));
 148  
 149          $editor->applyTransactions($releeph_request, $xactions);
 150        }
 151  
 152        $url = PhabricatorEnv::getProductionURI('/Y'.$releeph_request->getID());
 153        $results[$thing] = array(
 154          'thing'         => $thing,
 155          'branch'        => $releeph_branch->getDisplayNameWithDetail(),
 156          'commitName'    => $name,
 157          'commitID'      => $commit->getCommitIdentifier(),
 158          'url'           => $url,
 159          'requestID'     => $releeph_request->getID(),
 160          'requestor'     => $viewer_handle->getName(),
 161          'requestTime'   => $releeph_request->getDateCreated(),
 162          'existing'      => $existing_releeph_request !== null,
 163        );
 164      }
 165  
 166      return $results;
 167    }
 168  
 169  }


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