/src/applications/herald/controller/test/HeraldTestConsoleController.php

https://github.com/juneym/phabricator · PHP · 145 lines · 111 code · 19 blank · 15 comment · 18 complexity · 35247be021cf559d551682bf0db07cb7 MD5 · raw file

  1. <?php
  2. /*
  3. * Copyright 2011 Facebook, Inc.
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. class HeraldTestConsoleController extends HeraldController {
  18. public function processRequest() {
  19. $request = $this->getRequest();
  20. $user = $request->getUser();
  21. $request = $this->getRequest();
  22. $object_name = trim($request->getStr('object_name'));
  23. $e_name = true;
  24. $errors = array();
  25. if ($request->isFormPost()) {
  26. if (!$object_name) {
  27. $e_name = 'Required';
  28. $errors[] = 'An object name is required.';
  29. }
  30. if (!$errors) {
  31. $matches = null;
  32. $object = null;
  33. if (preg_match('/^D(\d+)$/', $object_name, $matches)) {
  34. $object = id(new DifferentialRevision())->load($matches[1]);
  35. if (!$object) {
  36. $e_name = 'Invalid';
  37. $errors[] = 'No Differential Revision with that ID exists.';
  38. }
  39. } else if (preg_match('/^r([A-Z]+)(\w+)$/', $object_name, $matches)) {
  40. $repo = id(new PhabricatorRepository())->loadOneWhere(
  41. 'callsign = %s',
  42. $matches[1]);
  43. if (!$repo) {
  44. $e_name = 'Invalid';
  45. $errors[] = 'There is no repository with the callsign '.
  46. $matches[1].'.';
  47. }
  48. $commit = id(new PhabricatorRepositoryCommit())->loadOneWhere(
  49. 'repositoryID = %d AND commitIdentifier = %s',
  50. $repo->getID(),
  51. $matches[2]);
  52. if (!$commit) {
  53. $e_name = 'Invalid';
  54. $errors[] = 'There is no commit with that identifier.';
  55. }
  56. $object = $commit;
  57. } else {
  58. $e_name = 'Invalid';
  59. $errors[] = 'This object name is not recognized.';
  60. }
  61. if (!$errors) {
  62. if ($object instanceof DifferentialRevision) {
  63. $adapter = new HeraldDifferentialRevisionAdapter(
  64. $object,
  65. $object->loadActiveDiff());
  66. } else if ($object instanceof PhabricatorRepositoryCommit) {
  67. $data = id(new PhabricatorRepositoryCommitData())->loadOneWhere(
  68. 'commitID = %d',
  69. $object->getID());
  70. $adapter = new HeraldCommitAdapter(
  71. $repo,
  72. $object,
  73. $data);
  74. } else {
  75. throw new Exception("Can not build adapter for object!");
  76. }
  77. $rules = HeraldRule::loadAllByContentTypeWithFullData(
  78. $adapter->getHeraldTypeName());
  79. $engine = new HeraldEngine();
  80. $effects = $engine->applyRules($rules, $adapter);
  81. $dry_run = new HeraldDryRunAdapter();
  82. $engine->applyEffects($effects, $dry_run);
  83. $xscript = $engine->getTranscript();
  84. return id(new AphrontRedirectResponse())
  85. ->setURI('/herald/transcript/'.$xscript->getID().'/');
  86. }
  87. }
  88. }
  89. if ($errors) {
  90. $error_view = new AphrontErrorView();
  91. $error_view->setTitle('Form Errors');
  92. $error_view->setErrors($errors);
  93. } else {
  94. $error_view = null;
  95. }
  96. $form = id(new AphrontFormView())
  97. ->setUser($user)
  98. ->appendChild(
  99. '<p class="aphront-form-instructions">Enter an object to test rules '.
  100. 'for, like a Diffusion commit (e.g., <tt>rX123</tt>) or a '.
  101. 'Differential revision (e.g., <tt>D123</tt>). You will be shown the '.
  102. 'results of a dry run on the object.</p>')
  103. ->appendChild(
  104. id(new AphrontFormTextControl())
  105. ->setLabel('Object Name')
  106. ->setName('object_name')
  107. ->setError($e_name)
  108. ->setValue($object_name))
  109. ->appendChild(
  110. id(new AphrontFormSubmitControl())
  111. ->setValue('Test Rules'));
  112. $panel = new AphrontPanelView();
  113. $panel->setHeader('Test Herald Rules');
  114. $panel->setWidth(AphrontPanelView::WIDTH_FORM);
  115. $panel->appendChild($form);
  116. return $this->buildStandardPageResponse(
  117. array(
  118. $error_view,
  119. $panel,
  120. ),
  121. array(
  122. 'title' => 'Test Console',
  123. 'tab' => 'test',
  124. ));
  125. }
  126. }