1<!--
2Copyright 2014 The Chromium Authors. All rights reserved.
3Use of this source code is governed by a BSD-style license that can be
4found in the LICENSE file.
5-->
6
7<link rel="import" href="/lib/net.html">
8<link rel='import' href='ct-commit.html'>
9<link rel='import' href='ct-repositories.html'>
10
11<script>
12function CTCommitLog() {
13  this.commits = {};
14  this.lastRevision = {};
15  this._repositories = new CTRepositories();
16
17  this._repositories.names.forEach(function(name) {
18    this.commits[name] = {};
19  }.bind(this));
20}
21
22CTCommitLog.prototype.update = function() {
23  var requests = [];
24  Object.keys(this._repositories.repositories, (function(name, repository) {
25    var responseHandler = this._handleResponse.bind(this, repository.repositoryUrl, repository.name);
26    // FIXME: Turn net.js into net.html and import it at the top of this file.
27    requests.push(net.ajax({url: repository.dataUrl}).then(responseHandler));
28  }.bind(this)));
29  return Promise.all(requests);
30}
31
32CTCommitLog.prototype._handleResponse = function(repositoryUrl, repository, json) {
33  // FIXME: Fix googlesource to not do this outdated JSON XSS mitigation.
34  json = json.substring(')]}\n'.length);
35  JSON.parse(json).log.forEach(function(entry) {
36    var author = entry.author.email;
37    var message = entry.message;
38    var revision = CTCommit.findRevision(message);
39
40    // Check for existing commit for that revision, and possibly complete it.
41    // We update the existing commit instead of creating a new one so that the
42    // data-binding properly updates all the <ct-commit>'s.
43    var existingCommit = this.commits[repository][revision];
44    if (existingCommit && !existingCommit.isComplete) {
45      existingCommit.complete(author, message, revision);
46    } else {
47      var commit = CTCommit.create(author, message, repositoryUrl, repository);
48      this.commits[repository][commit.revision] = commit;
49    }
50  }.bind(this));
51
52  this._updateLastRevision(repository);
53}
54
55CTCommitLog.prototype._updateLastRevision = function(repository) {
56  var sortedCommits = Object.keys(this.commits[repository]).sort();
57  this.lastRevision[repository] = sortedCommits.last();
58}
59
60CTCommitLog.prototype.range = function(repository, revisions) {
61  var commits = [];
62  revisions.forEach(function(revision) {
63    var commit = this.commits[repository][revision];
64    if (!commit) {
65      var url = this._repositories.repositories[repository].repositoryUrl;
66      var commit = CTCommit.createIncomplete(url, revision, repository);
67      this.commits[repository][revision] = commit;
68    }
69
70    if (commit)
71      commits.push(commit);
72  }.bind(this));
73  return commits;
74}
75</script>
76