Skip to content

Commit 5d44da8

Browse files
author
Ender Tunc
committed
make the new field in MergeRequestPayload optional
1 parent 5a2db95 commit 5d44da8

File tree

3 files changed

+100
-21
lines changed

3 files changed

+100
-21
lines changed

modules/core/src/main/scala/org/scalasteward/core/forge/gitlab/GitLabApiAlg.scala

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ final private[gitlab] case class MergeRequestPayload(
4040
assignee_ids: Option[List[Int]],
4141
reviewer_ids: Option[List[Int]],
4242
target_project_id: Long,
43-
remove_source_branch: Boolean,
43+
remove_source_branch: Option[Boolean],
4444
source_branch: String,
4545
target_branch: Branch
4646
)
@@ -63,7 +63,7 @@ private[gitlab] object MergeRequestPayload {
6363
reviewer_ids = Option.when(reviewers.nonEmpty)(reviewers),
6464
labels = Option.when(data.labels.nonEmpty)(data.labels),
6565
target_project_id = projectId,
66-
remove_source_branch = removeSourceBranch,
66+
remove_source_branch = Option.when(removeSourceBranch)(removeSourceBranch),
6767
source_branch = data.head,
6868
target_branch = data.base
6969
)
@@ -138,7 +138,9 @@ private[gitlab] object GitLabJsonCodec {
138138
}
139139

