Polyfill parent-indexed change file API
When loading image diffs as API support for parent-indexed change files rolls out, request the fast version first and fall back to the existing, slower version if that fails. Bug: Issue 5751 Change-Id: I1d3916e2fdfda66a7925825c6b3fbfbf178b4c36
This commit is contained in:
@@ -331,7 +331,10 @@ limitations under the License.
|
|||||||
function() { return Promise.resolve(mockFile1); }));
|
function() { return Promise.resolve(mockFile1); }));
|
||||||
stubs.push(sandbox.stub(element.$.restAPI,
|
stubs.push(sandbox.stub(element.$.restAPI,
|
||||||
'getChangeFileContents',
|
'getChangeFileContents',
|
||||||
function() { return Promise.resolve(mockFile2); }));
|
function(changeId, patchNum, path, opt_parentIndex) {
|
||||||
|
return Promise.resolve(opt_parentIndex === 1 ? mockFile1 :
|
||||||
|
mockFile2);
|
||||||
|
}));
|
||||||
stubs.push(sandbox.stub(element.$.restAPI, '_getDiffComments',
|
stubs.push(sandbox.stub(element.$.restAPI, '_getDiffComments',
|
||||||
function() { return Promise.resolve(mockComments); }));
|
function() { return Promise.resolve(mockComments); }));
|
||||||
stubs.push(sandbox.stub(element.$.restAPI, 'getDiffDrafts',
|
stubs.push(sandbox.stub(element.$.restAPI, 'getDiffDrafts',
|
||||||
|
@@ -963,8 +963,9 @@
|
|||||||
},
|
},
|
||||||
|
|
||||||
_fetchB64File: function(url) {
|
_fetchB64File: function(url) {
|
||||||
return fetch(this.getBaseUrl() + url, {credentials: 'same-origin'}).then(
|
return fetch(this.getBaseUrl() + url, {credentials: 'same-origin'})
|
||||||
function(response) {
|
.then(function(response) {
|
||||||
|
if (!response.ok) { return Promise.reject(response.statusText); }
|
||||||
var type = response.headers.get('X-FYI-Content-Type');
|
var type = response.headers.get('X-FYI-Content-Type');
|
||||||
return response.text()
|
return response.text()
|
||||||
.then(function(text) {
|
.then(function(text) {
|
||||||
@@ -973,12 +974,14 @@
|
|||||||
});
|
});
|
||||||
},
|
},
|
||||||
|
|
||||||
getChangeFileContents: function(changeId, patchNum, path) {
|
getChangeFileContents: function(changeId, patchNum, path, opt_parentIndex) {
|
||||||
|
var parent = typeof opt_parentIndex === 'number' ?
|
||||||
|
'?parent=' + opt_parentIndex : '';
|
||||||
return this._fetchB64File(
|
return this._fetchB64File(
|
||||||
'/changes/' + encodeURIComponent(changeId) +
|
'/changes/' + encodeURIComponent(changeId) +
|
||||||
'/revisions/' + encodeURIComponent(patchNum) +
|
'/revisions/' + encodeURIComponent(patchNum) +
|
||||||
'/files/' + encodeURIComponent(path) +
|
'/files/' + encodeURIComponent(path) +
|
||||||
'/content');
|
'/content' + parent);
|
||||||
},
|
},
|
||||||
|
|
||||||
getCommitFileContents: function(projectName, commit, path) {
|
getCommitFileContents: function(projectName, commit, path) {
|
||||||
@@ -995,16 +998,17 @@
|
|||||||
|
|
||||||
if (diff.meta_a && diff.meta_a.content_type.indexOf('image/') === 0) {
|
if (diff.meta_a && diff.meta_a.content_type.indexOf('image/') === 0) {
|
||||||
if (patchRange.basePatchNum === 'PARENT') {
|
if (patchRange.basePatchNum === 'PARENT') {
|
||||||
// Need the commit info know the parent SHA.
|
// Note: we only attempt to get the image from the first parent.
|
||||||
promiseA = this.getCommitInfo(project, commit).then(function(info) {
|
promiseA = this.getChangeFileContents(changeNum, patchRange.patchNum,
|
||||||
if (info.parents.length !== 1) {
|
diff.meta_a.name, 1)
|
||||||
return Promise.reject('Change commit has multiple parents.');
|
.catch(function(result) {
|
||||||
}
|
// If getting the parent-indexed version of the image fails, it
|
||||||
var parent = info.parents[0].commit;
|
// may be because the API has not been rolled out. Fall back to
|
||||||
return this.getCommitFileContents(project, parent,
|
// getting the file from the commit using the slow API.
|
||||||
|
// NOTE(wyatta): Remove this when the rollout is complete.
|
||||||
|
return this._getImageFromCommit(project, commit,
|
||||||
diff.meta_a.name);
|
diff.meta_a.name);
|
||||||
}.bind(this));
|
}.bind(this));
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
promiseA = this.getChangeFileContents(changeNum,
|
promiseA = this.getChangeFileContents(changeNum,
|
||||||
patchRange.basePatchNum, diff.meta_a.name);
|
patchRange.basePatchNum, diff.meta_a.name);
|
||||||
@@ -1039,6 +1043,19 @@
|
|||||||
}.bind(this));
|
}.bind(this));
|
||||||
},
|
},
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Remove when parent-indexed file requests are completely rolled out.
|
||||||
|
*/
|
||||||
|
_getImageFromCommit: function(project, commit, path) {
|
||||||
|
return this.getCommitInfo(project, commit).then(function(info) {
|
||||||
|
if (info.parents.length !== 1) {
|
||||||
|
return Promise.reject('Change commit has multiple parents.');
|
||||||
|
}
|
||||||
|
var parent = info.parents[0].commit;
|
||||||
|
return this.getCommitFileContents(project, parent, path);
|
||||||
|
}.bind(this));
|
||||||
|
},
|
||||||
|
|
||||||
setChangeTopic: function(changeNum, topic) {
|
setChangeTopic: function(changeNum, topic) {
|
||||||
return this.send('PUT', '/changes/' + encodeURIComponent(changeNum) +
|
return this.send('PUT', '/changes/' + encodeURIComponent(changeNum) +
|
||||||
'/topic', {topic: topic});
|
'/topic', {topic: topic});
|
||||||
|
Reference in New Issue
Block a user