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
b2902fbe
Commit
b2902fbe
authored
Dec 14, 2019
by
Greg Messner
Browse files
Added support for merge request rebase (#484).
parent
182165ea
Changes
3
Hide whitespace changes
Inline
Side-by-side
src/main/java/org/gitlab4j/api/MergeRequestApi.java
View file @
b2902fbe
...
...
@@ -38,7 +38,7 @@ public class MergeRequestApi extends AbstractApi {
*
* @param filter a MergeRequestFilter instance with the filter settings
* @return all merge requests for the specified project matching the filter
* @throws GitLabApiException if any exception occurs
* @throws GitLabApiException if any exception occurs
put
*/
public
List
<
MergeRequest
>
getMergeRequests
(
MergeRequestFilter
filter
)
throws
GitLabApiException
{
return
(
getMergeRequests
(
filter
,
getDefaultPerPage
()).
all
());
...
...
@@ -1058,4 +1058,40 @@ public class MergeRequestApi extends AbstractApi {
public
List
<
Issue
>
getApprovalStatus
(
Object
projectIdOrPath
,
Integer
mergeRequestIid
)
throws
GitLabApiException
{
return
(
getClosesIssues
(
projectIdOrPath
,
mergeRequestIid
,
getDefaultPerPage
()).
all
());
}
/**
* Automatically rebase the source_branch of the merge request against its target_branch.
*
* This is an asynchronous request. The API will return a 202 Accepted response if the
* request is enqueued successfully
*
* <pre><code>GitLab Endpoint: PUT /projects/:id/merge_requests/:merge_request_iid/rebase</code></pre>
*
* @param projectIdOrPath the project in the form of an Integer(ID), String(path), or Project instance
* @param mergeRequestIid the internal ID of the merge request to rebase
* @return the merge request info containing the status of a merge request rebase
* @throws GitLabApiException if any exception occurs
*/
public
MergeRequest
rebaseMergeRequest
(
Object
projectIdOrPath
,
Integer
mergeRequestIid
)
throws
GitLabApiException
{
Response
response
=
put
(
Response
.
Status
.
ACCEPTED
,
null
,
"projects"
,
getProjectIdOrPath
(
projectIdOrPath
),
"merge_requests"
,
mergeRequestIid
,
"rebase"
);
return
(
response
.
readEntity
(
MergeRequest
.
class
));
}
/**
* Get the merge request info containing the status of a merge request rebase.
*
* <pre><code>GitLab Endpoint: GET /projects/:id/merge_requests/:merge_request_iid</code></pre>
*
* @param projectIdOrPath the project in the form of an Integer(ID), String(path), or Project instance
* @param mergeRequestIid the internal ID of the merge request being rebased
* @return the merge request info containing the status of a merge request rebase
* @throws GitLabApiException if any exception occurs
*/
public
MergeRequest
getRebaseStatus
(
Object
projectIdOrPath
,
Integer
mergeRequestIid
)
throws
GitLabApiException
{
GitLabApiForm
queryParams
=
new
GitLabApiForm
().
withParam
(
"include_rebase_in_progress"
,
true
);
Response
response
=
get
(
Response
.
Status
.
OK
,
queryParams
.
asMap
(),
"projects"
,
getProjectIdOrPath
(
projectIdOrPath
),
"merge_requests"
,
mergeRequestIid
);
return
(
response
.
readEntity
(
MergeRequest
.
class
));
}
}
src/main/java/org/gitlab4j/api/models/MergeRequest.java
View file @
b2902fbe
...
...
@@ -52,6 +52,7 @@ public class MergeRequest {
private
String
webUrl
;
private
Boolean
workInProgress
;
private
DiffRef
diffRefs
;
private
Boolean
rebaseInProgress
;
// The approval fields will only be available when listing approvals, approving or unapproving a merge reuest.
private
Integer
approvalsRequired
;
...
...
@@ -463,6 +464,14 @@ public class MergeRequest {
this
.
diffRefs
=
diffRefs
;
}
public
Boolean
getRebaseInProgress
()
{
return
rebaseInProgress
;
}
public
void
setRebaseInProgress
(
Boolean
rebaseInProgress
)
{
this
.
rebaseInProgress
=
rebaseInProgress
;
}
public
static
final
boolean
isValid
(
MergeRequest
mergeRequest
)
{
return
(
mergeRequest
!=
null
&&
mergeRequest
.
getId
()
!=
null
);
}
...
...
src/test/java/org/gitlab4j/api/TestMergeRequestApi.java
View file @
b2902fbe
...
...
@@ -3,10 +3,11 @@ package org.gitlab4j.api;
import
static
org
.
junit
.
Assert
.
assertEquals
;
import
static
org
.
junit
.
Assert
.
assertFalse
;
import
static
org
.
junit
.
Assert
.
assertNotNull
;
import
static
org
.
junit
.
Assert
.
fail
;
import
static
org
.
junit
.
Assume
.
assumeNotNull
;
import
java.util.List
;
import
java.util.Optional
;
import
java.util.stream.Stream
;
import
org.gitlab4j.api.models.Branch
;
import
org.gitlab4j.api.models.MergeRequest
;
...
...
@@ -41,6 +42,8 @@ public class TestMergeRequestApi extends AbstractIntegrationTest {
private
static
final
String
TEST_BRANCH_NAME
=
"feature/gitlab4j-merge-request-test"
;
private
static
final
String
TEST_MR_TITLE
=
"Merge Request test: gitlab4j-merge-request-test"
;
private
static
final
String
TEST_DESCRIPTION
=
"Description for Merge Request test"
;
private
static
final
String
TEST_REBASE_BRANCH_NAME
=
"feature/gitlab4j-merge-request-rebase-test"
;
private
static
final
String
TEST_REBASE_MR_TITLE
=
"Merge Request test: gitlab4j-merge-request-rebase-test"
;
private
static
GitLabApi
gitLabApi
;
private
static
Project
testProject
;
...
...
@@ -70,11 +73,17 @@ public class TestMergeRequestApi extends AbstractIntegrationTest {
try
{
Stream
<
MergeRequest
>
mergeRequests
=
gitLabApi
.
getMergeRequestApi
().
getMergeRequests
Stream
(
testProject
);
MergeRequest
mergeRequest
=
mergeRequests
.
filter
(
m
->
TEST_MR_TITLE
.
equals
(
m
.
getTitle
())).
findFirst
().
orElse
(
null
);
List
<
MergeRequest
>
mergeRequests
=
gitLabApi
.
getMergeRequestApi
().
getMergeRequests
(
testProject
);
MergeRequest
mergeRequest
=
mergeRequests
.
stream
().
filter
(
m
->
TEST_MR_TITLE
.
equals
(
m
.
getTitle
())).
findFirst
().
orElse
(
null
);
if
(
mergeRequest
!=
null
)
{
gitLabApi
.
getMergeRequestApi
().
deleteMergeRequest
(
testProject
,
mergeRequest
.
getIid
());
gitLabApi
.
getMergeRequestApi
().
deleteMergeRequest
(
testProject
,
mergeRequest
.
getIid
());
}
mergeRequest
=
mergeRequests
.
stream
().
filter
(
m
->
TEST_REBASE_MR_TITLE
.
equals
(
m
.
getTitle
())).
findFirst
().
orElse
(
null
);
if
(
mergeRequest
!=
null
)
{
gitLabApi
.
getMergeRequestApi
().
deleteMergeRequest
(
testProject
,
mergeRequest
.
getIid
());
}
}
catch
(
GitLabApiException
ignore
)
{
...
...
@@ -144,4 +153,77 @@ public class TestMergeRequestApi extends AbstractIntegrationTest {
}
}
}
@Test
public
void
testRebaseMergeRequest
()
throws
GitLabApiException
{
// Create a test branch
Branch
branch
=
gitLabApi
.
getRepositoryApi
().
createBranch
(
testProject
,
TEST_REBASE_BRANCH_NAME
,
"master"
);
assertNotNull
(
branch
);
// Create a new file in the test branch
RepositoryFile
repoFile
=
new
RepositoryFile
();
repoFile
.
setFilePath
(
"README-FOR-TESTING-REBASE-MERGE-REQUEST.md"
);
repoFile
.
setContent
(
"This is content"
);
gitLabApi
.
getRepositoryFileApi
().
createFile
(
testProject
,
repoFile
,
TEST_REBASE_BRANCH_NAME
,
"Initial commit."
);
MergeRequest
mr
=
null
;
try
{
MergeRequestParams
params
=
new
MergeRequestParams
()
.
withSourceBranch
(
TEST_REBASE_BRANCH_NAME
)
.
withTargetBranch
(
"master"
)
.
withTitle
(
TEST_REBASE_MR_TITLE
);
mr
=
gitLabApi
.
getMergeRequestApi
().
createMergeRequest
(
testProject
,
params
);
assertEquals
(
TEST_REBASE_MR_TITLE
,
mr
.
getTitle
());
MergeRequest
rebaseMr
=
gitLabApi
.
getMergeRequestApi
().
rebaseMergeRequest
(
testProject
,
mr
.
getIid
());
assertEquals
(
true
,
rebaseMr
.
getRebaseInProgress
());
// Wait up to 10 seconds for the rebase to complete
System
.
out
.
print
(
"Waiting for rebase to complete"
);
int
retries
=
0
;
while
(
true
)
{
System
.
out
.
print
(
"."
);
rebaseMr
=
gitLabApi
.
getMergeRequestApi
().
getRebaseStatus
(
testProject
,
mr
.
getId
());
if
(!
rebaseMr
.
getRebaseInProgress
())
{
System
.
out
.
println
(
"done"
);
break
;
}
if
(
retries
>=
10
)
{
System
.
out
.
println
(
"aborting!"
);
fail
(
"Merge request rebase is taking too long, failing test."
);
}
try
{
Thread
.
sleep
(
1000
);
}
catch
(
InterruptedException
e
)
{
}
retries
++;
}
gitLabApi
.
getMergeRequestApi
().
deleteMergeRequest
(
testProject
,
mr
.
getIid
());
Optional
<
MergeRequest
>
deletedMr
=
gitLabApi
.
getMergeRequestApi
().
getOptionalMergeRequest
(
testProject
,
mr
.
getIid
());
mr
=
null
;
assertFalse
(
deletedMr
.
isPresent
());
}
finally
{
if
(
mr
!=
null
)
{
try
{
gitLabApi
.
getMergeRequestApi
().
deleteMergeRequest
(
testProject
,
mr
.
getIid
());
}
catch
(
Exception
ignore
)
{
}
}
try
{
gitLabApi
.
getRepositoryApi
().
deleteBranch
(
testProject
,
TEST_REBASE_BRANCH_NAME
);
}
catch
(
GitLabApiException
ignore
)
{
}
}
}
}
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