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
0a399753
Commit
0a399753
authored
Mar 02, 2014
by
Greg Messner
Browse files
Refactored GitLabApi to group like API calls into a separate class, this
is the initial check-in of that class.
parent
fbbe0dbf
Changes
6
Hide whitespace changes
Inline
Side-by-side
src/main/java/com/messners/gitlab/api/AbstractApi.java
0 → 100644
View file @
0a399753
package
com.messners.gitlab.api
;
import
java.io.IOException
;
import
java.net.URL
;
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.UniformInterfaceException
;
import
com.sun.jersey.api.representation.Form
;
public
abstract
class
AbstractApi
{
private
GitLabApi
gitLabApi
;
public
AbstractApi
(
GitLabApi
gitLabApi
)
{
this
.
gitLabApi
=
gitLabApi
;
}
protected
GitLabApiClient
getApiClient
()
{
return
(
gitLabApi
.
getApiClient
());
}
/**
* Perform an HTTP GET call with the specified query parameters and path objects, returning
* a ClientResponse instance with the data returned from the endpoint.
*
* @param queryParams
* @param pathArgs
* @return a ClientResponse instance with the data returned from the endpoint
* @throws UniformInterfaceException
* @throws ClientHandlerException
* @throws IOException
*/
protected
ClientResponse
get
(
MultivaluedMap
<
String
,
String
>
queryParams
,
Object
...
pathArgs
)
throws
UniformInterfaceException
,
ClientHandlerException
,
IOException
{
return
(
getApiClient
().
get
(
queryParams
,
pathArgs
));
}
/**
* Perform an HTTP GET call with the specified query parameters and URL, returning
* a ClientResponse instance with the data returned from the endpoint.
*
* @param queryParams
* @param url
* @return a ClientResponse instance with the data returned from the endpoint
* @throws UniformInterfaceException
* @throws ClientHandlerException
*/
protected
ClientResponse
get
(
MultivaluedMap
<
String
,
String
>
queryParams
,
URL
url
)
throws
UniformInterfaceException
,
ClientHandlerException
{
return
(
getApiClient
().
get
(
queryParams
,
url
));
}
/**
* Perform an HTTP POST call with the specified form data and path objects, returning
* a ClientResponse instance with the data returned from the endpoint.
*
* @param formData
* @param pathArgs
* @return a ClientResponse instance with the data returned from the endpoint
* @throws UniformInterfaceException
* @throws ClientHandlerException
* @throws IOException
*/
protected
ClientResponse
post
(
Form
formData
,
Object
...
pathArgs
)
throws
UniformInterfaceException
,
ClientHandlerException
,
IOException
{
return
(
getApiClient
().
post
(
formData
,
pathArgs
));
}
/**
* Perform an HTTP POST call with the specified form data and URL, returning
* a ClientResponse instance with the data returned from the endpoint.
*
* @param formData
* @param url
* @return a ClientResponse instance with the data returned from the endpoint
* @throws UniformInterfaceException
* @throws ClientHandlerException
*/
protected
ClientResponse
post
(
Form
formData
,
URL
url
)
throws
UniformInterfaceException
,
ClientHandlerException
{
return
(
getApiClient
().
post
(
formData
,
url
));
}
/**
* Perform an HTTP DELETE call with the specified form data and path objects, returning
* a ClientResponse instance with the data returned from the endpoint.
*
* @param queryParams
* @param pathArgs
* @return a ClientResponse instance with the data returned from the endpoint
* @throws UniformInterfaceException
* @throws ClientHandlerException
* @throws IOException
*/
protected
ClientResponse
delete
(
MultivaluedMap
<
String
,
String
>
queryParams
,
Object
...
pathArgs
)
throws
UniformInterfaceException
,
ClientHandlerException
,
IOException
{
return
(
getApiClient
().
delete
(
queryParams
,
pathArgs
));
}
/**
* Perform an HTTP DELETE call with the specified form data and URL, returning
* a ClientResponse instance with the data returned from the endpoint.
*
* @param queryParams
* @param url
* @return a ClientResponse instance with the data returned from the endpoint
* @throws UniformInterfaceException
* @throws ClientHandlerException
*/
protected
ClientResponse
delete
(
MultivaluedMap
<
String
,
String
>
queryParams
,
URL
url
)
throws
UniformInterfaceException
,
ClientHandlerException
{
return
(
getApiClient
().
delete
(
queryParams
,
url
));
}
}
\ No newline at end of file
src/main/java/com/messners/gitlab/api/CommitsApi.java
0 → 100644
View file @
0a399753
package
com.messners.gitlab.api
;
import
java.io.IOException
;
import
java.util.List
;
import
com.messners.gitlab.api.models.Commit
;
import
com.sun.jersey.api.client.ClientResponse
;
import
com.sun.jersey.api.client.GenericType
;
public
class
CommitsApi
extends
AbstractApi
{
public
CommitsApi
(
GitLabApi
gitLabApi
)
{
super
(
gitLabApi
);
}
/**
* GET /projects/:id/repository/commits/branch
*
* @param branch
* @return
*/
public
List
<
Commit
>
getCommits
(
int
projectId
,
String
branch
)
throws
IOException
{
ClientResponse
response
=
get
(
null
,
"projects"
,
projectId
,
"repository"
,
"commits"
,
branch
);
return
(
response
.
getEntity
(
new
GenericType
<
List
<
Commit
>>()
{}));
}
}
src/main/java/com/messners/gitlab/api/MergeRequestApi.java
0 → 100644
View file @
0a399753
package
com.messners.gitlab.api
;
import
java.io.IOException
;
import
com.messners.gitlab.api.models.ErrorMessage
;
import
com.messners.gitlab.api.models.MergeRequest
;
import
com.messners.gitlab.api.models.MergeRequestComment
;
import
com.sun.jersey.api.client.ClientResponse
;
import
com.sun.jersey.api.representation.Form
;
public
class
MergeRequestApi
extends
AbstractApi
{
MergeRequestApi
(
GitLabApi
gitLabApi
)
{
super
(
gitLabApi
);
}
/**
* POST /projects/:id/merge_request/:merge_request_id/comments
*
* @param projectId
* @param mergeRequestId
* @param comments
* @throws IOException
*/
public
MergeRequestComment
addMergeRequestComment
(
Integer
projectId
,
Integer
mergeRequestId
,
String
comments
)
throws
IOException
{
Form
formData
=
new
Form
();
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.
*
* @param projectId the ID of a project, required
* @param sourceBranch the source branch, required
* @param targetBranch the target branch, required
* @param title the title for the merge request, required
* @param description the description of the merge request
* @param assigneeId the Assignee user ID, optional
* @return the created MergeRequest instance
* @throws IOException
*/
public
MergeRequest
createMergeRequest
(
Integer
projectId
,
String
sourceBranch
,
String
targetBranch
,
String
title
,
String
description
,
Integer
assigneeId
)
throws
IOException
{
/*
* 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
)
{
throw
new
RuntimeException
(
"projectId cannot be null"
);
}
Form
formData
=
new
Form
();
formData
.
add
(
"source_branch"
,
sourceBranch
);
formData
.
add
(
"target_branch"
,
targetBranch
);
formData
.
add
(
"title"
,
title
);
if
(
description
!=
null
)
{
formData
.
add
(
"description"
,
description
);
}
if
(
assigneeId
!=
null
)
{
formData
.
add
(
"assignee_id"
,
assigneeId
);
}
ClientResponse
response
=
post
(
formData
,
"projects"
,
projectId
,
"merge_requests"
);
if
(
response
.
getStatus
()
!=
ClientResponse
.
Status
.
CREATED
.
getStatusCode
())
{
ErrorMessage
errorMessage
=
response
.
getEntity
(
ErrorMessage
.
class
);
throw
new
IOException
(
errorMessage
.
getMessage
());
}
return
(
response
.
getEntity
(
MergeRequest
.
class
));
}
}
src/main/java/com/messners/gitlab/api/ProjectApi.java
0 → 100644
View file @
0a399753
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.Event
;
import
com.messners.gitlab.api.models.Project
;
import
com.sun.jersey.api.client.ClientResponse
;
import
com.sun.jersey.api.client.GenericType
;
import
com.sun.jersey.api.representation.Form
;
public
class
ProjectApi
extends
AbstractApi
{
ProjectApi
(
GitLabApi
gitLabApi
)
{
super
(
gitLabApi
);
}
/**
* Creates new project owned by the current user.
*
* @param project the Project instance with the configuration for the new project
* @return a Project instance with the newly created project info
* @throws IOException
*/
public
Project
createProject
(
Project
project
)
throws
IOException
{
return
(
createProject
(
project
,
null
));
}
/**
* Creates new project owned by the current user. The following properties on the Project instance
* are utilized in the creation of the project:
*
* name (required) - new project name
* description (optional) - short project description
* issuesEnabled (optional)
* wallEnabled (optional)
* mergeRequestsEnabled (optional)
* wikiEnabled (optional)
* snippetsEnabled (optional)
* isPublic (optional) - if true same as setting visibility_level = 20
* visibilityLevel (optional)
*
* @param project the Project instance with the configuration for the new project
* @param importUrl
* @return a Project instance with the newly created project info
* @throws IOException
*/
public
Project
createProject
(
Project
project
,
String
importUrl
)
throws
IOException
{
if
(
project
==
null
)
{
return
(
null
);
}
String
name
=
project
.
getName
();
if
(
name
==
null
||
name
.
trim
().
length
()
==
0
)
{
return
(
null
);
}
Form
formData
=
new
Form
();
formData
.
add
(
"name"
,
name
);
formData
.
add
(
"description"
,
project
.
getDescription
());
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
());
}
return
(
response
.
getEntity
(
Project
.
class
));
}
public
boolean
deleteProject
(
Integer
projectId
)
throws
IOException
{
if
(
projectId
==
null
)
{
throw
new
RuntimeException
(
"projectId cannot be null"
);
}
ClientResponse
response
=
delete
(
null
,
"projects"
,
projectId
);
return
(
response
.
getStatus
()
==
ClientResponse
.
Status
.
OK
.
getStatusCode
());
}
public
boolean
deleteProject
(
Project
project
)
throws
IOException
{
return
(
deleteProject
(
project
.
getId
()));
}
public
List
<
Project
>
getAllProjects
()
throws
IOException
{
ClientResponse
response
=
get
(
null
,
"projects"
,
"all"
);
return
(
response
.
getEntity
(
new
GenericType
<
List
<
Project
>>()
{}));
}
public
List
<
Project
>
getOwnedProjects
()
throws
IOException
{
ClientResponse
response
=
get
(
null
,
"projects"
,
"owned"
);
return
(
response
.
getEntity
(
new
GenericType
<
List
<
Project
>>()
{}));
}
public
Project
getProject
(
Integer
projectId
)
throws
IOException
{
ClientResponse
response
=
get
(
null
,
"projects"
,
projectId
);
return
(
response
.
getEntity
(
Project
.
class
));
}
public
List
<
Event
>
getProjectEvents
(
Integer
projectId
)
throws
IOException
{
ClientResponse
response
=
get
(
null
,
"project"
,
projectId
,
"events"
);
return
(
response
.
getEntity
(
new
GenericType
<
List
<
Event
>>()
{}));
}
public
List
<
Project
>
getProjects
()
throws
IOException
{
ClientResponse
response
=
get
(
null
,
"projects"
);
return
(
response
.
getEntity
(
new
GenericType
<
List
<
Project
>>()
{}));
}
}
src/main/java/com/messners/gitlab/api/RepositoryApi.java
0 → 100644
View file @
0a399753
package
com.messners.gitlab.api
;
import
java.io.IOException
;
import
com.messners.gitlab.api.models.Branch
;
import
com.sun.jersey.api.client.ClientResponse
;
public
class
RepositoryApi
extends
AbstractApi
{
public
RepositoryApi
(
GitLabApi
gitLabApi
)
{
super
(
gitLabApi
);
}
/**
* GET /projects/:id/repository/branches/:branch
*
* @param gitLabApi TODO
* @param projectId
* @param branchName
* @return
* @throws IOException
*/
public
Branch
getBranch
(
GitLabApi
gitLabApi
,
Integer
projectId
,
String
branchName
)
throws
IOException
{
ClientResponse
response
=
get
(
null
,
"projects"
,
projectId
,
"repository"
,
"branches"
,
branchName
);
return
(
response
.
getEntity
(
Branch
.
class
));
}
}
src/main/java/com/messners/gitlab/api/UserApi.java
0 → 100644
View file @
0a399753
package
com.messners.gitlab.api
;
import
java.io.IOException
;
import
com.messners.gitlab.api.models.User
;
import
com.sun.jersey.api.client.ClientResponse
;
public
class
UserApi
extends
AbstractApi
{
UserApi
(
GitLabApi
gitLabApi
)
{
super
(
gitLabApi
);
}
/**
* GET /users/:id
*
* @param userId
* @return
* @throws IOException
*/
public
User
getUser
(
int
userId
)
throws
IOException
{
ClientResponse
response
=
get
(
null
,
"users"
,
userId
);
return
(
response
.
getEntity
(
User
.
class
));
}
}
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