1/*
2 * Copyright (C) 2011 Apple Inc. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 *    notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 *    notice, this list of conditions and the following disclaimer in the
11 *    documentation and/or other materials provided with the distribution.
12 *
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
14 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
15 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
17 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
23 * THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26function RecentBuildsLoader(didLoadRecentBuildsCallback) {
27    this._didLoadRecentBuildsCallback = didLoadRecentBuildsCallback;
28}
29
30RecentBuildsLoader.prototype = {
31    start: function(builderName, maximumNumberOfBuilds) {
32        var url = this._buildbotBaseURL + "/json/builders/" + builderName + "/builds/?";
33        url += range(maximumNumberOfBuilds).map(function(n) { return "select=-" + (n + 1); }).join("&");
34        var self = this;
35        getResource(url, function(xhr) {
36            var data = JSON.parse(xhr.responseText);
37            var builds = [];
38            Object.keys(data).forEach(function(buildNumber) {
39                var build = data[buildNumber];
40
41                var buildInfo = {
42                    revision: parseInt(build.properties.first(function(property) { return property[0] === "got_revision"; })[1], 10),
43                    leakCount: 0,
44                    url: null,
45                };
46                for (var stepIndex = 0; stepIndex < build.steps.length; ++stepIndex) {
47                    var step = build.steps[stepIndex];
48                    if (step.name === "layout-test") {
49                        if (!("text" in step))
50                            continue;
51                        var strings = step.text;
52                        for (var stringIndex = 0; stringIndex < strings.length; ++stringIndex) {
53                            var match = /^(\d+) total leaks found!$/.exec(strings[stringIndex]);
54                            if (!match)
55                                continue;
56                            buildInfo.leakCount = parseInt(match[1], 10);
57                            break;
58                        }
59                    } else if (step.name === "MasterShellCommand") {
60                        if (!("urls" in step))
61                            return;
62                        if (!("view results" in step.urls))
63                            return;
64                        var url = step.urls["view results"];
65                        buildInfo.url = self._buildbotBaseURL + url.replace(/\/results\.html$/, "") + "/";
66                    }
67
68                    if (buildInfo.leakCount && buildInfo.url) {
69                        builds.push(buildInfo);
70                        break;
71                    }
72                }
73            });
74            // Sort descending by revision.
75            builds.sort(function(a, b) { return b.revision - a.revision; });
76            self._didLoadRecentBuildsCallback(builds);
77        });
78    },
79
80    _buildbotBaseURL: "http://build.webkit.org/",
81};
82