2015-10-14 00:46:12 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
final class DifferentialRevisionOperationController
|
|
|
|
extends DifferentialController {
|
|
|
|
|
|
|
|
public function handleRequest(AphrontRequest $request) {
|
|
|
|
$viewer = $this->getViewer();
|
|
|
|
$id = $request->getURIData('id');
|
|
|
|
|
|
|
|
$revision = id(new DifferentialRevisionQuery())
|
|
|
|
->withIDs(array($id))
|
|
|
|
->setViewer($viewer)
|
2015-10-14 00:46:30 +02:00
|
|
|
->needActiveDiffs(true)
|
2015-10-14 00:46:12 +02:00
|
|
|
->executeOne();
|
|
|
|
if (!$revision) {
|
|
|
|
return new Aphront404Response();
|
|
|
|
}
|
|
|
|
|
|
|
|
$detail_uri = "/D{$id}";
|
|
|
|
|
2015-10-26 20:58:37 +01:00
|
|
|
$op = new DrydockLandRepositoryOperation();
|
2015-10-27 19:51:59 +01:00
|
|
|
$barrier = $op->getBarrierToLanding($viewer, $revision);
|
|
|
|
if ($barrier) {
|
|
|
|
return $this->newDialog()
|
|
|
|
->setTitle($barrier['title'])
|
|
|
|
->appendParagraph($barrier['body'])
|
|
|
|
->addCancelButton($detail_uri);
|
2015-10-26 20:58:37 +01:00
|
|
|
}
|
|
|
|
|
2015-12-10 22:35:30 +01:00
|
|
|
$diff = $revision->getActiveDiff();
|
|
|
|
$repository = $revision->getRepository();
|
|
|
|
|
2015-12-10 23:50:58 +01:00
|
|
|
$default_ref = $this->loadDefaultRef($repository, $diff);
|
2015-12-10 23:04:18 +01:00
|
|
|
|
|
|
|
if ($default_ref) {
|
|
|
|
$v_ref = array($default_ref->getPHID());
|
|
|
|
} else {
|
|
|
|
$v_ref = array();
|
|
|
|
}
|
2015-12-10 22:35:30 +01:00
|
|
|
|
|
|
|
$e_ref = true;
|
|
|
|
|
|
|
|
$errors = array();
|
2015-10-14 00:46:12 +02:00
|
|
|
if ($request->isFormPost()) {
|
2015-10-14 00:46:30 +02:00
|
|
|
|
2015-12-10 23:04:18 +01:00
|
|
|
$v_ref = $request->getArr('refPHIDs');
|
|
|
|
$ref_phid = head($v_ref);
|
2015-12-10 22:35:30 +01:00
|
|
|
if (!strlen($ref_phid)) {
|
|
|
|
$e_ref = pht('Required');
|
|
|
|
$errors[] = pht(
|
|
|
|
'You must select a branch to land this revision onto.');
|
|
|
|
} else {
|
2015-12-10 23:04:18 +01:00
|
|
|
$ref = $this->newRefQuery($repository)
|
2015-12-10 22:35:30 +01:00
|
|
|
->withPHIDs(array($ref_phid))
|
|
|
|
->executeOne();
|
|
|
|
if (!$ref) {
|
|
|
|
$e_ref = pht('Invalid');
|
|
|
|
$errors[] = pht(
|
|
|
|
'You must select a branch from this repository to land this '.
|
|
|
|
'revision onto.');
|
|
|
|
}
|
|
|
|
}
|
2015-10-14 00:46:30 +02:00
|
|
|
|
2015-12-10 22:35:30 +01:00
|
|
|
if (!$errors) {
|
|
|
|
// NOTE: The operation is locked to the current active diff, so if the
|
|
|
|
// revision is updated before the operation applies nothing sneaky
|
|
|
|
// occurs.
|
2015-10-14 00:46:12 +02:00
|
|
|
|
2015-12-10 22:35:30 +01:00
|
|
|
$target = 'branch:'.$ref->getRefName();
|
2015-10-14 00:46:12 +02:00
|
|
|
|
2015-12-10 22:35:30 +01:00
|
|
|
$operation = DrydockRepositoryOperation::initializeNewOperation($op)
|
|
|
|
->setAuthorPHID($viewer->getPHID())
|
|
|
|
->setObjectPHID($revision->getPHID())
|
|
|
|
->setRepositoryPHID($repository->getPHID())
|
|
|
|
->setRepositoryTarget($target)
|
|
|
|
->setProperty('differential.diffPHID', $diff->getPHID());
|
|
|
|
|
|
|
|
$operation->save();
|
|
|
|
$operation->scheduleUpdate();
|
|
|
|
|
|
|
|
return id(new AphrontRedirectResponse())
|
|
|
|
->setURI($detail_uri);
|
|
|
|
}
|
2015-10-14 00:46:12 +02:00
|
|
|
}
|
|
|
|
|
2015-12-10 22:35:30 +01:00
|
|
|
$ref_datasource = id(new DiffusionRefDatasource())
|
|
|
|
->setParameters(
|
|
|
|
array(
|
|
|
|
'repositoryPHIDs' => array($repository->getPHID()),
|
2015-12-10 23:04:18 +01:00
|
|
|
'refTypes' => $this->getTargetableRefTypes(),
|
2015-12-10 22:35:30 +01:00
|
|
|
));
|
|
|
|
|
|
|
|
$form = id(new AphrontFormView())
|
|
|
|
->setUser($viewer)
|
|
|
|
->appendRemarkupInstructions(
|
2015-10-14 00:46:12 +02:00
|
|
|
pht(
|
2015-12-26 21:20:21 +01:00
|
|
|
'(NOTE) This feature is new and experimental.'))
|
2015-12-10 22:35:30 +01:00
|
|
|
->appendControl(
|
|
|
|
id(new AphrontFormTokenizerControl())
|
|
|
|
->setLabel(pht('Onto Branch'))
|
|
|
|
->setName('refPHIDs')
|
|
|
|
->setLimit(1)
|
|
|
|
->setError($e_ref)
|
2015-12-10 23:04:18 +01:00
|
|
|
->setValue($v_ref)
|
2015-12-26 21:20:21 +01:00
|
|
|
->setDatasource($ref_datasource));
|
2015-12-10 22:35:30 +01:00
|
|
|
|
|
|
|
return $this->newDialog()
|
|
|
|
->setWidth(AphrontDialogView::WIDTH_FORM)
|
|
|
|
->setTitle(pht('Land Revision'))
|
|
|
|
->setErrors($errors)
|
|
|
|
->appendForm($form)
|
2015-10-14 00:46:12 +02:00
|
|
|
->addCancelButton($detail_uri)
|
2015-12-26 21:20:21 +01:00
|
|
|
->addSubmitButton(pht('Land Revision'));
|
2015-10-14 00:46:12 +02:00
|
|
|
}
|
|
|
|
|
2015-12-10 23:04:18 +01:00
|
|
|
private function newRefQuery(PhabricatorRepository $repository) {
|
|
|
|
$viewer = $this->getViewer();
|
|
|
|
|
|
|
|
return id(new PhabricatorRepositoryRefCursorQuery())
|
|
|
|
->setViewer($viewer)
|
|
|
|
->withRepositoryPHIDs(array($repository->getPHID()))
|
|
|
|
->withRefTypes($this->getTargetableRefTypes());
|
|
|
|
}
|
|
|
|
|
|
|
|
private function getTargetableRefTypes() {
|
|
|
|
return array(
|
|
|
|
PhabricatorRepositoryRefCursor::TYPE_BRANCH,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2015-12-10 23:50:58 +01:00
|
|
|
private function loadDefaultRef(
|
|
|
|
PhabricatorRepository $repository,
|
|
|
|
DifferentialDiff $diff) {
|
|
|
|
$default_name = $this->getDefaultRefName($repository, $diff);
|
2015-12-10 23:04:18 +01:00
|
|
|
|
|
|
|
if (!strlen($default_name)) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2017-09-13 21:14:06 +02:00
|
|
|
// NOTE: See PHI68. This is a workaround to make "Land Revision" work
|
|
|
|
// until T11823 is fixed properly. If we find multiple refs with the same
|
|
|
|
// name (normally, duplicate "master" refs), just pick the first one.
|
|
|
|
|
|
|
|
$refs = $this->newRefQuery($repository)
|
2015-12-10 23:04:18 +01:00
|
|
|
->withRefNames(array($default_name))
|
2017-09-13 21:14:06 +02:00
|
|
|
->execute();
|
|
|
|
|
|
|
|
if ($refs) {
|
|
|
|
return head($refs);
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
2015-12-10 23:04:18 +01:00
|
|
|
}
|
|
|
|
|
2015-12-10 23:50:58 +01:00
|
|
|
private function getDefaultRefName(
|
|
|
|
PhabricatorRepository $repository,
|
|
|
|
DifferentialDiff $diff) {
|
|
|
|
|
|
|
|
$onto = $diff->loadTargetBranch();
|
|
|
|
if ($onto !== null) {
|
|
|
|
return $onto;
|
|
|
|
}
|
|
|
|
|
2015-12-10 23:04:18 +01:00
|
|
|
return $repository->getDefaultBranch();
|
|
|
|
}
|
|
|
|
|
2015-10-14 00:46:12 +02:00
|
|
|
}
|