Skip to content
GitLab
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in / Register
Toggle navigation
Menu
Open sidebar
佳 邓
Gitlab4j Api
Commits
aecdac90
Commit
aecdac90
authored
Mar 16, 2014
by
Greg Messner
Browse files
Refactored HTTP method implementations.
parent
2b2aaf0e
Changes
7
Hide whitespace changes
Inline
Side-by-side
src/main/java/com/messners/gitlab/api/AbstractApi.java
View file @
aecdac90
package
com.messners.gitlab.api
;
package
com.messners.gitlab.api
;
import
java.io.IOException
;
import
java.net.URL
;
import
java.net.URL
;
import
javax.ws.rs.core.MultivaluedMap
;
import
javax.ws.rs.core.MultivaluedMap
;
import
com.sun.jersey.api.client.ClientHandlerException
;
import
com.sun.jersey.api.client.ClientResponse
;
import
com.sun.jersey.api.client.ClientResponse
;
import
com.sun.jersey.api.client.UniformInterfaceException
;
import
com.sun.jersey.api.representation.Form
;
import
com.sun.jersey.api.representation.Form
;
public
abstract
class
AbstractApi
{
public
abstract
class
AbstractApi
{
...
@@ -28,16 +25,27 @@ public abstract class AbstractApi {
...
@@ -28,16 +25,27 @@ public abstract class AbstractApi {
* Perform an HTTP GET call with the specified query parameters and path objects, returning
* Perform an HTTP GET call with the specified query parameters and path objects, returning
* a ClientResponse instance with the data returned from the endpoint.
* a ClientResponse instance with the data returned from the endpoint.
*
*
* @param expectedStatus the HTTP status that should be returned from the server
* @param queryParams
* @param queryParams
* @param pathArgs
* @param pathArgs
* @return a ClientResponse instance with the data returned from the endpoint
* @return a ClientResponse instance with the data returned from the endpoint
* @throws UniformInterfaceException
* @throws GitLabApiException
* @throws ClientHandlerException
* @throws IOException
*/
*/
protected
ClientResponse
get
(
MultivaluedMap
<
String
,
String
>
queryParams
,
Object
...
pathArgs
)
protected
ClientResponse
get
(
ClientResponse
.
Status
expectedStatus
,
MultivaluedMap
<
String
,
String
>
queryParams
,
Object
...
pathArgs
)
throws
UniformInterfaceException
,
ClientHandlerException
,
IOException
{
throws
GitLabApiException
{
return
(
getApiClient
().
get
(
queryParams
,
pathArgs
));
try
{
ClientResponse
response
=
getApiClient
().
get
(
queryParams
,
pathArgs
);
if
(
response
.
getStatus
()
!=
expectedStatus
.
getStatusCode
())
{
throw
(
new
GitLabApiException
(
response
));
}
return
(
response
);
}
catch
(
Exception
e
)
{
throw
(
new
GitLabApiException
(
e
));
}
}
}
...
@@ -45,15 +53,27 @@ public abstract class AbstractApi {
...
@@ -45,15 +53,27 @@ public abstract class AbstractApi {
* Perform an HTTP GET call with the specified query parameters and URL, returning
* Perform an HTTP GET call with the specified query parameters and URL, returning
* a ClientResponse instance with the data returned from the endpoint.
* a ClientResponse instance with the data returned from the endpoint.
*
*
* @param expectedStatus the HTTP status that should be returned from the server
* @param queryParams
* @param queryParams
* @param url
* @param url
* @return a ClientResponse instance with the data returned from the endpoint
* @return a ClientResponse instance with the data returned from the endpoint
* @throws UniformInterfaceException
* @throws GitLabApiException
* @throws ClientHandlerException
*/
*/
protected
ClientResponse
get
(
MultivaluedMap
<
String
,
String
>
queryParams
,
URL
url
)
protected
ClientResponse
get
(
ClientResponse
.
Status
expectedStatus
,
MultivaluedMap
<
String
,
String
>
queryParams
,
URL
url
)
throws
UniformInterfaceException
,
ClientHandlerException
{
throws
GitLabApiException
{
return
(
getApiClient
().
get
(
queryParams
,
url
));
try
{
ClientResponse
response
=
getApiClient
().
get
(
queryParams
,
url
);
if
(
response
.
getStatus
()
!=
expectedStatus
.
getStatusCode
())
{
throw
(
new
GitLabApiException
(
response
));
}
return
(
response
);
}
catch
(
Exception
e
)
{
throw
(
new
GitLabApiException
(
e
));
}
}
}
...
@@ -61,16 +81,26 @@ public abstract class AbstractApi {
...
@@ -61,16 +81,26 @@ public abstract class AbstractApi {
* Perform an HTTP POST call with the specified form data and path objects, returning
* Perform an HTTP POST call with the specified form data and path objects, returning
* a ClientResponse instance with the data returned from the endpoint.
* a ClientResponse instance with the data returned from the endpoint.
*
*
* @param expectedStatus the HTTP status that should be returned from the server
* @param formData
* @param formData
* @param pathArgs
* @param pathArgs
* @return a ClientResponse instance with the data returned from the endpoint
* @return a ClientResponse instance with the data returned from the endpoint
* @throws UniformInterfaceException
* @throws GitLabApiException
* @throws ClientHandlerException
* @throws IOException
*/
*/
protected
ClientResponse
post
(
Form
formData
,
Object
...
pathArgs
)
protected
ClientResponse
post
(
ClientResponse
.
Status
expectedStatus
,
Form
formData
,
Object
...
pathArgs
)
throws
GitLabApiException
{
throws
UniformInterfaceException
,
ClientHandlerException
,
IOException
{
return
(
getApiClient
().
post
(
formData
,
pathArgs
));
try
{
ClientResponse
response
=
getApiClient
().
post
(
formData
,
pathArgs
);
if
(
response
.
getStatus
()
!=
expectedStatus
.
getStatusCode
())
{
throw
(
new
GitLabApiException
(
response
));
}
return
(
response
);
}
catch
(
Exception
e
)
{
throw
(
new
GitLabApiException
(
e
));
}
}
}
...
@@ -78,14 +108,26 @@ public abstract class AbstractApi {
...
@@ -78,14 +108,26 @@ public abstract class AbstractApi {
* Perform an HTTP POST call with the specified form data and URL, returning
* Perform an HTTP POST call with the specified form data and URL, returning
* a ClientResponse instance with the data returned from the endpoint.
* a ClientResponse instance with the data returned from the endpoint.
*
*
* @param expectedStatus the HTTP status that should be returned from the server
* @param formData
* @param formData
* @param url
* @param url
* @return a ClientResponse instance with the data returned from the endpoint
* @return a ClientResponse instance with the data returned from the endpoint
* @throws UniformInterfaceException
* @throws GitLabApiException
* @throws ClientHandlerException
*/
*/
protected
ClientResponse
post
(
Form
formData
,
URL
url
)
throws
UniformInterfaceException
,
ClientHandlerException
{
protected
ClientResponse
post
(
ClientResponse
.
Status
expectedStatus
,
Form
formData
,
URL
url
)
throws
GitLabApiException
{
return
(
getApiClient
().
post
(
formData
,
url
));
try
{
ClientResponse
response
=
getApiClient
().
post
(
formData
,
url
);
if
(
response
.
getStatus
()
!=
expectedStatus
.
getStatusCode
())
{
throw
(
new
GitLabApiException
(
response
));
}
return
(
response
);
}
catch
(
Exception
e
)
{
throw
(
new
GitLabApiException
(
e
));
}
}
}
...
@@ -94,16 +136,26 @@ public abstract class AbstractApi {
...
@@ -94,16 +136,26 @@ public abstract class AbstractApi {
* Perform an HTTP PUT call with the specified form data and path objects, returning
* Perform an HTTP PUT call with the specified form data and path objects, returning
* a ClientResponse instance with the data returned from the endpoint.
* a ClientResponse instance with the data returned from the endpoint.
*
*
* @param expectedStatus the HTTP status that should be returned from the server
* @param queryParams
* @param queryParams
* @param pathArgs
* @param pathArgs
* @return a ClientResponse instance with the data returned from the endpoint
* @return a ClientResponse instance with the data returned from the endpoint
* @throws UniformInterfaceException
* @throws GitLabApiException
* @throws ClientHandlerException
* @throws IOException
*/
*/
protected
ClientResponse
put
(
MultivaluedMap
<
String
,
String
>
queryParams
,
Object
...
pathArgs
)
protected
ClientResponse
put
(
ClientResponse
.
Status
expectedStatus
,
MultivaluedMap
<
String
,
String
>
queryParams
,
Object
...
pathArgs
)
throws
GitLabApiException
{
throws
UniformInterfaceException
,
ClientHandlerException
,
IOException
{
return
(
getApiClient
().
put
(
queryParams
,
pathArgs
));
try
{
ClientResponse
response
=
getApiClient
().
put
(
queryParams
,
pathArgs
);
if
(
response
.
getStatus
()
!=
expectedStatus
.
getStatusCode
())
{
throw
(
new
GitLabApiException
(
response
));
}
return
(
response
);
}
catch
(
Exception
e
)
{
throw
(
new
GitLabApiException
(
e
));
}
}
}
...
@@ -111,33 +163,54 @@ public abstract class AbstractApi {
...
@@ -111,33 +163,54 @@ public abstract class AbstractApi {
* Perform an HTTP PUT call with the specified form data and URL, returning
* Perform an HTTP PUT call with the specified form data and URL, returning
* a ClientResponse instance with the data returned from the endpoint.
* a ClientResponse instance with the data returned from the endpoint.
*
*
* @param expectedStatus the HTTP status that should be returned from the server
* @param queryParams
* @param queryParams
* @param url
* @param url
* @return a ClientResponse instance with the data returned from the endpoint
* @return a ClientResponse instance with the data returned from the endpoint
* @throws UniformInterfaceException
* @throws GitLabApiException
* @throws ClientHandlerException
*/
*/
protected
ClientResponse
put
(
MultivaluedMap
<
String
,
String
>
queryParams
,
URL
url
)
protected
ClientResponse
put
(
ClientResponse
.
Status
expectedStatus
,
MultivaluedMap
<
String
,
String
>
queryParams
,
URL
url
)
throws
GitLabApiException
{
throws
UniformInterfaceException
,
ClientHandlerException
{
return
(
getApiClient
().
put
(
queryParams
,
url
));
try
{
}
ClientResponse
response
=
getApiClient
().
put
(
queryParams
,
url
);
if
(
response
.
getStatus
()
!=
expectedStatus
.
getStatusCode
())
{
throw
(
new
GitLabApiException
(
response
));
}
return
(
response
);
}
catch
(
Exception
e
)
{
throw
(
new
GitLabApiException
(
e
));
}
}
/**
/**
* Perform an HTTP DELETE call with the specified form data and path objects, returning
* Perform an HTTP DELETE call with the specified form data and path objects, returning
* a ClientResponse instance with the data returned from the endpoint.
* a ClientResponse instance with the data returned from the endpoint.
*
*
* @param expectedStatus the HTTP status that should be returned from the server
* @param queryParams
* @param queryParams
* @param pathArgs
* @param pathArgs
* @return a ClientResponse instance with the data returned from the endpoint
* @return a ClientResponse instance with the data returned from the endpoint
* @throws UniformInterfaceException
* @throws GitLabApiException
* @throws ClientHandlerException
* @throws IOException
*/
*/
protected
ClientResponse
delete
(
MultivaluedMap
<
String
,
String
>
queryParams
,
Object
...
pathArgs
)
protected
ClientResponse
delete
(
ClientResponse
.
Status
expectedStatus
,
MultivaluedMap
<
String
,
String
>
queryParams
,
Object
...
pathArgs
)
throws
UniformInterfaceException
,
ClientHandlerException
,
IOException
{
throws
GitLabApiException
{
return
(
getApiClient
().
delete
(
queryParams
,
pathArgs
));
try
{
ClientResponse
response
=
getApiClient
().
delete
(
queryParams
,
pathArgs
);
if
(
response
.
getStatus
()
!=
expectedStatus
.
getStatusCode
())
{
throw
(
new
GitLabApiException
(
response
));
}
return
(
response
);
}
catch
(
Exception
e
)
{
throw
(
new
GitLabApiException
(
e
));
}
}
}
...
@@ -145,15 +218,37 @@ public abstract class AbstractApi {
...
@@ -145,15 +218,37 @@ public abstract class AbstractApi {
* Perform an HTTP DELETE call with the specified form data and URL, returning
* Perform an HTTP DELETE call with the specified form data and URL, returning
* a ClientResponse instance with the data returned from the endpoint.
* a ClientResponse instance with the data returned from the endpoint.
*
*
* @param expectedStatus the HTTP status that should be returned from the server
* @param queryParams
* @param queryParams
* @param url
* @param url
* @return a ClientResponse instance with the data returned from the endpoint
* @return a ClientResponse instance with the data returned from the endpoint
* @throws UniformInterfaceException
* @throws GitLabApiException
* @throws ClientHandlerException
*/
protected
ClientResponse
delete
(
ClientResponse
.
Status
expectedStatus
,
MultivaluedMap
<
String
,
String
>
queryParams
,
URL
url
)
throws
GitLabApiException
{
try
{
ClientResponse
response
=
getApiClient
().
delete
(
queryParams
,
url
);
if
(
response
.
getStatus
()
!=
expectedStatus
.
getStatusCode
())
{
throw
(
new
GitLabApiException
(
response
));
}
return
(
response
);
}
catch
(
Exception
e
)
{
throw
(
new
GitLabApiException
(
e
));
}
}
/**
*
* @param formData
* @param string
* @param email
*/
*/
protected
ClientResponse
delete
(
MultivaluedMap
<
String
,
String
>
queryParams
,
URL
url
)
protected
void
addFormParam
(
Form
formData
,
String
name
,
Object
value
)
throws
IllegalArgumentException
{
throws
UniformInterfaceException
,
ClientHandlerException
{
addFormParam
(
formData
,
name
,
value
,
false
);
return
(
getApiClient
().
delete
(
queryParams
,
url
));
}
}
...
...
src/main/java/com/messners/gitlab/api/CommitsApi.java
View file @
aecdac90
package
com.messners.gitlab.api
;
package
com.messners.gitlab.api
;
import
java.io.IOException
;
import
java.util.List
;
import
java.util.List
;
import
com.messners.gitlab.api.models.Commit
;
import
com.messners.gitlab.api.models.Commit
;
...
@@ -19,9 +18,10 @@ public class CommitsApi extends AbstractApi {
...
@@ -19,9 +18,10 @@ public class CommitsApi extends AbstractApi {
*
*
* @param branch
* @param branch
* @return
* @return
* @throws GitLabApiException
*/
*/
public
List
<
Commit
>
getCommits
(
int
projectId
,
String
branch
)
throws
IO
Exception
{
public
List
<
Commit
>
getCommits
(
int
projectId
,
String
branch
)
throws
GitLabApi
Exception
{
ClientResponse
response
=
get
(
null
,
"projects"
,
projectId
,
"repository"
,
"commits"
,
branch
);
ClientResponse
response
=
get
(
ClientResponse
.
Status
.
OK
,
null
,
"projects"
,
projectId
,
"repository"
,
"commits"
,
branch
);
return
(
response
.
getEntity
(
new
GenericType
<
List
<
Commit
>>()
{}));
return
(
response
.
getEntity
(
new
GenericType
<
List
<
Commit
>>()
{}));
}
}
}
}
src/main/java/com/messners/gitlab/api/GroupApi.java
View file @
aecdac90
package
com.messners.gitlab.api
;
package
com.messners.gitlab.api
;
import
java.io.IOException
;
import
java.util.List
;
import
java.util.List
;
import
com.messners.gitlab.api.models.Group
;
import
com.messners.gitlab.api.models.Group
;
import
com.messners.gitlab.api.models.Member
;
import
com.messners.gitlab.api.models.Member
;
import
com.sun.jersey.api.client.ClientResponse
;
import
com.sun.jersey.api.client.ClientResponse
;
import
com.sun.jersey.api.client.GenericType
;
import
com.sun.jersey.api.client.GenericType
;
import
com.sun.jersey.api.representation.Form
;
public
class
GroupApi
extends
AbstractApi
{
public
class
GroupApi
extends
AbstractApi
{
...
@@ -16,67 +16,109 @@ public class GroupApi extends AbstractApi {
...
@@ -16,67 +16,109 @@ public class GroupApi extends AbstractApi {
/**
/**
* Get a list of groups. (As user: my groups, as admin: all groups)
*
* GET /groups
* GET /groups
*
*
* @return
* @return
* @throws
IO
Exception
* @throws
GitLabApi
Exception
*/
*/
public
List
<
Group
>
getGroups
()
throws
IO
Exception
{
public
List
<
Group
>
getGroups
()
throws
GitLabApi
Exception
{
ClientResponse
response
=
get
(
null
,
"groups"
);
ClientResponse
response
=
get
(
ClientResponse
.
Status
.
OK
,
null
,
"groups"
);
return
(
response
.
getEntity
(
new
GenericType
<
List
<
Group
>>()
{}));
return
(
response
.
getEntity
(
new
GenericType
<
List
<
Group
>>()
{}));
}
}
/**
/**
* Get all details of a group.
*
* GET /groups/:id
* GET /groups/:id
*
*
* @param groupId
* @param groupId
* @return
* @return
* @throws
IO
Exception
* @throws
GitLabApi
Exception
*/
*/
public
Group
getGroup
(
int
groupId
)
throws
IO
Exception
{
public
Group
getGroup
(
int
groupId
)
throws
GitLabApi
Exception
{
ClientResponse
response
=
get
(
null
,
"groups"
,
groupId
);
ClientResponse
response
=
get
(
ClientResponse
.
Status
.
OK
,
null
,
"groups"
,
groupId
);
return
(
response
.
getEntity
(
Group
.
class
));
return
(
response
.
getEntity
(
Group
.
class
));
}
}
/**
/**
* Removes group with all projects inside.
*
* DELETE /groups/:id
* DELETE /groups/:id
*
*
* @param groupId
* @param groupId
* @return
* @throws GitLabApiException
* @throws IOException
*/
*/
public
boolean
deleteGroup
(
Integer
groupId
)
throws
IO
Exception
{
public
void
deleteGroup
(
Integer
groupId
)
throws
GitLabApi
Exception
{
if
(
groupId
==
null
)
{
if
(
groupId
==
null
)
{
throw
new
RuntimeException
(
"groupId cannot be null"
);
throw
new
RuntimeException
(
"groupId cannot be null"
);
}
}
ClientResponse
response
=
delete
(
null
,
"groups"
,
groupId
);
delete
(
ClientResponse
.
Status
.
OK
,
null
,
"groups"
,
groupId
);
return
(
response
.
getStatus
()
==
ClientResponse
.
Status
.
OK
.
getStatusCode
());
}
}
/**
/**
* Removes group with all projects inside.
*
* DELETE /groups/:id
*
*
* @param group
* @param group
* @return
* @throws GitLabApiException
* @throws IOException
*/
*/
public
boolean
deleteGroup
(
Group
group
)
throws
IO
Exception
{
public
void
deleteGroup
(
Group
group
)
throws
GitLabApi
Exception
{
return
(
deleteGroup
(
group
.
getId
())
)
;
deleteGroup
(
group
.
getId
());
}
}
/**
/**
* Get a list of group members viewable by the authenticated user.
*
* GET /groups/:id/members
* GET /groups/:id/members
*
*
* @return
* @return
* @throws IOException
* @throws GitLabApiException
*/
public
List
<
Member
>
getMembers
(
int
groupId
)
throws
GitLabApiException
{
ClientResponse
response
=
get
(
ClientResponse
.
Status
.
OK
,
null
,
"groups"
,
groupId
,
"members"
);
return
(
response
.
getEntity
(
new
GenericType
<
List
<
Member
>>()
{}));
}
/**
* Adds a user to the list of group members.
*
* POST /groups/:id/members
*
* @param groupId
* @param userId
* @param accessLevel
* @return
* @throws GitLabApiException
*/
public
Member
addMember
(
Integer
groupId
,
Integer
userId
,
Integer
accessLevel
)
throws
GitLabApiException
{
Form
formData
=
new
Form
();
formData
.
add
(
"user_id"
,
userId
);
formData
.
add
(
"access_level"
,
accessLevel
);
ClientResponse
response
=
post
(
ClientResponse
.
Status
.
OK
,
formData
,
"groups"
,
groupId
,
"members"
);
return
(
response
.
getEntity
(
Member
.
class
));
}
/**
* Removes member from the group team.
*
* DELETE /groups/:id/members/:user_id
*
* @param projectId
* @param userId
* @throws GitLabApiException
*/
*/
public
List
<
Member
>
getMembers
(
int
groupId
)
throws
IOException
{
public
void
removeMember
(
Integer
projectId
,
Integer
userId
)
throws
GitLabApiException
{
ClientResponse
response
=
get
(
null
,
"groups"
,
groupId
,
"members"
);
delete
(
ClientResponse
.
Status
.
OK
,
null
,
"groups"
,
projectId
,
"members"
,
userId
);
return
(
response
.
getEntity
(
new
GenericType
<
List
<
Member
>>()
{}));
}
}
}
}
src/main/java/com/messners/gitlab/api/MergeRequestApi.java
View file @
aecdac90
package
com.messners.gitlab.api
;
package
com.messners.gitlab.api
;
import
java.
io.IOException
;
import
java.
util.List
;
import
com.messners.gitlab.api.models.ErrorMessage
;
import
com.messners.gitlab.api.models.MergeRequest
;
import
com.messners.gitlab.api.models.MergeRequest
;
import
com.messners.gitlab.api.models.MergeRequestComment
;
import
com.messners.gitlab.api.models.MergeRequestComment
;
import
com.sun.jersey.api.client.ClientResponse
;
import
com.sun.jersey.api.client.ClientResponse
;
import
com.sun.jersey.api.client.GenericType
;
import
com.sun.jersey.api.representation.Form
;
import
com.sun.jersey.api.representation.Form
;
public
class
MergeRequestApi
extends
AbstractApi
{
public
class
MergeRequestApi
extends
AbstractApi
{
...
@@ -13,31 +13,43 @@ public class MergeRequestApi extends AbstractApi {
...
@@ -13,31 +13,43 @@ public class MergeRequestApi extends AbstractApi {
MergeRequestApi
(
GitLabApi
gitLabApi
)
{
MergeRequestApi
(
GitLabApi
gitLabApi
)
{
super
(
gitLabApi
);
super
(
gitLabApi
);
}
}
/**
/**
* POST /projects/:id/merge_request/:merge_request_id/comments
* Get all merge requests for this project.
*
* GET /projects/:id/merge_requests
*
* @param projectId
* @return
* @throws GitLabApiException
*/
public
List
<
MergeRequest
>
getMergeRequests
(
Integer
projectId
)
throws
GitLabApiException
{
ClientResponse
response
=
get
(
ClientResponse
.
Status
.
OK
,
null
,
"projects"
,
projectId
,
"merge_requests"
);
return
(
response
.
getEntity
(
new
GenericType
<
List
<
MergeRequest
>>()
{}));
}
/**
* Get information about a single merge request.
*
* GET /projects/:id/merge_request/:merge_request_id
*
*
* @param projectId
* @param projectId
* @param mergeRequestId
* @param mergeRequestId
* @
param comments
* @
return
* @throws
IO
Exception
* @throws
GitLabApi
Exception
*/
*/
public
MergeRequestComment
addMergeRequestComment
(
Integer
projectId
,
Integer
mergeRequestId
,
String
comments
)
throws
IOException
{
public
MergeRequest
getMergeRequest
(
Integer
projectId
,
Integer
mergeRequestId
)
throws
GitLabApiException
{
ClientResponse
response
=
get
(
ClientResponse
.
Status
.
OK
,
null
,
"projects"
,
projectId
,
"merge_request"
,
mergeRequestId
);
Form
formData
=
new
Form
();
return
(
response
.
getEntity
(
MergeRequest
.
class
));
formData
.
add
(
"note"
,
comments
);
ClientResponse
response
=
post
(
formData
,
"projects"
,
projectId
,
"merge_request"
,
mergeRequestId
,
"comments"
);
if
(
response
.
getStatus
()
!=
ClientResponse
.
Status
.
CREATED
.
getStatusCode
())
{
ErrorMessage
errorMessage
=
response
.
getEntity
(
ErrorMessage
.
class
);
throw
new
IOException
(
errorMessage
.
getMessage
());
}
return
(
response
.
getEntity
(
MergeRequestComment
.
class
));
}
}
/**
/**
* Creates a merge request and optionally assignes it.
* Creates a merge request and optionally assigns a reviewer to it.
*
* POST /projects/:id/merge_requests
*
*
* @param projectId the ID of a project, required
* @param projectId the ID of a project, required
* @param sourceBranch the source branch, required
* @param sourceBranch the source branch, required
...
@@ -46,43 +58,80 @@ public class MergeRequestApi extends AbstractApi {
...
@@ -46,43 +58,80 @@ public class MergeRequestApi extends AbstractApi {
* @param description the description of the merge request
* @param description the description of the merge request
* @param assigneeId the Assignee user ID, optional
* @param assigneeId the Assignee user ID, optional
* @return the created MergeRequest instance
* @return the created MergeRequest instance
* @throws
IO
Exception
* @throws
GitLabApi
Exception
*/
*/
public
MergeRequest
createMergeRequest
(
Integer
projectId
,
String
sourceBranch
,
String
targetBranch
,
String
title
,
String
description
,
Integer
assigneeId
)
public
MergeRequest
createMergeRequest
(
Integer
projectId
,
String
sourceBranch
,
String
targetBranch
,
String
title
,
String
description
,
Integer
assigneeId
)
throws
IO
Exception
{
throws
GitLabApi
Exception
{
/*
* Parameters:
* id (required) - The ID of a project
* source_branch (required) - The source branch
* target_branch (required) - The target branch
* assignee_id (optional) - Assignee user ID
* title (required) - Title of MR
*/
if
(
projectId
==
null
)
{
if
(
projectId
==
null
)
{
throw
new
RuntimeException
(
"projectId cannot be null"
);
throw
new
RuntimeException
(
"projectId cannot be null"
);
}
}
Form
formData
=
new
Form
();
Form
formData
=
new
Form
();
formData
.
add
(
"source_branch"
,
sourceBranch
);
addFormParam
(
formData
,
"source_branch"
,
sourceBranch
,
true
);
formData
.
add
(
"target_branch"
,
targetBranch
);
addFormParam
(
formData
,
"target_branch"
,
targetBranch
,
true
);
formData
.
add
(
"title"
,
title
);
addFormParam
(
formData
,
"title"
,
title
,
true
);
addFormParam
(
formData
,
"description"
,
description
,
false
);
addFormParam
(
formData
,
"assignee_id"
,
assigneeId
,
false
);
if
(
description
!=
null
)
{
ClientResponse
response
=
post
(
ClientResponse
.
Status
.
CREATED
,
formData
,
"projects"
,
projectId
,
"merge_requests"
);
formData
.
add
(
"description"
,
description
);
return
(
response
.
getEntity
(
MergeRequest
.
class
));
}
}
/**
* Updates an existing merge request. You can change branches, title, or even close the MR.
*
* PUT /projects/:id/merge_request/:merge_request_id
*
* @param projectId
* @param mergeRequestId
* @param sourceBranch
* @param targetBranch
* @param title
* @param description
* @param assigneeId
* @return
* @throws GitLabApiException
*/
public
MergeRequest
updateMergeRequest
(
Integer
projectId
,
Integer
mergeRequestId
,
String
sourceBranch
,
String
targetBranch
,
String
title
,
String
description
,
Integer
assigneeId
)
throws
GitLabApiException
{
if
(
assignee
Id
!
=
null
)
{
if
(
project
Id
=
=
null
)
{
formData
.
add
(
"assignee_id"
,
assigneeId
);
throw
new
RuntimeException
(
"mergeRequestId cannot be null"
);
}
}
ClientResponse
response
=
post
(
formData
,
"projects"
,
projectId
,
"merge_requests"
);
if
(
mergeRequestId
==
null
)
{
if
(
response
.
getStatus
()
!=
ClientResponse
.
Status
.
CREATED
.
getStatusCode
())
{
throw
new
RuntimeException
(
"projectId cannot be null"
);
ErrorMessage
errorMessage
=
response
.
getEntity
(
ErrorMessage
.
class
);
throw
new
IOException
(
errorMessage
.
getMessage
());
}
}
return
(
response
.
getEntity
(
MergeRequest
.
class
));
Form
formData
=
new
Form
();
addFormParam
(
formData
,
"source_branch"
,
sourceBranch
,
false
);
addFormParam
(
formData
,
"target_branch"
,
targetBranch
,
false
);
addFormParam
(
formData
,
"title"
,
title
,
false
);
addFormParam
(
formData
,
"description"
,
description
,
false
);
addFormParam
(
formData
,
"assignee_id"
,
assigneeId
,
false
);
ClientResponse
response
=
put
(
ClientResponse
.
Status
.
OK
,
formData
,
"projects"
,
projectId
,
"merge_request"
,
mergeRequestId
);
return
(
response
.
getEntity
(
MergeRequest
.
class
));
}
/**
* Adds a comment to a merge request.
*
* POST /projects/:id/merge_request/:merge_request_id/comments
*
* @param projectId
* @param mergeRequestId
* @param comments
* @throws GitLabApiException
*/
public
MergeRequestComment
addMergeRequestComment
(
Integer
projectId
,
Integer
mergeRequestId
,
String
comments
)
throws
GitLabApiException
{
Form
formData
=
new
Form
();
formData
.
add
(
"note"
,
comments
);
ClientResponse
response
=
post
(
ClientResponse
.
Status
.
OK
,
formData
,
"projects"
,
projectId
,
"merge_request"
,
mergeRequestId
,
"comments"
);
return
(
response
.
getEntity
(
MergeRequestComment
.
class
));
}
}
}
}
src/main/java/com/messners/gitlab/api/ProjectApi.java
View file @
aecdac90
package
com.messners.gitlab.api
;
package
com.messners.gitlab.api
;
import
java.io.IOException
;
import
java.util.List
;
import
java.util.List
;
import
com.messners.gitlab.api.models.ErrorMessage
;
import
com.messners.gitlab.api.models.Event
;
import
com.messners.gitlab.api.models.Event
;
import
com.messners.gitlab.api.models.Member
;
import
com.messners.gitlab.api.models.Member
;
import
com.messners.gitlab.api.models.Project
;
import
com.messners.gitlab.api.models.Project
;
...
@@ -25,11 +23,11 @@ public class ProjectApi extends AbstractApi {
...
@@ -25,11 +23,11 @@ public class ProjectApi extends AbstractApi {
* GET /projects
* GET /projects
*
*
* @return
* @return
* @throws
IO
Exception
* @throws
GitLabApi
Exception
*/
*/
public
List
<
Project
>
getProjects
()
throws
IO
Exception
{
public
List
<
Project
>
getProjects
()
throws
GitLabApi
Exception
{
ClientResponse
response
=
get
(
null
,
"projects"
);
ClientResponse
response
=
get
(
ClientResponse
.
Status
.
OK
,
null
,
"projects"
);
return
(
response
.
getEntity
(
new
GenericType
<
List
<
Project
>>()
{}));
return
(
response
.
getEntity
(
new
GenericType
<
List
<
Project
>>()
{}));
}
}
...
@@ -39,11 +37,11 @@ public class ProjectApi extends AbstractApi {
...
@@ -39,11 +37,11 @@ public class ProjectApi extends AbstractApi {
* GET /projects/all
* GET /projects/all
*
*
* @return
* @return
* @throws
IO
Exception
* @throws
GitLabApi
Exception
*/
*/
public
List
<
Project
>
getAllProjects
()
throws
IO
Exception
{
public
List
<
Project
>
getAllProjects
()
throws
GitLabApi
Exception
{
ClientResponse
response
=
get
(
null
,
"projects"
,
"all"
);
ClientResponse
response
=
get
(
ClientResponse
.
Status
.
OK
,
null
,
"projects"
,
"all"
);
return
(
response
.
getEntity
(
new
GenericType
<
List
<
Project
>>()
{}));
return
(
response
.
getEntity
(
new
GenericType
<
List
<
Project
>>()
{}));
}
}
...
@@ -53,11 +51,11 @@ public class ProjectApi extends AbstractApi {
...
@@ -53,11 +51,11 @@ public class ProjectApi extends AbstractApi {
* GET /projects/owned
* GET /projects/owned
*
*
* @return
* @return
* @throws
IO
Exception
* @throws
GitLabApi
Exception
*/
*/
public
List
<
Project
>
getOwnedProjects
()
throws
IO
Exception
{
public
List
<
Project
>
getOwnedProjects
()
throws
GitLabApi
Exception
{
ClientResponse
response
=
get
(
null
,
"projects"
,
"owned"
);
ClientResponse
response
=
get
(
ClientResponse
.
Status
.
OK
,
null
,
"projects"
,
"owned"
);
return
(
response
.
getEntity
(
new
GenericType
<
List
<
Project
>>()
{}));
return
(
response
.
getEntity
(
new
GenericType
<
List
<
Project
>>()
{}));
}
}
...
@@ -68,13 +66,32 @@ public class ProjectApi extends AbstractApi {
...
@@ -68,13 +66,32 @@ public class ProjectApi extends AbstractApi {
*
*
* @param projectId
* @param projectId
* @return
* @return
* @throws
IO
Exception
* @throws
GitLabApi
Exception
*/
*/
public
Project
getProject
(
Integer
projectId
)
throws
IO
Exception
{
public
Project
getProject
(
Integer
projectId
)
throws
GitLabApi
Exception
{
ClientResponse
response
=
get
(
null
,
"projects"
,
projectId
);
ClientResponse
response
=
get
(
ClientResponse
.
Status
.
OK
,
null
,
"projects"
,
projectId
);
return
(
response
.
getEntity
(
Project
.
class
));
return
(
response
.
getEntity
(
Project
.
class
));
}
}
/**
* Create a new project in the specified group.
*
* @param groupId
* @param projectName
* @return
* @throws GitLabApiException
*/
public
Project
createProject
(
Integer
groupId
,
String
projectName
)
throws
GitLabApiException
{
Form
formData
=
new
Form
();
addFormParam
(
formData
,
"namespace_id"
,
groupId
);
addFormParam
(
formData
,
"name"
,
projectName
,
true
);
ClientResponse
response
=
post
(
ClientResponse
.
Status
.
CREATED
,
formData
,
"projects"
);
return
(
response
.
getEntity
(
Project
.
class
));
}
/**
/**
* Creates new project owned by the current user.
* Creates new project owned by the current user.
...
@@ -82,8 +99,9 @@ public class ProjectApi extends AbstractApi {
...
@@ -82,8 +99,9 @@ public class ProjectApi extends AbstractApi {
* @param project the Project instance with the configuration for the new project
* @param project the Project instance with the configuration for the new project
* @return a Project instance with the newly created project info
* @return a Project instance with the newly created project info
* @throws IOException
* @throws IOException
* @throws GitLabApiException
*/
*/
public
Project
createProject
(
Project
project
)
throws
IO
Exception
{
public
Project
createProject
(
Project
project
)
throws
GitLabApi
Exception
{
return
(
createProject
(
project
,
null
));
return
(
createProject
(
project
,
null
));
}
}
...
@@ -104,9 +122,9 @@ public class ProjectApi extends AbstractApi {
...
@@ -104,9 +122,9 @@ public class ProjectApi extends AbstractApi {
* @param project the Project instance with the configuration for the new project
* @param project the Project instance with the configuration for the new project
* @param importUrl
* @param importUrl
* @return a Project instance with the newly created project info
* @return a Project instance with the newly created project info
* @throws
IO
Exception
* @throws
GitLabApi
Exception
*/
*/
public
Project
createProject
(
Project
project
,
String
importUrl
)
throws
IO
Exception
{
public
Project
createProject
(
Project
project
,
String
importUrl
)
throws
GitLabApi
Exception
{
if
(
project
==
null
)
{
if
(
project
==
null
)
{
return
(
null
);
return
(
null
);
...
@@ -118,23 +136,22 @@ public class ProjectApi extends AbstractApi {
...
@@ -118,23 +136,22 @@ public class ProjectApi extends AbstractApi {
}
}
Form
formData
=
new
Form
();
Form
formData
=
new
Form
();
formData
.
add
(
"name"
,
name
);
if
(
project
.
getNamespace
()
!=
null
)
{
formData
.
add
(
"description"
,
project
.
getDescription
());
addFormParam
(
formData
,
"namespace_id"
,
project
.
getNamespace
().
getId
());
formData
.
add
(
"issues_enabled"
,
project
.
getIssuesEnabled
());
}
formData
.
add
(
"wall_enabled"
,
project
.
getWallEnabled
());
formData
.
add
(
"merge_requests_enabled"
,
project
.
getMergeRequestsEnabled
());
formData
.
add
(
"wiki_enabled"
,
project
.
getWikiEnabled
());
formData
.
add
(
"snippets_enabled"
,
project
.
getSnippetsEnabled
());
formData
.
add
(
"public"
,
project
.
getPublic
());
formData
.
add
(
"visibility_level"
,
project
.
getVisibilityLevel
());
formData
.
add
(
"import_url"
,
importUrl
);
ClientResponse
response
=
post
(
formData
,
"projects"
);
if
(
response
.
getStatus
()
!=
ClientResponse
.
Status
.
CREATED
.
getStatusCode
())
{
ErrorMessage
errorMessage
=
response
.
getEntity
(
ErrorMessage
.
class
);
throw
new
RuntimeException
(
errorMessage
.
getMessage
());
}
addFormParam
(
formData
,
"name"
,
name
,
true
);
addFormParam
(
formData
,
"description"
,
project
.
getDescription
());
addFormParam
(
formData
,
"issues_enabled"
,
project
.
getIssuesEnabled
());
addFormParam
(
formData
,
"wall_enabled"
,
project
.
getWallEnabled
());
addFormParam
(
formData
,
"merge_requests_enabled"
,
project
.
getMergeRequestsEnabled
());
addFormParam
(
formData
,
"wiki_enabled"
,
project
.
getWikiEnabled
());
addFormParam
(
formData
,
"snippets_enabled"
,
project
.
getSnippetsEnabled
());
addFormParam
(
formData
,
"public"
,
project
.
getPublic
());
addFormParam
(
formData
,
"visibility_level"
,
project
.
getVisibilityLevel
());
addFormParam
(
formData
,
"import_url"
,
importUrl
);
ClientResponse
response
=
post
(
ClientResponse
.
Status
.
CREATED
,
formData
,
"projects"
);
return
(
response
.
getEntity
(
Project
.
class
));
return
(
response
.
getEntity
(
Project
.
class
));
}
}
...
@@ -145,17 +162,15 @@ public class ProjectApi extends AbstractApi {
...
@@ -145,17 +162,15 @@ public class ProjectApi extends AbstractApi {
* DELETE /projects/:id
* DELETE /projects/:id
*
*
* @param projectId
* @param projectId
* @return
* @throws GitLabApiException
* @throws IOException
*/
*/
public
boolean
deleteProject
(
Integer
projectId
)
throws
IO
Exception
{
public
void
deleteProject
(
Integer
projectId
)
throws
GitLabApi
Exception
{
if
(
projectId
==
null
)
{
if
(
projectId
==
null
)
{
throw
new
RuntimeException
(
"projectId cannot be null"
);
throw
new
RuntimeException
(
"projectId cannot be null"
);
}
}
ClientResponse
response
=
delete
(
null
,
"projects"
,
projectId
);
delete
(
ClientResponse
.
Status
.
OK
,
null
,
"projects"
,
projectId
);
return
(
response
.
getStatus
()
==
ClientResponse
.
Status
.
OK
.
getStatusCode
());
}
}
...
@@ -165,11 +180,10 @@ public class ProjectApi extends AbstractApi {
...
@@ -165,11 +180,10 @@ public class ProjectApi extends AbstractApi {
* DELETE /projects/:id
* DELETE /projects/:id
*
*
* @param project
* @param project
* @return
* @throws GitLabApiException
* @throws IOException
*/
*/
public
boolean
deleteProject
(
Project
project
)
throws
IO
Exception
{
public
void
deleteProject
(
Project
project
)
throws
GitLabApi
Exception
{
return
(
deleteProject
(
project
.
getId
())
)
;
deleteProject
(
project
.
getId
());
}
}
...
@@ -180,10 +194,10 @@ public class ProjectApi extends AbstractApi {
...
@@ -180,10 +194,10 @@ public class ProjectApi extends AbstractApi {
*
*
* @param projectId
* @param projectId
* @return
* @return
* @throws
IO
Exception
* @throws
GitLabApi
Exception
*/
*/
public
List
<
Member
>
getMembers
(
Integer
projectId
)
throws
IO
Exception
{
public
List
<
Member
>
getMembers
(
Integer
projectId
)
throws
GitLabApi
Exception
{
ClientResponse
response
=
get
(
null
,
"project"
,
projectId
,
"members"
);
ClientResponse
response
=
get
(
ClientResponse
.
Status
.
OK
,
null
,
"project
s
"
,
projectId
,
"members"
);
return
(
response
.
getEntity
(
new
GenericType
<
List
<
Member
>>()
{}));
return
(
response
.
getEntity
(
new
GenericType
<
List
<
Member
>>()
{}));
}
}
...
@@ -196,10 +210,10 @@ public class ProjectApi extends AbstractApi {
...
@@ -196,10 +210,10 @@ public class ProjectApi extends AbstractApi {
* @param projectId
* @param projectId
* @param userId
* @param userId
* @return
* @return
* @throws
IO
Exception
* @throws
GitLabApi
Exception
*/
*/
public
Member
getMember
(
Integer
projectId
,
Integer
userId
)
throws
IO
Exception
{
public
Member
getMember
(
Integer
projectId
,
Integer
userId
)
throws
GitLabApi
Exception
{
ClientResponse
response
=
get
(
null
,
"project"
,
projectId
,
"members"
,
userId
);
ClientResponse
response
=
get
(
ClientResponse
.
Status
.
OK
,
null
,
"project
s
"
,
projectId
,
"members"
,
userId
);
return
(
response
.
getEntity
(
Member
.
class
));
return
(
response
.
getEntity
(
Member
.
class
));
}
}
...
@@ -215,14 +229,14 @@ public class ProjectApi extends AbstractApi {
...
@@ -215,14 +229,14 @@ public class ProjectApi extends AbstractApi {
* @param userId
* @param userId
* @param accessLevel
* @param accessLevel
* @return
* @return
* @throws
IO
Exception
* @throws
GitLabApi
Exception
*/
*/
public
Member
addMember
(
Integer
projectId
,
Integer
userId
,
Integer
accessLevel
)
throws
IO
Exception
{
public
Member
addMember
(
Integer
projectId
,
Integer
userId
,
Integer
accessLevel
)
throws
GitLabApi
Exception
{
Form
formData
=
new
Form
();
Form
formData
=
new
Form
();
formData
.
add
(
"user_id"
,
userId
);
formData
.
add
(
"user_id"
,
userId
);
formData
.
add
(
"access_level"
,
accessLevel
);
formData
.
add
(
"access_level"
,
accessLevel
);
ClientResponse
response
=
post
(
formData
,
"project"
,
projectId
,
"members"
);
ClientResponse
response
=
post
(
ClientResponse
.
Status
.
OK
,
formData
,
"project
s
"
,
projectId
,
"members"
);
return
(
response
.
getEntity
(
Member
.
class
));
return
(
response
.
getEntity
(
Member
.
class
));
}
}
...
@@ -234,10 +248,10 @@ public class ProjectApi extends AbstractApi {
...
@@ -234,10 +248,10 @@ public class ProjectApi extends AbstractApi {
*
*
* @param projectId
* @param projectId
* @param userId
* @param userId
* @throws
IO
Exception
* @throws
GitLabApi
Exception
*/
*/
public
boolean
removeMember
(
Integer
projectId
,
Integer
userId
)
throws
IO
Exception
{
public
boolean
removeMember
(
Integer
projectId
,
Integer
userId
)
throws
GitLabApi
Exception
{
ClientResponse
response
=
delete
(
null
,
"project"
,
projectId
,
"members"
,
userId
);
ClientResponse
response
=
delete
(
ClientResponse
.
Status
.
OK
,
null
,
"project
s
"
,
projectId
,
"members"
,
userId
);
return
(
response
.
getStatus
()
==
ClientResponse
.
Status
.
OK
.
getStatusCode
());
return
(
response
.
getStatus
()
==
ClientResponse
.
Status
.
OK
.
getStatusCode
());
}
}
...
@@ -249,10 +263,10 @@ public class ProjectApi extends AbstractApi {
...
@@ -249,10 +263,10 @@ public class ProjectApi extends AbstractApi {
*
*
* @param projectId
* @param projectId
* @return
* @return
* @throws
IO
Exception
* @throws
GitLabApi
Exception
*/
*/
public
List
<
Event
>
getProjectEvents
(
Integer
projectId
)
throws
IO
Exception
{
public
List
<
Event
>
getProjectEvents
(
Integer
projectId
)
throws
GitLabApi
Exception
{
ClientResponse
response
=
get
(
null
,
"project"
,
projectId
,
"events"
);
ClientResponse
response
=
get
(
ClientResponse
.
Status
.
OK
,
null
,
"project
s
"
,
projectId
,
"events"
);
return
(
response
.
getEntity
(
new
GenericType
<
List
<
Event
>>()
{}));
return
(
response
.
getEntity
(
new
GenericType
<
List
<
Event
>>()
{}));
}
}
...
@@ -264,11 +278,11 @@ public class ProjectApi extends AbstractApi {
...
@@ -264,11 +278,11 @@ public class ProjectApi extends AbstractApi {
*
*
* @param projectId
* @param projectId
* @return
* @return
* @throws
IO
Exception
* @throws
GitLabApi
Exception
*/
*/
public
List
<
ProjectHook
>
getHooks
(
Integer
projectId
)
throws
IO
Exception
{
public
List
<
ProjectHook
>
getHooks
(
Integer
projectId
)
throws
GitLabApi
Exception
{
ClientResponse
response
=
get
(
null
,
"projects"
,
projectId
,
"hooks"
);
ClientResponse
response
=
get
(
ClientResponse
.
Status
.
OK
,
null
,
"projects"
,
projectId
,
"hooks"
);
return
(
response
.
getEntity
(
new
GenericType
<
List
<
ProjectHook
>>()
{}
));
return
(
response
.
getEntity
(
new
GenericType
<
List
<
ProjectHook
>>()
{}
));
}
}
...
@@ -280,11 +294,11 @@ public class ProjectApi extends AbstractApi {
...
@@ -280,11 +294,11 @@ public class ProjectApi extends AbstractApi {
* @param projectId
* @param projectId
* @param hookId
* @param hookId
* @return
* @return
* @throws
IO
Exception
* @throws
GitLabApi
Exception
*/
*/
public
ProjectHook
getHook
(
Integer
projectId
,
Integer
hookId
)
throws
IO
Exception
{
public
ProjectHook
getHook
(
Integer
projectId
,
Integer
hookId
)
throws
GitLabApi
Exception
{
ClientResponse
response
=
get
(
null
,
"projects"
,
projectId
,
"hooks"
,
hookId
);
ClientResponse
response
=
get
(
ClientResponse
.
Status
.
OK
,
null
,
"projects"
,
projectId
,
"hooks"
,
hookId
);
return
(
response
.
getEntity
(
ProjectHook
.
class
));
return
(
response
.
getEntity
(
ProjectHook
.
class
));
}
}
...
@@ -299,11 +313,11 @@ public class ProjectApi extends AbstractApi {
...
@@ -299,11 +313,11 @@ public class ProjectApi extends AbstractApi {
* @param doIssuesEvents
* @param doIssuesEvents
* @param doMergeRequestsEvents
* @param doMergeRequestsEvents
* @return
* @return
* @throws
IO
Exception
* @throws
GitLabApi
Exception
*/
*/
public
ProjectHook
addHook
(
Project
project
,
String
url
,
public
ProjectHook
addHook
(
Project
project
,
String
url
,
boolean
doPushEvents
,
boolean
doIssuesEvents
,
boolean
doMergeRequestsEvents
)
boolean
doPushEvents
,
boolean
doIssuesEvents
,
boolean
doMergeRequestsEvents
)
throws
IO
Exception
{
throws
GitLabApi
Exception
{
if
(
project
==
null
)
{
if
(
project
==
null
)
{
return
(
null
);
return
(
null
);
...
@@ -324,11 +338,11 @@ public class ProjectApi extends AbstractApi {
...
@@ -324,11 +338,11 @@ public class ProjectApi extends AbstractApi {
* @param doIssuesEvents
* @param doIssuesEvents
* @param doMergeRequestsEvents
* @param doMergeRequestsEvents
* @return
* @return
* @throws
IO
Exception
* @throws
GitLabApi
Exception
*/
*/
public
ProjectHook
addHook
(
Integer
projectId
,
String
url
,
public
ProjectHook
addHook
(
Integer
projectId
,
String
url
,
boolean
doPushEvents
,
boolean
doIssuesEvents
,
boolean
doMergeRequestsEvents
)
boolean
doPushEvents
,
boolean
doIssuesEvents
,
boolean
doMergeRequestsEvents
)
throws
IO
Exception
{
throws
GitLabApi
Exception
{
Form
formData
=
new
Form
();
Form
formData
=
new
Form
();
formData
.
add
(
"url"
,
url
);
formData
.
add
(
"url"
,
url
);
...
@@ -336,12 +350,7 @@ public class ProjectApi extends AbstractApi {
...
@@ -336,12 +350,7 @@ public class ProjectApi extends AbstractApi {
formData
.
add
(
"issues_enabled"
,
doIssuesEvents
);
formData
.
add
(
"issues_enabled"
,
doIssuesEvents
);
formData
.
add
(
"merge_requests_events"
,
doMergeRequestsEvents
);
formData
.
add
(
"merge_requests_events"
,
doMergeRequestsEvents
);
ClientResponse
response
=
post
(
formData
,
"projects"
,
projectId
,
"hooks"
);
ClientResponse
response
=
post
(
ClientResponse
.
Status
.
CREATED
,
formData
,
"projects"
,
projectId
,
"hooks"
);
if
(
response
.
getStatus
()
!=
ClientResponse
.
Status
.
CREATED
.
getStatusCode
())
{
ErrorMessage
errorMessage
=
response
.
getEntity
(
ErrorMessage
.
class
);
throw
new
RuntimeException
(
errorMessage
.
getMessage
());
}
return
(
response
.
getEntity
(
ProjectHook
.
class
));
return
(
response
.
getEntity
(
ProjectHook
.
class
));
}
}
...
@@ -357,10 +366,10 @@ public class ProjectApi extends AbstractApi {
...
@@ -357,10 +366,10 @@ public class ProjectApi extends AbstractApi {
* @param doIssuesEvents
* @param doIssuesEvents
* @param doMergeRequestsEvents
* @param doMergeRequestsEvents
* @return
* @return
* @throws
IO
Exception
* @throws
GitLabApi
Exception
*/
*/
public
ProjectHook
modifyHook
(
ProjectHook
hook
)
public
ProjectHook
modifyHook
(
ProjectHook
hook
)
throws
IO
Exception
{
throws
GitLabApi
Exception
{
Form
formData
=
new
Form
();
Form
formData
=
new
Form
();
formData
.
add
(
"url"
,
hook
.
getUrl
());
formData
.
add
(
"url"
,
hook
.
getUrl
());
...
@@ -368,7 +377,7 @@ public class ProjectApi extends AbstractApi {
...
@@ -368,7 +377,7 @@ public class ProjectApi extends AbstractApi {
formData
.
add
(
"issues_enabled"
,
hook
.
getIssuesEvents
());
formData
.
add
(
"issues_enabled"
,
hook
.
getIssuesEvents
());
formData
.
add
(
"merge_requests_events"
,
hook
.
getMergeRequestsEvents
());
formData
.
add
(
"merge_requests_events"
,
hook
.
getMergeRequestsEvents
());
ClientResponse
response
=
put
(
formData
,
"projects"
,
hook
.
getProjectId
(),
"hooks"
,
hook
.
getId
());
ClientResponse
response
=
put
(
ClientResponse
.
Status
.
OK
,
formData
,
"projects"
,
hook
.
getProjectId
(),
"hooks"
,
hook
.
getId
());
return
(
response
.
getEntity
(
ProjectHook
.
class
));
return
(
response
.
getEntity
(
ProjectHook
.
class
));
}
}
}
}
src/main/java/com/messners/gitlab/api/RepositoryApi.java
View file @
aecdac90
package
com.messners.gitlab.api
;
package
com.messners.gitlab.api
;
import
java.io.IOException
;
import
java.util.List
;
import
java.util.List
;
import
com.messners.gitlab.api.models.Branch
;
import
com.messners.gitlab.api.models.Branch
;
...
@@ -8,6 +7,7 @@ import com.messners.gitlab.api.models.Tag;
...
@@ -8,6 +7,7 @@ import com.messners.gitlab.api.models.Tag;
import
com.messners.gitlab.api.models.TreeItem
;
import
com.messners.gitlab.api.models.TreeItem
;
import
com.sun.jersey.api.client.ClientResponse
;
import
com.sun.jersey.api.client.ClientResponse
;
import
com.sun.jersey.api.client.GenericType
;
import
com.sun.jersey.api.client.GenericType
;
import
com.sun.jersey.api.representation.Form
;
/**
/**
...
@@ -23,97 +23,115 @@ public class RepositoryApi extends AbstractApi {
...
@@ -23,97 +23,115 @@ public class RepositoryApi extends AbstractApi {
/**
/**
* Get a list of repository branches from a project, sorted by name alphabetically.
*
* GET /projects/:id/repository/branches
* GET /projects/:id/repository/branches
*
*
* @param projectId
* @param projectId
* @return
* @return
* @throws
IO
Exception
* @throws
GitLabApi
Exception
*/
*/
public
List
<
Branch
>
getBranches
(
Integer
projectId
)
throws
IO
Exception
{
public
List
<
Branch
>
getBranches
(
Integer
projectId
)
throws
GitLabApi
Exception
{
ClientResponse
response
=
get
(
null
,
"projects"
,
projectId
,
"repository"
,
"branches"
);
ClientResponse
response
=
get
(
ClientResponse
.
Status
.
OK
,
null
,
"projects"
,
projectId
,
"repository"
,
"branches"
);
return
(
response
.
getEntity
(
new
GenericType
<
List
<
Branch
>>()
{}));
return
(
response
.
getEntity
(
new
GenericType
<
List
<
Branch
>>()
{}));
}
}
/**
/**
* Get a single project repository branch.
*
* GET /projects/:id/repository/branches/:branch
* GET /projects/:id/repository/branches/:branch
*
*
* @param projectId
* @param projectId
* @param branchName
* @param branchName
* @return
* @return
* @throws
IO
Exception
* @throws
GitLabApi
Exception
*/
*/
public
Branch
getBranch
(
Integer
projectId
,
String
branchName
)
throws
IO
Exception
{
public
Branch
getBranch
(
Integer
projectId
,
String
branchName
)
throws
GitLabApi
Exception
{
ClientResponse
response
=
get
(
null
,
"projects"
,
projectId
,
"repository"
,
"branches"
,
branchName
);
ClientResponse
response
=
get
(
ClientResponse
.
Status
.
OK
,
null
,
"projects"
,
projectId
,
"repository"
,
"branches"
,
branchName
);
return
(
response
.
getEntity
(
Branch
.
class
));
return
(
response
.
getEntity
(
Branch
.
class
));
}
}
/**
/**
* Protects a single project repository branch. This is an idempotent function,
* protecting an already protected repository branch will not produce an error.
*
* PUT /projects/:id/repository/branches/:branch/protect
* PUT /projects/:id/repository/branches/:branch/protect
*
*
* @param projectId
* @param projectId
* @param branchName
* @param branchName
* @return
* @return
* @throws
IO
Exception
* @throws
GitLabApi
Exception
*/
*/
public
Branch
protectBranch
(
Integer
projectId
,
String
branchName
)
throws
IO
Exception
{
public
Branch
protectBranch
(
Integer
projectId
,
String
branchName
)
throws
GitLabApi
Exception
{
ClientResponse
response
=
put
(
null
,
"projects"
,
projectId
,
"repository"
,
"branches"
,
branchName
,
"protect"
);
ClientResponse
response
=
put
(
ClientResponse
.
Status
.
OK
,
null
,
"projects"
,
projectId
,
"repository"
,
"branches"
,
branchName
,
"protect"
);
return
(
response
.
getEntity
(
Branch
.
class
));
return
(
response
.
getEntity
(
Branch
.
class
));
}
}
/**
/**
* Unprotects a single project repository branch. This is an idempotent function, unprotecting an
* already unprotected repository branch will not produce an error.
*
* PUT /projects/:id/repository/branches/:branch/unprotect
* PUT /projects/:id/repository/branches/:branch/unprotect
*
*
* @param projectId
* @param projectId
* @param branchName
* @param branchName
* @return
* @return
* @throws
IO
Exception
* @throws
GitLabApi
Exception
*/
*/
public
Branch
unprotectBranch
(
Integer
projectId
,
String
branchName
)
throws
IO
Exception
{
public
Branch
unprotectBranch
(
Integer
projectId
,
String
branchName
)
throws
GitLabApi
Exception
{
ClientResponse
response
=
put
(
null
,
"projects"
,
projectId
,
"repository"
,
"branches"
,
branchName
,
"unprotect"
);
ClientResponse
response
=
put
(
ClientResponse
.
Status
.
OK
,
null
,
"projects"
,
projectId
,
"repository"
,
"branches"
,
branchName
,
"unprotect"
);
return
(
response
.
getEntity
(
Branch
.
class
));
return
(
response
.
getEntity
(
Branch
.
class
));
}
}
/**
/**
* Get a list of repository tags from a project, sorted by name in reverse alphabetical order.
*
* GET /projects/:id/repository/tags
* GET /projects/:id/repository/tags
*
*
* @param projectId
* @param projectId
* @return
* @return
* @throws
IO
Exception
* @throws
GitLabApi
Exception
*/
*/
public
List
<
Tag
>
getTags
(
Integer
projectId
)
throws
IO
Exception
{
public
List
<
Tag
>
getTags
(
Integer
projectId
)
throws
GitLabApi
Exception
{
ClientResponse
response
=
put
(
null
,
"projects"
,
projectId
,
"repository"
,
"tags"
);
ClientResponse
response
=
put
(
ClientResponse
.
Status
.
OK
,
null
,
"projects"
,
projectId
,
"repository"
,
"tags"
);
return
(
response
.
getEntity
(
new
GenericType
<
List
<
Tag
>>()
{}));
return
(
response
.
getEntity
(
new
GenericType
<
List
<
Tag
>>()
{}));
}
}
/**
/**
* Get a list of repository files and directories in a project.
*
* GET /projects/:id/repository/tree
* GET /projects/:id/repository/tree
*
*
* @param projectId
* @param projectId
* @return
* @return
* @throws
IO
Exception
* @throws
GitLabApi
Exception
*/
*/
public
List
<
TreeItem
>
getTree
(
Integer
projectId
)
throws
IO
Exception
{
public
List
<
TreeItem
>
getTree
(
Integer
projectId
)
throws
GitLabApi
Exception
{
ClientResponse
response
=
put
(
null
,
"projects"
,
projectId
,
"repository"
,
"tree"
);
ClientResponse
response
=
put
(
ClientResponse
.
Status
.
OK
,
null
,
"projects"
,
projectId
,
"repository"
,
"tree"
);
return
(
response
.
getEntity
(
new
GenericType
<
List
<
TreeItem
>>()
{}));
return
(
response
.
getEntity
(
new
GenericType
<
List
<
TreeItem
>>()
{}));
}
}
/**
/**
* Get the raw file contents for a file by commit sha and path.
*
* GET /projects/:id/repository/blobs/:sha
* GET /projects/:id/repository/blobs/:sha
*
*
* @param projectId
* @param projectId
* @param commitOrBranchName
* @param commitOrBranchName
* @return
* @return
* @throws
IO
Exception
* @throws
GitLabApi
Exception
*/
*/
public
String
getRawFileContent
(
Integer
projectId
,
String
commitOrBranchName
)
throws
IOException
{
public
String
getRawFileContent
(
Integer
projectId
,
String
commitOrBranchName
,
String
filepath
)
throws
GitLabApiException
{
ClientResponse
response
=
get
(
null
,
"projects"
,
projectId
,
"repository"
,
"blobs"
,
commitOrBranchName
);
Form
formData
=
new
Form
();
addFormParam
(
formData
,
"filepath"
,
filepath
,
true
);
ClientResponse
response
=
get
(
ClientResponse
.
Status
.
OK
,
formData
,
"projects"
,
projectId
,
"repository"
,
"blobs"
,
commitOrBranchName
);
return
(
response
.
getEntity
(
String
.
class
));
return
(
response
.
getEntity
(
String
.
class
));
}
}
}
}
src/main/java/com/messners/gitlab/api/UserApi.java
View file @
aecdac90
package
com.messners.gitlab.api
;
package
com.messners.gitlab.api
;
import
java.io.IOException
;
import
java.util.List
;
import
java.util.List
;
import
com.messners.gitlab.api.models.ErrorMessage
;
import
com.messners.gitlab.api.models.User
;
import
com.messners.gitlab.api.models.User
;
import
com.sun.jersey.api.client.ClientResponse
;
import
com.sun.jersey.api.client.ClientResponse
;
import
com.sun.jersey.api.client.GenericType
;
import
com.sun.jersey.api.client.GenericType
;
...
@@ -17,31 +15,37 @@ public class UserApi extends AbstractApi {
...
@@ -17,31 +15,37 @@ public class UserApi extends AbstractApi {
/**
/**
* Get a list of users.
*
* GET /users
* GET /users
*
*
* @return
* @return
* @throws
IO
Exception
* @throws
GitLabApi
Exception
*/
*/
public
List
<
User
>
getProjects
()
throws
IO
Exception
{
public
List
<
User
>
getProjects
()
throws
GitLabApi
Exception
{
ClientResponse
response
=
get
(
null
,
"users"
);
ClientResponse
response
=
get
(
ClientResponse
.
Status
.
OK
,
null
,
"users"
);
return
(
response
.
getEntity
(
new
GenericType
<
List
<
User
>>()
{}));
return
(
response
.
getEntity
(
new
GenericType
<
List
<
User
>>()
{}));
}
}
/**
/**
* Get a single user.
*
* GET /users/:id
* GET /users/:id
*
*
* @param userId
* @param userId
* @return
* @return
* @throws
IO
Exception
* @throws
GitLabApi
Exception
*/
*/
public
User
getUser
(
int
userId
)
throws
IO
Exception
{
public
User
getUser
(
int
userId
)
throws
GitLabApi
Exception
{
ClientResponse
response
=
get
(
null
,
"users"
,
userId
);
ClientResponse
response
=
get
(
ClientResponse
.
Status
.
OK
,
null
,
"users"
,
userId
);
return
(
response
.
getEntity
(
User
.
class
));
return
(
response
.
getEntity
(
User
.
class
));
}
}
/**
/**
* Creates a new user. Note only administrators can create new users.
*
* POST /users
* POST /users
*
*
* email (required) - Email
* email (required) - Email
...
@@ -61,9 +65,9 @@ public class UserApi extends AbstractApi {
...
@@ -61,9 +65,9 @@ public class UserApi extends AbstractApi {
*
*
* @param user
* @param user
* @return
* @return
* @throws
IO
Exception
* @throws
GitLabApi
Exception
*/
*/
public
User
createUser
(
User
user
,
String
password
,
Integer
projectsLimit
)
throws
IO
Exception
{
public
User
createUser
(
User
user
,
String
password
,
Integer
projectsLimit
)
throws
GitLabApi
Exception
{
Form
formData
=
new
Form
();
Form
formData
=
new
Form
();
addFormParam
(
formData
,
"email"
,
user
.
getEmail
(),
true
);
addFormParam
(
formData
,
"email"
,
user
.
getEmail
(),
true
);
...
@@ -81,18 +85,15 @@ public class UserApi extends AbstractApi {
...
@@ -81,18 +85,15 @@ public class UserApi extends AbstractApi {
addFormParam
(
formData
,
"admin"
,
user
.
getIsAdmin
(),
false
);
addFormParam
(
formData
,
"admin"
,
user
.
getIsAdmin
(),
false
);
addFormParam
(
formData
,
"can_create_group"
,
user
.
getCanCreateGroup
(),
false
);
addFormParam
(
formData
,
"can_create_group"
,
user
.
getCanCreateGroup
(),
false
);
ClientResponse
response
=
post
(
formData
,
"users"
);
ClientResponse
response
=
post
(
ClientResponse
.
Status
.
CREATED
,
formData
,
"users"
);
if
(
response
.
getStatus
()
!=
ClientResponse
.
Status
.
CREATED
.
getStatusCode
())
{
ErrorMessage
errorMessage
=
response
.
getEntity
(
ErrorMessage
.
class
);
throw
new
RuntimeException
(
errorMessage
.
getMessage
());
}
return
(
response
.
getEntity
(
User
.
class
));
return
(
response
.
getEntity
(
User
.
class
));
}
}
/**
/**
* POST /users
* Modifies an existing user. Only administrators can change attributes of a user.
*
* PUT /users/:id
*
*
* email (required) - Email
* email (required) - Email
* password (required) - Password
* password (required) - Password
...
@@ -111,9 +112,9 @@ public class UserApi extends AbstractApi {
...
@@ -111,9 +112,9 @@ public class UserApi extends AbstractApi {
*
*
* @param user
* @param user
* @return
* @return
* @throws
IO
Exception
* @throws
GitLabApi
Exception
*/
*/
public
User
modifyUser
(
User
user
,
String
password
,
Integer
projectsLimit
)
throws
IO
Exception
{
public
User
modifyUser
(
User
user
,
String
password
,
Integer
projectsLimit
)
throws
GitLabApi
Exception
{
Form
formData
=
new
Form
();
Form
formData
=
new
Form
();
addFormParam
(
formData
,
"email"
,
user
.
getEmail
(),
false
);
addFormParam
(
formData
,
"email"
,
user
.
getEmail
(),
false
);
...
@@ -131,33 +132,38 @@ public class UserApi extends AbstractApi {
...
@@ -131,33 +132,38 @@ public class UserApi extends AbstractApi {
addFormParam
(
formData
,
"admin"
,
user
.
getIsAdmin
(),
false
);
addFormParam
(
formData
,
"admin"
,
user
.
getIsAdmin
(),
false
);
addFormParam
(
formData
,
"can_create_group"
,
user
.
getCanCreateGroup
(),
false
);
addFormParam
(
formData
,
"can_create_group"
,
user
.
getCanCreateGroup
(),
false
);
ClientResponse
response
=
put
(
formData
,
"users"
,
user
.
getId
());
ClientResponse
response
=
put
(
ClientResponse
.
Status
.
OK
,
formData
,
"users"
,
user
.
getId
());
return
(
response
.
getEntity
(
User
.
class
));
return
(
response
.
getEntity
(
User
.
class
));
}
}
/**
/**
* Deletes a user. Available only for administrators.
*
* DELETE /users/:id
* DELETE /users/:id
*
*
* @param userId
* @param userId
* @throws GitLabApiException
*/
*/
public
boolean
deleteUser
(
Integer
userId
)
throws
IO
Exception
{
public
void
deleteUser
(
Integer
userId
)
throws
GitLabApi
Exception
{
if
(
userId
==
null
)
{
if
(
userId
==
null
)
{
throw
new
RuntimeException
(
"userId cannot be null"
);
throw
new
RuntimeException
(
"userId cannot be null"
);
}
}
ClientResponse
response
=
delete
(
null
,
"users"
,
userId
);
delete
(
ClientResponse
.
Status
.
OK
,
null
,
"users"
,
userId
);
return
(
response
.
getStatus
()
==
ClientResponse
.
Status
.
OK
.
getStatusCode
());
}
}
/**
/**
* Deletes a user. Available only for administrators.
*
* DELETE /users/:id
* DELETE /users/:id
*
*
* @param user
* @param user
* @throws GitLabApiException
*/
*/
public
boolean
deleteUser
(
User
user
)
throws
IO
Exception
{
public
void
deleteUser
(
User
user
)
throws
GitLabApi
Exception
{
return
(
deleteUser
(
user
.
getId
())
)
;
deleteUser
(
user
.
getId
());
}
}
}
}
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment