Path: blob/master/src/applications/differential/controller/DifferentialRevisionOperationController.php
12256 views
<?php12final class DifferentialRevisionOperationController3extends DifferentialController {45public function handleRequest(AphrontRequest $request) {6$viewer = $this->getViewer();7$id = $request->getURIData('id');89$revision = id(new DifferentialRevisionQuery())10->withIDs(array($id))11->setViewer($viewer)12->needActiveDiffs(true)13->executeOne();14if (!$revision) {15return new Aphront404Response();16}1718$detail_uri = "/D{$id}";1920$op = new DrydockLandRepositoryOperation();21$barrier = $op->getBarrierToLanding($viewer, $revision);22if ($barrier) {23return $this->newDialog()24->setTitle($barrier['title'])25->appendParagraph($barrier['body'])26->addCancelButton($detail_uri);27}2829$diff = $revision->getActiveDiff();30$repository = $revision->getRepository();3132$default_ref = $this->loadDefaultRef($repository, $diff);3334if ($default_ref) {35$v_ref = array($default_ref->getPHID());36} else {37$v_ref = array();38}3940$e_ref = true;4142$errors = array();43if ($request->isFormPost()) {4445$v_ref = $request->getArr('refPHIDs');46$ref_phid = head($v_ref);47if (!strlen($ref_phid)) {48$e_ref = pht('Required');49$errors[] = pht(50'You must select a branch to land this revision onto.');51} else {52$ref = $this->newRefQuery($repository)53->withPHIDs(array($ref_phid))54->executeOne();55if (!$ref) {56$e_ref = pht('Invalid');57$errors[] = pht(58'You must select a branch from this repository to land this '.59'revision onto.');60}61}6263if (!$errors) {64// NOTE: The operation is locked to the current active diff, so if the65// revision is updated before the operation applies nothing sneaky66// occurs.6768$target = 'branch:'.$ref->getRefName();6970$operation = DrydockRepositoryOperation::initializeNewOperation($op)71->setAuthorPHID($viewer->getPHID())72->setObjectPHID($revision->getPHID())73->setRepositoryPHID($repository->getPHID())74->setRepositoryTarget($target)75->setProperty('differential.diffPHID', $diff->getPHID());7677$operation->save();78$operation->scheduleUpdate();7980return id(new AphrontRedirectResponse())81->setURI($detail_uri);82}83}8485$ref_datasource = id(new DiffusionRefDatasource())86->setParameters(87array(88'repositoryPHIDs' => array($repository->getPHID()),89'refTypes' => $this->getTargetableRefTypes(),90));9192$form = id(new AphrontFormView())93->setUser($viewer)94->appendRemarkupInstructions(95pht(96'(NOTE) This feature is new and experimental.'))97->appendControl(98id(new AphrontFormTokenizerControl())99->setLabel(pht('Onto Branch'))100->setName('refPHIDs')101->setLimit(1)102->setError($e_ref)103->setValue($v_ref)104->setDatasource($ref_datasource));105106return $this->newDialog()107->setWidth(AphrontDialogView::WIDTH_FORM)108->setTitle(pht('Land Revision'))109->setErrors($errors)110->appendForm($form)111->addCancelButton($detail_uri)112->addSubmitButton(pht('Land Revision'));113}114115private function newRefQuery(PhabricatorRepository $repository) {116$viewer = $this->getViewer();117118return id(new PhabricatorRepositoryRefCursorQuery())119->setViewer($viewer)120->withRepositoryPHIDs(array($repository->getPHID()))121->withRefTypes($this->getTargetableRefTypes());122}123124private function getTargetableRefTypes() {125return array(126PhabricatorRepositoryRefCursor::TYPE_BRANCH,127);128}129130private function loadDefaultRef(131PhabricatorRepository $repository,132DifferentialDiff $diff) {133$default_name = $this->getDefaultRefName($repository, $diff);134135if (!strlen($default_name)) {136return null;137}138139return $this->newRefQuery($repository)140->withRefNames(array($default_name))141->executeOne();142}143144private function getDefaultRefName(145PhabricatorRepository $repository,146DifferentialDiff $diff) {147148$onto = $diff->loadTargetBranch();149if ($onto !== null) {150return $onto;151}152153return $repository->getDefaultBranch();154}155156}157158159