Fix some corner cases with the download dropdown

Corner cases fixed:

- Fixes support for File addition.
- Fixes left side content URL when diffing against
  non base revision, e.g. 2..3.
- Fixes support for File Deletion.
- Fixes support for File Renames.

Bug: Issue 11682
Change-Id: I8a91a8bfafad89fce7103a30a4fba91b4fdcdf99
This commit is contained in:
Paladox none
2019-10-06 13:33:54 +00:00
parent 992852bb1f
commit b55da6eb23
6 changed files with 122 additions and 29 deletions

View File

@@ -1409,7 +1409,7 @@ limitations under the License.
theme: 'DEFAULT',
ignore_whitespace: 'IGNORE_NONE',
};
diff._diff = mock.diffResponse;
diff.diff = mock.diffResponse;
};
const renderAndGetNewDiffs = function(index) {

View File

@@ -47,7 +47,7 @@ limitations under the License.
base-image="[[_baseImage]]"
revision-image=[[_revisionImage]]
blame="[[_blame]]"
diff="[[_diff]]"></gr-diff>
diff="[[diff]]"></gr-diff>
<gr-rest-api-interface id="restAPI"></gr-rest-api-interface>
<gr-reporting id="reporting" category="diff"></gr-reporting>
</template>

View File

@@ -94,7 +94,7 @@
},
isImageDiff: {
type: Boolean,
computed: '_computeIsImageDiff(_diff)',
computed: '_computeIsImageDiff(diff)',
notify: true,
},
commitRange: Object,
@@ -164,8 +164,13 @@
_baseImage: Object,
/** @type {?Object} */
_revisionImage: Object,
_diff: Object,
/**
* This is a DiffInfo object.
*/
diff: {
type: Object,
notify: true,
},
/** @type {?Object} */
_blame: {
@@ -237,7 +242,7 @@
this.removeEventListener('render', callback);
};
this.addEventListener('render', callback);
this._diff = diff;
this.diff = diff;
});
})
.catch(err => {

View File

@@ -269,7 +269,7 @@ limitations under the License.
<gr-dropdown
link
down-arrow
items="[[_computeDownloadDropdownLinks(_change.project, _changeNum, _patchRange, _path)]]"
items="[[_computeDownloadDropdownLinks(_change.project, _changeNum, _patchRange, _path, _diff)]]"
horizontal-align="left">
<span class="downloadTitle">
Download
@@ -330,6 +330,7 @@ limitations under the License.
class$="[[_computeDiffClass(_panelFloatingDisabled)]]"
is-image-diff="{{_isImageDiff}}"
files-weblinks="{{_filesWeblinks}}"
diff="{{_diff}}"
change-num="[[_changeNum]]"
commit-range="[[_commitRange]]"
patch-range="[[_patchRange]]"

View File

@@ -84,6 +84,10 @@
/** @type {?} */
_changeComments: Object,
_changeNum: String,
/**
* This is a DiffInfo object.
* This is retrieved and owned by a child component.
*/
_diff: Object,
// An array specifically formatted to be used in a gr-dropdown-list
// element for selected a file to view.
@@ -880,33 +884,63 @@
history.replaceState(null, '', url);
},
_computeDownloadDropdownLinks(project, changeNum, patchRange, path) {
_computeDownloadDropdownLinks(
project, changeNum, patchRange, path, diff) {
if (!patchRange || !patchRange.patchNum) { return []; }
return [
const links = [
{
url: this._computeDownloadPatchLink(
project, changeNum, patchRange, path),
name: 'Patch',
},
{
// We pass 1 here to indicate this is parent 1.
url: this._computeDownloadFileLink(
project, changeNum, patchRange, path, 1),
name: 'Left Content',
},
{
// We pass 0 here to indicate this is parent 0.
url: this._computeDownloadFileLink(
project, changeNum, patchRange, path, 0),
name: 'Right Content',
},
];
if (diff && diff.meta_a) {
let leftPath = path;
if (diff.change_type === 'RENAMED') {
leftPath = diff.meta_a.name;
}
links.push(
{
url: this._computeDownloadFileLink(
project, changeNum, patchRange, leftPath, true),
name: 'Left Content',
}
);
}
if (diff && diff.meta_b) {
links.push(
{
url: this._computeDownloadFileLink(
project, changeNum, patchRange, path, false),
name: 'Right Content',
}
);
}
return links;
},
_computeDownloadFileLink(project, changeNum, patchRange, path, parent) {
return this.changeBaseURL(project, changeNum, patchRange.patchNum) +
`/files/${encodeURIComponent(path)}/download?parent=${parent}`;
_computeDownloadFileLink(
project, changeNum, patchRange, path, isBase) {
let patchNum = patchRange.patchNum;
const comparedAgainsParent = patchRange.basePatchNum === 'PARENT';
if (isBase && !comparedAgainsParent) {
patchNum = patchRange.basePatchNum;
}
let url = this.changeBaseURL(project, changeNum, patchNum) +
`/files/${encodeURIComponent(path)}/download`;
if (isBase && comparedAgainsParent) {
url += '?parent=1';
}
return url;
},
_computeDownloadPatchLink(project, changeNum, patchRange, path) {

View File

@@ -1122,26 +1122,79 @@ limitations under the License.
},
{
url: '/changes/test~12/revisions/1' +
'/files/index.php/download?parent=0',
'/files/index.php/download',
name: 'Right Content',
},
];
const side = {
meta_a: true,
meta_b: true,
};
const base = {
patchNum: 1,
basePatchNum: 'PARENT',
};
assert.deepEqual(
element._computeDownloadDropdownLinks(
'test', 12, {patchNum: 1}, 'index.php'),
'test', 12, base, 'index.php', side),
downloadLinks);
});
test('_computeDownloadDropdownLinks diff returns renamed', () => {
const downloadLinks = [
{
url: '/changes/test~12/revisions/3/patch?zip&path=index.php',
name: 'Patch',
},
{
url: '/changes/test~12/revisions/2' +
'/files/index2.php/download',
name: 'Left Content',
},
{
url: '/changes/test~12/revisions/3' +
'/files/index.php/download',
name: 'Right Content',
},
];
const side = {
change_type: 'RENAMED',
meta_a: {
name: 'index2.php',
},
meta_b: true,
};
const base = {
patchNum: 3,
basePatchNum: 2,
};
assert.deepEqual(
element._computeDownloadDropdownLinks(
'test', 12, base, 'index.php', side),
downloadLinks);
});
test('_computeDownloadFileLink', () => {
const base = {
patchNum: 1,
basePatchNum: 'PARENT',
};
assert.equal(
element._computeDownloadFileLink(
'test', 12, {patchNum: 1}, 'index.php', 1),
'test', 12, base, 'index.php', true),
'/changes/test~12/revisions/1/files/index.php/download?parent=1');
assert.equal(
element._computeDownloadFileLink(
'test', 12, {patchNum: 1}, 'index.php', 0),
'/changes/test~12/revisions/1/files/index.php/download?parent=0');
'test', 12, base, 'index.php', false),
'/changes/test~12/revisions/1/files/index.php/download');
});
test('_computeDownloadPatchLink', () => {