PageRenderTime 48ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/src/applications/releeph/field/specification/ReleephDiffSizeFieldSpecification.php

http://github.com/facebook/phabricator
PHP | 118 lines | 100 code | 16 blank | 2 comment | 4 complexity | 54eb1bbcfd536dc683eabaefa84dddbc MD5 | raw file
Possible License(s): JSON, MPL-2.0-no-copyleft-exception, Apache-2.0, BSD-3-Clause, LGPL-2.0, MIT, LGPL-2.1, LGPL-3.0
  1. <?php
  2. final class ReleephDiffSizeFieldSpecification
  3. extends ReleephFieldSpecification {
  4. const LINES_WEIGHT = 1;
  5. const PATHS_WEIGHT = 30;
  6. const MAX_POINTS = 1000;
  7. public function getFieldKey() {
  8. return 'commit:size';
  9. }
  10. public function getName() {
  11. return pht('Size');
  12. }
  13. public function renderPropertyViewValue(array $handles) {
  14. $requested_object = $this->getObject()->getRequestedObject();
  15. if (!($requested_object instanceof DifferentialRevision)) {
  16. return null;
  17. }
  18. $diff_rev = $requested_object;
  19. $diffs = id(new DifferentialDiff())->loadAllWhere(
  20. 'revisionID = %d AND creationMethod != %s',
  21. $diff_rev->getID(),
  22. 'commit');
  23. $all_changesets = array();
  24. $most_recent_changesets = null;
  25. foreach ($diffs as $diff) {
  26. $changesets = id(new DifferentialChangeset())->loadAllWhere(
  27. 'diffID = %d',
  28. $diff->getID());
  29. $all_changesets += $changesets;
  30. $most_recent_changesets = $changesets;
  31. }
  32. // The score is based on all changesets for all versions of this diff
  33. $all_changes = $this->countLinesAndPaths($all_changesets);
  34. $points =
  35. self::LINES_WEIGHT * $all_changes['code']['lines'] +
  36. self::PATHS_WEIGHT * count($all_changes['code']['paths']);
  37. // The blurb is just based on the most recent version of the diff
  38. $mr_changes = $this->countLinesAndPaths($most_recent_changesets);
  39. $test_tag = '';
  40. if ($mr_changes['tests']['paths']) {
  41. Javelin::initBehavior('phabricator-tooltips');
  42. require_celerity_resource('aphront-tooltip-css');
  43. $test_blurb = pht(
  44. "%d line(s) and %d path(s) contain changes to test code:\n",
  45. $mr_changes['tests']['lines'],
  46. count($mr_changes['tests']['paths']));
  47. foreach ($mr_changes['tests']['paths'] as $mr_test_path) {
  48. $test_blurb .= sprintf("%s\n", $mr_test_path);
  49. }
  50. $test_tag = javelin_tag(
  51. 'span',
  52. array(
  53. 'sigil' => 'has-tooltip',
  54. 'meta' => array(
  55. 'tip' => $test_blurb,
  56. 'align' => 'E',
  57. 'size' => 'auto',
  58. ),
  59. 'style' => '',
  60. ),
  61. ' + tests');
  62. }
  63. $blurb = hsprintf('%s%s.',
  64. pht(
  65. '%d line(s) and %d path(s) over %d diff(s)',
  66. $mr_changes['code']['lines'],
  67. $mr_changes['code']['paths'],
  68. count($diffs)),
  69. $test_tag);
  70. return id(new AphrontProgressBarView())
  71. ->setValue($points)
  72. ->setMax(self::MAX_POINTS)
  73. ->setCaption($blurb)
  74. ->render();
  75. }
  76. private function countLinesAndPaths(array $changesets) {
  77. assert_instances_of($changesets, 'DifferentialChangeset');
  78. $lines = 0;
  79. $paths_touched = array();
  80. $test_lines = 0;
  81. $test_paths_touched = array();
  82. foreach ($changesets as $ch) {
  83. if ($this->getReleephProject()->isTestFile($ch->getFilename())) {
  84. $test_lines += $ch->getAddLines() + $ch->getDelLines();
  85. $test_paths_touched[] = $ch->getFilename();
  86. } else {
  87. $lines += $ch->getAddLines() + $ch->getDelLines();
  88. $paths_touched[] = $ch->getFilename();
  89. }
  90. }
  91. return array(
  92. 'code' => array(
  93. 'lines' => $lines,
  94. 'paths' => array_unique($paths_touched),
  95. ),
  96. 'tests' => array(
  97. 'lines' => $test_lines,
  98. 'paths' => array_unique($test_paths_touched),
  99. ),
  100. );
  101. }
  102. }