Search in sources :

Example 1 with BranchNameKey

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);
}
Also used : Project(com.google.gerrit.entities.Project) BranchNameKey(com.google.gerrit.entities.BranchNameKey) Change(com.google.gerrit.entities.Change) ChangeData(com.google.gerrit.server.query.change.ChangeData) StorageException(com.google.gerrit.exceptions.StorageException) CmdLineException(org.kohsuke.args4j.CmdLineException)

Example 2 with BranchNameKey

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);
}
Also used : BranchNameKey(com.google.gerrit.entities.BranchNameKey) ReviewInput(com.google.gerrit.extensions.api.changes.ReviewInput) Test(org.junit.Test) AbstractDaemonTest(com.google.gerrit.acceptance.AbstractDaemonTest)

Example 3 with BranchNameKey

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()));
}
Also used : ResourceConflictException(com.google.gerrit.extensions.restapi.ResourceConflictException) BranchNameKey(com.google.gerrit.entities.BranchNameKey) LabelType(com.google.gerrit.entities.LabelType) ReviewInput(com.google.gerrit.extensions.api.changes.ReviewInput) PushOneCommit(com.google.gerrit.acceptance.PushOneCommit) Test(org.junit.Test) AbstractDaemonTest(com.google.gerrit.acceptance.AbstractDaemonTest)

Example 4 with BranchNameKey

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);
}
Also used : BranchNameKey(com.google.gerrit.entities.BranchNameKey) AuthException(com.google.gerrit.extensions.restapi.AuthException) ReviewInput(com.google.gerrit.extensions.api.changes.ReviewInput) Test(org.junit.Test) AbstractDaemonTest(com.google.gerrit.acceptance.AbstractDaemonTest)

Example 5 with BranchNameKey

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");
}
Also used : ResourceConflictException(com.google.gerrit.extensions.restapi.ResourceConflictException) BranchNameKey(com.google.gerrit.entities.BranchNameKey) PushOneCommit(com.google.gerrit.acceptance.PushOneCommit) Test(org.junit.Test) AbstractDaemonTest(com.google.gerrit.acceptance.AbstractDaemonTest)

Aggregations

BranchNameKey (com.google.gerrit.entities.BranchNameKey)75 Test (org.junit.Test)48 Project (com.google.gerrit.entities.Project)26 AbstractDaemonTest (com.google.gerrit.acceptance.AbstractDaemonTest)25 Config (org.eclipse.jgit.lib.Config)19 SubmoduleSubscription (com.google.gerrit.entities.SubmoduleSubscription)18 PushOneCommit (com.google.gerrit.acceptance.PushOneCommit)16 RevCommit (org.eclipse.jgit.revwalk.RevCommit)16 ResourceConflictException (com.google.gerrit.extensions.restapi.ResourceConflictException)15 AuthException (com.google.gerrit.extensions.restapi.AuthException)13 Change (com.google.gerrit.entities.Change)12 IOException (java.io.IOException)11 ObjectId (org.eclipse.jgit.lib.ObjectId)11 StorageException (com.google.gerrit.exceptions.StorageException)10 ChangeData (com.google.gerrit.server.query.change.ChangeData)9 Repository (org.eclipse.jgit.lib.Repository)9 PatchSet (com.google.gerrit.entities.PatchSet)8 CodeReviewCommit (com.google.gerrit.server.git.CodeReviewCommit)8 HashMap (java.util.HashMap)7 Ref (org.eclipse.jgit.lib.Ref)7