Skip to content

Commit ad6d05f

Browse files
committed
WIP
# Conflicts: # modules/core/src/test/scala/org/scalasteward/core/data/GroupedUpdateTest.scala
1 parent 1574d0c commit ad6d05f

4 files changed

Lines changed: 82 additions & 32 deletions

File tree

modules/core/src/main/scala/org/scalasteward/core/data/Update.scala

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ import org.scalasteward.core.util.Nel
2525
import org.scalasteward.core.coursier.VersionsCache.VersionWithFirstSeen
2626
import org.scalasteward.core.nurture.UpdatesForGivenEdit
2727

28+
import scala.collection.immutable
29+
2830
case class ArtifactForUpdate(
2931
crossDependency: CrossDependency,
3032
newerGroupId: Option[GroupId] = None,
@@ -245,19 +247,23 @@ object Update {
245247
}
246248

247249
/**
248-
* If a set of ApplicableUpdateSet all have the same GroupId, then they could be a Update.ForGroupId()
250+
* If a set of ApplicableUpdateSet all have the same GroupId, then they could be an Update.ForGroupId()
249251
*
250252
* If there is only one artifact within a ApplicableUpdateSet for a given group id, just create a
251253
* Update.ForArtifactId
252254
*
253255
* Otherwise, create a Grouped()?
254256
*/
255-
def groupByGroupId(updates: List[UpdatesForGivenEdit]): List[Single] = {
257+
def groupByGroupId(updates: List[UpdatesForGivenEdit]): List[Update] = {
256258
val groups0 =
257259
updates.groupByNel(s => (s.commonGroupId, s.commonVersionUpdate))
258-
val groups1 = groups0.map { case ((Some(_), Some(versionUpdate)), group) =>
259-
if (group.tail.isEmpty) group.head
260-
else ForGroupId(group.flatMap(_.artifactUpdates), versionUpdate.nextVersion)
260+
val groups1: Iterable[Update] = groups0.map {
261+
case ((Some(_), Some(versionUpdate)), updatesWithCommonMavenGroupId) =>
262+
val affectedArtifacts = updatesWithCommonMavenGroupId.flatMap(_.artifactsForUpdate)
263+
if (affectedArtifacts.size == 1) Update.ForArtifactId(affectedArtifacts.head, updatesWithCommonMavenGroupId.head.nextVersion)
264+
else ForGroupId(updatesWithCommonMavenGroupId.flatMap(_.artifactsForUpdate), versionUpdate.nextVersion)
265+
case (_, group) =>
266+
Update.Grouped("some random name", None, group.flatMap(_.asUpdatesForArtifactId).toList)
261267
}
262268
groups1.toList.distinct.sorted
263269
}
@@ -274,13 +280,21 @@ object Update {
274280
groups.foldLeft((List.empty[Grouped], updates)) { case ((grouped, notGrouped), group) =>
275281
notGrouped.partition(group.matches) match {
276282
case (Nil, rest) => (grouped, rest)
277-
case (matched, rest) => (grouped :+ Grouped(group.name, group.title, matched.flatMap(_.artifactUpdates)), rest)
283+
case (matched, rest) => (grouped :+ Grouped(group.name, group.title, matched.flatMap(_.asUpdatesForArtifactId.toList)), rest)
278284
}
279285
}
280286

281287
implicit val SingleOrder: Order[Single] =
282288
Order.by((u: Single) => (u.crossDependencies, u.nextVersion))
283289

290+
implicit val GroupedOrder: Order[Grouped] =
291+
Order.by((u: Grouped) => (u.name, u.asSingleUpdates))
292+
293+
implicit val UpdateOrder: Order[Update] = Order.by {
294+
case g: Grouped => (1, Some(g), None)
295+
case s: Single => (2, None, Some(s))
296+
}
297+
284298
// Encoder and Decoder instances
285299

286300
// ForArtifactId

modules/core/src/main/scala/org/scalasteward/core/nurture/NurtureAlg.scala

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -38,17 +38,25 @@ import org.typelevel.log4cats.Logger
3838

3939
case class UpdatesForGivenEdit(
4040
edits: List[Substring.Replacement],
41-
artifactUpdates: List[Update.ForArtifactId]
41+
artifactsForUpdate: Nel[ArtifactForUpdate],
42+
nextVersion: Version
4243
) {
4344
val commonGroupId: Option[GroupId] = {
44-
val updatesByGroupId = artifactUpdates.groupBy(_.groupId)
45+
val updatesByGroupId = artifactsForUpdate.groupBy(_.groupId)
4546
Option.when(updatesByGroupId.size == 1)(updatesByGroupId.keys.head)
4647
}
4748

48-
val commonVersionUpdate: Option[Version.Update] = {
49-
val updatesByVersionUpdate = artifactUpdates.groupBy(_.versionUpdate)
49+
val commonCurrentVersion: Option[Version] = {
50+
val updatesByVersionUpdate = artifactsForUpdate.groupBy(_.currentVersion)
5051
Option.when(updatesByVersionUpdate.size == 1)(updatesByVersionUpdate.keys.head)
5152
}
53+
54+
val commonVersionUpdate: Option[Version.Update] =
55+
commonCurrentVersion.map(Version.Update(_, nextVersion))
56+
57+
lazy val asUpdatesForArtifactId: Nel[Update.ForArtifactId] = for {
58+
artifactForUpdate <- artifactsForUpdate
59+
} yield Update.ForArtifactId(artifactForUpdate, nextVersion)
5260
}
5361

5462
final class NurtureAlg[F[_]](config: ForgeCfg)(implicit
@@ -70,11 +78,13 @@ final class NurtureAlg[F[_]](config: ForgeCfg)(implicit
7078
baseBranch <- cloneAndSync(data.repo, fork)
7179
applicableUpdateSets <- updates
7280
.traverse(update =>
73-
editAlg.findUpdateReplacements(fork.repo, data.config, update).map(update -> _)
81+
editAlg
82+
.findUpdateReplacements(fork.repo, data.config, update)
83+
.map(update -> (_, update.nextVersion))
7484
)
75-
.map { x =>
76-
x.toList.groupMap(_._2)(_._1).map { case (r, a) =>
77-
UpdatesForGivenEdit(r, a)
85+
.map {
86+
_.toList.groupMap(_._2)(_._1).flatMap { case ((r, nextVersion), a) =>
87+
Nel.fromList(a.map(_.artifactForUpdate)).map(UpdatesForGivenEdit(r, _, nextVersion))
7888
}
7989
}
8090

modules/core/src/main/scala/org/scalasteward/core/repoconfig/PullRequestGroup.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ case class PullRequestGroup(
3434
def matches(update: Update.ForArtifactId): Boolean = filter.exists(_.matches(update))
3535

3636
def matches(updates: UpdatesForGivenEdit): Boolean =
37-
filter.exists(f => updates.artifactUpdates.exists(update => f.matches(update)))
37+
filter.exists(f => updates.asUpdatesForArtifactId.exists(update => f.matches(update)))
3838
}
3939

4040
object PullRequestGroup {

modules/core/src/test/scala/org/scalasteward/core/data/GroupedUpdateTest.scala

Lines changed: 43 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,38 @@ import cats.data.NonEmptyList
44
import cats.implicits.catsSyntaxOptionId
55
import munit.FunSuite
66
import org.scalasteward.core.TestSyntax.*
7+
import org.scalasteward.core.edit.update.data.Substring
8+
import org.scalasteward.core.edit.update.data.Substring.Position
9+
import org.scalasteward.core.nurture.UpdatesForGivenEdit
710
import org.scalasteward.core.repoconfig.{PullRequestGroup, PullRequestUpdateFilter}
811

912
class GroupedUpdateTest extends FunSuite {
10-
val updateSingleSpecs2Core: Update.ForArtifactId =
13+
def stubReplacementFor(u: Update.ForArtifactId): Substring.Replacement = Substring.Replacement(
14+
Position("build.sbt", u.artifactForUpdate.hashCode(), u.currentVersion.value),
15+
u.nextVersion.value
16+
)
17+
18+
private def stubEditFor(single: Update.ForArtifactId) = UpdatesForGivenEdit(
19+
List(stubReplacementFor(single)),
20+
single.artifactsForUpdate,
21+
single.nextVersion
22+
)
23+
24+
def stubFoo(updates: UpdatesForGivenEdit*): List[Update.ForArtifactId] =
25+
updates.flatMap(_.asUpdatesForArtifactId.toList).toList
26+
27+
val updateSingleSpecs2Core = stubEditFor(
1128
("org.specs2".g % "specs2-core".a % "3.9.3" %> "3.9.5").single
29+
)
1230

13-
val updateSingleSpecs2Scalacheck: Update.ForArtifactId =
14-
("org.specs2".g % "specs2-scalacheck".a % "3.9.3" %> "3.9.5").single
31+
val updateSingleSpecs2Scalacheck =
32+
stubEditFor(("org.specs2".g % "specs2-scalacheck".a % "3.9.3" %> "3.9.5").single)
1533

16-
val updateSingleTypelevelAlgebra: Update.ForArtifactId =
17-
("org.typelevel".g % "algebra".a % "2.1.1" %> "2.2.0").single
34+
val updateSingleTypelevelAlgebra =
35+
stubEditFor(("org.typelevel".g % "algebra".a % "2.1.1" %> "2.2.0").single)
1836

19-
val updateSingleCirceCore: Update.ForArtifactId =
20-
("circe".g % "core".a % "1.4.2" %> "1.5.0").single
37+
val updateSingleCirceCore =
38+
stubEditFor(("circe".g % "core".a % "1.4.2" %> "1.5.0").single)
2139

2240
test(
2341
"GroupedUpdate.from: Do not group a Update.Single if the groupId does not match the filter"
@@ -76,7 +94,7 @@ class GroupedUpdateTest extends FunSuite {
7694
assertEquals(
7795
grouped,
7896
List(
79-
Update.Grouped("typelevel", Some("update typelevel"), List(updateSingleTypelevelAlgebra))
97+
Update.Grouped("typelevel", Some("update typelevel"), stubFoo(updateSingleTypelevelAlgebra))
8098
)
8199
)
82100
assertEquals(notGrouped, List.empty)
@@ -103,7 +121,9 @@ class GroupedUpdateTest extends FunSuite {
103121
Update.Grouped(
104122
"specs2",
105123
Some("update specs2"),
106-
List(updateSingleSpecs2Core, updateSingleSpecs2Scalacheck)
124+
List(updateSingleSpecs2Core, updateSingleSpecs2Scalacheck).flatMap(
125+
_.asUpdatesForArtifactId.toList
126+
)
107127
)
108128
)
109129
)
@@ -143,9 +163,15 @@ class GroupedUpdateTest extends FunSuite {
143163
Update.Grouped(
144164
"specs2",
145165
Some("update specs2"),
146-
List(updateSingleSpecs2Core, updateSingleSpecs2Scalacheck)
166+
List(updateSingleSpecs2Core, updateSingleSpecs2Scalacheck).flatMap(
167+
_.asUpdatesForArtifactId.toList
168+
)
147169
),
148-
Update.Grouped("typelevel", Some("update typelevel"), List(updateSingleTypelevelAlgebra))
170+
Update.Grouped(
171+
"typelevel",
172+
Some("update typelevel"),
173+
List(updateSingleTypelevelAlgebra).flatMap(_.asUpdatesForArtifactId.toList)
174+
)
149175
)
150176
)
151177
assertEquals(notGrouped, List(updateSingleCirceCore))
@@ -174,7 +200,7 @@ class GroupedUpdateTest extends FunSuite {
174200
Update.Grouped(
175201
"org",
176202
Some("update org"),
177-
List(updateSingleSpecs2Core, updateSingleSpecs2Scalacheck, updateSingleTypelevelAlgebra)
203+
stubFoo(updateSingleSpecs2Core, updateSingleSpecs2Scalacheck, updateSingleTypelevelAlgebra)
178204
)
179205
)
180206
)
@@ -203,7 +229,7 @@ class GroupedUpdateTest extends FunSuite {
203229
Update.Grouped(
204230
"all wildcard",
205231
Some("update all wildcard"),
206-
List(
232+
stubFoo(
207233
updateSingleTypelevelAlgebra,
208234
updateSingleSpecs2Core,
209235
updateSingleSpecs2Scalacheck
@@ -233,7 +259,7 @@ class GroupedUpdateTest extends FunSuite {
233259
Update.Grouped(
234260
"specs2 core",
235261
Some("update specs2 core"),
236-
List(updateSingleSpecs2Core)
262+
stubFoo(updateSingleSpecs2Core)
237263
)
238264
)
239265
)
@@ -262,7 +288,7 @@ class GroupedUpdateTest extends FunSuite {
262288
Update.Grouped(
263289
"specs2 core",
264290
Some("update specs2 core"),
265-
List(updateSingleSpecs2Core)
291+
stubFoo(updateSingleSpecs2Core)
266292
)
267293
)
268294
)
@@ -315,7 +341,7 @@ class GroupedUpdateTest extends FunSuite {
315341
Update.Grouped(
316342
"specs2",
317343
Some("update specs2"),
318-
List(updateSingleSpecs2Scalacheck, updateSingleSpecs2Core)
344+
stubFoo(updateSingleSpecs2Scalacheck, updateSingleSpecs2Core)
319345
)
320346
)
321347
)
@@ -345,7 +371,7 @@ class GroupedUpdateTest extends FunSuite {
345371
Update.Grouped(
346372
"specs2",
347373
Some("update specs2"),
348-
List(updateSingleSpecs2Scalacheck, updateSingleSpecs2Core)
374+
stubFoo(updateSingleSpecs2Scalacheck, updateSingleSpecs2Core)
349375
)
350376
)
351377
)

0 commit comments

Comments
 (0)