summaryrefslogtreecommitdiffstats
path: root/polygerrit-ui/app/samples/coverage-plugin.html
blob: 9bec658708d588ff18304ef2245314cc9d97cb3c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
<dom-module id="coverage-plugin">
  <script>

    function populateWithDummyData(coverageData) {
      coverageData['NewFile'] = {
        linesMissingCoverage: [1, 2, 3],
        totalLines: 5,
        changeNum: 94,
        patchNum: 2,
      };
      coverageData['/COMMIT_MSG'] = {
        linesMissingCoverage: [3, 4, 7, 14],
        totalLines: 14,
        changeNum: 94,
        patchNum: 2,
      };
      coverageData['DEPS'] = {
        linesMissingCoverage: [3, 4, 7, 14],
        totalLines: 16,
        changeNum: 77001,
        patchNum: 1,
      };
      coverageData['go/sklog/sklog.go'] = {
        linesMissingCoverage: [3, 322, 323, 324],
        totalLines: 350,
        changeNum: 85963,
        patchNum: 13,
      };
    }

    Gerrit.install(plugin => {
      const coverageData = {};
      let displayCoverage = false;
      const annotationApi = plugin.annotationApi();
      annotationApi.addLayer(context => {
        if (Object.keys(coverageData).length === 0) {
           // Coverage data is not ready yet.
          return;
        }
        const path = context.path;
        const line = context.line;
          // Highlight lines missing coverage with this background color if
          // coverage should be displayed, else do nothing.
        const cssClass = displayCoverage
                         ? Gerrit.css('background-color: #EF9B9B')
                         : Gerrit.css('');
        if (coverageData[path] &&
              coverageData[path].changeNum === context.changeNum &&
              coverageData[path].patchNum === context.patchNum) {
          const linesMissingCoverage = coverageData[path].linesMissingCoverage;
          if (linesMissingCoverage.includes(line.afterNumber)) {
            context.annotateRange(0, line.text.length, cssClass, 'right');
          }
        }
      }).enableToggleCheckbox('Display Coverage', checkbox => {
        // Checkbox is attached so now add the notifier that will be controlled
        // by the checkbox.
        annotationApi.addNotifier(notifyFunc => {
          new Promise(resolve => setTimeout(resolve, 3000)).then(() => {
            populateWithDummyData(coverageData);
            checkbox.disabled = false;
            checkbox.onclick = e => {
              displayCoverage = e.target.checked;
              Object.keys(coverageData).forEach(file => {
                notifyFunc(file, 0, coverageData[file].totalLines, 'right');
              });
            };
          });
        });
      });
    });
  </script>
</dom-module>