Commit ec05c595 authored by Greg Messner's avatar Greg Messner
Browse files

Added support for getting the merge base (#288).

parent d2c52565
......@@ -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<Contributor> 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).
*
* <pre><code>GitLab Endpoint: GET /projects/:id/repository/merge_base</code></pre>
*
* @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<String> 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).
*
* <pre><code>GitLab Endpoint: GET /projects/:id/repository/merge_base</code></pre>
*
* @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<Commit> getOptionalMergeBase(Object projectIdOrPath, List<String> refs) throws GitLabApiException {
try {
return (Optional.ofNullable(getMergeBase(projectIdOrPath, refs)));
} catch (GitLabApiException glae) {
return (GitLabApi.createOptionalFromException(glae));
}
}
}
......@@ -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<String> refs = Arrays.asList(TEST_BRANCH1, TEST_BRANCH2);
Commit mergeBase = gitLabApi.getRepositoryApi().getMergeBase(project, refs);
assertNotNull(mergeBase);
}
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment