Search in sources :

Example 6 with BranchNameKey

use of com.google.gerrit.entities.BranchNameKey in project gerrit by GerritCodeReview.

the class MoveChangeIT method moveMergeCommitChange.

@Test
public void moveMergeCommitChange() throws Exception {
    // Move a change which has a merge commit as the current PS
    // Create a merge commit and push for review
    PushOneCommit.Result r1 = createChange();
    PushOneCommit.Result r2 = createChange();
    TestRepository<?>.CommitBuilder commitBuilder = testRepo.branch("HEAD").commit().insertChangeId();
    commitBuilder.parent(r1.getCommit()).parent(r2.getCommit()).message("Move change Merge Commit").author(admin.newIdent()).committer(new PersonIdent(admin.newIdent(), testRepo.getDate()));
    RevCommit c = commitBuilder.create();
    pushHead(testRepo, "refs/for/master", false, false);
    // Try to move the merge commit to another branch
    BranchNameKey newBranch = BranchNameKey.create(r1.getChange().change().getProject(), "moveTest");
    createBranch(newBranch);
    String changeId = GitUtil.getChangeId(testRepo, c).get();
    move(changeId, newBranch.branch());
    assertThat(gApi.changes().id(changeId).get().branch).isEqualTo("moveTest");
}
Also used : TestRepository(org.eclipse.jgit.junit.TestRepository) BranchNameKey(com.google.gerrit.entities.BranchNameKey) PersonIdent(org.eclipse.jgit.lib.PersonIdent) PushOneCommit(com.google.gerrit.acceptance.PushOneCommit) RevCommit(org.eclipse.jgit.revwalk.RevCommit) Test(org.junit.Test) AbstractDaemonTest(com.google.gerrit.acceptance.AbstractDaemonTest)

Example 7 with BranchNameKey

use of com.google.gerrit.entities.BranchNameKey in project gerrit by GerritCodeReview.

the class MoveChangeIT method moveChangeKeepAllVotesOnlyAllowedForAdmins.

@Test
public void moveChangeKeepAllVotesOnlyAllowedForAdmins() throws Exception {
    // Keep all votes options is only permitted for admins.
    BranchNameKey destinationBranch = BranchNameKey.create(project, "dest");
    createBranch(destinationBranch);
    BranchNameKey sourceBranch = BranchNameKey.create(project, "source");
    createBranch(sourceBranch);
    String changeId = createChangeInBranch(sourceBranch.branch()).getChangeId();
    // Grant change permissions to the registered users.
    projectOperations.project(project).forUpdate().add(allow(Permission.PUSH).ref(destinationBranch.branch()).group(REGISTERED_USERS)).update();
    projectOperations.project(project).forUpdate().add(allow(Permission.ABANDON).ref(sourceBranch.branch()).group(REGISTERED_USERS)).update();
    requestScopeOperations.setApiUser(user.id());
    AuthException thrown = assertThrows(AuthException.class, () -> move(changeId, destinationBranch.shortName(), true));
    assertThat(thrown).hasMessageThat().isEqualTo("move is not permitted with keepAllVotes option");
    requestScopeOperations.setApiUser(admin.id());
    move(changeId, destinationBranch.branch(), true);
    assertThat(info(changeId).branch).isEqualTo(destinationBranch.shortName());
}
Also used : BranchNameKey(com.google.gerrit.entities.BranchNameKey) AuthException(com.google.gerrit.extensions.restapi.AuthException) Test(org.junit.Test) AbstractDaemonTest(com.google.gerrit.acceptance.AbstractDaemonTest)

Example 8 with BranchNameKey

use of com.google.gerrit.entities.BranchNameKey in project gerrit by GerritCodeReview.

the class CreateChangeIT method canCreateChangeOnRefsMetaDashboards.

@Test
public void canCreateChangeOnRefsMetaDashboards() throws Exception {
    String branchName = "refs/meta/dashboards/project_1";
    requestScopeOperations.setApiUser(admin.id());
    projectOperations.project(project).forUpdate().add(allow(CREATE).ref(branchName).group(REGISTERED_USERS)).add(allow(READ).ref(branchName).group(REGISTERED_USERS)).update();
    BranchNameKey branchNameKey = BranchNameKey.create(project, branchName);
    createBranch(branchNameKey);
    requestScopeOperations.setApiUser(user.id());
    ChangeInput ci = newChangeInput(ChangeStatus.NEW);
    ci.subject = "Subject";
    ci.branch = branchName;
    assertThat(gApi.changes().create(ci).info().branch).isEqualTo(branchName);
}
Also used : BranchNameKey(com.google.gerrit.entities.BranchNameKey) ChangeInput(com.google.gerrit.extensions.common.ChangeInput) AbstractDaemonTest(com.google.gerrit.acceptance.AbstractDaemonTest) Test(org.junit.Test)

