Merge "Allow SetReviewersCommand to accept a commit hash as argument" into stable-2.14
diff --git a/Documentation/cmd-set-reviewers.txt b/Documentation/cmd-set-reviewers.txt
index 0a757fd..f8a272d 100644
--- a/Documentation/cmd-set-reviewers.txt
+++ b/Documentation/cmd-set-reviewers.txt
@@ -20,7 +20,7 @@
Changes can be specified in the
link:rest-api-changes.html#change-id[same format] supported by the REST
-API.
+API, as well as with the commit SHA-1.
== OPTIONS
@@ -77,6 +77,13 @@
Iac6b2ac2
----
+Add all project owners as reviewers to commit 13dff08acca571b22542ebd2e31acf4572ea0b86.
+----
+ $ ssh -p 29418 review.example.com gerrit set-reviewers \
+ -a "'Project Owners'" \
+ 13dff08acca571b22542ebd2e31acf4572ea0b86
+----
+
GERRIT
------
Part of link:index.html[Gerrit Code Review]
diff --git a/gerrit-acceptance-tests/src/test/java/com/google/gerrit/acceptance/ssh/SetReviewersIT.java b/gerrit-acceptance-tests/src/test/java/com/google/gerrit/acceptance/ssh/SetReviewersIT.java
new file mode 100644
index 0000000..c79f18d
--- /dev/null
+++ b/gerrit-acceptance-tests/src/test/java/com/google/gerrit/acceptance/ssh/SetReviewersIT.java
@@ -0,0 +1,70 @@
+// 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.ssh;
+
+import static com.google.common.truth.Truth.assert_;
+import static org.junit.Assert.assertTrue;
+
+import com.google.gerrit.acceptance.AbstractDaemonTest;
+import com.google.gerrit.acceptance.PushOneCommit;
+import com.google.gerrit.acceptance.UseSsh;
+import org.junit.Test;
+
+@UseSsh
+public class SetReviewersIT extends AbstractDaemonTest {
+
+ @Test
+ public void addByCommitHash() throws Exception {
+ PushOneCommit.Result change = createChange();
+ adminSshSession.exec(
+ "gerrit set-reviewers -a "
+ + user.email
+ + " "
+ + change.getCommit().getId().toString().split("\\s+")[1]);
+ assert_()
+ .withFailureMessage(adminSshSession.getError())
+ .that(adminSshSession.hasError())
+ .isFalse();
+ assertTrue(change.getChange().getReviewers().all().contains(user.id));
+ }
+
+ @Test
+ public void addByChangeID() throws Exception {
+ PushOneCommit.Result change = createChange();
+ adminSshSession.exec("gerrit set-reviewers -a " + user.email + " " + change.getChangeId());
+ assert_()
+ .withFailureMessage(adminSshSession.getError())
+ .that(adminSshSession.hasError())
+ .isFalse();
+ assertTrue(change.getChange().getReviewers().all().contains(user.id));
+ }
+
+ @Test
+ public void removeReviewer() throws Exception {
+ PushOneCommit.Result change = createChange();
+ adminSshSession.exec("gerrit set-reviewers -a " + user.email + " " + change.getChangeId());
+ assert_()
+ .withFailureMessage(adminSshSession.getError())
+ .that(adminSshSession.hasError())
+ .isFalse();
+ assertTrue(change.getChange().getReviewers().all().contains(user.id));
+ adminSshSession.exec("gerrit set-reviewers -r " + user.email + " " + change.getChangeId());
+ assert_()
+ .withFailureMessage(adminSshSession.getError())
+ .that(adminSshSession.hasError())
+ .isFalse();
+ assertTrue(change.getChange().getReviewers().all().asList().isEmpty());
+ }
+}
diff --git a/gerrit-server/src/main/java/com/google/gerrit/server/ChangeFinder.java b/gerrit-server/src/main/java/com/google/gerrit/server/ChangeFinder.java
index 2f3a76f..fa68473 100644
--- a/gerrit-server/src/main/java/com/google/gerrit/server/ChangeFinder.java
+++ b/gerrit-server/src/main/java/com/google/gerrit/server/ChangeFinder.java
@@ -16,6 +16,7 @@
import com.google.common.primitives.Ints;
import com.google.gerrit.reviewdb.client.Change;
+import com.google.gerrit.reviewdb.client.RevId;
import com.google.gerrit.server.change.ChangeTriplet;
import com.google.gerrit.server.project.ChangeControl;
import com.google.gerrit.server.project.NoSuchChangeException;
@@ -53,6 +54,11 @@
// to force rereading in case the index is stale.
InternalChangeQuery query = queryProvider.get().noFields();
+ //Try commit hash
+ if (id.matches("^([0-9a-fA-F]{4," + RevId.LEN + "})$")) {
+ return asChangeControls(query.byCommit(id), user);
+ }
+
// Try legacy id
if (!id.isEmpty() && id.charAt(0) != '0') {
Integer n = Ints.tryParse(id);