GetChange: populate ChangeInfo.metaRevId with the NoteDb meta ref SHA1
The NoteDb meta SHA1 is the unique identifier the state of a change,
together with the robot-comment SHA1.
Change-Id: I8b4cf63f9e8255ab9c2d91ddf002820f46a288c8
diff --git a/Documentation/rest-api-changes.txt b/Documentation/rest-api-changes.txt
index 096b068..3b9320c 100644
--- a/Documentation/rest-api-changes.txt
+++ b/Documentation/rest-api-changes.txt
@@ -6467,6 +6467,8 @@
Only set if link:#current-revision[the current revision] is requested
(in which case it will only contain a key for the current revision) or
if link:#all-revisions[all revisions] are requested.
+|`meta_rev_id` |optional|
+The SHA1 of the NoteDb meta ref.
|`tracking_ids` |optional|
A list of link:#tracking-id-info[TrackingIdInfo] entities describing
references to external tracking systems. Only set if
diff --git a/java/com/google/gerrit/extensions/common/ChangeInfo.java b/java/com/google/gerrit/extensions/common/ChangeInfo.java
index 7ed2f95..528efe3 100644
--- a/java/com/google/gerrit/extensions/common/ChangeInfo.java
+++ b/java/com/google/gerrit/extensions/common/ChangeInfo.java
@@ -70,6 +70,7 @@
public String submissionId;
public Integer cherryPickOfChange;
public Integer cherryPickOfPatchSet;
+ public String metaRevId;
/**
* Whether the change contains conflicts.
diff --git a/java/com/google/gerrit/server/change/ChangeJson.java b/java/com/google/gerrit/server/change/ChangeJson.java
index f292245..a461b59 100644
--- a/java/com/google/gerrit/server/change/ChangeJson.java
+++ b/java/com/google/gerrit/server/change/ChangeJson.java
@@ -54,6 +54,7 @@
import com.google.gerrit.entities.PatchSet;
import com.google.gerrit.entities.PatchSetApproval;
import com.google.gerrit.entities.Project;
+import com.google.gerrit.entities.RefNames;
import com.google.gerrit.entities.SubmitRecord;
import com.google.gerrit.entities.SubmitRecord.Status;
import com.google.gerrit.entities.SubmitRequirement;
@@ -75,6 +76,7 @@
import com.google.gerrit.extensions.common.SubmitRequirementInfo;
import com.google.gerrit.extensions.common.TrackingIdInfo;
import com.google.gerrit.extensions.restapi.Url;
+import com.google.gerrit.index.RefState;
import com.google.gerrit.index.query.QueryResult;
import com.google.gerrit.metrics.Description;
import com.google.gerrit.metrics.Description.Units;
@@ -570,6 +572,15 @@
out.totalCommentCount = cd.totalCommentCount();
out.unresolvedCommentCount = cd.unresolvedCommentCount();
+ if (cd.getRefStates() != null) {
+ String metaName = RefNames.changeMetaRef(cd.getId());
+ Optional<RefState> metaState =
+ cd.getRefStates().values().stream().filter(r -> r.ref().equals(metaName)).findAny();
+
+ // metaState should always be there, but it doesn't hurt to be extra careful.
+ metaState.ifPresent(rs -> out.metaRevId = rs.id().getName());
+ }
+
if (user.isIdentifiedUser()) {
Collection<String> stars = cd.stars(user.getAccountId());
out.starred = stars.contains(StarredChangesUtil.DEFAULT_LABEL) ? true : null;
diff --git a/javatests/com/google/gerrit/acceptance/rest/change/ChangeMetaIT.java b/javatests/com/google/gerrit/acceptance/rest/change/ChangeMetaIT.java
new file mode 100644
index 0000000..3787a35
--- /dev/null
+++ b/javatests/com/google/gerrit/acceptance/rest/change/ChangeMetaIT.java
@@ -0,0 +1,58 @@
+// Copyright (C) 2017 The Android Open Source Project
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+package com.google.gerrit.acceptance.rest.change;
+
+import static com.google.common.truth.Truth.assertThat;
+import static com.google.gerrit.entities.RefNames.changeMetaRef;
+
+import com.google.common.collect.Iterables;
+import com.google.gerrit.acceptance.AbstractDaemonTest;
+import com.google.gerrit.acceptance.PushOneCommit;
+import com.google.gerrit.entities.Change;
+import com.google.gerrit.extensions.common.ChangeInfo;
+import org.eclipse.jgit.lib.Repository;
+import org.junit.Test;
+
+/** Test handling of the NoteDb commit hash in the GetChange endpoint */
+public class ChangeMetaIT extends AbstractDaemonTest {
+ @Test
+ public void metaSha1_fromIndex() throws Exception {
+ PushOneCommit.Result result = createChange();
+ String changeId = result.getChangeId();
+
+ try (AutoCloseable ignored = disableNoteDb()) {
+ ChangeInfo change =
+ Iterables.getOnlyElement(gApi.changes().query().withQuery("change:" + changeId).get());
+
+ try (Repository repo = repoManager.openRepository(project)) {
+ assertThat(change.metaRevId)
+ .isEqualTo(
+ repo.exactRef(changeMetaRef(Change.id(change._number))).getObjectId().getName());
+ }
+ }
+ }
+
+ @Test
+ public void metaSha1_fromNoteDb() throws Exception {
+ PushOneCommit.Result result = createChange();
+ String changeId = result.getChangeId();
+ ChangeInfo before = gApi.changes().id(changeId).get();
+ try (Repository repo = repoManager.openRepository(project)) {
+ assertThat(before.metaRevId)
+ .isEqualTo(
+ repo.exactRef(changeMetaRef(Change.id(before._number))).getObjectId().getName());
+ }
+ }
+}