diff --git a/src/main/java/org/gitlab4j/api/RepositoryApi.java b/src/main/java/org/gitlab4j/api/RepositoryApi.java index 7f71f6a1bcf4445ec572eb525d636d7a4fcf0867..1fd6087f00eaaa1eadde01fa460b50792d723635 100644 --- a/src/main/java/org/gitlab4j/api/RepositoryApi.java +++ b/src/main/java/org/gitlab4j/api/RepositoryApi.java @@ -16,6 +16,7 @@ import javax.ws.rs.core.Response; import org.gitlab4j.api.GitLabApi.ApiVersion; import org.gitlab4j.api.models.Branch; +import org.gitlab4j.api.models.Commit; import org.gitlab4j.api.models.CompareResults; import org.gitlab4j.api.models.Contributor; import org.gitlab4j.api.models.Tag; @@ -742,4 +743,40 @@ public class RepositoryApi extends AbstractApi { public Stream getContributorsStream(Object projectIdOrPath) throws GitLabApiException { return (getContributors(projectIdOrPath, getDefaultPerPage()).stream()); } + + /** + * Get the common ancestor for 2 or more refs (commit SHAs, branch names or tags). + * + *
GitLab Endpoint: GET /projects/:id/repository/merge_base
+ * + * @param projectIdOrPath the project in the form of an Integer(ID), String(path), or Project instance + * @param refs a List of 2 or more refs (commit SHAs, branch names or tags) + * @return the Commit instance containing the common ancestor + * @throws GitLabApiException if any exception occurs + */ + public Commit getMergeBase(Object projectIdOrPath, List refs) throws GitLabApiException { + GitLabApiForm queryParams = new GitLabApiForm().withParam("refs", refs, true); + Response response = get(Response.Status.OK, queryParams.asMap(), "projects", + getProjectIdOrPath(projectIdOrPath), "repository", "merge_base"); + return (response.readEntity(Commit.class)); + } + + /** + * Get an Optional instance with the value of the common ancestor + * for 2 or more refs (commit SHAs, branch names or tags). + * + *
GitLab Endpoint: GET /projects/:id/repository/merge_base
+ * + * @param projectIdOrPath the project in the form of an Integer(ID), String(path), or Project instance + * @param refs a List of 2 or more refs (commit SHAs, branch names or tags) + * @return an Optional instance with the Commit instance containing the common ancestor as the value + * @throws GitLabApiException if any exception occurs + */ + public Optional getOptionalMergeBase(Object projectIdOrPath, List refs) throws GitLabApiException { + try { + return (Optional.ofNullable(getMergeBase(projectIdOrPath, refs))); + } catch (GitLabApiException glae) { + return (GitLabApi.createOptionalFromException(glae)); + } + } } diff --git a/src/test/java/org/gitlab4j/api/TestRepositoryApi.java b/src/test/java/org/gitlab4j/api/TestRepositoryApi.java index 5211247fc822ed1741b3183675e400ce8f6364ca..965e7759043f89221fb1869957b41b37f54a33d0 100644 --- a/src/test/java/org/gitlab4j/api/TestRepositoryApi.java +++ b/src/test/java/org/gitlab4j/api/TestRepositoryApi.java @@ -12,6 +12,7 @@ import java.io.InputStream; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.StandardCopyOption; +import java.util.Arrays; import java.util.List; import org.gitlab4j.api.models.Branch; @@ -54,6 +55,8 @@ public class TestRepositoryApi { } private static final String TEST_BRANCH_NAME = "feature/test_branch"; + private static final String TEST_BRANCH1 = "feature/test_branch1"; + private static final String TEST_BRANCH2 = "feature/test_branch2"; private static final String TEST_PROTECT_BRANCH_NAME = "feature/protect_branch"; private static GitLabApi gitLabApi; @@ -101,6 +104,16 @@ public class TestRepositoryApi { } catch (GitLabApiException ignore) { } + try { + gitLabApi.getRepositoryApi().deleteBranch(project.getId(), TEST_BRANCH1); + } catch (GitLabApiException ignore) { + } + + try { + gitLabApi.getRepositoryApi().deleteBranch(project.getId(), TEST_BRANCH2); + } catch (GitLabApiException ignore) { + } + gitLabApi.getRepositoryApi().deleteBranch(project.getId(), TEST_PROTECT_BRANCH_NAME); } catch (GitLabApiException ignore) { @@ -200,4 +213,20 @@ public class TestRepositoryApi { assertNotNull(unprotectedBranch); assertFalse(unprotectedBranch.getProtected()); } + + @Test + public void testMergeBase() throws GitLabApiException { + + Project project = gitLabApi.getProjectApi().getProject(TEST_NAMESPACE, TEST_PROJECT_NAME); + assertNotNull(project); + + Branch branch1 = gitLabApi.getRepositoryApi().createBranch(project.getId(), TEST_BRANCH1, "master"); + assertNotNull(branch1); + Branch branch2 = gitLabApi.getRepositoryApi().createBranch(project.getId(), TEST_BRANCH2, "master"); + assertNotNull(branch2); + + List refs = Arrays.asList(TEST_BRANCH1, TEST_BRANCH2); + Commit mergeBase = gitLabApi.getRepositoryApi().getMergeBase(project, refs); + assertNotNull(mergeBase); + } }