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
a28210cc
Commit
a28210cc
authored
May 21, 2017
by
Greg Messner
Browse files
ProjectApi enhancements.
parent
bbf3ee69
Changes
4
Hide whitespace changes
Inline
Side-by-side
src/main/java/org/gitlab4j/api/AbstractApi.java
View file @
a28210cc
...
@@ -30,6 +30,10 @@ public abstract class AbstractApi {
...
@@ -30,6 +30,10 @@ public abstract class AbstractApi {
return
(
gitLabApi
.
getApiVersion
()
==
apiVersion
);
return
(
gitLabApi
.
getApiVersion
()
==
apiVersion
);
}
}
protected
int
getDefaultPerPage
()
{
return
(
gitLabApi
.
getDefaultPerPage
());
}
protected
GitLabApiClient
getApiClient
()
{
protected
GitLabApiClient
getApiClient
()
{
return
(
gitLabApi
.
getApiClient
());
return
(
gitLabApi
.
getApiClient
());
}
}
...
...
src/main/java/org/gitlab4j/api/GitLabApi.java
View file @
a28210cc
...
@@ -10,6 +10,8 @@ import org.gitlab4j.api.models.Session;
...
@@ -10,6 +10,8 @@ import org.gitlab4j.api.models.Session;
*/
*/
public
class
GitLabApi
{
public
class
GitLabApi
{
public
static
final
int
DEFAULT_PER_PAGE
=
9999
;
public
enum
ApiVersion
{
public
enum
ApiVersion
{
V3
,
V4
;
V3
,
V4
;
...
@@ -20,6 +22,7 @@ public class GitLabApi {
...
@@ -20,6 +22,7 @@ public class GitLabApi {
GitLabApiClient
apiClient
;
GitLabApiClient
apiClient
;
private
ApiVersion
apiVersion
;
private
ApiVersion
apiVersion
;
private
int
defaultPerPage
=
DEFAULT_PER_PAGE
;
private
CommitsApi
commitsApi
;
private
CommitsApi
commitsApi
;
private
GroupApi
groupApi
;
private
GroupApi
groupApi
;
private
MergeRequestApi
mergeRequestApi
;
private
MergeRequestApi
mergeRequestApi
;
...
@@ -201,6 +204,24 @@ public class GitLabApi {
...
@@ -201,6 +204,24 @@ public class GitLabApi {
return
(
apiVersion
);
return
(
apiVersion
);
}
}
/**
* Get the default number per page for calls that return multiple items.
*
* @return the default number per page for calls that return multiple item
*/
public
int
getDefaultPerPage
()
{
return
(
defaultPerPage
);
}
/**
* Set the default number per page for calls that return multiple items.
*
* @param defaultPerPage the new default number per page for calls that return multiple item
*/
public
void
setDefaultPerPage
(
int
defaultPerPage
)
{
this
.
defaultPerPage
=
defaultPerPage
;
}
/**
/**
* Return the GitLabApiClient associated with this instance. This is used by all the sub API classes
* Return the GitLabApiClient associated with this instance. This is used by all the sub API classes
* to communicate with the GitLab API.
* to communicate with the GitLab API.
...
...
src/main/java/org/gitlab4j/api/ProjectApi.java
View file @
a28210cc
...
@@ -15,7 +15,6 @@ import org.gitlab4j.api.models.Member;
...
@@ -15,7 +15,6 @@ import org.gitlab4j.api.models.Member;
import
org.gitlab4j.api.models.Project
;
import
org.gitlab4j.api.models.Project
;
import
org.gitlab4j.api.models.ProjectHook
;
import
org.gitlab4j.api.models.ProjectHook
;
import
org.gitlab4j.api.models.Visibility
;
import
org.gitlab4j.api.models.Visibility
;
import
org.glassfish.jersey.uri.UriComponent
;
/**
/**
* This class provides an entry point to all the GitLab API project calls.
* This class provides an entry point to all the GitLab API project calls.
...
@@ -24,7 +23,7 @@ public class ProjectApi extends AbstractApi {
...
@@ -24,7 +23,7 @@ public class ProjectApi extends AbstractApi {
public
ProjectApi
(
GitLabApi
gitLabApi
)
{
public
ProjectApi
(
GitLabApi
gitLabApi
)
{
super
(
gitLabApi
);
super
(
gitLabApi
);
}
}
/**
/**
* Get a list of projects accessible by the authenticated user.
* Get a list of projects accessible by the authenticated user.
...
@@ -35,7 +34,66 @@ public class ProjectApi extends AbstractApi {
...
@@ -35,7 +34,66 @@ public class ProjectApi extends AbstractApi {
* @throws GitLabApiException if any exception occurs
* @throws GitLabApiException if any exception occurs
*/
*/
public
List
<
Project
>
getProjects
()
throws
GitLabApiException
{
public
List
<
Project
>
getProjects
()
throws
GitLabApiException
{
Response
response
=
get
(
Response
.
Status
.
OK
,
null
,
"projects"
);
Form
formData
=
new
GitLabApiForm
().
withParam
(
"per_page"
,
getDefaultPerPage
());
Response
response
=
get
(
Response
.
Status
.
OK
,
formData
.
asMap
(),
"projects"
);
return
(
response
.
readEntity
(
new
GenericType
<
List
<
Project
>>()
{
}));
}
/**
* Get a list of projects accessible by the authenticated user and matching the supplied filter parameters.
* All filter parameters are optional.
*
* GET /projects
*
* @param archived limit by archived status
* @param visibility limit by visibility public, internal, or private
* @param orderBy return projects ordered by id, name, path, created_at, updated_at, or last_activity_at fields, default is created_at
* @param sort return projects sorted in asc or desc order. Default is desc
* @param search return list of projects matching the search criteria
* @param simple return only the ID, URL, name, and path of each project
* @param owned limit by projects owned by the current user
* @param membership limit by projects that the current user is a member of
* @param starred limit by projects starred by the current user
* @param statistics include project statistics
* @return a list of projects accessible by the authenticated user and matching the supplied parameters
* @throws GitLabApiException if any exception occurs
*/
public
List
<
Project
>
getProjects
(
Boolean
archived
,
Visibility
visibility
,
String
orderBy
,
String
sort
,
String
search
,
Boolean
simple
,
Boolean
owned
,
Boolean
membership
,
Boolean
starred
,
Boolean
statistics
)
throws
GitLabApiException
{
GitLabApiForm
formData
=
new
GitLabApiForm
()
.
withParam
(
"archived"
,
archived
)
.
withParam
(
"visibility"
,
visibility
)
.
withParam
(
"order_by"
,
orderBy
)
.
withParam
(
"sort"
,
sort
)
.
withParam
(
"search"
,
search
)
.
withParam
(
"simple"
,
simple
)
.
withParam
(
"owned"
,
owned
)
.
withParam
(
"membership"
,
membership
)
.
withParam
(
"starred"
,
starred
)
.
withParam
(
"statistics"
,
statistics
)
.
withParam
(
"per_page"
,
getDefaultPerPage
());
Response
response
=
get
(
Response
.
Status
.
OK
,
formData
.
asMap
(),
"projects"
);
return
(
response
.
readEntity
(
new
GenericType
<
List
<
Project
>>()
{
}));
}
/**
* Get a list of projects accessible by the authenticated user that match the provided search string.
*
* GET /projects?search=search
*
* @param search the project name search criteria
* @return a list of projects accessible by the authenticated user that match the provided search string
* @throws GitLabApiException if any exception occurs
*/
public
List
<
Project
>
getProjects
(
String
search
)
throws
GitLabApiException
{
Form
formData
=
new
GitLabApiForm
().
withParam
(
"search"
,
search
).
withParam
(
"per_page"
,
getDefaultPerPage
());
Response
response
=
get
(
Response
.
Status
.
OK
,
formData
.
asMap
(),
"projects"
);
return
(
response
.
readEntity
(
new
GenericType
<
List
<
Project
>>()
{
return
(
response
.
readEntity
(
new
GenericType
<
List
<
Project
>>()
{
}));
}));
}
}
...
@@ -49,7 +107,7 @@ public class ProjectApi extends AbstractApi {
...
@@ -49,7 +107,7 @@ public class ProjectApi extends AbstractApi {
* @throws GitLabApiException if any exception occurs
* @throws GitLabApiException if any exception occurs
*/
*/
public
List
<
Project
>
getMemberProjects
()
throws
GitLabApiException
{
public
List
<
Project
>
getMemberProjects
()
throws
GitLabApiException
{
Form
formData
=
new
GitLabApiForm
().
withParam
(
"membership"
,
true
);
Form
formData
=
new
GitLabApiForm
().
withParam
(
"membership"
,
true
)
.
withParam
(
"per_page"
,
getDefaultPerPage
())
;
Response
response
=
get
(
Response
.
Status
.
OK
,
formData
.
asMap
(),
"projects"
);
Response
response
=
get
(
Response
.
Status
.
OK
,
formData
.
asMap
(),
"projects"
);
return
(
response
.
readEntity
(
new
GenericType
<
List
<
Project
>>()
{
return
(
response
.
readEntity
(
new
GenericType
<
List
<
Project
>>()
{
}));
}));
...
@@ -62,24 +120,47 @@ public class ProjectApi extends AbstractApi {
...
@@ -62,24 +120,47 @@ public class ProjectApi extends AbstractApi {
*
*
* @return a list of all GitLab projects
* @return a list of all GitLab projects
* @throws GitLabApiException if any exception occurs
* @throws GitLabApiException if any exception occurs
* @deprecated Will be removed, no longer supported by the GitLab API
*/
*/
public
List
<
Project
>
getAllProjects
()
throws
GitLabApiException
{
public
List
<
Project
>
getAllProjects
()
throws
GitLabApiException
{
Response
response
=
get
(
Response
.
Status
.
OK
,
UriComponent
.
decodeQuery
(
"per_page=9999"
,
true
),
"projects"
,
"all"
);
if
(!
isApiVersion
(
ApiVersion
.
V3
))
{
throw
new
GitLabApiException
(
"Not supported by GitLab API version "
+
this
.
getApiVersion
());
}
Form
formData
=
new
GitLabApiForm
().
withParam
(
"per_page"
,
getDefaultPerPage
());
Response
response
=
get
(
Response
.
Status
.
OK
,
formData
.
asMap
(),
"projects"
,
"all"
);
return
(
response
.
readEntity
(
new
GenericType
<
List
<
Project
>>()
{
return
(
response
.
readEntity
(
new
GenericType
<
List
<
Project
>>()
{
}));
}));
}
}
/**
/**
* Get a list of projects owned by the authenticated user.
* Get a list of projects owned by the authenticated user.
*
*
* GET /projects
/owned
* GET /projects
*
*
* @return a list of projects owned by the authenticated user
* @return a list of projects owned by the authenticated user
* @throws GitLabApiException if any exception occurs
* @throws GitLabApiException if any exception occurs
*/
*/
public
List
<
Project
>
getOwnedProjects
()
throws
GitLabApiException
{
public
List
<
Project
>
getOwnedProjects
()
throws
GitLabApiException
{
Form
formData
=
new
GitLabApiForm
().
withParam
(
"owned"
,
true
);
Form
formData
=
new
GitLabApiForm
().
withParam
(
"owned"
,
true
).
withParam
(
"per_page"
,
getDefaultPerPage
());
Response
response
=
get
(
Response
.
Status
.
OK
,
formData
.
asMap
(),
"projects"
);
return
(
response
.
readEntity
(
new
GenericType
<
List
<
Project
>>()
{
}));
}
/**
* Get a list of projects starred by the authenticated user.
*
* GET /projects
*
* @return a list of projects starred by the authenticated user
* @throws GitLabApiException if any exception occurs
*/
public
List
<
Project
>
getStarredProjects
()
throws
GitLabApiException
{
Form
formData
=
new
GitLabApiForm
().
withParam
(
"starred"
,
true
).
withParam
(
"per_page"
,
getDefaultPerPage
());
Response
response
=
get
(
Response
.
Status
.
OK
,
formData
.
asMap
(),
"projects"
);
Response
response
=
get
(
Response
.
Status
.
OK
,
formData
.
asMap
(),
"projects"
);
return
(
response
.
readEntity
(
new
GenericType
<
List
<
Project
>>()
{
return
(
response
.
readEntity
(
new
GenericType
<
List
<
Project
>>()
{
}));
}));
...
@@ -691,7 +772,8 @@ public class ProjectApi extends AbstractApi {
...
@@ -691,7 +772,8 @@ public class ProjectApi extends AbstractApi {
* @throws GitLabApiException if any exception occurs
* @throws GitLabApiException if any exception occurs
*/
*/
public
List
<
Issue
>
getIssues
(
Integer
projectId
)
throws
GitLabApiException
{
public
List
<
Issue
>
getIssues
(
Integer
projectId
)
throws
GitLabApiException
{
Response
response
=
get
(
Response
.
Status
.
OK
,
null
,
"projects"
,
projectId
,
"issues"
);
Form
formData
=
new
GitLabApiForm
().
withParam
(
"per_page"
,
getDefaultPerPage
());
Response
response
=
get
(
Response
.
Status
.
OK
,
formData
.
asMap
(),
"projects"
,
projectId
,
"issues"
);
return
(
response
.
readEntity
(
new
GenericType
<
List
<
Issue
>>()
{
return
(
response
.
readEntity
(
new
GenericType
<
List
<
Issue
>>()
{
}));
}));
}
}
...
...
src/test/java/org/gitlab4j/api/TestProjectApi.java
View file @
a28210cc
...
@@ -5,6 +5,8 @@ import static org.junit.Assert.assertNotNull;
...
@@ -5,6 +5,8 @@ import static org.junit.Assert.assertNotNull;
import
static
org
.
junit
.
Assert
.
assertTrue
;
import
static
org
.
junit
.
Assert
.
assertTrue
;
import
static
org
.
junit
.
Assume
.
assumeTrue
;
import
static
org
.
junit
.
Assume
.
assumeTrue
;
import
java.util.List
;
import
org.gitlab4j.api.GitLabApi.ApiVersion
;
import
org.gitlab4j.api.GitLabApi.ApiVersion
;
import
org.gitlab4j.api.models.Project
;
import
org.gitlab4j.api.models.Project
;
import
org.gitlab4j.api.models.Visibility
;
import
org.gitlab4j.api.models.Visibility
;
...
@@ -127,13 +129,37 @@ public class TestProjectApi {
...
@@ -127,13 +129,37 @@ public class TestProjectApi {
}
}
@Test
@Test
public
void
testDelete
()
throws
GitLabApiException
{
public
void
testListProjects
()
throws
GitLabApiException
{
List
<
Project
>
projects
=
gitLabApi
.
getProjectApi
().
getProjects
();
assertNotNull
(
projects
);
assertTrue
(
projects
.
size
()
>=
2
);
int
matchCount
=
0
;
for
(
Project
project
:
projects
)
{
if
(
TEST_PROJECT_NAME
.
equals
(
project
.
getName
()))
matchCount
++;
else
if
(
TEST_PROJECT_NAME_2
.
equals
(
project
.
getName
()))
matchCount
++;
}
assertEquals
(
2
,
matchCount
);
projects
=
gitLabApi
.
getProjectApi
().
getProjects
(
TEST_PROJECT_NAME
);
assertNotNull
(
projects
);
assertEquals
(
2
,
projects
.
size
());
assertEquals
(
TEST_PROJECT_NAME_2
,
projects
.
get
(
0
).
getName
());
assertEquals
(
TEST_PROJECT_NAME
,
projects
.
get
(
1
).
getName
());
}
@Test
public
void
testRemoveByDelete
()
throws
GitLabApiException
{
Project
project
=
gitLabApi
.
getProjectApi
().
getProject
(
TEST_NAMESPACE
,
TEST_PROJECT_NAME
);
Project
project
=
gitLabApi
.
getProjectApi
().
getProject
(
TEST_NAMESPACE
,
TEST_PROJECT_NAME
);
gitLabApi
.
getProjectApi
().
deleteProject
(
project
);
gitLabApi
.
getProjectApi
().
deleteProject
(
project
);
}
}
@Test
@Test
public
void
testParameterBased
Create
()
throws
GitLabApiException
{
public
void
test
Create
ParameterBased
()
throws
GitLabApiException
{
Project
newProject
=
gitLabApi
.
getProjectApi
().
createProject
(
Project
newProject
=
gitLabApi
.
getProjectApi
().
createProject
(
TEST_PROJECT_NAME_2
,
null
,
"GitLab4J test project."
,
true
,
true
,
true
,
true
,
Visibility
.
PUBLIC
,
null
,
null
);
TEST_PROJECT_NAME_2
,
null
,
"GitLab4J test project."
,
true
,
true
,
true
,
true
,
Visibility
.
PUBLIC
,
null
,
null
);
...
@@ -145,10 +171,12 @@ public class TestProjectApi {
...
@@ -145,10 +171,12 @@ public class TestProjectApi {
assertEquals
(
true
,
newProject
.
getWikiEnabled
());
assertEquals
(
true
,
newProject
.
getWikiEnabled
());
assertEquals
(
true
,
newProject
.
getSnippetsEnabled
());
assertEquals
(
true
,
newProject
.
getSnippetsEnabled
());
assertTrue
(
Visibility
.
PUBLIC
==
newProject
.
getVisibility
()
||
Boolean
.
TRUE
==
newProject
.
getPublic
());
assertTrue
(
Visibility
.
PUBLIC
==
newProject
.
getVisibility
()
||
Boolean
.
TRUE
==
newProject
.
getPublic
());
}
try
{
@Test
gitLabApi
.
getProjectApi
().
deleteProject
(
newProject
);
public
void
testRemoveByDeleteParameterBased
()
throws
GitLabApiException
{
}
catch
(
Exception
ignore
)
{
Project
project
=
gitLabApi
.
getProjectApi
().
getProject
(
TEST_NAMESPACE
,
TEST_PROJECT_NAME_2
);
}
gitLabApi
.
getProjectApi
().
deleteProject
(
project
);
}
}
}
}
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