Handle mutually exclusive star labels when change is starred or ignored

The star labels 'star' (default star) and 'ignore' (for ignoring a
change) are mutually exclusive. Trying to set one if the other is
already set failed with an internal server error. Handle this case and
return 409 Conflict instead.

In addition this adds a test to verify that setting an invalid star
label is correctly rejected with 400 Bad Request. This case was already
correctly handled in the Stars.Post REST endpoint (sets multiple stars
at once), but not in the StarredChanges.Create REST endpoint (adds a
single star).

Bug: Issue 7238
Change-Id: Id4bb5604116f4959d8ecba66146bab72437205f6
Signed-off-by: Edwin Kempin <ekempin@google.com>
This commit is contained in:
Edwin Kempin
2017-09-18 14:06:42 +02:00
parent 17ecf653a7
commit c8cfdb0855
4 changed files with 80 additions and 12 deletions

View File

@@ -81,6 +81,7 @@ import com.google.gerrit.extensions.api.changes.ReviewInput;
import com.google.gerrit.extensions.api.changes.ReviewInput.DraftHandling;
import com.google.gerrit.extensions.api.changes.ReviewResult;
import com.google.gerrit.extensions.api.changes.RevisionApi;
import com.google.gerrit.extensions.api.changes.StarsInput;
import com.google.gerrit.extensions.api.projects.BranchInput;
import com.google.gerrit.extensions.api.projects.ConfigInput;
import com.google.gerrit.extensions.client.ChangeKind;
@@ -122,6 +123,7 @@ import com.google.gerrit.reviewdb.client.PatchSetApproval;
import com.google.gerrit.reviewdb.client.Project;
import com.google.gerrit.reviewdb.client.RefNames;
import com.google.gerrit.server.ChangeMessagesUtil;
import com.google.gerrit.server.StarredChangesUtil;
import com.google.gerrit.server.change.ChangeResource;
import com.google.gerrit.server.change.PostReview;
import com.google.gerrit.server.config.AnonymousCowardNameProvider;
@@ -3465,4 +3467,51 @@ public class ChangeIT extends AbstractDaemonTest {
gApi.changes().id(changeId).ignore(true);
assertThat(gApi.changes().id(changeId).ignored()).isFalse();
}
@Test
public void cannotIgnoreStarredChange() throws Exception {
String changeId = createChange().getChangeId();
setApiUser(user);
gApi.accounts().self().starChange(changeId);
assertThat(gApi.changes().id(changeId).get().starred).isTrue();
exception.expect(ResourceConflictException.class);
exception.expectMessage(
"The labels "
+ StarredChangesUtil.DEFAULT_LABEL
+ " and "
+ StarredChangesUtil.IGNORE_LABEL
+ " are mutually exclusive. Only one of them can be set.");
gApi.changes().id(changeId).ignore(true);
}
@Test
public void cannotStarIgnoredChange() throws Exception {
String changeId = createChange().getChangeId();
setApiUser(user);
gApi.changes().id(changeId).ignore(true);
assertThat(gApi.changes().id(changeId).ignored()).isTrue();
exception.expect(ResourceConflictException.class);
exception.expectMessage(
"The labels "
+ StarredChangesUtil.DEFAULT_LABEL
+ " and "
+ StarredChangesUtil.IGNORE_LABEL
+ " are mutually exclusive. Only one of them can be set.");
gApi.accounts().self().starChange(changeId);
}
@Test
public void cannotSetInvalidLabel() throws Exception {
String changeId = createChange().getChangeId();
// label cannot contain whitespace
String invalidLabel = "invalid label";
exception.expect(BadRequestException.class);
exception.expectMessage("invalid labels: " + invalidLabel);
gApi.accounts().self().setStars(changeId, new StarsInput(ImmutableSet.of(invalidLabel)));
}
}