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
8d6ea6a2
Unverified
Commit
8d6ea6a2
authored
Feb 29, 2020
by
Hrotkó Gábor
Committed by
GitHub
Feb 28, 2020
Browse files
Add method to get branches with a search term (#515)
parent
accc9f0d
Changes
4
Hide whitespace changes
Inline
Side-by-side
README.md
View file @
8d6ea6a2
...
@@ -500,7 +500,11 @@ List<Release> releases = gitLabApi.getReleasesApi().getReleases(projectId);
...
@@ -500,7 +500,11 @@ List<Release> releases = gitLabApi.getReleasesApi().getReleases(projectId);
#### RepositoryApi
#### RepositoryApi
```
java
```
java
// Get a list of repository branches from a project, sorted by name alphabetically
// Get a list of repository branches from a project, sorted by name alphabetically
List
<
Branch
>
branches
=
gitLabApi
.
getRepositoryApi
().
getBranches
();
List
<
Branch
>
branches
=
gitLabApi
.
getRepositoryApi
().
getBranches
(
projectId
);
```
```
java
// Search repository branches from a project, by name
List
<
Branch
>
branches
=
gitLabApi
.
getRepositoryApi
().
getBranches
(
projectId
,
searchTerm
);
```
```
#### RepositoryFileApi
#### RepositoryFileApi
...
...
src/main/java/org/gitlab4j/api/RepositoryApi.java
View file @
8d6ea6a2
...
@@ -12,6 +12,7 @@ import java.util.stream.Stream;
...
@@ -12,6 +12,7 @@ import java.util.stream.Stream;
import
javax.ws.rs.core.Form
;
import
javax.ws.rs.core.Form
;
import
javax.ws.rs.core.GenericType
;
import
javax.ws.rs.core.GenericType
;
import
javax.ws.rs.core.MediaType
;
import
javax.ws.rs.core.MediaType
;
import
javax.ws.rs.core.MultivaluedMap
;
import
javax.ws.rs.core.Response
;
import
javax.ws.rs.core.Response
;
import
org.gitlab4j.api.GitLabApi.ApiVersion
;
import
org.gitlab4j.api.GitLabApi.ApiVersion
;
...
@@ -41,7 +42,7 @@ public class RepositoryApi extends AbstractApi {
...
@@ -41,7 +42,7 @@ public class RepositoryApi extends AbstractApi {
* @throws GitLabApiException if any exception occurs
* @throws GitLabApiException if any exception occurs
*/
*/
public
List
<
Branch
>
getBranches
(
Object
projectIdOrPath
)
throws
GitLabApiException
{
public
List
<
Branch
>
getBranches
(
Object
projectIdOrPath
)
throws
GitLabApiException
{
return
(
getBranches
(
projectIdOrPath
,
getDefaultPerPage
()).
all
()
);
return
getBranches
(
projectIdOrPath
,
null
);
}
}
/**
/**
...
@@ -73,8 +74,7 @@ public class RepositoryApi extends AbstractApi {
...
@@ -73,8 +74,7 @@ public class RepositoryApi extends AbstractApi {
* @throws GitLabApiException if any exception occurs
* @throws GitLabApiException if any exception occurs
*/
*/
public
Pager
<
Branch
>
getBranches
(
Object
projectIdOrPath
,
int
itemsPerPage
)
throws
GitLabApiException
{
public
Pager
<
Branch
>
getBranches
(
Object
projectIdOrPath
,
int
itemsPerPage
)
throws
GitLabApiException
{
return
(
new
Pager
<
Branch
>(
this
,
Branch
.
class
,
itemsPerPage
,
null
,
"projects"
,
return
getBranches
(
projectIdOrPath
,
null
,
itemsPerPage
);
getProjectIdOrPath
(
projectIdOrPath
),
"repository"
,
"branches"
));
}
}
/**
/**
...
@@ -87,7 +87,7 @@ public class RepositoryApi extends AbstractApi {
...
@@ -87,7 +87,7 @@ public class RepositoryApi extends AbstractApi {
* @throws GitLabApiException if any exception occurs
* @throws GitLabApiException if any exception occurs
*/
*/
public
Stream
<
Branch
>
getBranchesStream
(
Object
projectIdOrPath
)
throws
GitLabApiException
{
public
Stream
<
Branch
>
getBranchesStream
(
Object
projectIdOrPath
)
throws
GitLabApiException
{
return
(
getBranches
(
projectIdOrPath
,
getDefaultPerPage
()).
stream
()
);
return
getBranches
Stream
(
projectIdOrPath
,
null
);
}
}
/**
/**
...
@@ -106,6 +106,55 @@ public class RepositoryApi extends AbstractApi {
...
@@ -106,6 +106,55 @@ public class RepositoryApi extends AbstractApi {
return
(
response
.
readEntity
(
Branch
.
class
));
return
(
response
.
readEntity
(
Branch
.
class
));
}
}
/**
* Get a List of repository branches from a project, sorted by name alphabetically, filter by the search term.
*
* <pre><code>GitLab Endpoint: GET /projects/:id/repository/branches?search=:search</code></pre>
*
* @param projectIdOrPath the project in the form of an Integer(ID), String(path), or Project instance
* @param search the branch name search term
* @return the List of repository branches for the specified project ID and search term
* @throws GitLabApiException if any exception occurs
*/
public
List
<
Branch
>
getBranches
(
Object
projectIdOrPath
,
String
search
)
throws
GitLabApiException
{
return
(
getBranches
(
projectIdOrPath
,
search
,
getDefaultPerPage
()).
all
());
}
/**
* Get a Stream of repository branches from a project, sorted by name alphabetically, filter by the search term.
*
* <pre><code>GitLab Endpoint: GET /projects/:id/repository/branches?search=:search</code></pre>
*
* @param projectIdOrPath the project in the form of an Integer(ID), String(path), or Project instance
* @param search the branch name search term
* @return the Stream of repository branches for the specified project ID and search term
*
* @throws GitLabApiException if any exception occurs
*/
public
Stream
<
Branch
>
getBranchesStream
(
Object
projectIdOrPath
,
String
search
)
throws
GitLabApiException
{
return
(
getBranches
(
projectIdOrPath
,
search
,
getDefaultPerPage
()).
stream
());
}
/**
* Get a Pager of repository branches from a project, sorted by name alphabetically, filter by the search term.
*
* <pre><code>GitLab Endpoint: GET /projects/:id/repository/branches?search=:search</code></pre>
*
* @param projectIdOrPath the project in the form of an Integer(ID), String(path), or Project instance
* @param search the branch name search term
* @param itemsPerPage the number of Project instances that will be fetched per page
* @return the list of repository branches for the specified project ID and search term
*
* @throws GitLabApiException if any exception occurs
*/
public
Pager
<
Branch
>
getBranches
(
Object
projectIdOrPath
,
String
search
,
int
itemsPerPage
)
throws
GitLabApiException
{
MultivaluedMap
<
String
,
String
>
queryParams
=
(
search
==
null
?
null
:
new
GitLabApiForm
().
withParam
(
"search"
,
urlEncode
(
search
)).
asMap
()
);
return
(
new
Pager
<
Branch
>(
this
,
Branch
.
class
,
itemsPerPage
,
queryParams
,
"projects"
,
getProjectIdOrPath
(
projectIdOrPath
),
"repository"
,
"branches"
));
}
/**
/**
* Get an Optional instance with the value for the specific repository branch.
* Get an Optional instance with the value for the specific repository branch.
*
*
...
@@ -222,7 +271,7 @@ public class RepositoryApi extends AbstractApi {
...
@@ -222,7 +271,7 @@ public class RepositoryApi extends AbstractApi {
}
}
/**
/**
* Get a
list
of repository files and directories in a project.
* Get a
Stream
of repository files and directories in a project.
*
*
* <pre><code>GitLab Endpoint: GET /projects/:id/repository/tree</code></pre>
* <pre><code>GitLab Endpoint: GET /projects/:id/repository/tree</code></pre>
*
*
...
...
src/test/java/org/gitlab4j/api/IntegrationTestSuite.java
View file @
8d6ea6a2
...
@@ -128,7 +128,7 @@ public class IntegrationTestSuite implements PropertyConstants {
...
@@ -128,7 +128,7 @@ public class IntegrationTestSuite implements PropertyConstants {
AccessTokenUtils
.
revokePersonalAccessToken
(
AccessTokenUtils
.
revokePersonalAccessToken
(
TEST_HOST_URL
,
TEST_LOGIN_USERNAME
,
TEST_LOGIN_PASSWORD
,
TEST_HOST_URL
,
TEST_LOGIN_USERNAME
,
TEST_LOGIN_PASSWORD
,
TEST_PRIVATE_TOKEN_NAME
,
Arrays
.
asList
(
Scope
.
API
,
Scope
.
SUDO
));
TEST_PRIVATE_TOKEN_NAME
,
Arrays
.
asList
(
Scope
.
API
,
Scope
.
SUDO
));
System
.
out
.
format
(
"Revo
v
ed '%s'%n"
,
TEST_PRIVATE_TOKEN_NAME
);
System
.
out
.
format
(
"Revo
k
ed '%s'%n"
,
TEST_PRIVATE_TOKEN_NAME
);
}
catch
(
Exception
ignore
)
{}
}
catch
(
Exception
ignore
)
{}
}
}
...
@@ -137,7 +137,7 @@ public class IntegrationTestSuite implements PropertyConstants {
...
@@ -137,7 +137,7 @@ public class IntegrationTestSuite implements PropertyConstants {
AccessTokenUtils
.
revokePersonalAccessToken
(
AccessTokenUtils
.
revokePersonalAccessToken
(
TEST_HOST_URL
,
TEST_LOGIN_USERNAME
,
TEST_LOGIN_PASSWORD
,
TEST_HOST_URL
,
TEST_LOGIN_USERNAME
,
TEST_LOGIN_PASSWORD
,
TEST_ACCESS_TOKEN_NAME
,
Arrays
.
asList
(
Scope
.
API
,
Scope
.
SUDO
));
TEST_ACCESS_TOKEN_NAME
,
Arrays
.
asList
(
Scope
.
API
,
Scope
.
SUDO
));
System
.
out
.
format
(
"Revo
v
ed '%s'%n"
,
TEST_ACCESS_TOKEN_NAME
);
System
.
out
.
format
(
"Revo
k
ed '%s'%n"
,
TEST_ACCESS_TOKEN_NAME
);
}
catch
(
Exception
ignore
)
{}
}
catch
(
Exception
ignore
)
{}
}
}
}
}
...
...
src/test/java/org/gitlab4j/api/TestRepositoryApi.java
View file @
8d6ea6a2
...
@@ -44,6 +44,7 @@ import org.junit.runners.MethodSorters;
...
@@ -44,6 +44,7 @@ import org.junit.runners.MethodSorters;
public
class
TestRepositoryApi
extends
AbstractIntegrationTest
{
public
class
TestRepositoryApi
extends
AbstractIntegrationTest
{
private
static
final
String
TEST_BRANCH_NAME
=
"feature/test_branch"
;
private
static
final
String
TEST_BRANCH_NAME
=
"feature/test_branch"
;
private
static
final
String
TEST_BRANCH_SEARCH_TERM
=
"test"
;
private
static
final
String
TEST_BRANCH1
=
"feature/test_branch1"
;
private
static
final
String
TEST_BRANCH1
=
"feature/test_branch1"
;
private
static
final
String
TEST_BRANCH2
=
"feature/test_branch2"
;
private
static
final
String
TEST_BRANCH2
=
"feature/test_branch2"
;
private
static
final
String
TEST_PROTECT_BRANCH_NAME
=
"feature/protect_branch"
;
private
static
final
String
TEST_PROTECT_BRANCH_NAME
=
"feature/protect_branch"
;
...
@@ -107,6 +108,12 @@ public class TestRepositoryApi extends AbstractIntegrationTest {
...
@@ -107,6 +108,12 @@ public class TestRepositoryApi extends AbstractIntegrationTest {
Branch
fetchedBranch
=
gitLabApi
.
getRepositoryApi
().
getBranch
(
project
.
getId
(),
TEST_BRANCH_NAME
);
Branch
fetchedBranch
=
gitLabApi
.
getRepositoryApi
().
getBranch
(
project
.
getId
(),
TEST_BRANCH_NAME
);
assertNotNull
(
fetchedBranch
);
assertNotNull
(
fetchedBranch
);
List
<
Branch
>
searchBranches
=
gitLabApi
.
getRepositoryApi
().
getBranches
(
project
.
getId
(),
TEST_BRANCH_SEARCH_TERM
);
assertEquals
(
searchBranches
.
size
(),
1
);
searchBranches
=
gitLabApi
.
getRepositoryApi
().
getBranches
(
project
.
getId
());
assertTrue
(
searchBranches
.
size
()
>
1
);
assertEquals
(
branch
.
getName
(),
fetchedBranch
.
getName
());
assertEquals
(
branch
.
getName
(),
fetchedBranch
.
getName
());
}
}
...
...
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