Commit c64bf6c1 authored by Tim Olshansky's avatar Tim Olshansky

Merge pull request #11 from adamretter/master

Features for Getting Groups, Group Membersm Creating Groups and Creating Projects
parents 5639651b 284c6e98
......@@ -22,6 +22,13 @@
<email>tim.olshansky@gmail.com</email>
</developer>
</developers>
<contributors>
<contributor>
<name>Adam Retter</name>
<email>adam.retter@googlemail.com</email>
<organization>Evolved Binary Ltd</organization>
</contributor>
</contributors>
<licenses>
<license>
......
......@@ -10,17 +10,8 @@ import java.util.List;
import org.codehaus.jackson.map.DeserializationConfig;
import org.codehaus.jackson.map.ObjectMapper;
import org.gitlab.api.http.GitlabHTTPRequestor;
import org.gitlab.api.models.GitlabBranch;
import org.gitlab.api.models.GitlabCommit;
import org.gitlab.api.models.GitlabIssue;
import org.gitlab.api.models.GitlabMergeRequest;
import org.gitlab.api.models.GitlabMilestone;
import org.gitlab.api.models.GitlabNamespace;
import org.gitlab.api.models.GitlabNote;
import org.gitlab.api.models.GitlabProject;
import org.gitlab.api.models.GitlabProjectHook;
import org.gitlab.api.models.GitlabProjectMember;
import org.gitlab.api.models.GitlabSession;
import org.gitlab.api.http.Query;
import org.gitlab.api.models.*;
/**
* Gitlab API Wrapper class
......@@ -86,6 +77,87 @@ public class GitlabAPI {
return new URL(_hostUrl + tailAPIUrl);
}
public GitlabGroup getGroup(Integer groupId) throws IOException {
String tailUrl = GitlabGroup.URL + "/" + groupId;
return retrieve().to(tailUrl, GitlabGroup.class);
}
public List<GitlabGroup> getGroups() throws IOException {
String tailUrl = GitlabGroup.URL;
return retrieve().getAll(tailUrl, GitlabGroup[].class);
}
/**
* Gets all members of a Group
*
* @param group The GitLab Group
*
* @return The Group Members
*/
public List<GitlabGroupMember> getGroupMembers(GitlabGroup group) throws IOException {
return getGroupMembers(group.getId());
}
/**
* Gets all members of a Group
*
* @param groupId The id of the GitLab Group
*
* @return The Group Members
*/
public List<GitlabGroupMember> getGroupMembers(Integer groupId) throws IOException {
String tailUrl = GitlabGroup.URL + "/" + groupId + GitlabGroupMember.URL;
return Arrays.asList(retrieve().to(tailUrl, GitlabGroupMember[].class));
}
/**
* Creates a Group
*
* @param name The name of the group. The
* name will also be used as the path
* of the group.
*
* @return The GitLab Group
*/
public GitlabGroup createGroup(String name) throws IOException {
return createGroup(name, name);
}
/**
* Creates a Group
*
* @param name The name of the group
* @param path The path for the group
*
* @return The GitLab Group
*/
public GitlabGroup createGroup(String name, String path) throws IOException {
return createGroup(name, path, null, null);
}
/**
* Creates a Group
*
* @param name The name of the group
* @param path The path for the group
* @param ldapCn LDAP Group Name to sync with, null otherwise
* @param ldapAccess Access level for LDAP group members, null otherwise
*
* @return The GitLab Group
*/
public GitlabGroup createGroup(String name, String path, String ldapCn, GitlabAccessLevel ldapAccess) throws IOException {
Query query = new Query()
.append("name", name)
.append("path", path)
.appendIf("ldap_cn", ldapCn)
.appendIf("ldap_access", ldapAccess);
String tailUrl = GitlabGroup.URL + query.toString();
return dispatch().to(tailUrl, GitlabGroup.class);
}
public GitlabProject getProject(Integer projectId) throws IOException {
String tailUrl = GitlabProject.URL + "/" + projectId;
return retrieve().to(tailUrl, GitlabProject.class);
......@@ -97,9 +169,103 @@ public class GitlabAPI {
}
public List<GitlabProject> getAllProjects() throws IOException {
String tailUrl = GitlabProject.URL;
String tailUrl = GitlabProject.URL + "/all";
return retrieve().getAll(tailUrl, GitlabProject[].class);
}
/**
* Creates a private Project
*
* @param name The name of the project
*
* @return The GitLab Project
*/
public GitlabProject createProject(String name) throws IOException {
return createProject(name, null, null, null, null, null, null, null, null, null, null);
}
/**
* Creates a Project
*
* @param name The name of the project
* @param namespaceId The Namespace for the new project, otherwise null indicates to use the GitLab default (user)
* @param description A description for the project, null otherwise
* @param issuesEnabled Whether Issues should be enabled, otherwise null indicates to use GitLab default
* @param wallEnabled Whether The Wall should be enabled, otherwise null indicates to use GitLab default
* @param mergeRequestsEnabled Whether Merge Requests should be enabled, otherwise null indicates to use GitLab default
* @param wikiEnabled Whether a Wiki should be enabled, otherwise null indicates to use GitLab default
* @param snippetsEnabled Whether Snippets should be enabled, otherwise null indicates to use GitLab default
* @param publik Whether the project is public or private, if true same as setting visibilityLevel = 20, otherwise null indicates to use GitLab default
* @param visibilityLevel The visibility level of the project, otherwise null indicates to use GitLab default
* @param importUrl The Import URL for the project, otherwise null
*
* @return the Gitlab Project
*/
public GitlabProject createProject(String name, Integer namespaceId, String description, Boolean issuesEnabled, Boolean wallEnabled, Boolean mergeRequestsEnabled, Boolean wikiEnabled, Boolean snippetsEnabled, Boolean publik, Integer visibilityLevel, String importUrl) throws IOException {
Query query = new Query()
.append("name", name)
.appendIf("namespace_id", namespaceId)
.appendIf("description", description)
.appendIf("issues_enabled", issuesEnabled)
.appendIf("wall_enabled", wallEnabled)
.appendIf("merge_requests_enabled", mergeRequestsEnabled)
.appendIf("wiki_enabled", wikiEnabled)
.appendIf("snippets_enabled", snippetsEnabled)
.appendIf("public", publik)
.appendIf("visibility_level", visibilityLevel)
.appendIf("import_url", importUrl);
String tailUrl = GitlabProject.URL + query.toString();
return dispatch().to(tailUrl, GitlabProject.class);
}
/**
* Creates a Project for a specific User
*
* @param userId The id of the user to create the project for
* @param name The name of the project
*
* @return The GitLab Project
*/
public GitlabProject createUserProject(Integer userId, String name) throws IOException {
return createUserProject(userId, name, null, null, null, null, null, null, null, null, null);
}
/**
* Creates a Project for a specific User
*
* @param userId The id of the user to create the project for
* @param name The name of the project
* @param description A description for the project, null otherwise
* @param defaultBranch The default branch for the project, otherwise null indicates to use GitLab default (master)
* @param issuesEnabled Whether Issues should be enabled, otherwise null indicates to use GitLab default
* @param wallEnabled Whether The Wall should be enabled, otherwise null indicates to use GitLab default
* @param mergeRequestsEnabled Whether Merge Requests should be enabled, otherwise null indicates to use GitLab default
* @param wikiEnabled Whether a Wiki should be enabled, otherwise null indicates to use GitLab default
* @param snippetsEnabled Whether Snippets should be enabled, otherwise null indicates to use GitLab default
* @param publik Whether the project is public or private, if true same as setting visibilityLevel = 20, otherwise null indicates to use GitLab default
* @param visibilityLevel The visibility level of the project, otherwise null indicates to use GitLab default
*
* @return The GitLab Project
*/
public GitlabProject createUserProject(Integer userId, String name, String description, String defaultBranch, Boolean issuesEnabled, Boolean wallEnabled, Boolean mergeRequestsEnabled, Boolean wikiEnabled, Boolean snippetsEnabled, Boolean publik, Integer visibilityLevel) throws IOException {
Query query = new Query()
.append("name", name)
.appendIf("description", description)
.appendIf("default_branch", defaultBranch)
.appendIf("issues_enabled", issuesEnabled)
.appendIf("wall_enabled", wallEnabled)
.appendIf("merge_requests_enabled", mergeRequestsEnabled)
.appendIf("wiki_enabled", wikiEnabled)
.appendIf("snippets_enabled", snippetsEnabled)
.appendIf("public", publik)
.appendIf("visibility_level", visibilityLevel);
String tailUrl = GitlabProject.URL + "/user/" + userId + query.toString();
return dispatch().to(tailUrl, GitlabProject.class);
}
public List<GitlabMergeRequest> getOpenMergeRequests(GitlabProject project) throws IOException {
List<GitlabMergeRequest> allMergeRequests = getAllMergeRequests(project);
......@@ -158,8 +324,12 @@ public class GitlabAPI {
if (projectId == null) {
projectId = mergeRequest.getProjectId();
}
Query query = new Query()
.append("ref_name", mergeRequest.getSourceBranch());
String tailUrl = GitlabProject.URL + "/" + projectId +
"/repository" + GitlabCommit.URL + "?ref_name=" + mergeRequest.getSourceBranch();
"/repository" + GitlabCommit.URL + query.toString();
GitlabCommit[] commits = retrieve().to(tailUrl, GitlabCommit[].class);
return Arrays.asList(commits);
......@@ -207,12 +377,18 @@ public class GitlabAPI {
}
public GitlabProjectHook addProjectHook(GitlabProject project, String url) throws IOException {
String tailUrl = GitlabProject.URL + "/" + project.getId() + GitlabProjectHook.URL + "?url=" + URLEncoder.encode(url, "UTF-8");
Query query = new Query()
.append("url", url);
String tailUrl = GitlabProject.URL + "/" + project.getId() + GitlabProjectHook.URL + query.toString();
return dispatch().to(tailUrl, GitlabProjectHook.class);
}
public GitlabProjectHook editProjectHook(GitlabProject project, String hookId, String url) throws IOException {
String tailUrl = GitlabProject.URL + "/" + project.getId() + GitlabProjectHook.URL + "/" + hookId + "?url=" + URLEncoder.encode(url, "UTF-8");
Query query = new Query()
.append("url", url);
String tailUrl = GitlabProject.URL + "/" + project.getId() + GitlabProjectHook.URL + "/" + hookId + query.toString();
return retrieve().method("PUT").to(tailUrl, GitlabProjectHook.class);
}
......
......@@ -291,7 +291,7 @@ public class GitlabHTTPRequestor {
return null;
}
} catch (SSLHandshakeException e) {
throw new SSLHandshakeException("You can disable certificate checking by setting ignoreCertificateErrors on GitlabHTTPRequestor");
throw new SSLHandshakeException("You can disable certificate checking by setting ignoreCertificateErrors on GitlabHTTPRequestor. SSL Error: " + e.getMessage());
} finally {
IOUtils.closeQuietly(reader);
}
......
package org.gitlab.api.http;
import org.gitlab.api.models.GitlabAccessLevel;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;
/**
* Models the Query
* aspect of a URL
*/
public class Query {
private class Tuple<T1, T2> {
T1 _1;
T2 _2;
public Tuple(T1 _1, T2 _2) {
this._1 = _1;
this._2 = _2;
}
}
/**
* The type of params is:
* Tuple<name, Tuple<value, URLEncoder.encode(value, "UTF-8")>>
*/
private final List<Tuple<String, Tuple<String, String>>> params = new ArrayList<Tuple<String, Tuple<String, String>>>();
/**
* Appends a parameter to the query
*
* @param name Parameter name
* @param value Parameter value
*
* @throws java.io.UnsupportedEncodingException If the provided value cannot be URL Encoded
*/
public Query append(final String name, final String value) throws UnsupportedEncodingException {
params.add(new Tuple(name, new Tuple(value, URLEncoder.encode(value, "UTF-8"))));
return this;
}
/**
* Conditionally append a parameter to the query
* if the value of the parameter is not null
*
* @param name Parameter name
* @param value Parameter value
*
* @throws java.io.UnsupportedEncodingException If the provided value cannot be URL Encoded
*/
public Query appendIf(final String name, final String value) throws UnsupportedEncodingException {
if(value != null) {
append(name, value);
}
return this;
}
/**
* Conditionally append a parameter to the query
* if the value of the parameter is not null
*
* @param name Parameter name
* @param value Parameter value
*
* @throws java.io.UnsupportedEncodingException If the provided value cannot be URL Encoded
*/
public Query appendIf(final String name, final Integer value) throws UnsupportedEncodingException {
if(value != null) {
append(name, value.toString());
}
return this;
}
/**
* Conditionally append a parameter to the query
* if the value of the parameter is not null
*
* @param name Parameter name
* @param value Parameter value
*
* @throws java.io.UnsupportedEncodingException If the provided value cannot be URL Encoded
*/
public Query appendIf(final String name, final Boolean value) throws UnsupportedEncodingException {
if(value != null) {
append(name, value.toString());
}
return this;
}
/**
* Conditionally append a parameter to the query
* if the value of the parameter is not null
*
* @param name Parameter name
* @param value Parameter value
*
* @throws java.io.UnsupportedEncodingException If the provided value cannot be URL Encoded
*/
public Query appendIf(final String name, final GitlabAccessLevel value) throws UnsupportedEncodingException {
if(value != null) {
append(name, Integer.toString(value.accessValue));
}
return this;
}
/**
* Returns a Query suitable for appending
* to a URI
*/
@Override
public String toString() {
final StringBuilder builder = new StringBuilder();
for(final Tuple<String, Tuple<String, String>> param : params) {
if(builder.length() == 0) {
builder.append('?');
} else {
builder.append('&');
}
builder.append(param._1);
builder.append('=');
builder.append(param._2._2);
}
return builder.toString();
}
}
package org.gitlab.api.models;
import org.codehaus.jackson.annotate.JsonProperty;
public abstract class GitlabAbstractMember extends GitlabUser {
public static final String URL = "/members";
@JsonProperty("access_level")
private int _accessLevel;
public GitlabAccessLevel getAccessLevel() {
return GitlabAccessLevel.fromAccessValue(_accessLevel);
}
public void setAccessLevel(GitlabAccessLevel accessLevel) {
_accessLevel = accessLevel.accessValue;
}
}
package org.gitlab.api.models;
public enum GitlabAccessLevel {
Guest(10),
Reporter(20),
Developer(30),
Master(40),
Owner(50);
public final int accessValue;
GitlabAccessLevel(int accessValue) {
this.accessValue = accessValue;
}
public static GitlabAccessLevel fromAccessValue(final int accessValue) throws IllegalArgumentException {
for(final GitlabAccessLevel gitlabAccessLevel : GitlabAccessLevel.values()) {
if(gitlabAccessLevel.accessValue == accessValue) {
return gitlabAccessLevel;
}
}
throw new IllegalArgumentException("No GitLab Access Level enum constant with access value: " + accessValue);
}
}
\ No newline at end of file
package org.gitlab.api.models;
import org.codehaus.jackson.annotate.JsonProperty;
public class GitlabGroup {
public static final String URL = "/groups";
private Integer _id;
private String _name;
private String _path;
@JsonProperty("ldap_cn")
private String _ldapCn;
@JsonProperty("ldap_access")
private Integer _ldapAccess;
public Integer getId() {
return _id;
}
public void setId(Integer id) {
_id = id;
}
public String getName() {
return _name;
}
public void setName(String name) {
_name = name;
}
public String getPath() {
return _path;
}
public void setPath(String path) {
_path = path;
}
public String getLdapCn() {
return _ldapCn;
}
public void setLdapCn(String ldapCn) {
this._ldapCn = ldapCn;
}
public GitlabAccessLevel getLdapAccess() {
return GitlabAccessLevel.fromAccessValue(_ldapAccess);
}
public void setLdapAccess(GitlabAccessLevel ldapGitlabAccessLevel) {
this._ldapAccess = ldapGitlabAccessLevel.accessValue;
}
}
package org.gitlab.api.models;
public class GitlabGroupMember extends GitlabAbstractMember {
}
......@@ -9,6 +9,10 @@ public class GitlabProject {
private Integer _id;
private String _name;
@JsonProperty("name_with_namespace")
private String _nameWithNamespace;
private String _description;
@JsonProperty("default_branch")
......@@ -18,6 +22,9 @@ public class GitlabProject {
private boolean _public;
private String _path;
@JsonProperty("visibility_level")
private Integer _visibilityLevel;
@JsonProperty("path_with_namespace")
private String _pathWithNamespace;
......@@ -27,6 +34,9 @@ public class GitlabProject {
@JsonProperty("merge_requests_enabled")
private boolean _mergeRequestsEnabled;
@JsonProperty("snippets_enabled")
private boolean _snippetsEnabled;
@JsonProperty("wall_enabled")
private boolean _wallEnabled;
......@@ -60,6 +70,14 @@ public class GitlabProject {
_name = name;
}
public String getNameWithNamespace() {
return _nameWithNamespace;
}
public void setNameWithNamespace(String nameWithNamespace) {
this._nameWithNamespace = nameWithNamespace;
}
public String getDescription() {
return _description;
}
......@@ -76,6 +94,14 @@ public class GitlabProject {
_defaultBranch = defaultBranch;
}
public Integer getVisibilityLevel() {
return _visibilityLevel;
}
public void setVisibilityLevel(Integer visibilityLevel) {
this._visibilityLevel = visibilityLevel;
}
public GitlabUser getOwner() {
return _owner;
}
......@@ -116,6 +142,14 @@ public class GitlabProject {
_mergeRequestsEnabled = mergeRequestsEnabled;
}
public boolean isSnippetsEnabled() {
return _snippetsEnabled;
}
public void setSnippetsEnabled(boolean snippetsEnabled) {
this._snippetsEnabled = snippetsEnabled;
}
public boolean isWallEnabled() {
return _wallEnabled;
}
......
package org.gitlab.api.models;
public class GitlabProjectMember extends GitlabUser {
public static final String URL = "/members";
private int _accessLevel;
public int getAccessLevel() {
return _accessLevel;
}
public void setAccessLevel(int accessLevel) {
_accessLevel = accessLevel;
}
public class GitlabProjectMember extends GitlabAbstractMember {
}
package org.gitlab.api.http;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
import java.io.UnsupportedEncodingException;
public class QueryTest {
@Test
public void mutableStyle_append() throws UnsupportedEncodingException {
Query query = new Query();
query.append("p1", "v1");
query.append("p2", "v2");
assertEquals("?p1=v1&p2=v2", query.toString());
}
@Test
public void fluentStyle_append() throws UnsupportedEncodingException {
Query query = new Query()
.append("p1", "v1")
.append("p2", "v2");
assertEquals("?p1=v1&p2=v2", query.toString());
}
@Test
public void mixedStyle_append() throws UnsupportedEncodingException {
Query query = new Query()
.append("p1", "v1");
query.append("p2", "v2");
query = query.append("p3", "v3");
assertEquals("?p1=v1&p2=v2&p3=v3", query.toString());
}
@Test
public void conditionalAppend_notNull() throws UnsupportedEncodingException {
Query query = new Query()
.appendIf("p1", "v1")
.appendIf("p2", "v2");
assertEquals("?p1=v1&p2=v2", query.toString());
}
@Test
public void conditionalAppend_null() throws UnsupportedEncodingException {
Query query = new Query()
.appendIf("p1", (String) null);
assertEquals("", query.toString());
}
@Test
public void conditionalAppend_null_notNull() throws UnsupportedEncodingException {
Query query = new Query()
.appendIf("p1", (String)null)
.appendIf("p2", "v2");
assertEquals("?p2=v2", query.toString());
}
@Test
public void append_encodes_values() throws UnsupportedEncodingException {
Query query = new Query()
.append("p1", "v 1")
.append("p2", "v 2");
assertEquals("?p1=v+1&p2=v+2", query.toString());
}
}
Markdown is supported
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