blob: 8807917f478b545f9802921a214563ce3b290466 [file] [log] [blame]
<!--
Copyright (C) 2016 The Android Open Source Project
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<script>
(function(window) {
'use strict';
/** @polymerBehavior Gerrit.PatchSetBehavior */
var PatchSetBehavior = {
/**
* Given an object of revisions, get a particular revision based on patch
* num.
*
* @param {Object} revisions The object of revisions given by the API
* @param {number|string} patchNum The number index of the revision
* @return {Object} The correspondent revision obj from {revisions}
*/
getRevisionByPatchNum: function(revisions, patchNum) {
patchNum = parseInt(patchNum, 10);
for (var rev in revisions) {
if (revisions.hasOwnProperty(rev) &&
revisions[rev]._number === patchNum) {
return revisions[rev];
}
}
},
computeAllPatchSets: function(change) {
var patchNums = [];
for (var commit in change.revisions) {
if (change.revisions.hasOwnProperty(commit)) {
patchNums.push({
num: change.revisions[commit]._number,
desc: change.revisions[commit].description,
});
}
}
return patchNums.sort(function(a, b) { return a.num - b.num; });
},
computeLatestPatchNum: function(allPatchSets) {
return allPatchSets[allPatchSets.length - 1].num;
},
/**
* Check whether there is no newer patch than the latest patch that was
* available when this change was loaded.
* @return {Promise} A promise that yields true if the latest patch has been
* loaded, and false if a newer patch has been uploaded in the meantime.
*/
fetchIsLatestKnown: function(change, restAPI) {
var knownLatest = this.computeLatestPatchNum(
this.computeAllPatchSets(change));
return restAPI.getChangeDetail(change._number)
.then(function(detail) {
var actualLatest = this.computeLatestPatchNum(
this.computeAllPatchSets(detail));
return actualLatest <= knownLatest;
}.bind(this));
},
};
window.Gerrit = window.Gerrit || {};
window.Gerrit.PatchSetBehavior = PatchSetBehavior;
})(window);
</script>