Differential D10349 Diff 24927 src/applications/diffusion/controller/DiffusionRepositoryEditBranchesController.php
Changeset View
Changeset View
Standalone View
Standalone View
src/applications/diffusion/controller/DiffusionRepositoryEditBranchesController.php
Show All 37 Lines | switch ($repository->getVersionControlSystem()) { | ||||
default: | default: | ||||
throw new Exception( | throw new Exception( | ||||
pht('Repository has unknown version control system!')); | pht('Repository has unknown version control system!')); | ||||
} | } | ||||
$edit_uri = $this->getRepositoryControllerURI($repository, 'edit/'); | $edit_uri = $this->getRepositoryControllerURI($repository, 'edit/'); | ||||
$v_default = $repository->getHumanReadableDetail('default-branch'); | $v_default = $repository->getHumanReadableDetail('default-branch'); | ||||
$v_track = $repository->getHumanReadableDetail( | $v_track = $repository->getDetail( | ||||
'branch-filter', | 'branch-filter', | ||||
array()); | array()); | ||||
$v_autoclose = $repository->getHumanReadableDetail( | $v_track = array_keys($v_track); | ||||
$v_autoclose = $repository->getDetail( | |||||
'close-commits-filter', | 'close-commits-filter', | ||||
array()); | array()); | ||||
$v_autoclose = array_keys($v_autoclose); | |||||
$e_track = null; | |||||
$e_autoclose = null; | |||||
$validation_exception = null; | |||||
if ($request->isFormPost()) { | if ($request->isFormPost()) { | ||||
$v_default = $request->getStr('default'); | $v_default = $request->getStr('default'); | ||||
$v_track = $request->getStrList('track'); | |||||
$v_autoclose = $request->getStrList('autoclose'); | $v_track = $this->processBranches($request->getStr('track')); | ||||
if (!$is_hg) { | |||||
$v_autoclose = $this->processBranches($request->getStr('autoclose')); | |||||
} | |||||
$xactions = array(); | $xactions = array(); | ||||
$template = id(new PhabricatorRepositoryTransaction()); | $template = id(new PhabricatorRepositoryTransaction()); | ||||
$type_default = PhabricatorRepositoryTransaction::TYPE_DEFAULT_BRANCH; | $type_default = PhabricatorRepositoryTransaction::TYPE_DEFAULT_BRANCH; | ||||
$type_track = PhabricatorRepositoryTransaction::TYPE_TRACK_ONLY; | $type_track = PhabricatorRepositoryTransaction::TYPE_TRACK_ONLY; | ||||
$type_autoclose = PhabricatorRepositoryTransaction::TYPE_AUTOCLOSE_ONLY; | $type_autoclose = PhabricatorRepositoryTransaction::TYPE_AUTOCLOSE_ONLY; | ||||
$xactions[] = id(clone $template) | $xactions[] = id(clone $template) | ||||
->setTransactionType($type_default) | ->setTransactionType($type_default) | ||||
->setNewValue($v_default); | ->setNewValue($v_default); | ||||
$xactions[] = id(clone $template) | $xactions[] = id(clone $template) | ||||
->setTransactionType($type_track) | ->setTransactionType($type_track) | ||||
->setNewValue($v_track); | ->setNewValue($v_track); | ||||
if (!$is_hg) { | if (!$is_hg) { | ||||
$xactions[] = id(clone $template) | $xactions[] = id(clone $template) | ||||
->setTransactionType($type_autoclose) | ->setTransactionType($type_autoclose) | ||||
->setNewValue($v_autoclose); | ->setNewValue($v_autoclose); | ||||
} | } | ||||
id(new PhabricatorRepositoryEditor()) | $editor = id(new PhabricatorRepositoryEditor()) | ||||
->setContinueOnNoEffect(true) | ->setContinueOnNoEffect(true) | ||||
->setContentSourceFromRequest($request) | ->setContentSourceFromRequest($request) | ||||
->setActor($viewer) | ->setActor($viewer); | ||||
->applyTransactions($repository, $xactions); | |||||
try { | |||||
$editor->applyTransactions($repository, $xactions); | |||||
return id(new AphrontRedirectResponse())->setURI($edit_uri); | return id(new AphrontRedirectResponse())->setURI($edit_uri); | ||||
} catch (PhabricatorApplicationTransactionValidationException $ex) { | |||||
$validation_exception = $ex; | |||||
$e_track = $validation_exception->getShortMessage($type_track); | |||||
$e_autoclose = $validation_exception->getShortMessage($type_autoclose); | |||||
} | |||||
} | } | ||||
$content = array(); | $content = array(); | ||||
$crumbs = $this->buildApplicationCrumbs(); | $crumbs = $this->buildApplicationCrumbs(); | ||||
$crumbs->addTextCrumb(pht('Edit Branches')); | $crumbs->addTextCrumb(pht('Edit Branches')); | ||||
$title = pht('Edit Branches (%s)', $repository->getName()); | $title = pht('Edit Branches (%s)', $repository->getName()); | ||||
$policies = id(new PhabricatorPolicyQuery()) | $policies = id(new PhabricatorPolicyQuery()) | ||||
->setViewer($viewer) | ->setViewer($viewer) | ||||
->setObject($repository) | ->setObject($repository) | ||||
->execute(); | ->execute(); | ||||
$rows = array(); | |||||
$rows[] = array( | |||||
array( | |||||
'master', | |||||
), | |||||
pht('Select only master.'), | |||||
); | |||||
$rows[] = array( | |||||
array( | |||||
'master', | |||||
'develop', | |||||
'release', | |||||
), | |||||
pht('Select master, develop, and release.'), | |||||
); | |||||
$rows[] = array( | |||||
array( | |||||
'master', | |||||
'regexp(/^release-/)', | |||||
), | |||||
pht('Select master, and all branches which start with "release-".'), | |||||
); | |||||
$rows[] = array( | |||||
array( | |||||
'regexp(/^(?!temp-)/)', | |||||
), | |||||
pht('Select all branches which do not start with "temp-".'), | |||||
); | |||||
foreach ($rows as $k => $row) { | |||||
$rows[$k][0] = phutil_tag( | |||||
'pre', | |||||
array(), | |||||
implode("\n", $row[0])); | |||||
} | |||||
$example_table = id(new AphrontTableView($rows)) | |||||
->setHeaders( | |||||
array( | |||||
pht('Example'), | |||||
pht('Effect'), | |||||
)) | |||||
->setColumnClasses( | |||||
array( | |||||
'', | |||||
'wide', | |||||
)); | |||||
$v_track = implode("\n", $v_track); | |||||
$v_autoclose = implode("\n", $v_autoclose); | |||||
$form = id(new AphrontFormView()) | $form = id(new AphrontFormView()) | ||||
->setUser($viewer) | ->setUser($viewer) | ||||
->appendRemarkupInstructions( | ->appendRemarkupInstructions( | ||||
pht( | pht( | ||||
'You can choose a **Default Branch** for viewing this repository.'. | 'You can choose a **Default Branch** for viewing this repository.')) | ||||
"\n\n". | |||||
'If you want to import only some branches into Diffusion, you can '. | |||||
'list them in **Track Only**. Other branches will be ignored. If '. | |||||
'you do not specify any branches, all branches are tracked.'. | |||||
"\n\n". | |||||
'If you have **Autoclose** enabled, Phabricator can close tasks and '. | |||||
'revisions when corresponding commits are pushed to the repository. '. | |||||
'If you want to autoclose objects only when commits appear on '. | |||||
'specific branches, you can list those branches in **Autoclose '. | |||||
'Only**. By default, all branches autoclose objects.')) | |||||
->appendChild( | ->appendChild( | ||||
id(new AphrontFormTextControl()) | id(new AphrontFormTextControl()) | ||||
->setName('default') | ->setName('default') | ||||
->setLabel(pht('Default Branch')) | ->setLabel(pht('Default Branch')) | ||||
->setValue($v_default) | ->setValue($v_default)) | ||||
->setCaption( | ->appendRemarkupInstructions( | ||||
pht('Example: %s', phutil_tag('tt', array(), 'develop')))) | pht( | ||||
'If you want to import only some branches into Diffusion, you can '. | |||||
'list them in **Track Only**. Other branches will be ignored. If '. | |||||
'you do not specify any branches, all branches are tracked.')); | |||||
if (!$is_hg) { | |||||
$form->appendRemarkupInstructions( | |||||
pht( | |||||
'If you have **Autoclose** enabled for this repository, Phabricator '. | |||||
'can close tasks and revisions when corresponding commits are '. | |||||
'pushed to the repository. If you want to autoclose objects only '. | |||||
'when commits appear on specific branches, you can list those '. | |||||
'branches in **Autoclose Only**. By default, all tracked branches '. | |||||
'will autoclose objects.')); | |||||
} | |||||
$form | |||||
->appendRemarkupInstructions( | |||||
pht( | |||||
'When specifying branches, you should enter one branch name per '. | |||||
'line. You can use regular expressions to match branches by '. | |||||
'wrapping an expression in `regexp(...)`. For example:')) | |||||
->appendChild( | ->appendChild( | ||||
id(new AphrontFormTextControl()) | id(new AphrontFormMarkupControl()) | ||||
->setValue($example_table)) | |||||
->appendChild( | |||||
id(new AphrontFormTextAreaControl()) | |||||
->setName('track') | ->setName('track') | ||||
->setLabel(pht('Track Only')) | ->setLabel(pht('Track Only')) | ||||
->setValue($v_track) | ->setError($e_track) | ||||
->setCaption( | ->setValue($v_track)); | ||||
pht('Example: %s', phutil_tag('tt', array(), 'master, develop')))); | |||||
if (!$is_hg) { | if (!$is_hg) { | ||||
$form->appendChild( | $form->appendChild( | ||||
id(new AphrontFormTextControl()) | id(new AphrontFormTextAreaControl()) | ||||
->setName('autoclose') | ->setName('autoclose') | ||||
->setLabel(pht('Autoclose Only')) | ->setLabel(pht('Autoclose Only')) | ||||
->setValue($v_autoclose) | ->setError($e_autoclose) | ||||
->setCaption( | ->setValue($v_autoclose)); | ||||
pht('Example: %s', phutil_tag('tt', array(), 'master, release')))); | |||||
} | } | ||||
$form->appendChild( | $form->appendChild( | ||||
id(new AphrontFormSubmitControl()) | id(new AphrontFormSubmitControl()) | ||||
->setValue(pht('Save Branches')) | ->setValue(pht('Save Branches')) | ||||
->addCancelButton($edit_uri)); | ->addCancelButton($edit_uri)); | ||||
$form_box = id(new PHUIObjectBoxView()) | $form_box = id(new PHUIObjectBoxView()) | ||||
->setValidationException($validation_exception) | |||||
->setHeaderText($title) | ->setHeaderText($title) | ||||
->setForm($form); | ->setForm($form); | ||||
return $this->buildApplicationPage( | return $this->buildApplicationPage( | ||||
array( | array( | ||||
$crumbs, | $crumbs, | ||||
$form_box, | $form_box, | ||||
), | ), | ||||
array( | array( | ||||
'title' => $title, | 'title' => $title, | ||||
)); | )); | ||||
} | } | ||||
private function processBranches($string) { | |||||
$lines = phutil_split_lines($string, $retain_endings = false); | |||||
foreach ($lines as $key => $line) { | |||||
$lines[$key] = trim($line); | |||||
if (!strlen($lines[$key])) { | |||||
unset($lines[$key]); | |||||
} | |||||
} | |||||
return array_values($lines); | |||||
} | |||||
} | } |