Return project in GetRelated

This commit adds project as a mandatory return field in
RelatedChangeAndCommitInfo returned by GetRelated to fix a bug where
links to related changes would have 'undefined' as the project component.

Since we now always want to have the project along the change number it
seems to be a good practice to also return the project along with a
change number in the API similar to ChangeInfo.

Bug: Issue 6876
Change-Id: I64ac0d1b30a3b8034cf16afe96c9d1168cbde193
This commit is contained in:
Patrick Hiesel
2017-07-28 10:25:42 +02:00
parent ed403f9be2
commit cab63519a6
3 changed files with 14 additions and 3 deletions

View File

@@ -3548,6 +3548,7 @@ describing the related changes.
{ {
"changes": [ "changes": [
{ {
"project": "gerrit",
"change_id": "Ic62ae3103fca2214904dbf2faf4c861b5f0ae9b5", "change_id": "Ic62ae3103fca2214904dbf2faf4c861b5f0ae9b5",
"commit": { "commit": {
"commit": "78847477532e386f5a2185a4e8c90b2509e354e3", "commit": "78847477532e386f5a2185a4e8c90b2509e354e3",
@@ -3570,6 +3571,7 @@ describing the related changes.
"status": "NEW" "status": "NEW"
}, },
{ {
"project": "gerrit",
"change_id": "I5e4fc08ce34d33c090c9e0bf320de1b17309f774", "change_id": "I5e4fc08ce34d33c090c9e0bf320de1b17309f774",
"commit": { "commit": {
"commit": "b1cb4caa6be46d12b94c25aa68aebabcbb3f53fe", "commit": "b1cb4caa6be46d12b94c25aa68aebabcbb3f53fe",
@@ -6577,6 +6579,7 @@ a related change and commit.
[options="header",cols="1,^1,5"] [options="header",cols="1,^1,5"]
|=========================== |===========================
|Field Name ||Description |Field Name ||Description
|`project` ||The project of the change or commit.
|`change_id` |optional|The Change-Id of the change. |`change_id` |optional|The Change-Id of the change.
|`commit` ||The commit as a |`commit` ||The commit as a
link:#commit-info[CommitInfo] entity. link:#commit-info[CommitInfo] entity.

View File

@@ -563,9 +563,10 @@ public class GetRelatedIT extends AbstractDaemonTest {
return Iterables.getOnlyElement(queryProvider.get().byCommit(c)); return Iterables.getOnlyElement(queryProvider.get().byCommit(c));
} }
private static ChangeAndCommit changeAndCommit( private ChangeAndCommit changeAndCommit(
PatchSet.Id psId, ObjectId commitId, int currentRevisionNum) { PatchSet.Id psId, ObjectId commitId, int currentRevisionNum) {
ChangeAndCommit result = new ChangeAndCommit(); ChangeAndCommit result = new ChangeAndCommit();
result.project = project.get();
result._changeNumber = psId.getParentKey().get(); result._changeNumber = psId.getParentKey().get();
result.commit = new CommitInfo(); result.commit = new CommitInfo();
result.commit.commit = commitId.name(); result.commit.commit = commitId.name();
@@ -599,6 +600,7 @@ public class GetRelatedIT extends AbstractDaemonTest {
String name = "index " + i + " related to " + psId; String name = "index " + i + " related to " + psId;
ChangeAndCommit a = actual.get(i); ChangeAndCommit a = actual.get(i);
ChangeAndCommit e = expected[i]; ChangeAndCommit e = expected[i];
assertThat(a.project).named("project of " + name).isEqualTo(e.project);
assertThat(a._changeNumber).named("change ID of " + name).isEqualTo(e._changeNumber); assertThat(a._changeNumber).named("change ID of " + name).isEqualTo(e._changeNumber);
// Don't bother checking changeId; assume _changeNumber is sufficient. // Don't bother checking changeId; assume _changeNumber is sufficient.
assertThat(a._revisionNumber).named("revision of " + name).isEqualTo(e._revisionNumber); assertThat(a._revisionNumber).named("revision of " + name).isEqualTo(e._revisionNumber);

View File

@@ -21,6 +21,7 @@ import com.google.gerrit.extensions.common.CommitInfo;
import com.google.gerrit.extensions.restapi.RestReadView; import com.google.gerrit.extensions.restapi.RestReadView;
import com.google.gerrit.reviewdb.client.Change; import com.google.gerrit.reviewdb.client.Change;
import com.google.gerrit.reviewdb.client.PatchSet; import com.google.gerrit.reviewdb.client.PatchSet;
import com.google.gerrit.reviewdb.client.Project;
import com.google.gerrit.reviewdb.server.ReviewDb; import com.google.gerrit.reviewdb.server.ReviewDb;
import com.google.gerrit.server.CommonConverters; import com.google.gerrit.server.CommonConverters;
import com.google.gerrit.server.PatchSetUtil; import com.google.gerrit.server.PatchSetUtil;
@@ -102,7 +103,7 @@ public class GetRelated implements RestReadView<RevisionResource> {
} else { } else {
commit = d.commit(); commit = d.commit();
} }
result.add(new ChangeAndCommit(d.data().change(), ps, commit)); result.add(new ChangeAndCommit(rsrc.getProject(), d.data().change(), ps, commit));
} }
if (result.size() == 1) { if (result.size() == 1) {
@@ -137,6 +138,7 @@ public class GetRelated implements RestReadView<RevisionResource> {
} }
public static class ChangeAndCommit { public static class ChangeAndCommit {
public String project;
public String changeId; public String changeId;
public CommitInfo commit; public CommitInfo commit;
public Integer _changeNumber; public Integer _changeNumber;
@@ -146,7 +148,10 @@ public class GetRelated implements RestReadView<RevisionResource> {
public ChangeAndCommit() {} public ChangeAndCommit() {}
ChangeAndCommit(@Nullable Change change, @Nullable PatchSet ps, RevCommit c) { ChangeAndCommit(
Project.NameKey project, @Nullable Change change, @Nullable PatchSet ps, RevCommit c) {
this.project = project.get();
if (change != null) { if (change != null) {
changeId = change.getKey().get(); changeId = change.getKey().get();
_changeNumber = change.getChangeId(); _changeNumber = change.getChangeId();
@@ -171,6 +176,7 @@ public class GetRelated implements RestReadView<RevisionResource> {
@Override @Override
public String toString() { public String toString() {
return MoreObjects.toStringHelper(this) return MoreObjects.toStringHelper(this)
.add("project", project)
.add("changeId", changeId) .add("changeId", changeId)
.add("commit", toString(commit)) .add("commit", toString(commit))
.add("_changeNumber", _changeNumber) .add("_changeNumber", _changeNumber)