github.com/shashidharatd/test-infra@v0.0.0-20171006011030-71304e1ca560/prow/cmd/deck/static/script.js (about)

     1  "use strict";
     2  
     3  function getParameterByName(name) {  // http://stackoverflow.com/a/5158301/3694
     4      var match = RegExp('[?&]' + name + '=([^&/]*)').exec(window.location.search);
     5      return match && decodeURIComponent(match[1].replace(/\+/g, ' '));
     6  }
     7  
     8  function updateQueryStringParameter(uri, key, value) {
     9      var re = new RegExp("([?&])" + key + "=.*?(&|$)", "i");
    10      var separator = uri.indexOf('?') !== -1 ? "&" : "?";
    11      if (uri.match(re)) {
    12              return uri.replace(re, '$1' + key + "=" + value + '$2');
    13      } else {
    14          return uri + separator + key + "=" + value;
    15      }
    16  }
    17  
    18  function optionsForRepo(repo) {
    19      var opts = {
    20          types: {},
    21          repos: {},
    22          jobs: {},
    23          authors: {},
    24          pulls: {},
    25          states: {},
    26      };
    27  
    28      for (var i = 0; i < allBuilds.length; i++) {
    29          var build = allBuilds[i];
    30          opts.types[build.type] = true;
    31          opts.repos[build.repo] = true;
    32          if (!repo || repo == build.repo) {
    33              opts.jobs[build.job] = true;
    34              if (build.type === "presubmit") {
    35                  opts.authors[build.author] = true;
    36                  opts.pulls[build.number] = true;
    37                  opts.states[build.state] = true;
    38              }
    39          }
    40      }
    41  
    42      return opts;
    43  }
    44  
    45  function redrawOptions(opts) {
    46      var ts = Object.keys(opts.types).sort();
    47      addOptions(ts, "type");
    48      var rs = Object.keys(opts.repos).filter(function(r) { return r !== "/"; }).sort();
    49      addOptions(rs, "repo");
    50      var js = Object.keys(opts.jobs).sort();
    51      addOptions(js, "job");
    52      var as = Object.keys(opts.authors).sort(function (a, b) {
    53          return a.toLowerCase().localeCompare(b.toLowerCase());
    54      });
    55      addOptions(as, "author");
    56      var ps = Object.keys(opts.pulls).sort(function (a, b) {
    57          return parseInt(a) - parseInt(b);
    58      });
    59      addOptions(ps, "pull");
    60      var ss = Object.keys(opts.states).sort();
    61      addOptions(ss, "state");
    62  };
    63  
    64  window.onload = function() {
    65      // set dropdown based on options from query string
    66      redrawOptions(optionsForRepo(""));
    67      redraw();
    68  };
    69  
    70  function addOptions(s, p) {
    71      var sel = document.getElementById(p);
    72      while (sel.length > 1)
    73          sel.removeChild(sel.lastChild);
    74      var param = getParameterByName(p);
    75      for (var i = 0; i < s.length; i++) {
    76          var o = document.createElement("option");
    77          o.text = s[i];
    78          if (param && s[i] === param) {
    79              o.selected = true;
    80          }
    81          sel.appendChild(o);
    82      }
    83  }
    84  
    85  function selectionText(sel, t) {
    86      return sel.selectedIndex == 0 ? "" : sel.options[sel.selectedIndex].text;
    87  }
    88  
    89  function equalSelected(sel, t) {
    90      return sel === "" || sel == t;
    91  }
    92  
    93  function groupKey(build) {
    94      return build.repo + " " + build.number + " " + build.refs;
    95  }
    96  
    97  function redraw() {
    98      var modal = document.getElementById('rerun');
    99      var rerun_command = document.getElementById('rerun-content');
   100      window.onclick = function(event) {
   101          if (event.target == modal) {
   102              modal.style.display = "none";
   103          }
   104      };
   105      var builds = document.getElementById("builds").getElementsByTagName("tbody")[0];
   106      while (builds.firstChild)
   107          builds.removeChild(builds.firstChild);
   108  
   109      var args = [];
   110      function getSelection(name) {
   111          var sel = selectionText(document.getElementById(name));
   112          if (sel && opts && !opts[name + 's'][sel]) return "";
   113          if (sel !== "") args.push(name + "=" + encodeURIComponent(sel));
   114          return sel;
   115      }
   116  
   117      var opts = null;
   118      var repoSel = getSelection("repo");
   119      opts = optionsForRepo(repoSel);
   120  
   121      var typeSel = getSelection("type");
   122      var pullSel = getSelection("pull");
   123      var authorSel = getSelection("author");
   124      var jobSel = getSelection("job");
   125      var stateSel = getSelection("state");
   126  
   127      if (window.history && window.history.replaceState !== undefined) {
   128          if (args.length > 0) {
   129              history.replaceState(null, "", "/?" + args.join('&'));
   130          } else {
   131              history.replaceState(null, "", "/")
   132          }
   133      }
   134      redrawOptions(opts);
   135  
   136      var lastKey = '';
   137      for (var i = 0, emitted = 0; i < allBuilds.length && emitted < 500; i++) {
   138          var build = allBuilds[i];
   139          if (!equalSelected(typeSel, build.type)) continue;
   140          if (!equalSelected(repoSel, build.repo)) continue;
   141          if (!equalSelected(stateSel, build.state)) continue;
   142          if (!equalSelected(jobSel, build.job)) continue;
   143          if (build.type === "presubmit") {
   144              if (!equalSelected(pullSel, build.number)) continue;
   145              if (!equalSelected(authorSel, build.author)) continue;
   146          } else if (pullSel || authorSel) {
   147              continue;
   148          }
   149          emitted++;
   150  
   151          var r = document.createElement("tr");
   152          r.appendChild(stateCell(build.state));
   153          if (build.pod_name) {
   154              r.appendChild(createLinkCell("\u2261", "log?job=" + build.job + "&id=" + build.build_id, "Build log."));
   155          } else {
   156              r.appendChild(createTextCell(""));
   157          }
   158          r.appendChild(createRerunCell(modal, rerun_command, build.prow_job));
   159          var key = groupKey(build);
   160          if (key !== lastKey) {
   161              // This is a different PR or commit than the previous row.
   162              lastKey = key;
   163              r.className = "changed";
   164  
   165              if (build.type === "periodic") {
   166                  r.appendChild(createTextCell(""));
   167              } else {
   168                  r.appendChild(createLinkCell(build.repo, "https://github.com/" + build.repo, ""));
   169              }
   170              if (build.type === "presubmit") {
   171                  r.appendChild(prRevisionCell(build));
   172              } else if (build.type === "batch") {
   173                  r.appendChild(batchRevisionCell(build));
   174              } else if (build.type === "postsubmit") {
   175                  r.appendChild(pushRevisionCell(build));
   176              } else if (build.type === "periodic") {
   177                  r.appendChild(createTextCell(""));
   178              }
   179          } else {
   180              // Don't render identical cells for the same PR/commit.
   181              r.appendChild(createTextCell(""));
   182              r.appendChild(createTextCell(""));
   183          }
   184          if (build.url === "") {
   185              r.appendChild(createTextCell(build.job));
   186          } else {
   187              r.appendChild(createLinkCell(build.job, build.url, ""));
   188          }
   189          r.appendChild(createTextCell(build.started));
   190          r.appendChild(createTextCell(build.duration));
   191          builds.appendChild(r);
   192      }
   193  }
   194  
   195  function createTextCell(text) {
   196      var c = document.createElement("td");
   197      c.appendChild(document.createTextNode(text));
   198      return c;
   199  }
   200  
   201  function createLinkCell(text, url, title) {
   202      var c = document.createElement("td");
   203      var a = document.createElement("a");
   204      a.href = url;
   205      if (title !== "") {
   206          a.title = title;
   207      }
   208      a.appendChild(document.createTextNode(text));
   209      c.appendChild(a);
   210      return c;
   211  }
   212  
   213  function createRerunCell(modal, rerun_command, prowjob) {
   214      var url = "https://" + window.location.hostname + "/rerun?prowjob=" + prowjob;
   215      var c = document.createElement("td");
   216      var a = document.createElement("a");
   217      a.href = "#";
   218      a.title = "Show instructions for rerunning this job.";
   219      a.onclick = function() {
   220          modal.style.display = "block";
   221          rerun_command.innerHTML = "kubectl create -f \"<a href='" + url + "'>" + url + "</a>\"";
   222      };
   223      a.appendChild(document.createTextNode("\u27F3"));
   224      c.appendChild(a);
   225      return c;
   226  }
   227  
   228  function stateCell(state) {
   229      var c = document.createElement("td");
   230      c.className = state;
   231      if (state === "triggered" || state === "pending") {
   232          c.appendChild(document.createTextNode("\u2022"));
   233      } else if (state === "success") {
   234          c.appendChild(document.createTextNode("\u2713"));
   235      } else if (state === "failure" || state === "error" || state === "aborted") {
   236          c.appendChild(document.createTextNode("\u2717"));
   237      }
   238      return c;
   239  }
   240  
   241  function batchRevisionCell(build) {
   242      var c = document.createElement("td");
   243      var pr_refs = build.refs.split(",");
   244      for (var i = 1; i < pr_refs.length; i++) {
   245          if (i != 1) c.appendChild(document.createTextNode(", "));
   246          var pr = pr_refs[i].split(":")[0];
   247          var l = document.createElement("a");
   248          l.href = "https://github.com/" + build.repo + "/pull/" + pr;
   249          l.text = pr;
   250          c.appendChild(document.createTextNode("#"));
   251          c.appendChild(l);
   252      }
   253      return c;
   254  }
   255  
   256  function pushRevisionCell(build) {
   257      var c = document.createElement("td");
   258      var bl = document.createElement("a");
   259      bl.href = "https://github.com/" + build.repo + "/commit/" + build.base_sha;
   260      bl.text = build.base_ref + " (" + build.base_sha.slice(0, 7) + ")";
   261      c.appendChild(bl);
   262      return c;
   263  }
   264  
   265  function prRevisionCell(build) {
   266      var c = document.createElement("td");
   267      c.appendChild(document.createTextNode("#"));
   268      var pl = document.createElement("a");
   269      pl.href = "https://github.com/" + build.repo + "/pull/" + build.number;
   270      pl.text = build.number;
   271      c.appendChild(pl);
   272      c.appendChild(document.createTextNode(" ("));
   273      var cl = document.createElement("a");
   274      cl.href = "https://github.com/" + build.repo + "/pull/" + build.number + '/commits/' + build.pull_sha;
   275      cl.text = build.pull_sha.slice(0, 7);
   276      c.appendChild(cl);
   277      c.appendChild(document.createTextNode(") by "));
   278      var al = document.createElement("a");
   279      al.href = "https://github.com/" + build.author;
   280      al.text = build.author;
   281      c.appendChild(al);
   282      return c;
   283  }