Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
J
java-gitlab-api
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
沈俊林
java-gitlab-api
Commits
f1be856a
Commit
f1be856a
authored
Nov 21, 2015
by
Apanasevich Dmitrii
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Adds pagination to commit API
parent
1ff2df69
Changes
6
Hide whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
153 additions
and
7 deletions
+153
-7
build.gradle
build.gradle
+1
-1
GitlabAPI.java
src/main/java/org/gitlab/api/GitlabAPI.java
+30
-6
Pagination.java
src/main/java/org/gitlab/api/Pagination.java
+38
-0
Query.java
src/main/java/org/gitlab/api/http/Query.java
+4
-0
PaginationTest.java
src/test/java/org/gitlab/api/PaginationTest.java
+63
-0
QueryTest.java
src/test/java/org/gitlab/api/http/QueryTest.java
+17
-0
No files found.
build.gradle
View file @
f1be856a
...
...
@@ -11,7 +11,7 @@ sourceCompatibility = 1.7
targetCompatibility
=
1.7
group
=
'org.gitlab'
version
=
'1.1.
8
-SNAPSHOT'
version
=
'1.1.
9
-SNAPSHOT'
repositories
{
mavenLocal
()
...
...
src/main/java/org/gitlab/api/GitlabAPI.java
View file @
f1be856a
...
...
@@ -23,6 +23,7 @@ import java.util.List;
*
* @author @timols (Tim O)
*/
@SuppressWarnings
(
"unused"
)
public
class
GitlabAPI
{
public
static
final
ObjectMapper
MAPPER
=
new
ObjectMapper
().
configure
(
DeserializationFeature
.
FAIL_ON_UNKNOWN_PROPERTIES
,
false
);
...
...
@@ -770,6 +771,10 @@ public class GitlabAPI {
}
public
List
<
GitlabCommit
>
getCommits
(
GitlabMergeRequest
mergeRequest
)
throws
IOException
{
return
getCommits
(
mergeRequest
,
new
Pagination
());
}
public
List
<
GitlabCommit
>
getCommits
(
GitlabMergeRequest
mergeRequest
,
Pagination
pagination
)
throws
IOException
{
Integer
projectId
=
mergeRequest
.
getSourceProjectId
();
if
(
projectId
==
null
)
{
projectId
=
mergeRequest
.
getProjectId
();
...
...
@@ -778,6 +783,8 @@ public class GitlabAPI {
Query
query
=
new
Query
()
.
append
(
"ref_name"
,
mergeRequest
.
getSourceBranch
());
query
.
mergeWith
(
pagination
.
asQuery
());
String
tailUrl
=
GitlabProject
.
URL
+
"/"
+
projectId
+
"/repository"
+
GitlabCommit
.
URL
+
query
.
toString
();
...
...
@@ -787,14 +794,25 @@ public class GitlabAPI {
// gets all commits for a project
public
List
<
GitlabCommit
>
getAllCommits
(
Serializable
projectId
)
throws
IOException
{
String
tailUrl
=
GitlabProject
.
URL
+
"/"
+
sanitizeProjectId
(
projectId
)
+
"/repository"
+
GitlabCommit
.
URL
;
return
getAllCommits
(
projectId
,
new
Pagination
());
}
public
List
<
GitlabCommit
>
getAllCommits
(
Serializable
projectId
,
Pagination
pagination
)
throws
IOException
{
String
tailUrl
=
GitlabProject
.
URL
+
"/"
+
sanitizeProjectId
(
projectId
)
+
"/repository"
+
GitlabCommit
.
URL
+
pagination
;
return
retrieve
().
getAll
(
tailUrl
,
GitlabCommit
[].
class
);
}
// List commit diffs for a project ID and commit hash
// GET /projects/:id/repository/commits/:sha/diff
public
List
<
GitlabCommitDiff
>
getCommitDiffs
(
Serializable
projectId
,
String
commitHash
)
throws
IOException
{
String
tailUrl
=
GitlabProject
.
URL
+
"/"
+
sanitizeProjectId
(
projectId
)
+
"/repository/commits/"
+
commitHash
+
GitlabCommitDiff
.
URL
;
return
getCommitDiffs
(
projectId
,
commitHash
,
new
Pagination
());
}
public
List
<
GitlabCommitDiff
>
getCommitDiffs
(
Serializable
projectId
,
String
commitHash
,
Pagination
pagination
)
throws
IOException
{
String
tailUrl
=
GitlabProject
.
URL
+
"/"
+
sanitizeProjectId
(
projectId
)
+
"/repository/commits/"
+
commitHash
+
GitlabCommitDiff
.
URL
+
pagination
;
GitlabCommitDiff
[]
diffs
=
retrieve
().
to
(
tailUrl
,
GitlabCommitDiff
[].
class
);
return
Arrays
.
asList
(
diffs
);
}
...
...
@@ -802,14 +820,21 @@ public class GitlabAPI {
// List commit statuses for a project ID and commit hash
// GET /projects/:id/repository/commits/:sha/statuses
public
List
<
GitlabCommitStatus
>
getCommitStatuses
(
GitlabProject
project
,
String
commitHash
)
throws
IOException
{
String
tailUrl
=
GitlabProject
.
URL
+
"/"
+
project
.
getId
()
+
"/repository"
+
GitlabCommit
.
URL
+
"/"
+
commitHash
+
GitlabCommitStatus
.
URL
;
return
getCommitStatuses
(
project
,
commitHash
,
new
Pagination
());
}
public
List
<
GitlabCommitStatus
>
getCommitStatuses
(
GitlabProject
project
,
String
commitHash
,
Pagination
pagination
)
throws
IOException
{
String
tailUrl
=
GitlabProject
.
URL
+
"/"
+
project
.
getId
()
+
"/repository"
+
GitlabCommit
.
URL
+
"/"
+
commitHash
+
GitlabCommitStatus
.
URL
+
pagination
;
GitlabCommitStatus
[]
statuses
=
retrieve
().
to
(
tailUrl
,
GitlabCommitStatus
[].
class
);
return
Arrays
.
asList
(
statuses
);
}
// Submit new commit statuses for a project ID and commit hash
// GET /projects/:id/statuses/:sha
public
GitlabCommitStatus
createCommitStatus
(
GitlabProject
project
,
String
commitHash
,
String
state
,
String
ref
,
String
name
,
String
targetUrl
,
String
description
)
throws
IOException
{
public
GitlabCommitStatus
createCommitStatus
(
GitlabProject
project
,
String
commitHash
,
String
state
,
String
ref
,
String
name
,
String
targetUrl
,
String
description
)
throws
IOException
{
String
tailUrl
=
GitlabProject
.
URL
+
"/"
+
project
.
getId
()
+
GitlabCommitStatus
.
URL
+
"/"
+
commitHash
;
return
dispatch
()
.
with
(
"state"
,
state
)
...
...
@@ -956,8 +981,7 @@ public class GitlabAPI {
public
GitlabProjectHook
getProjectHook
(
GitlabProject
project
,
String
hookId
)
throws
IOException
{
String
tailUrl
=
GitlabProject
.
URL
+
"/"
+
project
.
getId
()
+
GitlabProjectHook
.
URL
+
"/"
+
hookId
;
GitlabProjectHook
hook
=
retrieve
().
to
(
tailUrl
,
GitlabProjectHook
.
class
);
return
hook
;
return
retrieve
().
to
(
tailUrl
,
GitlabProjectHook
.
class
);
}
public
GitlabProjectHook
addProjectHook
(
GitlabProject
project
,
String
url
)
throws
IOException
{
...
...
src/main/java/org/gitlab/api/Pagination.java
0 → 100644
View file @
f1be856a
package
org
.
gitlab
.
api
;
import
org.gitlab.api.http.Query
;
import
java.io.UnsupportedEncodingException
;
public
class
Pagination
{
public
static
final
String
PARAM_PAGE
=
"page"
;
public
static
final
String
PARAM_PER_PAGE
=
"per_page"
;
public
static
final
int
MAX_ITEMS_PER_PAGE
=
100
;
private
final
Query
paginationQuery
=
new
Query
();
public
void
setPage
(
int
page
)
{
try
{
paginationQuery
.
append
(
PARAM_PAGE
,
String
.
valueOf
(
page
));
}
catch
(
UnsupportedEncodingException
ignored
)
{
}
}
public
void
setPerPage
(
int
perPage
)
{
if
(
perPage
>
MAX_ITEMS_PER_PAGE
)
{
throw
new
IllegalArgumentException
(
"Max value for perPage is "
+
MAX_ITEMS_PER_PAGE
);
}
try
{
paginationQuery
.
append
(
PARAM_PER_PAGE
,
String
.
valueOf
(
perPage
));
}
catch
(
UnsupportedEncodingException
ignored
)
{
}
}
public
Query
asQuery
()
{
return
paginationQuery
;
}
@Override
public
String
toString
()
{
return
paginationQuery
.
toString
();
}
}
src/main/java/org/gitlab/api/http/Query.java
View file @
f1be856a
...
...
@@ -106,6 +106,10 @@ public class Query {
return
this
;
}
public
boolean
mergeWith
(
Query
query
)
{
return
params
.
addAll
(
query
.
params
);
}
/**
* Returns a Query suitable for appending
* to a URI
...
...
src/test/java/org/gitlab/api/PaginationTest.java
0 → 100644
View file @
f1be856a
package
org
.
gitlab
.
api
;
import
org.gitlab.api.http.Query
;
import
org.junit.Test
;
import
java.io.UnsupportedEncodingException
;
import
static
org
.
junit
.
Assert
.
assertEquals
;
public
class
PaginationTest
{
@Test
public
void
emptyPagination
()
{
Pagination
pagination
=
new
Pagination
();
final
Query
expectedQuery
=
new
Query
();
assertEquals
(
expectedQuery
.
toString
(),
pagination
.
toString
());
assertEquals
(
expectedQuery
.
toString
(),
pagination
.
asQuery
().
toString
());
}
@Test
public
void
pageOnlyPagination
()
throws
UnsupportedEncodingException
{
Pagination
pagination
=
new
Pagination
();
pagination
.
setPage
(
1
);
final
Query
expectedQuery
=
new
Query
()
.
append
(
Pagination
.
PARAM_PAGE
,
"1"
);
assertEquals
(
expectedQuery
.
toString
(),
pagination
.
toString
());
assertEquals
(
expectedQuery
.
toString
(),
pagination
.
asQuery
().
toString
());
}
@Test
public
void
perPageOnlyPagination
()
throws
UnsupportedEncodingException
{
Pagination
pagination
=
new
Pagination
();
pagination
.
setPerPage
(
50
);
final
Query
expectedQuery
=
new
Query
()
.
append
(
Pagination
.
PARAM_PER_PAGE
,
"50"
);
assertEquals
(
expectedQuery
.
toString
(),
pagination
.
toString
());
assertEquals
(
expectedQuery
.
toString
(),
pagination
.
asQuery
().
toString
());
}
@Test
(
expected
=
IllegalArgumentException
.
class
)
public
void
perPageException
()
{
Pagination
pagination
=
new
Pagination
();
pagination
.
setPerPage
(
Pagination
.
MAX_ITEMS_PER_PAGE
+
1
);
}
@Test
public
void
complexPagination
()
throws
UnsupportedEncodingException
{
Pagination
pagination
=
new
Pagination
();
pagination
.
setPage
(
1
);
pagination
.
setPerPage
(
50
);
final
Query
expectedQuery
=
new
Query
()
.
append
(
Pagination
.
PARAM_PAGE
,
"1"
)
.
append
(
Pagination
.
PARAM_PER_PAGE
,
"50"
);
assertEquals
(
expectedQuery
.
toString
(),
pagination
.
toString
());
assertEquals
(
expectedQuery
.
toString
(),
pagination
.
asQuery
().
toString
());
}
}
src/test/java/org/gitlab/api/http/QueryTest.java
View file @
f1be856a
...
...
@@ -5,6 +5,7 @@ import org.junit.Test;
import
java.io.UnsupportedEncodingException
;
import
static
org
.
junit
.
Assert
.
assertEquals
;
import
static
org
.
junit
.
Assert
.
assertTrue
;
public
class
QueryTest
{
...
...
@@ -74,4 +75,20 @@ public class QueryTest {
assertEquals
(
"?p1=v+1&p2=v+2"
,
query
.
toString
());
}
@Test
public
void
merge
()
throws
UnsupportedEncodingException
{
Query
sourceQuery
=
new
Query
()
.
append
(
"p1"
,
"v1"
)
.
append
(
"p2"
,
"v2"
);
Query
targetQuery
=
new
Query
()
.
append
(
"p3"
,
"v3"
)
.
append
(
"p2"
,
"v22"
);
boolean
mergeResult
=
targetQuery
.
mergeWith
(
sourceQuery
);
assertTrue
(
mergeResult
);
assertEquals
(
"?p3=v3&p2=v22&p1=v1&p2=v2"
,
targetQuery
.
toString
());
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a 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