Handle edits in GetRelated

GetRelated collects all patch sets of all open changes, but was not
including the edit itself.

Change-Id: Iff6389a11ea1befd998d6b21616661f14e2665e2
This commit is contained in:
Dave Borowitz 2014-09-11 15:52:16 +02:00
parent 59039f507d
commit 9091ccf6f1
3 changed files with 54 additions and 6 deletions

View File

@ -28,7 +28,10 @@ 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.server.change.GetRelated.ChangeAndCommit; import com.google.gerrit.server.change.GetRelated.ChangeAndCommit;
import com.google.gerrit.server.change.GetRelated.RelatedInfo; import com.google.gerrit.server.change.GetRelated.RelatedInfo;
import com.google.gerrit.server.edit.ChangeEditModifier;
import com.google.gerrit.server.edit.ChangeEditUtil;
import com.google.gwtorm.server.OrmException; import com.google.gwtorm.server.OrmException;
import com.google.inject.Inject;
import org.eclipse.jgit.api.ResetCommand.ResetType; import org.eclipse.jgit.api.ResetCommand.ResetType;
import org.eclipse.jgit.api.errors.GitAPIException; import org.eclipse.jgit.api.errors.GitAPIException;
@ -38,6 +41,11 @@ import java.io.IOException;
import java.util.List; import java.util.List;
public class GetRelatedIT extends AbstractDaemonTest { public class GetRelatedIT extends AbstractDaemonTest {
@Inject
private ChangeEditUtil editUtil;
@Inject
private ChangeEditModifier editModifier;
@Test @Test
public void getRelatedNoResult() throws GitAPIException, public void getRelatedNoResult() throws GitAPIException,
@ -139,15 +147,51 @@ public class GetRelatedIT extends AbstractDaemonTest {
} }
} }
@Test
public void getRelatedEdit() throws Exception {
add(git, "a.txt", "1");
Commit c1 = createCommit(git, admin.getIdent(), "subject: 1");
add(git, "b.txt", "2");
Commit c2 = createCommit(git, admin.getIdent(), "subject: 2");
add(git, "b.txt", "3");
Commit c3 = createCommit(git, admin.getIdent(), "subject: 3");
pushHead(git, "refs/for/master", false);
Change ch2 = getChange(c2);
editModifier.createEdit(ch2, getPatchSet(ch2));
String editRev = editUtil.byChange(ch2).get().getRevision().get();
List<ChangeAndCommit> related = getRelated(ch2.getId(), 0);
assertEquals(3, related.size());
assertEquals("related to " + c2.getChangeId(), c3.getChangeId(), related.get(0).changeId);
assertEquals("related to " + c2.getChangeId(), c2.getChangeId(), related.get(1).changeId);
assertEquals("has edit revision number", 0, related.get(1)._revisionNumber.intValue());
assertEquals("has edit revision " + editRev, editRev, related.get(1).commit.commit);
assertEquals("related to " + c2.getChangeId(), c1.getChangeId(), related.get(2).changeId);
}
private List<ChangeAndCommit> getRelated(PatchSet.Id ps) throws IOException { private List<ChangeAndCommit> getRelated(PatchSet.Id ps) throws IOException {
return getRelated(ps.getParentKey(), ps.get());
}
private List<ChangeAndCommit> getRelated(Change.Id changeId, int ps)
throws IOException {
String url = String.format("/changes/%d/revisions/%d/related", String url = String.format("/changes/%d/revisions/%d/related",
ps.getParentKey().get(), ps.get()); changeId.get(), ps);
return newGson().fromJson(adminSession.get(url).getReader(), return newGson().fromJson(adminSession.get(url).getReader(),
RelatedInfo.class).changes; RelatedInfo.class).changes;
} }
private PatchSet.Id getPatchSetId(Commit c) throws OrmException { private PatchSet.Id getPatchSetId(Commit c) throws OrmException {
return getChange(c).currentPatchSetId();
}
private PatchSet getPatchSet(Change c) throws OrmException {
return db.patchSets().get(c.currentPatchSetId());
}
private Change getChange(Commit c) throws OrmException {
return Iterables.getOnlyElement( return Iterables.getOnlyElement(
db.changes().byKey(new Change.Key(c.getChangeId()))).currentPatchSetId(); db.changes().byKey(new Change.Key(c.getChangeId())));
} }
} }

View File

@ -311,7 +311,7 @@ class RelatedChangesTab implements IsWidget {
PatchSet.Id id = info.patch_set_id(); PatchSet.Id id = info.patch_set_id();
return "#" + PageLinks.toChange( return "#" + PageLinks.toChange(
id.getParentKey(), id.getParentKey(),
String.valueOf(id.get())); id.getId());
} }
GitwebLink gw = Gerrit.getGitwebLink(); GitwebLink gw = Gerrit.getGitwebLink();

View File

@ -91,7 +91,7 @@ public class GetRelated implements RestReadView<RevisionResource> {
private List<ChangeAndCommit> walk(RevisionResource rsrc, RevWalk rw, Ref ref) private List<ChangeAndCommit> walk(RevisionResource rsrc, RevWalk rw, Ref ref)
throws OrmException, IOException { throws OrmException, IOException {
Map<Change.Id, Change> changes = allOpenChanges(rsrc); Map<Change.Id, Change> changes = allOpenChanges(rsrc);
Map<PatchSet.Id, PatchSet> patchSets = allPatchSets(changes.keySet()); Map<PatchSet.Id, PatchSet> patchSets = allPatchSets(rsrc, changes.keySet());
Map<String, PatchSet> commits = Maps.newHashMap(); Map<String, PatchSet> commits = Maps.newHashMap();
for (PatchSet p : patchSets.values()) { for (PatchSet p : patchSets.values()) {
@ -143,8 +143,8 @@ public class GetRelated implements RestReadView<RevisionResource> {
db.changes().byBranchOpenAll(rsrc.getChange().getDest())); db.changes().byBranchOpenAll(rsrc.getChange().getDest()));
} }
private Map<PatchSet.Id, PatchSet> allPatchSets(Collection<Change.Id> ids) private Map<PatchSet.Id, PatchSet> allPatchSets(RevisionResource rsrc,
throws OrmException { Collection<Change.Id> ids) throws OrmException {
int n = ids.size(); int n = ids.size();
ReviewDb db = dbProvider.get(); ReviewDb db = dbProvider.get();
List<ResultSet<PatchSet>> t = Lists.newArrayListWithCapacity(n); List<ResultSet<PatchSet>> t = Lists.newArrayListWithCapacity(n);
@ -158,6 +158,10 @@ public class GetRelated implements RestReadView<RevisionResource> {
r.put(p.getId(), p); r.put(p.getId(), p);
} }
} }
if (rsrc.getEdit().isPresent()) {
r.put(rsrc.getPatchSet().getId(), rsrc.getPatchSet());
}
return r; return r;
} }