1
2function mrbSectionInit(ctx){
3  $('#latest-builds').on('click', '.cancel-build-btn', function(e){
4    e.stopImmediatePropagation();
5    e.preventDefault();
6
7    var url = $(this).data('request-url');
8    var buildReqIds = $(this).data('buildrequest-id');
9
10    libtoaster.cancelABuild(url, buildReqIds, function () {
11      window.location.reload();
12    }, null);
13  });
14
15  $('#latest-builds').on('click', '.rebuild-btn', function(e){
16    e.stopImmediatePropagation();
17    e.preventDefault();
18
19    var url = $(this).data('request-url');
20    var target = $(this).data('target');
21
22    libtoaster.startABuild(url, target, function(){
23      window.location.reload();
24    }, null);
25  });
26
27  // cached version of buildData, so we can determine whether a build has
28  // changed since it was last fetched, and update the DOM appropriately
29  var buildData = {};
30
31  // returns the cached version of this build, or {} is there isn't a cached one
32  function getCached(build) {
33    return buildData[build.id] || {};
34  }
35
36  // returns true if a build's state changed to "Succeeded", "Failed"
37  // or "Cancelled" from some other value
38  function buildFinished(build) {
39    var cached = getCached(build);
40    return cached.state &&
41      cached.state !== build.state &&
42      (build.state == 'Succeeded' || build.state == 'Failed' ||
43       build.state == 'Cancelled');
44  }
45
46  // returns true if the state changed
47  function stateChanged(build) {
48    var cached = getCached(build);
49    return (cached.state !== build.state);
50  }
51
52  // returns true if the tasks_complete_percentage changed
53  function tasksProgressChanged(build) {
54    var cached = getCached(build);
55    return (cached.tasks_complete_percentage !== build.tasks_complete_percentage);
56  }
57
58  // returns true if the number of recipes parsed/to parse changed
59  function recipeProgressChanged(build) {
60    var cached = getCached(build);
61    return (cached.recipes_parsed_percentage !== build.recipes_parsed_percentage);
62  }
63
64  // returns true if the number of repos cloned/to clone changed
65  function cloneProgressChanged(build) {
66    var cached = getCached(build);
67    return (cached.repos_cloned_percentage !== build.repos_cloned_percentage);
68  }
69
70  function refreshMostRecentBuilds(){
71    libtoaster.getMostRecentBuilds(
72      libtoaster.ctx.mostRecentBuildsUrl,
73
74      // success callback
75      function (data) {
76        var build;
77        var tmpl;
78        var container;
79        var selector;
80        var colourClass;
81        var elements;
82
83        for (var i = 0; i < data.length; i++) {
84          build = data[i];
85
86          if (buildFinished(build)) {
87            // a build finished: reload the whole page so that the build
88            // shows up in the builds table
89            window.location.reload(true);
90          }
91          else if (stateChanged(build)) {
92            // update the whole template
93            build.warnings_pluralise = (build.warnings !== 1 ? 's' : '');
94            build.errors_pluralise = (build.errors !== 1 ? 's' : '');
95
96            tmpl = $.templates("#build-template");
97
98            html = $(tmpl.render(build));
99
100            selector = '[data-latest-build-result="' + build.id + '"] ' +
101              '[data-role="build-status-container"]';
102            container = $(selector);
103
104            // initialize bootstrap tooltips in the new HTML
105            html.find('span.glyphicon-question-sign').tooltip();
106
107            container.html(html);
108          }
109          else if (cloneProgressChanged(build)) {
110            // update the clone progress text
111            selector = '#repos-cloned-percentage-' + build.id;
112            $(selector).html(build.repos_cloned_percentage);
113            selector = '#repos-cloned-progressitem-' + build.id;
114            $(selector).html('('+build.progress_item+')');
115
116            // update the recipe progress bar
117            selector = '#repos-cloned-percentage-bar-' + build.id;
118            $(selector).width(build.repos_cloned_percentage + '%');
119          }
120          else if (tasksProgressChanged(build)) {
121            // update the task progress text
122            selector = '#build-pc-done-' + build.id;
123            $(selector).html(build.tasks_complete_percentage);
124
125            // update the task progress bar
126            selector = '#build-pc-done-bar-' + build.id;
127            $(selector).width(build.tasks_complete_percentage + '%');
128          }
129          else if (recipeProgressChanged(build)) {
130            // update the recipe progress text
131            selector = '#recipes-parsed-percentage-' + build.id;
132            $(selector).html(build.recipes_parsed_percentage);
133
134            // update the recipe progress bar
135            selector = '#recipes-parsed-percentage-bar-' + build.id;
136            $(selector).width(build.recipes_parsed_percentage + '%');
137          }
138
139          buildData[build.id] = build;
140        }
141      },
142
143      // fail callback
144      function (data) {
145        console.error(data);
146      }
147    );
148  }
149
150  window.setInterval(refreshMostRecentBuilds, 1500);
151  refreshMostRecentBuilds();
152}
153