Skip to content

Commit b8648fc

Browse files
authored
Merge pull request #3007 from endertunc/support-for-removing-source-branch-on-gitLab-merge-requests
#3006 Support for removing source branch on GitLab merge requests
2 parents c322669 + 1806c97 commit b8648fc

File tree

6 files changed

+118
-24
lines changed

6 files changed

+118
-24
lines changed

docs/help.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ All command line arguments for the `scala-steward` application.
55
```
66
Usage:
77
scala-steward validate-repo-config
8-
scala-steward --workspace <file> --repos-file <file> [--git-author-name <string>] --git-author-email <string> [--git-author-signing-key <string>] --git-ask-pass <file> [--sign-commits] [--forge-type <forge-type>] [--forge-api-host <uri>] --forge-login <string> [--do-not-fork] [--add-labels] [--ignore-opts-files] [--env-var <name=value>]... [--process-timeout <duration>] [--whitelist <string>]... [--read-only <string>]... [--enable-sandbox | --disable-sandbox] [--max-buffer-size <integer>] [--repo-config <uri>]... [--disable-default-repo-config] [--scalafix-migrations <uri>]... [--disable-default-scalafix-migrations] [--artifact-migrations <uri>]... [--disable-default-artifact-migrations] [--cache-ttl <duration>] [--bitbucket-use-default-reviewers] [--bitbucket-server-use-default-reviewers] [--gitlab-merge-when-pipeline-succeeds] [--gitlab-required-reviewers <integer>] [--azure-repos-organization <string>] [--github-app-id <integer> --github-app-key-file <file>] [--url-checker-test-url <uri>]... [--default-maven-repo <string>] [--refresh-backoff-period <duration>]
8+
scala-steward --workspace <file> --repos-file <file> [--git-author-name <string>] --git-author-email <string> [--git-author-signing-key <string>] --git-ask-pass <file> [--sign-commits] [--forge-type <forge-type>] [--forge-api-host <uri>] --forge-login <string> [--do-not-fork] [--add-labels] [--ignore-opts-files] [--env-var <name=value>]... [--process-timeout <duration>] [--whitelist <string>]... [--read-only <string>]... [--enable-sandbox | --disable-sandbox] [--max-buffer-size <integer>] [--repo-config <uri>]... [--disable-default-repo-config] [--scalafix-migrations <uri>]... [--disable-default-scalafix-migrations] [--artifact-migrations <uri>]... [--disable-default-artifact-migrations] [--cache-ttl <duration>] [--bitbucket-use-default-reviewers] [--bitbucket-server-use-default-reviewers] [--gitlab-merge-when-pipeline-succeeds] [--gitlab-required-reviewers <integer>] [--gitlab-remove-source-branch] [--azure-repos-organization <string>] [--github-app-id <integer> --github-app-key-file <file>] [--url-checker-test-url <uri>]... [--default-maven-repo <string>] [--refresh-backoff-period <duration>]
99
1010
1111
@@ -80,6 +80,8 @@ Options and flags:
8080
Whether to merge a gitlab merge request when the pipeline succeeds
8181
--gitlab-required-reviewers <integer>
8282
When set, the number of required reviewers for a merge request will be set to this number (non-negative integer). Is only used in the context of gitlab-merge-when-pipeline-succeeds being enabled, and requires that the configured access token have the appropriate privileges. Also requires a Gitlab Premium subscription.
83+
--gitlab-remove-source-branch
84+
Flag indicating if a merge request should remove the source branch when merging.
8385
--azure-repos-organization <string>
8486
The Azure organization (required when --forge-type is azure-repos)
8587
--github-app-id <integer>

modules/core/src/main/scala/org/scalasteward/core/application/Cli.scala

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -280,8 +280,16 @@ object Cli {
280280
"When set, the number of required reviewers for a merge request will be set to this number (non-negative integer). Is only used in the context of gitlab-merge-when-pipeline-succeeds being enabled, and requires that the configured access token have the appropriate privileges. Also requires a Gitlab Premium subscription."
281281
).validate("Required reviewers must be non-negative")(_ >= 0).orNone
282282

283+
private val gitlabRemoveSourceBranch: Opts[Boolean] =
284+
flag(
285+
"gitlab-remove-source-branch",
286+
"Flag indicating if a merge request should remove the source branch when merging."
287+
).orFalse
288+
283289
private val gitLabCfg: Opts[GitLabCfg] =
284-
(gitlabMergeWhenPipelineSucceeds, gitlabRequiredReviewers).mapN(GitLabCfg.apply)
290+
(gitlabMergeWhenPipelineSucceeds, gitlabRequiredReviewers, gitlabRemoveSourceBranch).mapN(
291+
GitLabCfg.apply
292+
)
285293

286294
private val githubAppId: Opts[Long] =
287295
option[Long](

modules/core/src/main/scala/org/scalasteward/core/application/Config.scala

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,8 @@ object Config {
158158

159159
final case class GitLabCfg(
160160
mergeWhenPipelineSucceeds: Boolean,
161-
requiredReviewers: Option[Int]
161+
requiredReviewers: Option[Int],
162+
removeSourceBranch: Boolean
162163
) extends ForgeSpecificCfg
163164

164165
final case class GiteaCfg(

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

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +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: Option[Boolean],
4344
source_branch: String,
4445
target_branch: Branch
4546
)
@@ -49,7 +50,8 @@ private[gitlab] object MergeRequestPayload {
4950
id: String,
5051
projectId: Long,
5152
data: NewPullRequestData,
52-
usernamesToUserIdsMapping: Map[String, Int]
53+
usernamesToUserIdsMapping: Map[String, Int],
54+
removeSourceBranch: Boolean
5355
): MergeRequestPayload = {
5456
val assignees = data.assignees.flatMap(usernamesToUserIdsMapping.get)
5557
val reviewers = data.reviewers.flatMap(usernamesToUserIdsMapping.get)
@@ -61,6 +63,7 @@ private[gitlab] object MergeRequestPayload {
6163
reviewer_ids = Option.when(reviewers.nonEmpty)(reviewers),
6264
labels = Option.when(data.labels.nonEmpty)(data.labels),
6365
target_project_id = projectId,
66+
remove_source_branch = Option.when(removeSourceBranch)(removeSourceBranch),
6467
source_branch = data.head,
6568
target_branch = data.base
6669
)
@@ -135,7 +138,9 @@ private[gitlab] object GitLabJsonCodec {
135138
}
136139

137140
implicit val projectIdDecoder: Decoder[ProjectId] = deriveDecoder
138-
implicit val mergeRequestPayloadEncoder: Encoder[MergeRequestPayload] = deriveEncoder
141+
implicit val mergeRequestPayloadEncoder: Encoder[MergeRequestPayload] =
142+
deriveEncoder[MergeRequestPayload].mapJson(_.dropNullValues)
143+
139144
implicit val updateStateEncoder: Encoder[UpdateState] = Encoder.instance { newState =>
140145
val encoded = newState.state match {
141146
case PullRequestState.Open => "open"
@@ -188,7 +193,8 @@ final class GitLabApiAlg[F[_]: Parallel](
188193
id = url.encodedProjectId(targetRepo),
189194
projectId = projectId.id,
190195
data = data,
191-
usernamesToUserIdsMapping = usernameMapping
196+
usernamesToUserIdsMapping = usernameMapping,
197+
removeSourceBranch = gitLabCfg.removeSourceBranch
192198
)
193199
res <- client.postWithBody[MergeRequestOut, MergeRequestPayload](
194200
uri = url.mergeRequest(targetRepo),

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)