diff --git a/Documentation/rest-api-changes.txt b/Documentation/rest-api-changes.txt index 096b068040..3b9320c34e 100644 --- a/Documentation/rest-api-changes.txt +++ b/Documentation/rest-api-changes.txt @@ -6467,6 +6467,8 @@ patch set to a link:#revision-info[RevisionInfo] entity. + 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 7ed2f954e1..528efe3ba0 100644 --- a/java/com/google/gerrit/extensions/common/ChangeInfo.java +++ b/java/com/google/gerrit/extensions/common/ChangeInfo.java @@ -70,6 +70,7 @@ public class ChangeInfo { 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 f292245132..a461b59b51 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.ChangeMessage; 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.RevisionInfo; 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 @@ public class ChangeJson { out.totalCommentCount = cd.totalCommentCount(); out.unresolvedCommentCount = cd.unresolvedCommentCount(); + if (cd.getRefStates() != null) { + String metaName = RefNames.changeMetaRef(cd.getId()); + Optional 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 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 0000000000..3787a35ca2 --- /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()); + } + } +}