Search in sources :

Example 6 with MergeableInfo

use of com.google.gerrit.extensions.common.MergeableInfo in project gerrit by GerritCodeReview.

the class RevisionIT method assertMergeable.

private void assertMergeable(String id, boolean expected) throws Exception {
    MergeableInfo m = gApi.changes().id(id).current().mergeable();
    assertThat(m.mergeable).isEqualTo(expected);
    assertThat(m.submitType).isEqualTo(SubmitType.MERGE_IF_NECESSARY);
    assertThat(m.mergeableInto).isNull();
    ChangeInfo c = gApi.changes().id(id).info();
    assertThat(c.mergeable).isEqualTo(expected);
}
Also used : ChangeInfo(com.google.gerrit.extensions.common.ChangeInfo) MergeableInfo(com.google.gerrit.extensions.common.MergeableInfo)

Example 7 with MergeableInfo

use of com.google.gerrit.extensions.common.MergeableInfo in project gerrit by GerritCodeReview.

the class RevisionIT method mergeableOtherBranches.

@Test
public void mergeableOtherBranches() throws Exception {
    String head = getHead(repo(), HEAD).name();
    createBranchWithRevision(BranchNameKey.create(project, "mergeable-other-branch"), head);
    createBranchWithRevision(BranchNameKey.create(project, "ignored"), head);
    PushOneCommit.Result change1 = createChange();
    try (ProjectConfigUpdate u = updateProject(project)) {
        u.getConfig().setBranchOrderSection(BranchOrderSection.create(ImmutableList.of("master", "nonexistent", "mergeable-other-branch")));
        u.save();
    }
    MergeableInfo mergeableInfo = gApi.changes().id(change1.getChangeId()).current().mergeableOtherBranches();
    assertThat(mergeableInfo.mergeableInto).containsExactly("mergeable-other-branch");
}
Also used : MergeableInfo(com.google.gerrit.extensions.common.MergeableInfo) PushOneCommit(com.google.gerrit.acceptance.PushOneCommit) AbstractDaemonTest(com.google.gerrit.acceptance.AbstractDaemonTest) Test(org.junit.Test)

Example 8 with MergeableInfo

use of com.google.gerrit.extensions.common.MergeableInfo in project gerrit by GerritCodeReview.

the class Mergeable method apply.

@Override
public Response<MergeableInfo> apply(RevisionResource resource) throws AuthException, ResourceConflictException, BadRequestException, IOException {
    Change change = resource.getChange();
    PatchSet ps = resource.getPatchSet();
    MergeableInfo result = new MergeableInfo();
    if (!change.isNew()) {
        throw new ResourceConflictException("change is " + ChangeUtil.status(change));
    } else if (!ps.id().equals(change.currentPatchSetId())) {
        // Only the current revision is mergeable. Others always fail.
        return Response.ok(result);
    }
    ChangeData cd = changeDataFactory.create(resource.getNotes());
    result.submitType = getSubmitType(cd);
    try (Repository git = gitManager.openRepository(change.getProject())) {
        ObjectId commit = ps.commitId();
        Ref ref = git.getRefDatabase().exactRef(change.getDest().branch());
        ProjectState projectState = projectCache.get(change.getProject()).orElseThrow(illegalState(change.getProject()));
        String strategy = mergeUtilFactory.create(projectState).mergeStrategyName();
        result.strategy = strategy;
        result.mergeable = isMergable(git, change, commit, ref, result.submitType, strategy);
        if (otherBranches) {
            result.mergeableInto = new ArrayList<>();
            Optional<BranchOrderSection> branchOrder = projectState.getBranchOrderSection();
            if (branchOrder.isPresent()) {
                int prefixLen = Constants.R_HEADS.length();
                List<String> names = branchOrder.get().getMoreStable(ref.getName());
                Map<String, Ref> refs = git.getRefDatabase().exactRef(names.toArray(new String[names.size()]));
                for (String n : names) {
                    Ref other = refs.get(n);
                    if (other == null) {
                        continue;
                    }
                    if (cache.get(commit, other, SubmitType.CHERRY_PICK, strategy, change.getDest(), git)) {
                        result.mergeableInto.add(other.getName().substring(prefixLen));
                    }
                }
            }
        }
    }
    return Response.ok(result);
}
Also used : ObjectId(org.eclipse.jgit.lib.ObjectId) PatchSet(com.google.gerrit.entities.PatchSet) Change(com.google.gerrit.entities.Change) BranchOrderSection(com.google.gerrit.entities.BranchOrderSection) ChangeData(com.google.gerrit.server.query.change.ChangeData) ResourceConflictException(com.google.gerrit.extensions.restapi.ResourceConflictException) Repository(org.eclipse.jgit.lib.Repository) Ref(org.eclipse.jgit.lib.Ref) MergeableInfo(com.google.gerrit.extensions.common.MergeableInfo) ProjectState(com.google.gerrit.server.project.ProjectState)

