Home | History | Annotate | Download | only in scripts
      1 /*
      2  * Copyright (C) 2011 Apple Inc. All rights reserved.
      3  * Copyright (C) 2012 Google Inc. All rights reserved.
      4  *
      5  * Redistribution and use in source and binary forms, with or without
      6  * modification, are permitted provided that the following conditions
      7  * are met:
      8  * 1. Redistributions of source code must retain the above copyright
      9  *    notice, this list of conditions and the following disclaimer.
     10  * 2. Redistributions in binary form must reproduce the above copyright
     11  *    notice, this list of conditions and the following disclaimer in the
     12  *    documentation and/or other materials provided with the distribution.
     13  *
     14  * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
     15  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
     16  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     17  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
     18  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     19  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     20  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     21  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     22  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     23  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
     24  * THE POSSIBILITY OF SUCH DAMAGE.
     25  */
     26 
     27 var trac = trac || {};
     28 
     29 (function() {
     30 
     31 function findUsingRegExp(string, regexp)
     32 {
     33     var match = regexp.exec(string);
     34     if (match)
     35         return match[1];
     36     return null;
     37 }
     38 
     39 function findReviewer(message)
     40 {
     41     var regexp = /(?:^|\n)\s*(?:TB)?R=(.+)/;
     42     var reviewers = findUsingRegExp(message, regexp);
     43     if (!reviewers)
     44         return null;
     45     return reviewers.replace(/\s*,\s*/g, ', ');
     46 }
     47 
     48 function findBugID(message)
     49 {
     50     var regexp = /(?:^|\n)\s*BUG=(.+)/;
     51     var value = findUsingRegExp(message, regexp);
     52     if (!value)
     53         return null;
     54     var result = value.split(/\s*,\s*/).map(function(id) {
     55         var parsedID = parseInt(id.replace(/[^\d]/g, ''), 10);
     56         return isNaN(parsedID) ? 0 : parsedID;
     57     }).filter(function(id) {
     58         return !!id;
     59     });
     60     return result.length ? result : null;
     61 }
     62 
     63 function findRevision(message)
     64 {
     65     var regexp = /git-svn-id: svn:\/\/svn.chromium.org\/blink\/trunk@(\d+)/;
     66     var svnRevision = parseInt(findUsingRegExp(message, regexp), 10);
     67     return isNaN(svnRevision) ? null : svnRevision;
     68 }
     69 
     70 function parseCommitMessage(message) {
     71     var lines = message.split('\n');
     72     var title = '';
     73     lines.some(function(line) {
     74         if (line) {
     75             title = line;
     76             return true;
     77         }
     78     });
     79     var summary = lines.join('\n').trim();
     80     return {
     81         title: title,
     82         summary: summary,
     83         bugID: findBugID(summary),
     84         reviewer: findReviewer(summary),
     85         revision: findRevision(summary),
     86     };
     87 }
     88 
     89 // FIXME: Consider exposing this method for unit testing.
     90 function parseCommitData(responseXML)
     91 {
     92     var commits = Array.prototype.map.call(responseXML.getElementsByTagName('entry'), function(logentry) {
     93         var author = $.trim(logentry.getElementsByTagName('author')[0].textContent);
     94         var time = logentry.getElementsByTagName('published')[0].textContent;
     95         var titleElement = logentry.getElementsByTagName('title')[0];
     96         var title = titleElement ? titleElement.textContent : null;
     97 
     98         // FIXME: This isn't a very high-fidelity reproduction of the commit message,
     99         // but it's good enough for our purposes.
    100         var message = parseCommitMessage(logentry.getElementsByTagName('content')[0].textContent);
    101 
    102         return {
    103             'revision': message.revision,
    104             'title': title || message.title,
    105             'time': time,
    106             'summary': message.title,
    107             'author': author,
    108             'reviewer': message.reviewer,
    109             'bugID': message.bugID,
    110             'message': message.summary,
    111             'revertedRevision': undefined,
    112         };
    113     });
    114     return commits;
    115 }
    116 
    117 trac.changesetURL = function(revision)
    118 {
    119     var queryParameters = {
    120         view: 'rev',
    121         revision: revision,
    122     };
    123     return config.kBlinkRevisionURL + '?' + $.param(queryParameters);
    124 };
    125 
    126 trac.recentCommitData = function(path, limit)
    127 {
    128     return net.xml('http://blink.lc/blink/atom').then(function(commitData) {
    129         return parseCommitData(commitData);
    130     });
    131 };
    132 
    133 })();
    134