~ubuntu-branches/ubuntu/wily/phabricator/wily-proposed

« back to all changes in this revision

Viewing changes to phabricator/src/applications/differential/customfield/DifferentialLintField.php

  • Committer: Package Import Robot
  • Author(s): Richard Sellam
  • Date: 2015-08-03 23:28:35 UTC
  • mfrom: (0.39.1) (0.38.1) (0.27.3) (2.1.10 sid)
  • Revision ID: package-import@ubuntu.com-20150803232835-qnomusa964oxnywb
Tags: 0~git20150803-1
* New snapshot release (closes: #789760)
* renamed arcanist bash-completion file (closes: #791632)
* depends on same version for libphutil (closes: #794462)
* added php5-mysqlnd alternative depends (closes: #792136)

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
<?php
2
2
 
3
3
final class DifferentialLintField
4
 
  extends DifferentialCustomField {
 
4
  extends DifferentialHarbormasterField {
5
5
 
6
6
  public function getFieldKey() {
7
7
    return 'differential:lint';
19
19
    return true;
20
20
  }
21
21
 
22
 
  public function renderPropertyViewLabel() {
 
22
  public function renderPropertyViewValue(array $handles) {
 
23
    return null;
 
24
  }
 
25
 
 
26
  public function shouldAppearInDiffPropertyView() {
 
27
    return true;
 
28
  }
 
29
 
 
30
  public function renderDiffPropertyViewLabel(DifferentialDiff $diff) {
23
31
    return $this->getFieldName();
24
32
  }
25
33
 
26
 
  public function getRequiredDiffPropertiesForRevisionView() {
 
34
  protected function getLegacyProperty() {
 
35
    return 'arc:lint';
 
36
  }
 
37
 
 
38
  protected function getDiffPropertyKeys() {
27
39
    return array(
28
40
      'arc:lint',
29
41
      'arc:lint-excuse',
30
 
      'arc:lint-postponed',
31
 
    );
32
 
  }
33
 
 
34
 
  public function renderPropertyViewValue(array $handles) {
35
 
    $diff = $this->getObject()->getActiveDiff();
36
 
 
37
 
    $path_changesets = mpull($diff->loadChangesets(), 'getID', 'getFilename');
38
 
 
39
 
    $lstar = DifferentialRevisionUpdateHistoryView::renderDiffLintStar($diff);
40
 
    $lmsg = DifferentialRevisionUpdateHistoryView::getDiffLintMessage($diff);
41
 
    $ldata = $diff->getProperty('arc:lint');
42
 
    $ltail = null;
43
 
 
44
 
    $rows = array();
45
 
 
46
 
    $rows[] = array(
47
 
      'style'     => 'star',
48
 
      'name'      => $lstar,
49
 
      'value'     => $lmsg,
50
 
      'show'      => true,
51
 
    );
52
 
 
53
 
    $excuse = $diff->getProperty('arc:lint-excuse');
54
 
    if ($excuse) {
55
 
      $rows[] = array(
56
 
        'style'   => 'excuse',
57
 
        'name'    => 'Excuse',
58
 
        'value'   => phutil_escape_html_newlines($excuse),
59
 
        'show'    => true,
60
 
      );
61
 
    }
62
 
 
63
 
    $show_limit = 10;
64
 
    $hidden = array();
65
 
 
66
 
    if ($ldata) {
67
 
      $ldata = igroup($ldata, 'path');
68
 
      foreach ($ldata as $path => $messages) {
69
 
 
70
 
        $rows[] = array(
71
 
          'style' => 'section',
72
 
          'name'  => $path,
73
 
          'show'  => $show_limit,
74
 
        );
75
 
 
76
 
        foreach ($messages as $message) {
77
 
          $path = idx($message, 'path');
78
 
          $line = idx($message, 'line');
79
 
 
80
 
          $code = idx($message, 'code');
81
 
          $severity = idx($message, 'severity');
82
 
 
83
 
          $name = idx($message, 'name');
84
 
          $description = idx($message, 'description');
85
 
 
86
 
          $line_link = pht('line %d', intval($line));
87
 
          if (isset($path_changesets[$path])) {
88
 
            $href = '#C'.$path_changesets[$path].'NL'.max(1, $line);
89
 
 
90
 
            // TODO: We are always showing the active diff
91
 
            // if ($diff->getID() != $this->getDiff()->getID()) {
92
 
            //  $href = '/D'.$diff->getRevisionID().'?id='.$diff->getID().$href;
93
 
            // }
94
 
 
95
 
            $line_link = phutil_tag(
96
 
              'a',
97
 
              array(
98
 
                'href' => $href,
99
 
              ),
100
 
              $line_link);
101
 
          }
102
 
 
103
 
          if ($show_limit) {
104
 
            --$show_limit;
105
 
            $show = true;
106
 
          } else {
107
 
            $show = false;
108
 
            if (empty($hidden[$severity])) {
109
 
              $hidden[$severity] = 0;
110
 
            }
111
 
            $hidden[$severity]++;
112
 
          }
113
 
 
114
 
          $rows[] = array(
115
 
            'style' => $this->getSeverityStyle($severity),
116
 
            'name'  => ucwords($severity),
117
 
            'value' => hsprintf(
118
 
              '(%s) %s at %s',
119
 
              $code,
120
 
              $name,
121
 
              $line_link),
122
 
            'show'  => $show,
123
 
          );
124
 
 
125
 
          if (!empty($message['locations'])) {
126
 
            $locations = array();
127
 
            foreach ($message['locations'] as $location) {
128
 
              $other_line = idx($location, 'line');
129
 
              $locations[] =
130
 
                idx($location, 'path', $path).
131
 
                ($other_line ? ":{$other_line}" : '');
132
 
            }
133
 
            $description .= "\n".pht(
134
 
              'Other locations: %s',
135
 
              implode(', ', $locations));
136
 
          }
137
 
 
138
 
          if (strlen($description)) {
139
 
            $rows[] = array(
140
 
              'style' => 'details',
141
 
              'value' => phutil_escape_html_newlines($description),
142
 
              'show'  => false,
143
 
            );
144
 
            if (empty($hidden['details'])) {
145
 
              $hidden['details'] = 0;
146
 
            }
147
 
            $hidden['details']++;
148
 
          }
149
 
        }
150
 
      }
151
 
    }
152
 
 
153
 
    $postponed = $diff->getProperty('arc:lint-postponed');
154
 
    if ($postponed) {
155
 
      foreach ($postponed as $linter) {
156
 
        $rows[] = array(
157
 
          'style' => $this->getPostponedStyle(),
158
 
          'name' => 'Postponed',
159
 
          'value' => $linter,
160
 
          'show'  => false,
161
 
          );
162
 
        if (empty($hidden['postponed'])) {
163
 
          $hidden['postponed'] = 0;
164
 
        }
165
 
        $hidden['postponed']++;
166
 
      }
167
 
    }
168
 
 
169
 
    $show_string = $this->renderShowString($hidden);
170
 
 
171
 
    $view = new DifferentialResultsTableView();
172
 
    $view->setRows($rows);
173
 
    $view->setShowMoreString($show_string);
174
 
 
175
 
    return $view->render();
176
 
  }
177
 
 
178
 
  private function getSeverityStyle($severity) {
179
 
    $map = array(
180
 
      ArcanistLintSeverity::SEVERITY_ERROR      => 'red',
181
 
      ArcanistLintSeverity::SEVERITY_WARNING    => 'yellow',
182
 
      ArcanistLintSeverity::SEVERITY_AUTOFIX    => 'yellow',
183
 
      ArcanistLintSeverity::SEVERITY_ADVICE     => 'yellow',
184
 
    );
185
 
    return idx($map, $severity);
186
 
  }
187
 
 
188
 
  private function getPostponedStyle() {
189
 
    return 'blue';
190
 
  }
191
 
 
192
 
  private function renderShowString(array $hidden) {
193
 
    if (!$hidden) {
194
 
      return null;
195
 
    }
196
 
 
197
 
    // Reorder hidden things by severity.
198
 
    $hidden = array_select_keys(
199
 
      $hidden,
200
 
      array(
201
 
        ArcanistLintSeverity::SEVERITY_ERROR,
202
 
        ArcanistLintSeverity::SEVERITY_WARNING,
203
 
        ArcanistLintSeverity::SEVERITY_AUTOFIX,
204
 
        ArcanistLintSeverity::SEVERITY_ADVICE,
205
 
        'details',
206
 
        'postponed',
207
 
      )) + $hidden;
208
 
 
209
 
    $show = array();
210
 
    foreach ($hidden as $key => $value) {
211
 
      switch ($key) {
212
 
        case ArcanistLintSeverity::SEVERITY_ERROR:
213
 
          $show[] = pht('%d Error(s)', $value);
214
 
          break;
215
 
        case ArcanistLintSeverity::SEVERITY_WARNING:
216
 
          $show[] = pht('%d Warning(s)', $value);
217
 
          break;
218
 
        case ArcanistLintSeverity::SEVERITY_AUTOFIX:
219
 
          $show[] = pht('%d Auto-Fix(es)', $value);
220
 
          break;
221
 
        case ArcanistLintSeverity::SEVERITY_ADVICE:
222
 
          $show[] = pht('%d Advice(s)', $value);
223
 
          break;
224
 
        case 'details':
225
 
          $show[] = pht('%d Detail(s)', $value);
226
 
          break;
227
 
        case 'postponed':
228
 
          $show[] = pht('%d Postponed', $value);
229
 
          break;
230
 
        default:
231
 
          $show[] = $value;
232
 
          break;
233
 
      }
234
 
    }
235
 
 
236
 
    return pht(
237
 
      'Show Full Lint Results (%s)',
238
 
      implode(', ', $show));
 
42
    );
 
43
  }
 
44
 
 
45
  protected function loadHarbormasterTargetMessages(array $target_phids) {
 
46
    return id(new HarbormasterBuildLintMessage())->loadAllWhere(
 
47
      'buildTargetPHID IN (%Ls) LIMIT 25',
 
48
      $target_phids);
 
49
  }
 
50
 
 
51
  protected function newHarbormasterMessageView(array $messages) {
 
52
    return id(new HarbormasterLintPropertyView())
 
53
      ->setLimit(25)
 
54
      ->setLintMessages($messages);
 
55
  }
 
56
 
 
57
  protected function newModernMessage(array $message) {
 
58
    return HarbormasterBuildLintMessage::newFromDictionary(
 
59
      new HarbormasterBuildTarget(),
 
60
      $this->getModernLintMessageDictionary($message));
239
61
  }
240
62
 
241
63
  public function getWarningsForDetailView() {
261
83
    return $warnings;
262
84
  }
263
85
 
 
86
  protected function renderHarbormasterStatus(
 
87
    DifferentialDiff $diff,
 
88
    array $messages) {
 
89
 
 
90
    $colors = array(
 
91
      DifferentialLintStatus::LINT_NONE => 'grey',
 
92
      DifferentialLintStatus::LINT_OKAY => 'green',
 
93
      DifferentialLintStatus::LINT_WARN => 'yellow',
 
94
      DifferentialLintStatus::LINT_FAIL => 'red',
 
95
      DifferentialLintStatus::LINT_SKIP => 'blue',
 
96
      DifferentialLintStatus::LINT_AUTO_SKIP => 'blue',
 
97
      DifferentialLintStatus::LINT_POSTPONED => 'blue',
 
98
    );
 
99
    $icon_color = idx($colors, $diff->getLintStatus(), 'grey');
 
100
 
 
101
    $message = DifferentialRevisionUpdateHistoryView::getDiffLintMessage($diff);
 
102
 
 
103
    $excuse = $diff->getProperty('arc:lint-excuse');
 
104
    if (strlen($excuse)) {
 
105
      $excuse = array(
 
106
        phutil_tag('strong', array(), pht('Excuse:')),
 
107
        ' ',
 
108
        phutil_escape_html_newlines($excuse),
 
109
      );
 
110
    }
 
111
 
 
112
    $status = id(new PHUIStatusListView())
 
113
      ->addItem(
 
114
        id(new PHUIStatusItemView())
 
115
          ->setIcon(PHUIStatusItemView::ICON_STAR, $icon_color)
 
116
          ->setTarget($message)
 
117
          ->setNote($excuse));
 
118
 
 
119
    return $status;
 
120
  }
 
121
 
 
122
  private function getModernLintMessageDictionary(array $map) {
 
123
    // Strip out `null` values to satisfy stricter typechecks.
 
124
    foreach ($map as $key => $value) {
 
125
      if ($value === null) {
 
126
        unset($map[$key]);
 
127
      }
 
128
    }
 
129
 
 
130
    // TODO: We might need to remap some stuff here?
 
131
    return $map;
 
132
  }
 
133
 
 
134
 
264
135
}