Example 9 with BranchNameKey

use of com.google.gerrit.entities.BranchNameKey in project gerrit by GerritCodeReview.

the class Submit method unmergeableChanges.

public Collection<ChangeData> unmergeableChanges(ChangeSet cs) throws IOException {
    Set<ChangeData> mergeabilityMap = new HashSet<>();
    Set<ObjectId> outDatedPatchsets = new HashSet<>();
    for (ChangeData change : cs.changes()) {
        mergeabilityMap.add(change);
        // Add all the patchsets commit ids except the current patchset.
        outDatedPatchsets.addAll(change.notes().getPatchSets().values().stream().map(p -> p.commitId()).collect(Collectors.toSet()));
        outDatedPatchsets.remove(change.currentPatchSet().commitId());
    }
    ListMultimap<BranchNameKey, ChangeData> cbb = cs.changesByBranch();
    for (BranchNameKey branch : cbb.keySet()) {
        Collection<ChangeData> targetBranch = cbb.get(branch);
        HashMap<Change.Id, RevCommit> commits = findCommits(targetBranch, branch.project());
        Set<ObjectId> allParents = Sets.newHashSetWithExpectedSize(cs.size());
        for (RevCommit commit : commits.values()) {
            for (RevCommit parent : commit.getParents()) {
                allParents.add(parent.getId());
            }
        }
        for (ChangeData change : targetBranch) {
            RevCommit commit = commits.get(change.getId());
            boolean isMergeCommit = commit.getParentCount() > 1;
            boolean isLastInChain = !allParents.contains(commit.getId());
            if (Arrays.stream(commit.getParents()).anyMatch(c -> outDatedPatchsets.contains(c.getId())) && !isCherryPickSubmit(change)) {
                // cherry-pick.
                continue;
            }
            // Recheck mergeability rather than using value stored in the index,
            // which may be stale.
            // TODO(dborowitz): This is ugly; consider providing a way to not read
            // stored fields from the index in the first place.
            change.setMergeable(null);
            Boolean mergeable = change.isMergeable();
            if (mergeable == null) {
                // Skip whole check, cannot determine if mergeable
                return null;
            }
            if (mergeable) {
                mergeabilityMap.remove(change);
            }
            if (isLastInChain && isMergeCommit && mergeable) {
                for (ChangeData c : targetBranch) {
                    mergeabilityMap.remove(c);
                }
                break;
            }
        }
    }
    return mergeabilityMap;
}
Also used : Arrays(java.util.Arrays) ListMultimap(com.google.common.collect.ListMultimap) ProjectCache(com.google.gerrit.server.project.ProjectCache) RepositoryNotFoundException(org.eclipse.jgit.errors.RepositoryNotFoundException) Inject(com.google.inject.Inject) PermissionBackend(com.google.gerrit.server.permissions.PermissionBackend) UpdateException(com.google.gerrit.server.update.UpdateException) RestModifyView(com.google.gerrit.extensions.restapi.RestModifyView) RevWalk(org.eclipse.jgit.revwalk.RevWalk) Config(org.eclipse.jgit.lib.Config) SubmitInput(com.google.gerrit.extensions.api.changes.SubmitInput) Map(java.util.Map) AuthException(com.google.gerrit.extensions.restapi.AuthException) UiAction(com.google.gerrit.extensions.webui.UiAction) EnumSet(java.util.EnumSet) GerritServerConfig(com.google.gerrit.server.config.GerritServerConfig) ImmutableMap(com.google.common.collect.ImmutableMap) SubmitType(com.google.gerrit.extensions.client.SubmitType) Collection(java.util.Collection) Set(java.util.Set) Collectors(java.util.stream.Collectors) Collectors.joining(java.util.stream.Collectors.joining) Sets(com.google.common.collect.Sets) BranchNameKey(com.google.gerrit.entities.BranchNameKey) SubmitTypeRecord(com.google.gerrit.entities.SubmitTypeRecord) ChangeData(com.google.gerrit.server.query.change.ChangeData) InternalChangeQuery(com.google.gerrit.server.query.change.InternalChangeQuery) ChangeJson(com.google.gerrit.server.change.ChangeJson) FluentLogger(com.google.common.flogger.FluentLogger) Singleton(com.google.inject.Singleton) PermissionBackendException(com.google.gerrit.server.permissions.PermissionBackendException) ConfigInvalidException(org.eclipse.jgit.errors.ConfigInvalidException) RevCommit(org.eclipse.jgit.revwalk.RevCommit) UnprocessableEntityException(com.google.gerrit.extensions.restapi.UnprocessableEntityException) ChangePermission(com.google.gerrit.server.permissions.ChangePermission) HashMap(java.util.HashMap) Response(com.google.gerrit.extensions.restapi.Response) HashSet(java.util.HashSet) Strings(com.google.common.base.Strings) MergeOp(com.google.gerrit.server.submit.MergeOp) ChangeResource(com.google.gerrit.server.change.ChangeResource) ChangeInfo(com.google.gerrit.extensions.common.ChangeInfo) UsedAt(com.google.gerrit.common.UsedAt) Change(com.google.gerrit.entities.Change) PatchSet(com.google.gerrit.entities.PatchSet) RestApiException(com.google.gerrit.extensions.restapi.RestApiException) ChangeUtil(com.google.gerrit.server.ChangeUtil) ProjectCache.illegalState(com.google.gerrit.server.project.ProjectCache.illegalState) CurrentUser(com.google.gerrit.server.CurrentUser) MergeSuperSet(com.google.gerrit.server.submit.MergeSuperSet) AccountResolver(com.google.gerrit.server.account.AccountResolver) ObjectIds.abbreviateName(com.google.gerrit.git.ObjectIds.abbreviateName) ChangeSet(com.google.gerrit.server.submit.ChangeSet) StorageException(com.google.gerrit.exceptions.StorageException) ProjectState(com.google.gerrit.server.project.ProjectState) MoreObjects(com.google.common.base.MoreObjects) ParameterizedString(com.google.gerrit.common.data.ParameterizedString) IOException(java.io.IOException) ObjectId(org.eclipse.jgit.lib.ObjectId) Provider(com.google.inject.Provider) GitRepositoryManager(com.google.gerrit.server.git.GitRepositoryManager) ResourceConflictException(com.google.gerrit.extensions.restapi.ResourceConflictException) IdentifiedUser(com.google.gerrit.server.IdentifiedUser) RevisionResource(com.google.gerrit.server.change.RevisionResource) ProjectUtil(com.google.gerrit.server.ProjectUtil) Project(com.google.gerrit.entities.Project) PatchSetUtil(com.google.gerrit.server.PatchSetUtil) Repository(org.eclipse.jgit.lib.Repository) ObjectId(org.eclipse.jgit.lib.ObjectId) ChangeData(com.google.gerrit.server.query.change.ChangeData) BranchNameKey(com.google.gerrit.entities.BranchNameKey) ObjectId(org.eclipse.jgit.lib.ObjectId) HashSet(java.util.HashSet) RevCommit(org.eclipse.jgit.revwalk.RevCommit)

