initial import
[vuplus_webkit] / Tools / BuildSlaveSupport / build.webkit.org-config / public_html / TestFailures / scripts / Trac.js
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
26 var trac = trac || {};
27
28 (function() {
29
30 function findUsingRegExp(string, regexp)
31 {
32     var match = regexp.exec(string);
33     if (match)
34         return match[1];
35     return null;
36 }
37
38 function findReviewer(message)
39 {
40     var regexp = /Reviewed by ([^.]+)/;
41     return findUsingRegExp(message, regexp);
42 }
43
44 function findAuthor(message)
45 {
46     var regexp = /Patch by ([^<]+) </;
47     return findUsingRegExp(message, regexp);
48 }
49
50 function findBugID(message)
51 {
52     var regexp = /\/show_bug.cgi\?id=(\d+)/;
53     return parseInt(findUsingRegExp(message, regexp), 10);
54 }
55
56 function findRevision(title)
57 {
58     var regexp = /^Revision (\d+):/;
59     return parseInt(findUsingRegExp(title, regexp), 10);
60 }
61
62 function findSummary(message)
63 {
64     var lines = message.split('\n');
65     for (var i = 0; i < lines.length; ++i) {
66         var line = lines[i]
67         if (findBugID(line))
68             continue;
69         if (findReviewer(line))
70             continue;
71         // Old-style commit message.
72         if (/^\d\d\d\d-\d\d-\d\d/.exec(line))
73             continue;
74         if (line.length > 0)
75             return line;
76     }
77 }
78
79 function findRevert(message)
80 {
81     var regexp = /rolling out r(\d+)\./;
82     var revision = findUsingRegExp(message, regexp);
83     if (!revision)
84         return undefined;
85     return parseInt(revision, 10);
86 }
87
88 // FIXME: Consider exposing this method for unit testing.
89 function parseCommitData(responseXML)
90 {
91     var commits = Array.prototype.map.call(responseXML.getElementsByTagName('item'), function(item) {
92         var title = item.getElementsByTagName('title')[0].textContent;
93         var author = item.getElementsByTagName('author')[0].textContent;
94         var time = item.getElementsByTagName('pubDate')[0].textContent;
95
96         // FIXME: This isn't a very high-fidelity reproduction of the commit message,
97         // but it's good enough for our purposes.
98         var container = document.createElement('div');
99         container.innerHTML = item.getElementsByTagName('description')[0].textContent;
100         var message = container.innerText;
101
102         return {
103             'revision': findRevision(title),
104             'title': title,
105             'time': time,
106             'summary': findSummary(message),
107             'author': findAuthor(message) || author,
108             'reviewer': findReviewer(message),
109             'bugID': findBugID(message),
110             'message': message,
111             'revertedRevision': findRevert(message),
112         };
113     });
114
115     return commits;
116 }
117
118 var g_cache = new base.AsynchronousCache(function(key, callback) {
119     var explodedKey = key.split('\n');
120
121     var path = explodedKey[0];
122     var startRevision = explodedKey[1];
123     var endRevision = explodedKey[2];
124
125     var url = trac.logURL(path, startRevision, endRevision, true, true);
126
127     net.get(url, function(commitData) {
128         callback(parseCommitData(commitData));
129     });
130 });
131
132 trac.changesetURL = function(revision)
133 {
134     return config.kTracURL + '/changeset/' + revision;
135 };
136
137 trac.logURL = function(path, startRevision, endRevision, showFullCommitLogs, formatAsRSS)
138 {
139     var queryParameters = {
140         rev: endRevision,
141         stop_rev: startRevision,
142         // Trac requires limit to be 1 more than the number of revisions we actually want to show.
143         // See <http://trac.edgewall.org/ticket/10317>.
144         limit: endRevision - startRevision + 2,
145     };
146
147     if (showFullCommitLogs)
148         queryParameters.verbose = 'on';
149     if (formatAsRSS)
150         queryParameters.format = 'rss';
151
152     return config.kTracURL + '/log/' + path + '?' + $.param(queryParameters);
153 };
154
155 trac.recentCommitData = function(path, limit, callback)
156 {
157     var url = config.kTracURL + '/log/' + path + '?' + $.param({
158         'verbose': 'on',
159         'format': 'rss',
160         'limit': limit,
161     });
162
163     net.get(url, function(commitData) {
164         callback(parseCommitData(commitData));
165     });
166 };
167
168 trac.commitDataForRevisionRange = function(path, startRevision, endRevision, callback)
169 {
170     var key = [path, startRevision, endRevision].join('\n');
171     g_cache.get(key, callback);
172 };
173
174 })();