Commit 3a3cbe80 authored by Greg Messner's avatar Greg Messner
Browse files

Issue #38 - Fixed getDiff() and added compare().

parent 5422825d
package org.gitlab4j.api; package org.gitlab4j.api;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.List; import java.util.List;
import javax.ws.rs.core.GenericType; import javax.ws.rs.core.GenericType;
...@@ -77,17 +79,39 @@ public class CommitsApi extends AbstractApi { ...@@ -77,17 +79,39 @@ public class CommitsApi extends AbstractApi {
} }
/** /**
* Get the diff of a commit in a project. * Get the list of diffs of a commit in a project.
* *
* GET /projects/:id/repository/commits/:sha/diff * GET /projects/:id/repository/commits/:sha/diff
* *
* @param projectId the project ID that the commit belongs to * @param projectId the project ID that the commit belongs to
* @param sha a commit hash or name of a branch or tag * @param sha a commit hash or name of a branch or tag
* @return the Diff instance for the specified project ID/sha pair * @return a List of Diff instances for the specified project ID/sha pair
* @throws GitLabApiException GitLabApiException if any exception occurs during execution * @throws GitLabApiException GitLabApiException if any exception occurs during execution
*/ */
public Diff getDiff(int projectId, String sha) throws GitLabApiException { public List<Diff> getDiff(int projectId, String sha) throws GitLabApiException {
Response response = get(Response.Status.OK, null, "projects", projectId, "repository", "commits", sha, "diff"); Response response = get(Response.Status.OK, null, "projects", projectId, "repository", "commits", sha, "diff");
return (response.readEntity(Diff.class)); return (response.readEntity(new GenericType<List<Diff>>() {}));
}
/**
* Get the list of diffs of a commit in a project.
*
* GET /projects/:id/repository/commits/:sha/diff
*
* @param projectPath the project path that the commit belongs to
* @param sha a commit hash or name of a branch or tag
* @return a List of Diff instances for the specified project ID/sha pair
* @throws GitLabApiException GitLabApiException if any exception occurs during execution
*/
public List<Diff> getDiff(String projectPath, String sha) throws GitLabApiException {
try {
projectPath = URLEncoder.encode(projectPath, "UTF-8");
} catch (UnsupportedEncodingException uee) {
throw (new GitLabApiException(uee));
}
Response response = get(Response.Status.OK, null, "projects", projectPath, "repository", "commits", sha, "diff");
return (response.readEntity(new GenericType<List<Diff>>() {}));
} }
} }
...@@ -3,6 +3,8 @@ package org.gitlab4j.api; ...@@ -3,6 +3,8 @@ package org.gitlab4j.api;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.nio.file.Files; import java.nio.file.Files;
import java.nio.file.StandardCopyOption; import java.nio.file.StandardCopyOption;
import java.util.List; import java.util.List;
...@@ -14,6 +16,7 @@ import javax.ws.rs.core.Response; ...@@ -14,6 +16,7 @@ import javax.ws.rs.core.Response;
import org.gitlab4j.api.GitLabApi.ApiVersion; import org.gitlab4j.api.GitLabApi.ApiVersion;
import org.gitlab4j.api.models.Branch; import org.gitlab4j.api.models.Branch;
import org.gitlab4j.api.models.CompareResults;
import org.gitlab4j.api.models.Tag; import org.gitlab4j.api.models.Tag;
import org.gitlab4j.api.models.TreeItem; import org.gitlab4j.api.models.TreeItem;
import org.gitlab4j.api.utils.FileUtils; import org.gitlab4j.api.utils.FileUtils;
...@@ -457,4 +460,44 @@ public class RepositoryApi extends AbstractApi { ...@@ -457,4 +460,44 @@ public class RepositoryApi extends AbstractApi {
throw new GitLabApiException(ioe); throw new GitLabApiException(ioe);
} }
} }
/**
* Compare branches, tags or commits. This can be accessed without authentication
* if the repository is publicly accessible.
*
* @param projectId the ID of the project owned by the authenticated user
* @param from the commit SHA or branch name
* @param to the commit SHA or branch name
* @return a CompareResults containing the results of the comparison
* @throws GitLabApiException
*/
public CompareResults compare(Integer projectId, String from, String to) throws GitLabApiException {
Form formData = new GitLabApiForm().withParam("from", from, true).withParam("to", to, true);
Response response = get(Response.Status.OK, formData.asMap(), "projects", projectId, "repository", "compare");
return (response.readEntity(CompareResults.class));
}
/**
* Compare branches, tags or commits. This can be accessed without authentication
* if the repository is publicly accessible.
*
* @param projectPath the path of the project owned by the authenticated user
* @param from the commit SHA or branch name
* @param to the commit SHA or branch name
* @return a CompareResults containing the results of the comparison
* @throws GitLabApiException
*/
public CompareResults compare(String projectPath, String from, String to) throws GitLabApiException {
Form formData = new GitLabApiForm().withParam("from", from, true).withParam("to", to, true);
try {
projectPath = URLEncoder.encode(projectPath, "UTF-8");
} catch (UnsupportedEncodingException uee) {
throw (new GitLabApiException(uee));
}
Response response = get(Response.Status.OK, formData.asMap(), "projects", projectPath, "repository", "compare");
return (response.readEntity(CompareResults.class));
}
} }
...@@ -11,8 +11,8 @@ public class Changes { ...@@ -11,8 +11,8 @@ public class Changes {
@JsonInclude(JsonInclude.Include.ALWAYS) @JsonInclude(JsonInclude.Include.ALWAYS)
@JsonProperty("b_mode") @JsonProperty("b_mode")
private String b_mode; private String b_mode;
private Boolean deletedFile; private Boolean deletedFile;
private String diff; private String diff;
private Boolean newFile; private Boolean newFile;
......
package org.gitlab4j.api.models;
import java.util.List;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class CompareResults {
private Commit commit;
private List<Commit> commits;;
private List<Diff> diffs;
private Boolean compareTimeout;
private Boolean compareSameRef;
public Commit getCommit() {
return commit;
}
public void setCommit(Commit commit) {
this.commit = commit;
}
public List<Commit> getCommits() {
return commits;
}
public void setCommits(List<Commit> commits) {
this.commits = commits;
}
public List<Diff> getDiffs() {
return diffs;
}
public void setDiffs(List<Diff> diffs) {
this.diffs = diffs;
}
public Boolean getCompareTimeout() {
return compareTimeout;
}
public void setCompareTimeout(Boolean compareTimeout) {
this.compareTimeout = compareTimeout;
}
public Boolean getCompareSameRef() {
return compareSameRef;
}
public void setCompareSameRef(Boolean compareSameRef) {
this.compareSameRef = compareSameRef;
}
}
...@@ -14,7 +14,7 @@ public class MergeRequest { ...@@ -14,7 +14,7 @@ public class MergeRequest {
private Integer approvalsBeforeMerge; private Integer approvalsBeforeMerge;
private Assignee assignee; private Assignee assignee;
private Author author; private Author author;
private Changes changes; private Diff changes;
private Date createdAt; private Date createdAt;
private String description; private String description;
private Integer downvotes; private Integer downvotes;
...@@ -67,11 +67,11 @@ public class MergeRequest { ...@@ -67,11 +67,11 @@ public class MergeRequest {
this.author = author; this.author = author;
} }
public Changes getChanges() { public Diff getChanges() {
return changes; return changes;
} }
public void setChanges(Changes changes) { public void setChanges(Diff changes) {
this.changes = changes; this.changes = changes;
} }
......
package org.gitlab4j.api;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assume.assumeTrue;
import java.util.List;
import org.gitlab4j.api.GitLabApi.ApiVersion;
import org.gitlab4j.api.models.Commit;
import org.gitlab4j.api.models.Diff;
import org.gitlab4j.api.models.Project;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.FixMethodOrder;
import org.junit.Test;
import org.junit.runners.MethodSorters;
/**
* In order for these tests to run you must set the following properties in test-gitlab4j.properties
*
* TEST_NAMESPACE
* TEST_PROJECT_NAME
* TEST_HOST_URL
* TEST_PRIVATE_TOKEN
*
* If any of the above are NULL, all tests in this class will be skipped.
*/
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
public class TestCommitsApi {
// The following needs to be set to your test repository
private static final String TEST_PROJECT_NAME;
private static final String TEST_NAMESPACE;
private static final String TEST_HOST_URL;
private static final String TEST_PRIVATE_TOKEN;
static {
TEST_NAMESPACE = TestUtils.getProperty("TEST_NAMESPACE");
TEST_PROJECT_NAME = TestUtils.getProperty("TEST_PROJECT_NAME");
TEST_HOST_URL = TestUtils.getProperty("TEST_HOST_URL");
TEST_PRIVATE_TOKEN = TestUtils.getProperty("TEST_PRIVATE_TOKEN");
}
private static GitLabApi gitLabApi;
public TestCommitsApi() {
super();
}
@BeforeClass
public static void setup() {
String problems = "";
if (TEST_NAMESPACE == null || TEST_NAMESPACE.trim().length() == 0) {
problems += "TEST_NAMESPACE cannot be empty\n";
}
if (TEST_PROJECT_NAME == null || TEST_PROJECT_NAME.trim().length() == 0) {
problems += "TEST_PROJECT_NAME cannot be empty\n";
}
if (TEST_HOST_URL == null || TEST_HOST_URL.trim().length() == 0) {
problems += "TEST_HOST_URL cannot be empty\n";
}
if (TEST_PRIVATE_TOKEN == null || TEST_PRIVATE_TOKEN.trim().length() == 0) {
problems += "TEST_PRIVATE_TOKEN cannot be empty\n";
}
if (problems.isEmpty()) {
gitLabApi = new GitLabApi(ApiVersion.V4, TEST_HOST_URL, TEST_PRIVATE_TOKEN);
} else {
System.err.print(problems);
}
}
@Before
public void beforeMethod() {
assumeTrue(gitLabApi != null);
}
@Test
public void testDiff() throws GitLabApiException {
Project project = gitLabApi.getProjectApi().getProject(TEST_NAMESPACE, TEST_PROJECT_NAME);
assertNotNull(project);
List<Commit> commits = gitLabApi.getCommitsApi().getCommits(project.getId());
assertNotNull(commits);
assertTrue(0 < commits.size());
List<Diff> diffs = gitLabApi.getCommitsApi().getDiff(project.getId(), commits.get(0).getId());
assertNotNull(diffs);
assertTrue(0 < diffs.size());
diffs = gitLabApi.getCommitsApi().getDiff(TEST_NAMESPACE + "/" + TEST_PROJECT_NAME, commits.get(0).getId());
assertNotNull(diffs);
assertTrue(0 < diffs.size());
}
}
package org.gitlab4j.api; package org.gitlab4j.api;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assume.assumeTrue; import static org.junit.Assume.assumeTrue;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardCopyOption;
import java.util.List;
import org.gitlab4j.api.GitLabApi.ApiVersion; import org.gitlab4j.api.GitLabApi.ApiVersion;
import org.gitlab4j.api.models.Branch;
import org.gitlab4j.api.models.Project;
import org.gitlab4j.api.models.Version; import org.gitlab4j.api.models.Version;
import org.junit.AfterClass;
import org.junit.Before; import org.junit.Before;
import org.junit.BeforeClass; import org.junit.BeforeClass;
import org.junit.FixMethodOrder;
import org.junit.Test; import org.junit.Test;
import org.junit.runners.MethodSorters;
/** /**
* In order for these tests to run you must set the following properties in test-gitlab4j.properties * In order for these tests to run you must set the following properties in test-gitlab4j.properties
* *
* TEST_NAMESPACE
* TEST_PROJECT_NAME
* TEST_HOST_URL * TEST_HOST_URL
* TEST_PRIVATE_TOKEN * TEST_PRIVATE_TOKEN
* *
* If any of the above are NULL, all tests in this class will be skipped. * If any of the above are NULL, all tests in this class will be skipped.
* *
* NOTE: &amp;FixMethodOrder(MethodSorters.NAME_ASCENDING) is very important to insure that testCreate() is executed first.
*/ */
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
public class TestGitLabApi { public class TestGitLabApi {
// The following needs to be set to your test repository // The following needs to be set to your test repository
private static final String TEST_PROJECT_NAME;
private static final String TEST_NAMESPACE;
private static final String TEST_HOST_URL; private static final String TEST_HOST_URL;
private static final String TEST_PRIVATE_TOKEN; private static final String TEST_PRIVATE_TOKEN;
static { static {
TEST_NAMESPACE = TestUtils.getProperty("TEST_NAMESPACE");
TEST_PROJECT_NAME = TestUtils.getProperty("TEST_PROJECT_NAME");
TEST_HOST_URL = TestUtils.getProperty("TEST_HOST_URL"); TEST_HOST_URL = TestUtils.getProperty("TEST_HOST_URL");
TEST_PRIVATE_TOKEN = TestUtils.getProperty("TEST_PRIVATE_TOKEN"); TEST_PRIVATE_TOKEN = TestUtils.getProperty("TEST_PRIVATE_TOKEN");
} }
private static final String TEST_BRANCH_NAME = "feature/test_branch";
private static GitLabApi gitLabApi; private static GitLabApi gitLabApi;
public TestGitLabApi() { public TestGitLabApi() {
...@@ -62,14 +38,6 @@ public class TestGitLabApi { ...@@ -62,14 +38,6 @@ public class TestGitLabApi {
public static void setup() { public static void setup() {
String problems = ""; String problems = "";
if (TEST_NAMESPACE == null || TEST_NAMESPACE.trim().length() == 0) {
problems += "TEST_NAMESPACE cannot be empty\n";
}
if (TEST_PROJECT_NAME == null || TEST_PROJECT_NAME.trim().length() == 0) {
problems += "TEST_PROJECT_NAME cannot be empty\n";
}
if (TEST_HOST_URL == null || TEST_HOST_URL.trim().length() == 0) { if (TEST_HOST_URL == null || TEST_HOST_URL.trim().length() == 0) {
problems += "TEST_HOST_URL cannot be empty\n"; problems += "TEST_HOST_URL cannot be empty\n";
} }
...@@ -85,18 +53,6 @@ public class TestGitLabApi { ...@@ -85,18 +53,6 @@ public class TestGitLabApi {
} }
} }
@AfterClass
public static void teardown() throws GitLabApiException {
if (gitLabApi != null) {
try {
Project project = gitLabApi.getProjectApi().getProject(TEST_NAMESPACE, TEST_PROJECT_NAME);
gitLabApi.getRepositoryApi().deleteBranch(project.getId(), TEST_BRANCH_NAME);
} catch (GitLabApiException ignore) {
}
}
}
@Before @Before
public void beforeMethod() { public void beforeMethod() {
assumeTrue(gitLabApi != null); assumeTrue(gitLabApi != null);
...@@ -110,125 +66,4 @@ public class TestGitLabApi { ...@@ -110,125 +66,4 @@ public class TestGitLabApi {
assertNotNull(version.getVersion()); assertNotNull(version.getVersion());
assertNotNull(version.getRevision()); assertNotNull(version.getRevision());
} }
@Test
public void testProjects() throws GitLabApiException {
List<Project> projects = gitLabApi.getProjectApi().getProjects();
assertTrue(projects != null);
}
@Test
public void testProjectPerPage() throws GitLabApiException {
List<Project> projects = gitLabApi.getProjectApi().getProjects(1, 10);
assertNotNull(projects);
assertEquals(10, projects.size());
}
@Test
public void testOwnedProjects() throws GitLabApiException {
List<Project> projects = gitLabApi.getProjectApi().getOwnedProjects();
assertTrue(projects != null);
}
@Test
public void testOwnedProjectsPerPage() throws GitLabApiException {
List<Project> projects = gitLabApi.getProjectApi().getOwnedProjects(1, 10);
assertTrue(projects != null);
assertTrue(projects.size() > 0);
}
@Test
public void testMemberProjects() throws GitLabApiException {
List<Project> projects = gitLabApi.getProjectApi().getMemberProjects();
assertTrue(projects != null);
}
@Test
public void testMemberProjectsPerPage() throws GitLabApiException {
List<Project> projects = gitLabApi.getProjectApi().getMemberProjects(1, 10);
assertTrue(projects != null);
assertTrue(projects.size() > 0);
}
@Test
public void testCreateBranch() throws GitLabApiException {
Project project = gitLabApi.getProjectApi().getProject(TEST_NAMESPACE, TEST_PROJECT_NAME);
assertNotNull(project);
Branch branch = gitLabApi.getRepositoryApi().createBranch(project.getId(), TEST_BRANCH_NAME, "master");
assertNotNull(branch);
Branch fetchedBranch = gitLabApi.getRepositoryApi().getBranch(project.getId(), TEST_BRANCH_NAME);
assertNotNull(fetchedBranch);
assertEquals(branch.getName(), fetchedBranch.getName());
}
@Test
public void testDeleteBranch() throws GitLabApiException {
Project project = gitLabApi.getProjectApi().getProject(TEST_NAMESPACE, TEST_PROJECT_NAME);
assertNotNull(project);
gitLabApi.getRepositoryApi().deleteBranch(project.getId(), TEST_BRANCH_NAME);
}
@Test
public void testRepositoryArchiveViaInputStream() throws GitLabApiException, IOException {
Project project = gitLabApi.getProjectApi().getProject(TEST_NAMESPACE, TEST_PROJECT_NAME);
assertNotNull(project);
InputStream in = gitLabApi.getRepositoryApi().getRepositoryArchive(project.getId(), "master");
Path target = Files.createTempFile(TEST_PROJECT_NAME + "-", ".tar.gz");
Files.copy(in, target, StandardCopyOption.REPLACE_EXISTING);
assertTrue(target.toFile().length() > 0);
Files.delete(target);
}
@Test
public void testRepositoryArchiveViaFile() throws GitLabApiException, IOException {
Project project = gitLabApi.getProjectApi().getProject(TEST_NAMESPACE, TEST_PROJECT_NAME);
assertNotNull(project);
File file = gitLabApi.getRepositoryApi().getRepositoryArchive(project.getId(), "master", null);
assertTrue(file.length() > 0);
file.delete();
file = gitLabApi.getRepositoryApi().getRepositoryArchive(project.getId(), "master", new File("."));
assertTrue(file.length() > 0);
file.delete();
}
@Test
public void testRawFileViaFile() throws GitLabApiException, IOException {
Project project = gitLabApi.getProjectApi().getProject(TEST_NAMESPACE, TEST_PROJECT_NAME);
assertNotNull(project);
File file = gitLabApi.getRepositoryFileApi().getRawFile(project.getId(), "master", "README", null);
assertTrue(file.length() > 0);
file.delete();
file = gitLabApi.getRepositoryFileApi().getRawFile(project.getId(), "master", "README", new File("."));
assertTrue(file.length() > 0);
file.delete();
}
@Test
public void testRepositoryFileViaInputStream() throws GitLabApiException, IOException {
Project project = gitLabApi.getProjectApi().getProject(TEST_NAMESPACE, TEST_PROJECT_NAME);
assertNotNull(project);
InputStream in = gitLabApi.getRepositoryFileApi().getRawFile(project.getId(), "master", "README");
Path target = Files.createTempFile(TEST_PROJECT_NAME + "-README", "");
Files.copy(in, target, StandardCopyOption.REPLACE_EXISTING);
assertTrue(target.toFile().length() > 0);
Files.delete(target);
}
} }
...@@ -9,6 +9,7 @@ import java.util.List; ...@@ -9,6 +9,7 @@ import java.util.List;
import org.gitlab4j.api.GitLabApi; import org.gitlab4j.api.GitLabApi;
import org.gitlab4j.api.models.Branch; import org.gitlab4j.api.models.Branch;
import org.gitlab4j.api.models.Commit; import org.gitlab4j.api.models.Commit;
import org.gitlab4j.api.models.CompareResults;
import org.gitlab4j.api.models.Diff; import org.gitlab4j.api.models.Diff;
import org.gitlab4j.api.models.Event; import org.gitlab4j.api.models.Event;
import org.gitlab4j.api.models.Group; import org.gitlab4j.api.models.Group;
...@@ -78,6 +79,17 @@ public class TestGitLabApiBeans { ...@@ -78,6 +79,17 @@ public class TestGitLabApiBeans {
} }
} }
@Test
public void testCompareResults() {
try {
CompareResults compareResults = makeFakeApiCall(CompareResults.class, "compare-results");
assertTrue(compareJson(compareResults, "compare-results"));
} catch (Exception e) {
e.printStackTrace();
}
}
@Test @Test
public void testDiff() { public void testDiff() {
......
...@@ -274,4 +274,42 @@ public class TestProjectApi { ...@@ -274,4 +274,42 @@ public class TestProjectApi {
gitLabApi.getProjectApi().deleteProject(project); gitLabApi.getProjectApi().deleteProject(project);
} }
@Test
public void testProjects() throws GitLabApiException {
List<Project> projects = gitLabApi.getProjectApi().getProjects();
assertTrue(projects != null);
}
@Test
public void testProjectPerPage() throws GitLabApiException {
List<Project> projects = gitLabApi.getProjectApi().getProjects(1, 10);
assertNotNull(projects);
assertEquals(10, projects.size());
}
@Test
public void testOwnedProjects() throws GitLabApiException {
List<Project> projects = gitLabApi.getProjectApi().getOwnedProjects();
assertTrue(projects != null);
}
@Test
public void testOwnedProjectsPerPage() throws GitLabApiException {
List<Project> projects = gitLabApi.getProjectApi().getOwnedProjects(1, 10);
assertTrue(projects != null);
assertTrue(projects.size() > 0);
}
@Test
public void testMemberProjects() throws GitLabApiException {
List<Project> projects = gitLabApi.getProjectApi().getMemberProjects();
assertTrue(projects != null);
}
@Test
public void testMemberProjectsPerPage() throws GitLabApiException {
List<Project> projects = gitLabApi.getProjectApi().getMemberProjects(1, 10);
assertTrue(projects != null);
assertTrue(projects.size() > 0);
}
} }
package org.gitlab4j.api;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assume.assumeTrue;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardCopyOption;
import java.util.List;
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.Project;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.FixMethodOrder;
import org.junit.Test;
import org.junit.runners.MethodSorters;
/**
* In order for these tests to run you must set the following properties in test-gitlab4j.properties
*
* TEST_NAMESPACE
* TEST_PROJECT_NAME
* TEST_HOST_URL
* TEST_PRIVATE_TOKEN
*
* If any of the above are NULL, all tests in this class will be skipped.
*
* NOTE: &amp;FixMethodOrder(MethodSorters.NAME_ASCENDING) is very important to insure that testCreate() is executed first.
*/
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
public class TestRepositoryApi {
// The following needs to be set to your test repository
private static final String TEST_PROJECT_NAME;
private static final String TEST_NAMESPACE;
private static final String TEST_HOST_URL;
private static final String TEST_PRIVATE_TOKEN;
static {
TEST_NAMESPACE = TestUtils.getProperty("TEST_NAMESPACE");
TEST_PROJECT_NAME = TestUtils.getProperty("TEST_PROJECT_NAME");
TEST_HOST_URL = TestUtils.getProperty("TEST_HOST_URL");
TEST_PRIVATE_TOKEN = TestUtils.getProperty("TEST_PRIVATE_TOKEN");
}
private static final String TEST_BRANCH_NAME = "feature/test_branch";
private static GitLabApi gitLabApi;
public TestRepositoryApi() {
super();
}
@BeforeClass
public static void setup() {
String problems = "";
if (TEST_NAMESPACE == null || TEST_NAMESPACE.trim().length() == 0) {
problems += "TEST_NAMESPACE cannot be empty\n";
}
if (TEST_PROJECT_NAME == null || TEST_PROJECT_NAME.trim().length() == 0) {
problems += "TEST_PROJECT_NAME cannot be empty\n";
}
if (TEST_HOST_URL == null || TEST_HOST_URL.trim().length() == 0) {
problems += "TEST_HOST_URL cannot be empty\n";
}
if (TEST_PRIVATE_TOKEN == null || TEST_PRIVATE_TOKEN.trim().length() == 0) {
problems += "TEST_PRIVATE_TOKEN cannot be empty\n";
}
if (problems.isEmpty()) {
gitLabApi = new GitLabApi(ApiVersion.V4, TEST_HOST_URL, TEST_PRIVATE_TOKEN);
} else {
System.err.print(problems);
}
}
@AfterClass
public static void teardown() throws GitLabApiException {
if (gitLabApi != null) {
try {
Project project = gitLabApi.getProjectApi().getProject(TEST_NAMESPACE, TEST_PROJECT_NAME);
gitLabApi.getRepositoryApi().deleteBranch(project.getId(), TEST_BRANCH_NAME);
} catch (GitLabApiException ignore) {
}
}
}
@Before
public void beforeMethod() {
assumeTrue(gitLabApi != null);
}
@Test
public void testCreateBranch() throws GitLabApiException {
Project project = gitLabApi.getProjectApi().getProject(TEST_NAMESPACE, TEST_PROJECT_NAME);
assertNotNull(project);
Branch branch = gitLabApi.getRepositoryApi().createBranch(project.getId(), TEST_BRANCH_NAME, "master");
assertNotNull(branch);
Branch fetchedBranch = gitLabApi.getRepositoryApi().getBranch(project.getId(), TEST_BRANCH_NAME);
assertNotNull(fetchedBranch);
assertEquals(branch.getName(), fetchedBranch.getName());
}
@Test
public void testDeleteBranch() throws GitLabApiException {
Project project = gitLabApi.getProjectApi().getProject(TEST_NAMESPACE, TEST_PROJECT_NAME);
assertNotNull(project);
gitLabApi.getRepositoryApi().deleteBranch(project.getId(), TEST_BRANCH_NAME);
}
@Test
public void testRepositoryArchiveViaInputStream() throws GitLabApiException, IOException {
Project project = gitLabApi.getProjectApi().getProject(TEST_NAMESPACE, TEST_PROJECT_NAME);
assertNotNull(project);
InputStream in = gitLabApi.getRepositoryApi().getRepositoryArchive(project.getId(), "master");
Path target = Files.createTempFile(TEST_PROJECT_NAME + "-", ".tar.gz");
Files.copy(in, target, StandardCopyOption.REPLACE_EXISTING);
assertTrue(target.toFile().length() > 0);
Files.delete(target);
}
@Test
public void testRepositoryArchiveViaFile() throws GitLabApiException, IOException {
Project project = gitLabApi.getProjectApi().getProject(TEST_NAMESPACE, TEST_PROJECT_NAME);
assertNotNull(project);
File file = gitLabApi.getRepositoryApi().getRepositoryArchive(project.getId(), "master", null);
assertTrue(file.length() > 0);
file.delete();
file = gitLabApi.getRepositoryApi().getRepositoryArchive(project.getId(), "master", new File("."));
assertTrue(file.length() > 0);
file.delete();
}
@Test
public void testRawFileViaFile() throws GitLabApiException, IOException {
Project project = gitLabApi.getProjectApi().getProject(TEST_NAMESPACE, TEST_PROJECT_NAME);
assertNotNull(project);
File file = gitLabApi.getRepositoryFileApi().getRawFile(project.getId(), "master", "README", null);
assertTrue(file.length() > 0);
file.delete();
file = gitLabApi.getRepositoryFileApi().getRawFile(project.getId(), "master", "README", new File("."));
assertTrue(file.length() > 0);
file.delete();
}
@Test
public void testRepositoryFileViaInputStream() throws GitLabApiException, IOException {
Project project = gitLabApi.getProjectApi().getProject(TEST_NAMESPACE, TEST_PROJECT_NAME);
assertNotNull(project);
InputStream in = gitLabApi.getRepositoryFileApi().getRawFile(project.getId(), "master", "README");
Path target = Files.createTempFile(TEST_PROJECT_NAME + "-README", "");
Files.copy(in, target, StandardCopyOption.REPLACE_EXISTING);
assertTrue(target.toFile().length() > 0);
Files.delete(target);
}
@Test
public void testCompare() throws GitLabApiException {
Project project = gitLabApi.getProjectApi().getProject(TEST_NAMESPACE, TEST_PROJECT_NAME);
assertNotNull(project);
List<Commit> commits = gitLabApi.getCommitsApi().getCommits(project.getId());
assertNotNull(commits);
assertTrue(commits.size() > 1);
int numCommits = commits.size();
CompareResults compareResults = gitLabApi.getRepositoryApi().compare(project.getId(), commits.get(numCommits - 1).getId(), commits.get(numCommits - 2).getId());
assertNotNull(compareResults);
compareResults = gitLabApi.getRepositoryApi().compare(TEST_NAMESPACE + "/" + TEST_PROJECT_NAME, commits.get(numCommits - 1).getId(), commits.get(numCommits - 2).getId());
assertNotNull(compareResults);
}
}
{
"commit": {
"id": "12d65c8dd2b2676fa3ac47d955accc085a37a9c1",
"short_id": "12d65c8dd2b",
"title": "JS fix",
"author_name": "Dmitriy Zaporozhets",
"author_email": "dmitriy.zaporozhets@gmail.com",
"created_at": "2014-02-27T10:27:00Z"
},
"commits": [{
"id": "12d65c8dd2b2676fa3ac47d955accc085a37a9c1",
"short_id": "12d65c8dd2b",
"title": "JS fix",
"author_name": "Dmitriy Zaporozhets",
"author_email": "dmitriy.zaporozhets@gmail.com",
"created_at": "2014-02-27T10:27:00Z"
}],
"diffs": [{
"old_path": "files/js/application.js",
"new_path": "files/js/application.js",
"a_mode": null,
"b_mode": "100644",
"diff": "--- a/files/js/application.js\n+++ b/files/js/application.js\n@@ -24,8 +24,10 @@\n //= require g.raphael-min\n //= require g.bar-min\n //= require branch-graph\n-//= require highlightjs.min\n-//= require ace/ace\n //= require_tree .\n //= require d3\n //= require underscore\n+\n+function fix() { \n+ alert(\"Fixed\")\n+}",
"new_file": false,
"renamed_file": false,
"deleted_file": false
}],
"compare_timeout": false,
"compare_same_ref": false
}
\ No newline at end of file
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