140140
implicit val projectIdDecoder: Decoder[ProjectId] = deriveDecoder
141-
implicit val mergeRequestPayloadEncoder: Encoder[MergeRequestPayload] = deriveEncoder
141+
implicit val mergeRequestPayloadEncoder: Encoder[MergeRequestPayload] =
142+
deriveEncoder[MergeRequestPayload].mapJson(_.dropNullValues)
143+
142144
implicit val updateStateEncoder: Encoder[UpdateState] = Encoder.instance { newState =>
143145
val encoded = newState.state match {
144146
case PullRequestState.Open => "open"

modules/core/src/test/scala/org/scalasteward/core/forge/gitlab/GitLabApiAlgTest.scala

Lines changed: 56 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -122,25 +122,61 @@ class GitLabApiAlgTest extends CatsEffectSuite with Http4sDsl[MockEff] {
122122

123123
private val gitlabApiAlg = ForgeSelection.forgeApiAlg[MockEff](
124124
config.forgeCfg.copy(tpe = ForgeType.GitLab),
125-
GitLabCfg(mergeWhenPipelineSucceeds = false, requiredReviewers = None),
125+
GitLabCfg(
126+
mergeWhenPipelineSucceeds = false,
127+
requiredReviewers = None,
128+
removeSourceBranch = false
129+
),
126130
user
127131
)
128132

129133
private val gitlabApiAlgNoFork = ForgeSelection.forgeApiAlg[MockEff](
130134
config.forgeCfg.copy(tpe = ForgeType.GitLab, doNotFork = true),
131-
GitLabCfg(mergeWhenPipelineSucceeds = false, requiredReviewers = None),
135+
GitLabCfg(
136+
mergeWhenPipelineSucceeds = false,
137+
requiredReviewers = None,
138+
removeSourceBranch = false
139+
),
140+
user
141+
)
142+
143+
private val gitlabApiAlgAutoMerge = ForgeSelection.forgeApiAlg[MockEff](
144+
config.forgeCfg.copy(tpe = ForgeType.GitLab, doNotFork = true),
145+
GitLabCfg(
146+
mergeWhenPipelineSucceeds = true,
147+
requiredReviewers = None,
148+
removeSourceBranch = false
149+
),
150+
user
151+
)
152+
153+
private val gitlabApiAlgRemoveSourceBranch = ForgeSelection.forgeApiAlg[MockEff](
154+
config.forgeCfg.copy(tpe = ForgeType.GitLab, doNotFork = true),
155+
GitLabCfg(
156+
mergeWhenPipelineSucceeds = false,
157+
requiredReviewers = None,
158+
removeSourceBranch = true
159+
),
132160
user
133161
)
134162

135163
private val gitlabApiAlgLessReviewersRequired = ForgeSelection.forgeApiAlg[MockEff](
136164
config.forgeCfg.copy(tpe = ForgeType.GitLab, doNotFork = true),
137-
GitLabCfg(mergeWhenPipelineSucceeds = true, requiredReviewers = Some(0)),
165+
GitLabCfg(
166+
mergeWhenPipelineSucceeds = true,
167+
requiredReviewers = Some(0),
168+
removeSourceBranch = false
169+
),
138170
user
139171
)
140172

141173
private val gitlabApiAlgWithAssigneeAndReviewers = ForgeSelection.forgeApiAlg[MockEff](
142174
config.forgeCfg.copy(tpe = ForgeType.GitLab, doNotFork = true),
143-
GitLabCfg(mergeWhenPipelineSucceeds = true, requiredReviewers = Some(0)),
175+
GitLabCfg(
176+
mergeWhenPipelineSucceeds = true,
177+
requiredReviewers = Some(0),
178+
removeSourceBranch = false
179+
),
144180
user
145181
)
146182

@@ -232,7 +268,7 @@ class GitLabApiAlgTest extends CatsEffectSuite with Http4sDsl[MockEff] {
232268
}
233269

234270
test("createPullRequest -- auto merge") {
235-
val prOut = gitlabApiAlgNoFork
271+
val prOut = gitlabApiAlgAutoMerge
236272
.createPullRequest(Repo("foo", "bar"), newPRData)
237273
.runA(state)
238274

@@ -345,6 +381,21 @@ class GitLabApiAlgTest extends CatsEffectSuite with Http4sDsl[MockEff] {
345381
assertIO(prOut, expected)
346382
}
347383

384+
test("createPullRequest -- remove source branch") {
385+
val prOut = gitlabApiAlgRemoveSourceBranch
386+
.createPullRequest(Repo("foo", "bar"), newPRData)
387+
.runA(state)
388+
389+
val expected = PullRequestOut(
390+
uri"https://gitlab.com/foo/bar/merge_requests/150",
391+
PullRequestState.Open,
392+
PullRequestNumber(150),
393+
"title"
394+
)
395+
396+
assertIO(prOut, expected)
397+
}
398+
348399
test("referencePullRequest") {
349400
val reference = gitlabApiAlg.referencePullRequest(PullRequestNumber(1347))
350401
assertEquals(reference, "!1347")

modules/core/src/test/scala/org/scalasteward/core/forge/gitlab/MergeRequestPayloadTest.scala

Lines changed: 39 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,13 @@ class MergeRequestPayloadTest extends FunSuite {
2222
private val projectId = 321L
2323

2424
test("asJson") {
25-
val obtained = MergeRequestPayload(id, projectId, data, Map.empty).asJson
25+
val obtained =
26+
MergeRequestPayload(id, projectId, data, Map.empty, removeSourceBranch = false).asJson
2627
val expected =
2728
json"""{
2829
"id" : "123",
2930
"title" : "Test MR title",
3031
"description" : "Test MR body",
31-
"labels" : null,
32-
"assignee_ids" : null,
33-
"reviewer_ids" : null,
3432
"target_project_id" : 321,
3533
"source_branch" : "source",
3634
"target_branch" : "master"
@@ -39,15 +37,18 @@ class MergeRequestPayloadTest extends FunSuite {
3937
}
4038

4139
test("asJson for draft MR") {
42-
val obtained = MergeRequestPayload(id, projectId, data.copy(draft = true), Map.empty).asJson
40+
val obtained = MergeRequestPayload(
41+
id,
42+
projectId,
43+
data.copy(draft = true),
44+
Map.empty,
45+
removeSourceBranch = false
46+
).asJson
4347
val expected =
4448
json"""{
4549
"id" : "123",
4650
"title" : "Draft: Test MR title",
4751
"description" : "Test MR body",
48-
"labels" : null,
49-
"assignee_ids" : null,
50-
"reviewer_ids" : null,
5152
"target_project_id" : 321,
5253
"source_branch" : "source",
5354
"target_branch" : "master"
@@ -57,15 +58,19 @@ class MergeRequestPayloadTest extends FunSuite {
5758

5859
test("asJson with labels") {
5960
val obtained =
60-
MergeRequestPayload(id, projectId, data.copy(labels = List("foo", "bar")), Map.empty).asJson
61+
MergeRequestPayload(
62+
id,
63+
projectId,
64+
data.copy(labels = List("foo", "bar")),
65+
Map.empty,
66+
removeSourceBranch = false
67+
).asJson
6168
val expected =
6269
json"""{
6370
"id" : "123",
6471
"title" : "Test MR title",
6572
"description" : "Test MR body",
6673
"labels" : [ "foo", "bar" ],
67-
"assignee_ids" : null,
68-
"reviewer_ids" : null,
6974
"target_project_id" : 321,
7075
"source_branch" : "source",
7176
"target_branch" : "master"
@@ -78,14 +83,14 @@ class MergeRequestPayloadTest extends FunSuite {
7883
id = id,
7984
projectId = projectId,
8085
data = data.copy(assignees = List("foo"), reviewers = List("bar")),
81-
usernamesToUserIdsMapping = Map("foo" -> 1, "bar" -> 2)
86+
usernamesToUserIdsMapping = Map("foo" -> 1, "bar" -> 2),
87+
removeSourceBranch = false
8288
).asJson
8389
val expected =
8490
json"""{
8591
"id" : "123",
8692
"title" : "Test MR title",
8793
"description" : "Test MR body",
88-
"labels" : null,
8994
"assignee_ids": [ 1 ],
9095
"reviewer_ids": [ 2 ],
9196
"target_project_id" : 321,
@@ -94,4 +99,25 @@ class MergeRequestPayloadTest extends FunSuite {
9499
}"""
95100
assertEquals(obtained, expected)
96101
}
102+
103+
test("asJson with remove source branch") {
104+
val obtained = MergeRequestPayload(
105+
id = id,
106+
projectId = projectId,
107+
data = data,
108+
usernamesToUserIdsMapping = Map.empty,
109+
removeSourceBranch = true
110+
).asJson
111+
val expected =
112+
json"""{
113+
"id" : "123",
114+
"title" : "Test MR title",
115+
"description" : "Test MR body",
116+
"target_project_id" : 321,
117+
"remove_source_branch": true,
118+
"source_branch" : "source",
119+
"target_branch" : "master"
120+
}"""
121+
assertEquals(obtained, expected)
122+
}
97123
}

0 commit comments

Comments
 (0)