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
facd6a53
Unverified
Commit
facd6a53
authored
Nov 09, 2019
by
Greg Messner
Committed by
GitHub
Nov 09, 2019
Browse files
Simplify create and update merge request (#464) (#473).
parent
cfaa09ca
Changes
4
Hide whitespace changes
Inline
Side-by-side
src/main/java/org/gitlab4j/api/MergeRequestApi.java
View file @
facd6a53
...
@@ -14,6 +14,7 @@ import org.gitlab4j.api.models.Commit;
...
@@ -14,6 +14,7 @@ import org.gitlab4j.api.models.Commit;
import
org.gitlab4j.api.models.Issue
;
import
org.gitlab4j.api.models.Issue
;
import
org.gitlab4j.api.models.MergeRequest
;
import
org.gitlab4j.api.models.MergeRequest
;
import
org.gitlab4j.api.models.MergeRequestFilter
;
import
org.gitlab4j.api.models.MergeRequestFilter
;
import
org.gitlab4j.api.models.MergeRequestParams
;
import
org.gitlab4j.api.models.Participant
;
import
org.gitlab4j.api.models.Participant
;
/**
/**
...
@@ -337,7 +338,24 @@ public class MergeRequestApi extends AbstractApi {
...
@@ -337,7 +338,24 @@ public class MergeRequestApi extends AbstractApi {
}
}
/**
/**
* Creates a merge request and optionally assigns a reviewer to it.
* Creates a merge request.
*
* <pre><code>GitLab Endpoint: POST /projects/:id/merge_requests</code></pre>
*
* @param projectIdOrPath the project in the form of an Integer(ID), String(path), or Project instance
* @param params a MergeRequestParams instance holding the info to create the merge request
* @return the created MergeRequest instance
* @throws GitLabApiException if any exception occurs
* @since GitLab Starter 8.17, GitLab CE 11.0.
*/
public
MergeRequest
createMergeRequest
(
Object
projectIdOrPath
,
MergeRequestParams
params
)
throws
GitLabApiException
{
GitLabApiForm
form
=
params
.
getForm
(
true
);
Response
response
=
post
(
Response
.
Status
.
CREATED
,
form
,
"projects"
,
getProjectIdOrPath
(
projectIdOrPath
),
"merge_requests"
);
return
(
response
.
readEntity
(
MergeRequest
.
class
));
}
/**
* Creates a merge request.
*
*
* <pre><code>GitLab Endpoint: POST /projects/:id/merge_requests</code></pre>
* <pre><code>GitLab Endpoint: POST /projects/:id/merge_requests</code></pre>
*
*
...
@@ -359,24 +377,23 @@ public class MergeRequestApi extends AbstractApi {
...
@@ -359,24 +377,23 @@ public class MergeRequestApi extends AbstractApi {
public
MergeRequest
createMergeRequest
(
Object
projectIdOrPath
,
String
sourceBranch
,
String
targetBranch
,
String
title
,
String
description
,
Integer
assigneeId
,
public
MergeRequest
createMergeRequest
(
Object
projectIdOrPath
,
String
sourceBranch
,
String
targetBranch
,
String
title
,
String
description
,
Integer
assigneeId
,
Integer
targetProjectId
,
String
[]
labels
,
Integer
milestoneId
,
Boolean
removeSourceBranch
,
Boolean
squash
)
throws
GitLabApiException
{
Integer
targetProjectId
,
String
[]
labels
,
Integer
milestoneId
,
Boolean
removeSourceBranch
,
Boolean
squash
)
throws
GitLabApiException
{
Form
formData
=
new
Form
();
MergeRequestParams
params
=
new
MergeRequestParams
()
addFormParam
(
formData
,
"source_branch"
,
sourceBranch
,
true
);
.
withSourceBranch
(
sourceBranch
)
addFormParam
(
formData
,
"target_branch"
,
targetBranch
,
true
);
.
withTargetBranch
(
targetBranch
)
addFormParam
(
formData
,
"title"
,
title
,
true
);
.
withTitle
(
title
)
addFormParam
(
formData
,
"description"
,
description
,
false
);
.
withDescription
(
description
)
addFormParam
(
formData
,
"assignee_id"
,
assigneeId
,
false
);
.
withAssigneeId
(
assigneeId
)
addFormParam
(
formData
,
"target_project_id"
,
targetProjectId
,
false
);
.
withTargetProjectId
(
targetProjectId
)
addFormParam
(
formData
,
"labels"
,
labels
==
null
?
null
:
String
.
join
(
","
,
labels
),
false
);
.
withLabels
(
labels
)
addFormParam
(
formData
,
"milestone_id"
,
milestoneId
,
false
);
.
withMilestoneId
(
milestoneId
)
addFormParam
(
formData
,
"remove_source_branch"
,
removeSourceBranch
,
false
);
.
withRemoveSourceBranch
(
removeSourceBranch
)
addFormParam
(
formData
,
"squash"
,
squash
,
false
);
.
withSquash
(
squash
);
Response
response
=
post
(
Response
.
Status
.
CREATED
,
formData
,
"projects"
,
getProjectIdOrPath
(
projectIdOrPath
),
"merge_requests"
);
return
(
createMergeRequest
(
projectIdOrPath
,
params
));
return
(
response
.
readEntity
(
MergeRequest
.
class
));
}
}
/**
/**
* Creates a merge request
and optionally assigns a reviewer to it
.
* Creates a merge request.
*
*
* <pre><code>GitLab Endpoint: POST /projects/:id/merge_requests</code></pre>
* <pre><code>GitLab Endpoint: POST /projects/:id/merge_requests</code></pre>
*
*
...
@@ -395,7 +412,19 @@ public class MergeRequestApi extends AbstractApi {
...
@@ -395,7 +412,19 @@ public class MergeRequestApi extends AbstractApi {
*/
*/
public
MergeRequest
createMergeRequest
(
Object
projectIdOrPath
,
String
sourceBranch
,
String
targetBranch
,
String
title
,
String
description
,
Integer
assigneeId
,
public
MergeRequest
createMergeRequest
(
Object
projectIdOrPath
,
String
sourceBranch
,
String
targetBranch
,
String
title
,
String
description
,
Integer
assigneeId
,
Integer
targetProjectId
,
String
[]
labels
,
Integer
milestoneId
,
Boolean
removeSourceBranch
)
throws
GitLabApiException
{
Integer
targetProjectId
,
String
[]
labels
,
Integer
milestoneId
,
Boolean
removeSourceBranch
)
throws
GitLabApiException
{
return
createMergeRequest
(
projectIdOrPath
,
sourceBranch
,
targetBranch
,
title
,
description
,
assigneeId
,
targetProjectId
,
labels
,
milestoneId
,
removeSourceBranch
,
null
);
MergeRequestParams
params
=
new
MergeRequestParams
()
.
withSourceBranch
(
sourceBranch
)
.
withTargetBranch
(
targetBranch
)
.
withTitle
(
title
)
.
withDescription
(
description
)
.
withAssigneeId
(
assigneeId
)
.
withTargetProjectId
(
targetProjectId
)
.
withLabels
(
labels
)
.
withMilestoneId
(
milestoneId
)
.
withRemoveSourceBranch
(
removeSourceBranch
);
return
(
createMergeRequest
(
projectIdOrPath
,
params
));
}
}
/**
/**
...
@@ -414,54 +443,33 @@ public class MergeRequestApi extends AbstractApi {
...
@@ -414,54 +443,33 @@ public class MergeRequestApi extends AbstractApi {
*/
*/
public
MergeRequest
createMergeRequest
(
Object
projectIdOrPath
,
String
sourceBranch
,
String
targetBranch
,
String
title
,
String
description
,
Integer
assigneeId
)
public
MergeRequest
createMergeRequest
(
Object
projectIdOrPath
,
String
sourceBranch
,
String
targetBranch
,
String
title
,
String
description
,
Integer
assigneeId
)
throws
GitLabApiException
{
throws
GitLabApiException
{
return
createMergeRequest
(
projectIdOrPath
,
sourceBranch
,
targetBranch
,
title
,
description
,
assigneeId
,
null
,
null
,
null
,
null
);
MergeRequestParams
params
=
new
MergeRequestParams
()
.
withSourceBranch
(
sourceBranch
)
.
withTargetBranch
(
targetBranch
)
.
withTitle
(
title
)
.
withDescription
(
description
)
.
withAssigneeId
(
assigneeId
);
return
(
createMergeRequest
(
projectIdOrPath
,
params
));
}
}
/**
/**
* Updates an existing merge request. You can change branches, title, or even close the MR.
* Updates an existing merge request. You can change branches, title, or even close the merge request.
*
* <p>NOTE: GitLab API V4 uses IID (internal ID), V3 uses ID to identify the merge request.</p>
*
*
* <pre><code>GitLab Endpoint: PUT /projects/:id/merge_requests/:merge_request_iid</code></pre>
* <pre><code>GitLab Endpoint: PUT /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 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 update
* @param mergeRequestIid the internal ID of the merge request to update
* @param targetBranch the target branch, optional
* @param params a MergeRequestParams instance holding the info to update the merge request
* @param title the title for the merge request
* @param assigneeId the Assignee user ID, optional
* @param description the description of the merge request, optional
* @param stateEvent new state for the merge request, optional
* @param labels comma separated list of labels, optional
* @param milestoneId the ID of a milestone, optional
* @param removeSourceBranch Flag indicating if a merge request should remove the source
* branch when merging, optional
* @param squash Squash commits into a single commit when merging, optional
* @param discussionLocked Flag indicating if the merge request's discussion is locked, optional
* @param allowCollaboration Allow commits from members who can merge to the target branch,
* optional
* @return the updated merge request
* @return the updated merge request
* @throws GitLabApiException if any exception occurs
* @throws GitLabApiException if any exception occurs
*/
*/
public
MergeRequest
updateMergeRequest
(
Object
projectIdOrPath
,
Integer
mergeRequestIid
,
public
MergeRequest
updateMergeRequest
(
Object
projectIdOrPath
,
Integer
mergeRequestIid
,
MergeRequestParams
params
)
throws
GitLabApiException
{
String
targetBranch
,
String
title
,
Integer
assigneeId
,
String
description
,
GitLabApiForm
form
=
params
.
getForm
(
false
);
StateEvent
stateEvent
,
String
labels
,
Integer
milestoneId
,
Boolean
removeSourceBranch
,
Response
response
=
put
(
Response
.
Status
.
OK
,
form
.
asMap
(),
Boolean
squash
,
Boolean
discussionLocked
,
Boolean
allowCollaboration
)
"projects"
,
getProjectIdOrPath
(
projectIdOrPath
),
"merge_requests"
,
mergeRequestIid
);
throws
GitLabApiException
{
return
(
response
.
readEntity
(
MergeRequest
.
class
));
Form
formData
=
new
GitLabApiForm
()
.
withParam
(
"target_branch"
,
targetBranch
)
.
withParam
(
"title"
,
title
)
.
withParam
(
"assignee_id"
,
assigneeId
)
.
withParam
(
"description"
,
description
)
.
withParam
(
"state_event"
,
stateEvent
)
.
withParam
(
"labels"
,
labels
)
.
withParam
(
"milestone_id"
,
milestoneId
)
.
withParam
(
"remove_source_branch"
,
removeSourceBranch
)
.
withParam
(
"squash"
,
squash
)
.
withParam
(
"discussion_locked"
,
discussionLocked
)
.
withParam
(
"allow_collaboration"
,
allowCollaboration
);
return
updateMergeRequest
(
projectIdOrPath
,
mergeRequestIid
,
formData
);
}
}
/**
/**
...
@@ -480,68 +488,40 @@ public class MergeRequestApi extends AbstractApi {
...
@@ -480,68 +488,40 @@ public class MergeRequestApi extends AbstractApi {
* @param stateEvent new state for the merge request, optional
* @param stateEvent new state for the merge request, optional
* @param labels comma separated list of labels, optional
* @param labels comma separated list of labels, optional
* @param milestoneId the ID of a milestone, optional
* @param milestoneId the ID of a milestone, optional
* @param removeSourceBranch Flag indicating if a merge request should remove the source
* branch when merging, optional
* @param squash Squash commits into a single commit when merging, optional
* @param discussionLocked Flag indicating if the merge request's discussion is locked, optional
* @param allowCollaboration Allow commits from members who can merge to the target branch,
* optional
* @return the updated merge request
* @return the updated merge request
* @throws GitLabApiException if any exception occurs
* @throws GitLabApiException if any exception occurs
*/
*/
@Deprecated
public
MergeRequest
updateMergeRequest
(
Object
projectIdOrPath
,
Integer
mergeRequestIid
,
public
MergeRequest
updateMergeRequest
(
Object
projectIdOrPath
,
Integer
mergeRequestIid
,
String
targetBranch
,
String
targetBranch
,
String
title
,
Integer
assigneeId
,
String
description
,
String
title
,
Integer
assigneeId
,
String
description
,
StateEvent
stateEvent
,
String
labels
,
StateEvent
stateEvent
,
String
labels
,
Integer
milestoneId
,
Boolean
removeSourceBranch
,
Integer
milestoneId
)
throws
GitLabApiException
{
Boolean
squash
,
Boolean
discussionLocked
,
Boolean
allowCollaboration
)
throws
GitLabApiException
{
Form
formData
=
new
GitLabApiForm
()
.
withParam
(
"target_branch"
,
targetBranch
)
.
withParam
(
"title"
,
title
)
.
withParam
(
"assignee_id"
,
assigneeId
)
.
withParam
(
"description"
,
description
)
.
withParam
(
"state_event"
,
stateEvent
)
.
withParam
(
"labels"
,
labels
)
.
withParam
(
"milestone_id"
,
milestoneId
);
return
updateMergeRequest
(
projectIdOrPath
,
mergeRequestIid
,
formData
);
}
/**
* Updates an existing merge request. You can change branches, title, or even close the MR.
*
* <p>NOTE: GitLab API V4 uses IID (internal ID), V3 uses ID to identify the merge request.</p>
*
* <pre><code>GitLab Endpoint: PUT /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 to update
* @param sourceBranch the source branch
* @param targetBranch the target branch
* @param title the title for the merge request
* @param description the description of the merge request
* @param assigneeId the Assignee user ID, optional
* @return the updated merge request
* @throws GitLabApiException if any exception occurs
* @deprecated as of release 4.4.3
*/
@Deprecated
public
MergeRequest
updateMergeRequest
(
Object
projectIdOrPath
,
Integer
mergeRequestIid
,
String
sourceBranch
,
String
targetBranch
,
String
title
,
String
description
,
Integer
assigneeId
)
throws
GitLabApiException
{
Form
formData
=
new
Form
();
addFormParam
(
formData
,
"source_branch"
,
sourceBranch
,
false
);
addFormParam
(
formData
,
"target_branch"
,
targetBranch
,
false
);
addFormParam
(
formData
,
"title"
,
title
,
false
);
addFormParam
(
formData
,
"description"
,
description
,
false
);
addFormParam
(
formData
,
"assignee_id"
,
assigneeId
,
false
);
return
updateMergeRequest
(
projectIdOrPath
,
mergeRequestIid
,
formData
);
}
protected
MergeRequest
updateMergeRequest
(
Object
projectIdOrPath
,
Integer
mergeRequestIid
,
Form
formData
)
throws
GitLabApiException
{
if
(
mergeRequestIid
==
null
)
{
throw
new
RuntimeException
(
"mergeRequestId cannot be null"
);
}
Response
response
=
put
(
Response
.
Status
.
OK
,
formData
.
asMap
(),
"projects"
,
getProjectIdOrPath
(
projectIdOrPath
),
String
[]
labelsArray
=
null
;
"merge_requests"
,
mergeRequestIid
);
if
(
labels
!=
null
)
{
return
(
response
.
readEntity
(
MergeRequest
.
class
));
labelsArray
=
labels
.
split
(
","
,
-
1
);
}
MergeRequestParams
params
=
new
MergeRequestParams
()
.
withTargetBranch
(
targetBranch
)
.
withTitle
(
title
)
.
withAssigneeId
(
assigneeId
)
.
withDescription
(
description
)
.
withStateEvent
(
stateEvent
)
.
withLabels
(
labelsArray
)
.
withMilestoneId
(
milestoneId
)
.
withRemoveSourceBranch
(
removeSourceBranch
)
.
withDiscussionLocked
(
discussionLocked
)
.
withAllowCollaboration
(
allowCollaboration
)
.
withSquash
(
squash
);
return
(
updateMergeRequest
(
projectIdOrPath
,
mergeRequestIid
,
params
));
}
}
/**
/**
...
...
src/main/java/org/gitlab4j/api/models/MergeRequestParams.java
0 → 100644
View file @
facd6a53
package
org.gitlab4j.api.models
;
import
java.util.Arrays
;
import
java.util.List
;
import
org.gitlab4j.api.Constants.StateEvent
;
import
org.gitlab4j.api.GitLabApiForm
;
/**
* This class provides the form parameters for creating and updating merge requests.
*/
public
class
MergeRequestParams
{
private
String
sourceBranch
;
private
String
targetBranch
;
private
String
title
;
private
Integer
assigneeId
;
private
List
<
Integer
>
assigneeIds
;
private
Integer
milestoneId
;
private
List
<
String
>
labels
;
private
String
description
;
private
Integer
targetProjectId
;
private
StateEvent
stateEvent
;
private
Boolean
removeSourceBranch
;
private
Boolean
squash
;
private
Boolean
discussionLocked
;
private
Boolean
allowCollaboration
;
private
Integer
approvalsBeforeMerge
;
/**
* Set the source branch. This is for merge request creation only.
*
* @param sourceBranch the sourceBranch to set
* @return the reference to this MergeRequestParams instance
*/
public
MergeRequestParams
withSourceBranch
(
String
sourceBranch
)
{
this
.
sourceBranch
=
sourceBranch
;
return
(
this
);
}
/**
* Set the target branch.
*
* @param targetBranch the targetBranch to set
* @return the reference to this MergeRequestParams instance
*/
public
MergeRequestParams
withTargetBranch
(
String
targetBranch
)
{
this
.
targetBranch
=
targetBranch
;
return
(
this
);
}
/**
* Set the title of the merge request.
*
* @param title the title to set
* @return the reference to this MergeRequestParams instance
*/
public
MergeRequestParams
withTitle
(
String
title
)
{
this
.
title
=
title
;
return
(
this
);
}
/**
* Set the assignee user ID.
*
* @param assigneeId the assigneeId to set
* @return the reference to this MergeRequestParams instance
*/
public
MergeRequestParams
withAssigneeId
(
Integer
assigneeId
)
{
this
.
assigneeId
=
assigneeId
;
return
(
this
);
}
/**
* The ID of the user(s) to assign the merge request to. Set to 0 or provide
* an empty value to unassign all assignees.
*
* @param assigneeIds the assigneeIds to set
* @return the reference to this MergeRequestParams instance
*/
public
MergeRequestParams
withAssigneeIds
(
List
<
Integer
>
assigneeIds
)
{
this
.
assigneeIds
=
assigneeIds
;
return
(
this
);
}
/**
* Set the milestone ID field value.
*
* @param milestoneId the milestoneId to set
* @return the reference to this MergeRequestParams instance
*/
public
MergeRequestParams
withMilestoneId
(
Integer
milestoneId
)
{
this
.
milestoneId
=
milestoneId
;
return
(
this
);
}
/**
* Set the labels for the merge request.
*
* @param labels the List of labels to set
* @return the reference to this MergeRequestParams instance
*/
public
MergeRequestParams
withLabels
(
List
<
String
>
labels
)
{
this
.
labels
=
labels
;
return
(
this
);
}
/**
* Set the labels for the merge request.
*
* @param labels the array of labels to set
* @return the reference to this MergeRequestParams instance
*/
public
MergeRequestParams
withLabels
(
String
[]
labels
)
{
this
.
labels
=
(
labels
!=
null
?
Arrays
.
asList
(
labels
)
:
null
);
return
(
this
);
}
/**
* Set the description of the merge request. Limited to 1,048,576 characters.
*
* @param description the description to set
* @return the reference to this MergeRequestParams instance
*/
public
MergeRequestParams
withDescription
(
String
description
)
{
this
.
description
=
description
;
return
(
this
);
}
/**
* Set the target project ID. This is for merge request creation only.
*
* @param targetProjectId the targetProjectId to set
* @return the reference to this MergeRequestParams instance
*/
public
MergeRequestParams
withTargetProjectId
(
Integer
targetProjectId
)
{
this
.
targetProjectId
=
targetProjectId
;
return
(
this
);
}
/**
* New state (close/reopen). This is for merge request updates only.
*
* @param stateEvent the stateEvent to set
* @return the reference to this MergeRequestParams instance
*/
public
MergeRequestParams
withStateEvent
(
StateEvent
stateEvent
)
{
this
.
stateEvent
=
stateEvent
;
return
(
this
);
}
/**
* Flag indicating if a merge request should remove the source branch when merging.
*
* @param removeSourceBranch the removeSourceBranch to set
* @return the reference to this MergeRequestParams instance
*/
public
MergeRequestParams
withRemoveSourceBranch
(
Boolean
removeSourceBranch
)
{
this
.
removeSourceBranch
=
removeSourceBranch
;
return
(
this
);
}
/**
* Squash commits into a single commit when merging.
*
* @param squash the squash to set
* @return the reference to this MergeRequestParams instance
*/
public
MergeRequestParams
withSquash
(
Boolean
squash
)
{
this
.
squash
=
squash
;
return
(
this
);
}
/**
* Flag indicating if the merge request’s discussion is locked. If the discussion is locked only
* project members can add, edit or resolve comments. This is for merge request updates only.
*
* @param discussionLocked the discussionLocked to set
* @return the reference to this MergeRequestParams instance
*/
public
MergeRequestParams
withDiscussionLocked
(
Boolean
discussionLocked
)
{
this
.
discussionLocked
=
discussionLocked
;
return
(
this
);
}
/**
* Allow commits from members who can merge to the target branch.
*
* @param allowCollaboration the allowCollaboration to set
* @return the reference to this MergeRequestParams instance
*/
public
MergeRequestParams
withAllowCollaboration
(
Boolean
allowCollaboration
)
{
this
.
allowCollaboration
=
allowCollaboration
;
return
(
this
);
}
/**
* Set the approvals_before_merge field value. This is for merge request creation only.
*
* @param approvalsBeforeMerge the approvalsBeforeMerge to set
* @return the reference to this MergeRequestParams instance
*/
public
MergeRequestParams
withApprovalsBeforeMerge
(
Integer
approvalsBeforeMerge
)
{
this
.
approvalsBeforeMerge
=
approvalsBeforeMerge
;
return
(
this
);
}
/**
* Get the form params specified by this instance.
*
* @param isCreate set to true if this is for a create merge request API call,
* set to false if this is for an update merge request
* @return a GitLabApiForm instance holding the form parameters for this MergeRequestParams instance
*/
public
GitLabApiForm
getForm
(
boolean
isCreate
)
{
GitLabApiForm
form
=
new
GitLabApiForm
()
.
withParam
(
"target_branch"
,
targetBranch
,
isCreate
)
.
withParam
(
"title"
,
title
,
isCreate
)
.
withParam
(
"assignee_id"
,
assigneeId
)
.
withParam
(
"assignee_ids"
,
assigneeIds
)
.
withParam
(
"milestone_id"
,
milestoneId
)
.
withParam
(
"labels"
,
(
labels
!=
null
?
String
.
join
(
","
,
labels
)
:
null
))
.
withParam
(
"description"
,
description
)
.
withParam
(
"remove_source_branch"
,
removeSourceBranch
)
.
withParam
(
"squash"
,
squash
)
.
withParam
(
"allow_collaboration"
,
allowCollaboration
);
if
(
isCreate
)
{
form
.
withParam
(
"source_branch"
,
sourceBranch
,
true
)
.
withParam
(
"target_project_id"
,
targetProjectId
)
.
withParam
(
"approvals_before_merge"
,
approvalsBeforeMerge
);
}
else
{
form
.
withParam
(
"state_event"
,
stateEvent
)
.
withParam
(
"discussion_locked"
,
discussionLocked
);
}
return
(
form
);
}
}
src/test/java/org/gitlab4j/api/TestMergeRequestApi.java
View file @
facd6a53
package
org.gitlab4j.api
;
package
org.gitlab4j.api
;
import
static
org
.
hamcrest
.
Matchers
.
hasEntry
;
import
static
org
.
junit
.
Assert
.
assertEquals
;
import
static
org
.
junit
.
Assert
.
assertEquals
;
import
static
org
.
junit
.
Assert
.
assertThat
;
import
static
org
.
junit
.
Assert
.
assertFalse
;
import
static
org
.
mockito
.
ArgumentMatchers
.
any
;
import
static
org
.
junit
.
Assert
.
assertNotNull
;
import
static
org
.
mockito
.
Mockito
.
when
;
import
static
org
.
junit
.
Assume
.
assumeNotNull
;
import
static
org
.
mockito
.
MockitoAnnotations
.
initMocks
;
import
java.util.Collections
;
import
java.util.Optional
;
import
java.util.stream.Stream
;
import
javax.ws.rs.core.MultivaluedMap
;
import
org.gitlab4j.api.models.Branch
;
import
org.gitlab4j.api.models.MergeRequest
;
import
org.gitlab4j.api.models.MergeRequest
;
import
org.gitlab4j.api.models.MergeRequestParams
;
import
org.gitlab4j.api.models.Project
;
import
org.gitlab4j.api.models.RepositoryFile
;
import
org.gitlab4j.api.models.User
;
import
org.junit.AfterClass
;
import
org.junit.Before
;
import
org.junit.Before
;
import
org.junit.BeforeClass
;
import
org.junit.FixMethodOrder
;
import
org.junit.Test
;
import
org.junit.Test
;
import
org.mockito.ArgumentCaptor
;
import
org.junit.experimental.categories.Category
;
import
org.mockito.Captor
;
import
org.junit.runners.MethodSorters
;
import
org.mockito.Mock
;
import
org.mockito.Mockito
;
public
class
TestMergeRequestApi
{
/**
* In order for these tests to run you must set the following properties in test-gitlab4j.properties
*
* TEST_NAMESPACE
* TEST_PROJECT_NAME
* TEST_HOST_URL
* TEST_PRIVATE_TOKEN
*
* If any of the above are NULL, all tests in this class will be skipped.
*
* NOTE: &FixMethodOrder(MethodSorters.NAME_ASCENDING) is very important to insure that testCreate() is executed first.
*/
@Category
(
IntegrationTest
.
class
)
@FixMethodOrder
(
MethodSorters
.
NAME_ASCENDING
)
public
class
TestMergeRequestApi
extends
AbstractIntegrationTest
{
@Mock
private
GitLabApi
gitLabApi
;
private
static
final
String
TEST_BRANCH_NAME
=
"feature/gitlab4j-merge-request-test"
;
@Mock
private
GitLabApiClient
gitLabApiClient
;
private
static
final
String
TEST_MR_TITLE
=
"Merge Request test: gitlab4j-merge-request-test"
;
@Captor
private
ArgumentCaptor
<
MultivaluedMap
<
String
,
String
>>
attributeCaptor
;
private
static
final
String
TEST_DESCRIPTION
=
"Description for Merge Request test"
;
private
MockResponse
response
;
@SuppressWarnings
(
"deprecation"
)
private
static
GitLabApi
gitLabApi
;
@Before
private
static
Project
testProject
;
public
void
setUp
()
throws
Exception
{
private
static
User
currentUser
;
initMocks
(
this
);
response
=
new
MockResponse
(
MergeRequest
.
class
,
"merge-request.json"
,
null
);
public
TestMergeRequestApi
()
{
when
(
gitLabApi
.
getApiClient
()).
thenReturn
(
gitLabApiClient
);
super
();
when
(
gitLabApiClient
.
validateSecretToken
(
any
())).
thenReturn
(
true
);
when
(
gitLabApiClient
.
put
(
attributeCaptor
.
capture
(),
Mockito
.<
Object
>
anyVararg
()))
.
thenReturn
(
response
);
}
}
@Test
@BeforeClass
public
void
whenAllArgumentsNull_thenNoAttributesSent
()
throws
Exception
{
public
static
void
setup
()
{
new
MergeRequestApi
(
gitLabApi
).
updateMergeRequest
(
1
,
2
,
null
,
null
,
null
,
null
,
null
,
null
,
null
,
null
,
null
,
null
,
null
);
// Must setup the connection to the GitLab test server
assertEquals
(
0
,
attributeCaptor
.
getValue
().
size
());
gitLabApi
=
baseTestSetup
();
testProject
=
getTestProject
();
currentUser
=
getCurrentUser
();
teardown
();
}
}
@Test
@AfterClass
public
void
falseBooleansAreSerializedCorrectly
()
throws
Exception
{
public
static
void
teardown
()
{
new
MergeRequestApi
(
gitLabApi
).
updateMergeRequest
(
1
,
2
,
null
,
null
,
null
,
null
,
null
,
null
,
null
,
null
,
null
,
null
,
false
);
if
(
testProject
==
null
)
{
assertThat
(
attributeCaptor
.
getValue
(),
return
;
hasEntry
(
"allow_collaboration"
,
Collections
.
singletonList
(
"false"
)));
}
try
{
Stream
<
MergeRequest
>
mergeRequests
=
gitLabApi
.
getMergeRequestApi
().
getMergeRequestsStream
(
testProject
);
MergeRequest
mergeRequest
=
mergeRequests
.
filter
(
m
->
TEST_MR_TITLE
.
equals
(
m
.
getTitle
())).
findFirst
().
orElse
(
null
);
if
(
mergeRequest
!=
null
)
{
gitLabApi
.
getMergeRequestApi
().
deleteMergeRequest
(
testProject
,
mergeRequest
.
getIid
());
}
}
catch
(
GitLabApiException
ignore
)
{
}
try
{
gitLabApi
.
getRepositoryApi
().
deleteBranch
(
testProject
,
TEST_BRANCH_NAME
);
}
catch
(
GitLabApiException
ignore
)
{
}
}
@Before
public
void
beforeMethod
()
{
assumeNotNull
(
testProject
);
}
}
@Test
@Test
public
void
trueBooleansAreSerializedCorrectly
()
throws
Exception
{
public
void
testCreateAndUpdateMergeRequest
()
throws
GitLabApiException
{
new
MergeRequestApi
(
gitLabApi
).
updateMergeRequest
(
1
,
2
,
null
,
null
,
null
,
null
,
null
,
null
,
null
,
null
,
null
,
null
,
true
);
// Create a test branch
assertThat
(
attributeCaptor
.
getValue
(),
Branch
branch
=
gitLabApi
.
getRepositoryApi
().
createBranch
(
testProject
,
TEST_BRANCH_NAME
,
"master"
);
hasEntry
(
"allow_collaboration"
,
Collections
.
singletonList
(
"true"
)));
assertNotNull
(
branch
);
// Create a new file in the test branch
RepositoryFile
repoFile
=
new
RepositoryFile
();
repoFile
.
setFilePath
(
"README-FOR-TESTING-MERGE-REQUEST.md"
);
repoFile
.
setContent
(
"This is content"
);
gitLabApi
.
getRepositoryFileApi
().
createFile
(
testProject
,
repoFile
,
TEST_BRANCH_NAME
,
"Initial commit."
);
MergeRequest
mr
=
null
;
try
{
MergeRequestParams
params
=
new
MergeRequestParams
()
.
withSourceBranch
(
TEST_BRANCH_NAME
)
.
withTargetBranch
(
"master"
)
.
withTitle
(
TEST_MR_TITLE
);
mr
=
gitLabApi
.
getMergeRequestApi
().
createMergeRequest
(
testProject
,
params
);
assertEquals
(
TEST_MR_TITLE
,
mr
.
getTitle
());
params
=
new
MergeRequestParams
()
.
withAssigneeId
(
currentUser
.
getId
())
.
withDescription
(
TEST_DESCRIPTION
)
.
withDiscussionLocked
(
true
);
MergeRequest
updatedMr
=
gitLabApi
.
getMergeRequestApi
().
updateMergeRequest
(
testProject
,
mr
.
getIid
(),
params
);
assertEquals
(
currentUser
.
getId
(),
updatedMr
.
getAssignee
().
getId
());
assertEquals
(
TEST_DESCRIPTION
,
updatedMr
.
getDescription
());
assertEquals
(
true
,
updatedMr
.
getDiscussionLocked
());
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_BRANCH_NAME
);
}
catch
(
GitLabApiException
ignore
)
{
}
}
}
}
}
}
src/test/java/org/gitlab4j/api/TestUnitMergeRequestApi.java
0 → 100644
View file @
facd6a53
package
org.gitlab4j.api
;
import
static
org
.
hamcrest
.
Matchers
.
hasEntry
;
import
static
org
.
junit
.
Assert
.
assertEquals
;
import
static
org
.
junit
.
Assert
.
assertThat
;
import
static
org
.
mockito
.
ArgumentMatchers
.
any
;
import
static
org
.
mockito
.
Mockito
.
when
;
import
static
org
.
mockito
.
MockitoAnnotations
.
initMocks
;
import
java.util.Collections
;
import
javax.ws.rs.core.MultivaluedMap
;
import
org.gitlab4j.api.models.MergeRequest
;
import
org.junit.Before
;
import
org.junit.Test
;
import
org.mockito.ArgumentCaptor
;
import
org.mockito.Captor
;
import
org.mockito.Mock
;
import
org.mockito.Mockito
;
public
class
TestUnitMergeRequestApi
{
@Mock
private
GitLabApi
mockGitLabApi
;
@Mock
private
GitLabApiClient
mockedGitLabApiClient
;
@Captor
private
ArgumentCaptor
<
MultivaluedMap
<
String
,
String
>>
attributeCaptor
;
private
MockResponse
mockedResponse
;
@Before
public
void
setUp
()
throws
Exception
{
initMocks
(
this
);
mockedResponse
=
new
MockResponse
(
MergeRequest
.
class
,
"merge-request.json"
,
null
);
when
(
mockGitLabApi
.
getApiClient
()).
thenReturn
(
mockedGitLabApiClient
);
when
(
mockedGitLabApiClient
.
validateSecretToken
(
any
())).
thenReturn
(
true
);
when
(
mockedGitLabApiClient
.
put
(
attributeCaptor
.
capture
(),
Mockito
.<
Object
>
any
()))
.
thenReturn
(
mockedResponse
);
}
@Test
public
void
whenAllArgumentsNull_thenNoAttributesSent
()
throws
Exception
{
new
MergeRequestApi
(
mockGitLabApi
).
updateMergeRequest
(
1
,
2
,
null
,
null
,
null
,
null
,
null
,
null
,
null
,
null
,
null
,
null
,
null
);
assertEquals
(
0
,
attributeCaptor
.
getValue
().
size
());
}
@Test
public
void
falseBooleansAreSerializedCorrectly
()
throws
Exception
{
new
MergeRequestApi
(
mockGitLabApi
).
updateMergeRequest
(
1
,
2
,
null
,
null
,
null
,
null
,
null
,
null
,
null
,
null
,
null
,
null
,
false
);
assertThat
(
attributeCaptor
.
getValue
(),
hasEntry
(
"allow_collaboration"
,
Collections
.
singletonList
(
"false"
)));
}
@Test
public
void
trueBooleansAreSerializedCorrectly
()
throws
Exception
{
new
MergeRequestApi
(
mockGitLabApi
).
updateMergeRequest
(
1
,
2
,
null
,
null
,
null
,
null
,
null
,
null
,
null
,
null
,
null
,
null
,
true
);
assertThat
(
attributeCaptor
.
getValue
(),
hasEntry
(
"allow_collaboration"
,
Collections
.
singletonList
(
"true"
)));
}
}
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