Example 9 with MergeableInfo

use of com.google.gerrit.extensions.common.MergeableInfo in project gerrit by GerritCodeReview.

the class CheckMergeabilityIT method getMergeableInfo.

private MergeableInfo getMergeableInfo(String targetBranch, String source, String strategy) throws Exception {
    String url = "/projects/" + project.get() + "/branches/" + targetBranch;
    url += "/mergeable?source=" + source;
    if (!Strings.isNullOrEmpty(strategy)) {
        url += "&strategy=" + strategy;
    }
    RestResponse r = userRestSession.get(url);
    r.assertOK();
    MergeableInfo result = newGson().fromJson(r.getReader(), MergeableInfo.class);
    r.consume();
    return result;
}
Also used : MergeableInfo(com.google.gerrit.extensions.common.MergeableInfo) RestResponse(com.google.gerrit.acceptance.RestResponse)

Example 10 with MergeableInfo

use of com.google.gerrit.extensions.common.MergeableInfo in project gerrit by GerritCodeReview.

the class CheckMergeabilityIT method assertCommitMerged.

private void assertCommitMerged(String targetBranch, String source, String strategy) throws Exception {
    MergeableInfo mergeableInfo = getMergeableInfo(targetBranch, source, strategy);
    assertThat(mergeableInfo.mergeable).isTrue();
    assertThat(mergeableInfo.commitMerged).isTrue();
}
Also used : MergeableInfo(com.google.gerrit.extensions.common.MergeableInfo)

Aggregations

MergeableInfo (com.google.gerrit.extensions.common.MergeableInfo)11 Ref (org.eclipse.jgit.lib.Ref)4 Repository (org.eclipse.jgit.lib.Repository)4 ResourceConflictException (com.google.gerrit.extensions.restapi.ResourceConflictException)3 BadRequestException (com.google.gerrit.extensions.restapi.BadRequestException)2 ResourceNotFoundException (com.google.gerrit.extensions.restapi.ResourceNotFoundException)2 InMemoryInserter (com.google.gerrit.server.git.InMemoryInserter)2 ProjectState (com.google.gerrit.server.project.ProjectState)2 ChangeData (com.google.gerrit.server.query.change.ChangeData)2 ObjectId (org.eclipse.jgit.lib.ObjectId)2 ObjectInserter (org.eclipse.jgit.lib.ObjectInserter)2 Merger (org.eclipse.jgit.merge.Merger)2 ResolveMerger (org.eclipse.jgit.merge.ResolveMerger)2 RevCommit (org.eclipse.jgit.revwalk.RevCommit)2 RevWalk (org.eclipse.jgit.revwalk.RevWalk)2 AbstractDaemonTest (com.google.gerrit.acceptance.AbstractDaemonTest)1 PushOneCommit (com.google.gerrit.acceptance.PushOneCommit)1 RestResponse (com.google.gerrit.acceptance.RestResponse)1 BranchOrderSection (com.google.gerrit.entities.BranchOrderSection)1 Change (com.google.gerrit.entities.Change)1