Example 10 with BranchNameKey

use of com.google.gerrit.entities.BranchNameKey in project gerrit by GerritCodeReview.

the class SubmoduleCommitsTest method createGitlinksCommit_subprojectMoved.

@Test
public void createGitlinksCommit_subprojectMoved() throws Exception {
    createRepo(subProject, MASTER);
    createRepo(superProject, MASTER);
    when(mockProjectCache.get(any())).thenReturn(Optional.of(mockProjectState));
    mergeOpRepoManager = new MergeOpRepoManager(repoManager, mockProjectCache, null, null);
    ObjectId subprojectCommit = getTip(subProject, MASTER);
    RevCommit superprojectTip = directUpdateSubmodule(superProject, MASTER, Project.nameKey("dir-x"), subprojectCommit);
    assertThat(readGitLink(superProject, superprojectTip, "dir-x")).isEqualTo(subprojectCommit);
    RevCommit newSubprojectCommit = addCommit(subProject, MASTER);
    BranchNameKey superBranch = BranchNameKey.create(superProject, MASTER);
    BranchNameKey subBranch = BranchNameKey.create(subProject, MASTER);
    SubmoduleSubscription ss = new SubmoduleSubscription(superBranch, subBranch, "dir-x");
    SubmoduleCommits helper = new SubmoduleCommits(mergeOpRepoManager, ident, new Config());
    Optional<CodeReviewCommit> newGitLinksCommit = helper.composeGitlinksCommit(BranchNameKey.create(superProject, MASTER), ImmutableList.of(ss));
    assertThat(newGitLinksCommit).isPresent();
    assertThat(newGitLinksCommit.get().getParent(0)).isEqualTo(superprojectTip);
    assertThat(readGitLink(superProject, newGitLinksCommit.get(), "dir-x")).isEqualTo(newSubprojectCommit);
}
Also used : BranchNameKey(com.google.gerrit.entities.BranchNameKey) AnyObjectId(org.eclipse.jgit.lib.AnyObjectId) ObjectId(org.eclipse.jgit.lib.ObjectId) Config(org.eclipse.jgit.lib.Config) SubmoduleSubscription(com.google.gerrit.entities.SubmoduleSubscription) CodeReviewCommit(com.google.gerrit.server.git.CodeReviewCommit) RevCommit(org.eclipse.jgit.revwalk.RevCommit) Test(org.junit.Test)

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