use of com.google.gerrit.entities.BranchNameKey in project gerrit by GerritCodeReview.
the class ChangeIdHandler method parseArguments.
@Override
public final int parseArguments(Parameters params) throws CmdLineException {
String token = params.getParameter(0);
List<String> tokens = Splitter.on(',').splitToList(token);
if (tokens.size() != 3) {
throw new CmdLineException(owner, localizable("change should be specified as <project>,<branch>,<change-id>"));
}
try {
Change.Key key = Change.Key.parse(tokens.get(2));
Project.NameKey project = Project.nameKey(tokens.get(0));
BranchNameKey branch = BranchNameKey.create(project, tokens.get(1));
List<ChangeData> changes = queryProvider.get().byBranchKey(branch, key);
if (!changes.isEmpty()) {
if (changes.size() > 1) {
String msg = "\"%s\": resolves to multiple changes";
logger.atSevere().log(msg, token);
throw new CmdLineException(owner, localizable(msg), token);
}
setter.addValue(changes.get(0).getId());
return 1;
}
} catch (IllegalArgumentException e) {
throw new CmdLineException(owner, localizable("Change-Id is not valid: %s"), e.getMessage());
} catch (StorageException e) {
throw new CmdLineException(owner, localizable("Database error: %s"), e.getMessage());
}
throw new CmdLineException(owner, localizable("\"%s\": change not found"), token);
}
use of com.google.gerrit.entities.BranchNameKey in project gerrit by GerritCodeReview.
the class MoveChangeIT method moveChangeKeepAllVotesNoLabelInDestination.
@Test
public void moveChangeKeepAllVotesNoLabelInDestination() throws Exception {
BranchNameKey destinationBranch = BranchNameKey.create(project, "dest");
createBranch(destinationBranch);
BranchNameKey sourceBranch = BranchNameKey.create(project, "source");
createBranch(sourceBranch);
String testLabelA = "Label-A";
// The label has the range [-1; 1]
configLabel(testLabelA, LabelFunction.NO_BLOCK, ImmutableList.of(sourceBranch.branch()));
// Registered users have permissions for the entire range [-1; 1] on all branches.
projectOperations.project(project).forUpdate().add(allowLabel(testLabelA).ref("refs/heads/*").group(REGISTERED_USERS).range(-1, +1)).update();
String changeId = createChangeInBranch(sourceBranch.branch()).getChangeId();
requestScopeOperations.setApiUser(user.id());
ReviewInput userReviewInput = new ReviewInput();
userReviewInput.label(testLabelA, 1);
gApi.changes().id(changeId).current().review(userReviewInput);
assertLabelVote(user, changeId, testLabelA, (short) 1);
requestScopeOperations.setApiUser(admin.id());
assertThat(atrScope.get().getUser().getAccountId()).isEqualTo(admin.id());
// Move the change to the destination branch.
assertThat(info(changeId).branch).isEqualTo(sourceBranch.shortName());
move(changeId, destinationBranch.shortName(), true);
assertThat(info(changeId).branch).isEqualTo(destinationBranch.shortName());
// Label is missing in the destination branch.
assertThat(gApi.changes().id(changeId).current().reviewer(user.email()).votes()).isEmpty();
// Move the change back to the source, the label is kept.
move(changeId, sourceBranch.shortName(), true);
assertThat(info(changeId).branch).isEqualTo(sourceBranch.shortName());
assertLabelVote(user, changeId, testLabelA, (short) 1);
}
use of com.google.gerrit.entities.BranchNameKey in project gerrit by GerritCodeReview.
the class MoveChangeIT method moveChangeWithCurrentPatchSetLocked.
@Test
public void moveChangeWithCurrentPatchSetLocked() throws Exception {
// Move change that is locked
PushOneCommit.Result r = createChange();
BranchNameKey newBranch = BranchNameKey.create(r.getChange().change().getProject(), "moveTest");
createBranch(newBranch);
LabelType patchSetLock = TestLabels.patchSetLock();
try (ProjectConfigUpdate u = updateProject(project)) {
u.getConfig().upsertLabelType(patchSetLock);
u.save();
}
projectOperations.project(project).forUpdate().add(allowLabel(patchSetLock.getName()).ref("refs/heads/*").group(REGISTERED_USERS).range(0, 1)).update();
revision(r).review(new ReviewInput().label("Patch-Set-Lock", 1));
ResourceConflictException thrown = assertThrows(ResourceConflictException.class, () -> move(r.getChangeId(), newBranch.branch()));
assertThat(thrown).hasMessageThat().contains(String.format("The current patch set of change %s is locked", r.getChange().getId()));
}
use of com.google.gerrit.entities.BranchNameKey in project gerrit by GerritCodeReview.
the class MoveChangeIT method moveChangeKeepAllVotesOutOfUserPermissionRange.
@Test
public void moveChangeKeepAllVotesOutOfUserPermissionRange() throws Exception {
BranchNameKey destinationBranch = BranchNameKey.create(project, "dest");
createBranch(destinationBranch);
BranchNameKey sourceBranch = BranchNameKey.create(project, "source");
createBranch(sourceBranch);
String testLabelA = "Label-A";
// The label has the range [-2; 2]
configLabel(project, testLabelA, LabelFunction.NO_BLOCK, value(2, "Passes"), value(1, "Mostly ok"), value(0, "No score"), value(-1, "Needs work"), value(-2, "Failed"));
// Registered users have [-2; 2] permissions on the source.
projectOperations.project(project).forUpdate().add(allowLabel(testLabelA).ref(sourceBranch.branch()).group(REGISTERED_USERS).range(-2, +2)).update();
// Registered users have [-1; 1] permissions on the destination.
projectOperations.project(project).forUpdate().add(allowLabel(testLabelA).ref(destinationBranch.branch()).group(REGISTERED_USERS).range(-1, +1)).update();
String changeId = createChangeInBranch(sourceBranch.branch()).getChangeId();
requestScopeOperations.setApiUser(user.id());
// Vote within the range of the source branch.
ReviewInput userReviewInput = new ReviewInput();
userReviewInput.label(testLabelA, 2);
gApi.changes().id(changeId).current().review(userReviewInput);
assertLabelVote(user, changeId, testLabelA, (short) 2);
requestScopeOperations.setApiUser(admin.id());
assertThat(atrScope.get().getUser().getAccountId()).isEqualTo(admin.id());
// Move the change to the destination branch.
assertThat(info(changeId).branch).isEqualTo(sourceBranch.shortName());
move(changeId, destinationBranch.branch(), true);
// User does not have label permissions for the same vote on the destination branch.
requestScopeOperations.setApiUser(user.id());
AuthException thrown = assertThrows(AuthException.class, () -> gApi.changes().id(changeId).current().review(userReviewInput));
assertThat(thrown).hasMessageThat().isEqualTo(String.format("Applying label \"%s\": 2 is restricted", testLabelA));
// Label is kept even though the user's permission range is different from the source.
// Since we do not squash users votes based on the destination branch access label
// configuration, this is working as intended.
// It's the same behavior as when a project owner reduces user's permission range on label.
// Administrators should take this into account.
assertThat(info(changeId).branch).isEqualTo(destinationBranch.shortName());
assertLabelVote(user, changeId, testLabelA, (short) 2);
requestScopeOperations.setApiUser(admin.id());
// Move the change back to the source, the label is kept.
move(changeId, sourceBranch.shortName(), true);
assertThat(info(changeId).branch).isEqualTo(sourceBranch.shortName());
assertLabelVote(user, changeId, testLabelA, (short) 2);
}
use of com.google.gerrit.entities.BranchNameKey in project gerrit by GerritCodeReview.
the class MoveChangeIT method moveChangeToNonExistentRef.
@Test
public void moveChangeToNonExistentRef() throws Exception {
// Move change to a non-existing branch
PushOneCommit.Result r = createChange();
BranchNameKey newBranch = BranchNameKey.create(r.getChange().change().getProject(), "does_not_exist");
ResourceConflictException thrown = assertThrows(ResourceConflictException.class, () -> move(r.getChangeId(), newBranch.branch()));
assertThat(thrown).hasMessageThat().contains("Destination " + newBranch.branch() + " not found in the project");
}
Aggregations