Add getFileContent to rest API interface

Change edits must be able to get file content from non edit patch sets.
This new function gets the file content and type, regardless of what
patch set is being referenced (edit or otherwise).

This change also uses the new function in gr-editor-view.

Bug: Issue 4437
Change-Id: Id0e395df42aa5c635d126573814cb4fdd61486cb
This commit is contained in:
Kasper Nilsson
2018-01-16 14:05:58 -08:00
parent 8708244450
commit 75bf5b9357
5 changed files with 62 additions and 28 deletions

View File

@@ -413,7 +413,7 @@ limitations under the License.
stubs.push(sandbox.stub(element.$.restAPI, 'getCommitInfo',
() => Promise.resolve(mockCommit)));
stubs.push(sandbox.stub(element.$.restAPI,
'getChangeFileContents',
'getB64FileContents',
(changeId, patchNum, path, opt_parentIndex) => {
return Promise.resolve(opt_parentIndex === 1 ? mockFile1 :
mockFile2);

View File

@@ -125,13 +125,14 @@
},
_getFileData(changeNum, path) {
return this.$.restAPI.getFileInChangeEdit(changeNum, path).then(res => {
if (!res.ok) { return; }
return this.$.restAPI.getFileContent(changeNum, path, this.EDIT_NAME)
.then(res => {
if (!res.ok) { return; }
this._type = res.type || '';
this._newContent = res.content || '';
this._content = res.content || '';
});
this._type = res.type || '';
this._newContent = res.content || '';
this._content = res.content || '';
});
},
_saveEdit() {

View File

@@ -219,7 +219,7 @@ suite('gr-editor-view tests', () => {
});
test('res.ok', () => {
sandbox.stub(element.$.restAPI, 'getFileInChangeEdit')
sandbox.stub(element.$.restAPI, 'getFileContent')
.returns(Promise.resolve({
ok: true,
type: 'text/javascript',
@@ -235,7 +235,7 @@ suite('gr-editor-view tests', () => {
});
test('!res.ok', () => {
sandbox.stub(element.$.restAPI, 'getFileInChangeEdit')
sandbox.stub(element.$.restAPI, 'getFileContent')
.returns(Promise.resolve({}));
// Ensure no data is set with a bad response.
@@ -247,7 +247,7 @@ suite('gr-editor-view tests', () => {
});
test('content is undefined', () => {
sandbox.stub(element.$.restAPI, 'getFileInChangeEdit')
sandbox.stub(element.$.restAPI, 'getFileContent')
.returns(Promise.resolve({
ok: true,
type: 'text/javascript',
@@ -261,7 +261,7 @@ suite('gr-editor-view tests', () => {
});
test('content and type is undefined', () => {
sandbox.stub(element.$.restAPI, 'getFileInChangeEdit')
sandbox.stub(element.$.restAPI, 'getFileContent')
.returns(Promise.resolve({
ok: true,
}));

View File

@@ -1401,25 +1401,51 @@
.then(response => this.getResponseObject(response));
},
/**
* @param {number|string} changeNum
* @param {string} path
* @param {number|string} patchNum
*/
getFileContent(changeNum, path, patchNum) {
const promise = this.patchNumEquals(patchNum, this.EDIT_NAME) ?
this._getFileInChangeEdit(changeNum, path) :
this._getFileInRevision(changeNum, path, patchNum);
return promise.then(res => {
if (!res.ok) { return res; }
// The file type (used for syntax highlighting) is identified in the
// X-FYI-Content-Type header of the response.
const type = res.headers.get('X-FYI-Content-Type');
return this.getResponseObject(res).then(content => {
return {content, type, ok: true};
});
});
},
/**
* Gets a file in a specific change and revision.
* @param {number|string} changeNum
* @param {string} path
* @param {number|string} patchNum
*/
_getFileInRevision(changeNum, path, patchNum) {
const e = `/files/${encodeURIComponent(path)}/content`;
const headers = {Accept: 'application/json'};
return this.getChangeURLAndSend(changeNum, 'GET', patchNum, e, null, null,
null, null, headers);
},
/**
* Gets a file in a change edit.
* @param {number|string} changeNum
* @param {string} path
*/
getFileInChangeEdit(changeNum, path) {
_getFileInChangeEdit(changeNum, path) {
const e = '/edit/' + encodeURIComponent(path);
const headers = {Accept: 'application/json'};
return this.getChangeURLAndSend(changeNum, 'GET', null, e, null, null,
null, null, headers).then(res => {
if (!res.ok) { return res; }
// The file type (used for syntax highlighting) is identified in the
// X-FYI-Content-Type header of the response.
const type = res.headers.get('X-FYI-Content-Type');
return this.getResponseObject(res).then(content => {
return {content, type, ok: true};
});
});
null, null, headers);
},
rebaseChangeEdit(changeNum) {
@@ -1774,7 +1800,7 @@
* @param {string} path
* @param {number=} opt_parentIndex
*/
getChangeFileContents(changeId, patchNum, path, opt_parentIndex) {
getB64FileContents(changeId, patchNum, path, opt_parentIndex) {
const parent = typeof opt_parentIndex === 'number' ?
'?parent=' + opt_parentIndex : '';
return this._changeBaseURL(changeId, patchNum).then(url => {
@@ -1790,10 +1816,10 @@
if (diff.meta_a && diff.meta_a.content_type.startsWith('image/')) {
if (patchRange.basePatchNum === 'PARENT') {
// Note: we only attempt to get the image from the first parent.
promiseA = this.getChangeFileContents(changeNum, patchRange.patchNum,
promiseA = this.getB64FileContents(changeNum, patchRange.patchNum,
diff.meta_a.name, 1);
} else {
promiseA = this.getChangeFileContents(changeNum,
promiseA = this.getB64FileContents(changeNum,
patchRange.basePatchNum, diff.meta_a.name);
}
} else {
@@ -1801,7 +1827,7 @@
}
if (diff.meta_b && diff.meta_b.content_type.startsWith('image/')) {
promiseB = this.getChangeFileContents(changeNum, patchRange.patchNum,
promiseB = this.getB64FileContents(changeNum, patchRange.patchNum,
diff.meta_b.name);
} else {
promiseB = Promise.resolve(null);

View File

@@ -1248,7 +1248,7 @@ limitations under the License.
'/projects/gerrit%2Fproject/dashboards/default%3Amain');
});
test('getFileInChangeEdit', () => {
test('getFileContent', () => {
sandbox.stub(element, 'getChangeURLAndSend')
.returns(Promise.resolve({
ok: 'true',
@@ -1264,10 +1264,17 @@ limitations under the License.
sandbox.stub(element, 'getResponseObject')
.returns(Promise.resolve('new content'));
return element.getFileInChangeEdit('1', 'test/path').then(res => {
const edit = element.getFileContent('1', 'tst/path', 'EDIT').then(res => {
assert.deepEqual(res,
{content: 'new content', type: 'text/java', ok: true});
});
const normal = element.getFileContent('1', 'tst/path', '3').then(res => {
assert.deepEqual(res,
{content: 'new content', type: 'text/java', ok: true});
});
return Promise.all([edit, normal]);
});
});
</script>