Skip to content

Commit 3811557

Browse files
committed
WIP
1 parent 61a104f commit 3811557

4 files changed

Lines changed: 77 additions & 26 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: 38 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,35 @@ 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+
val updateSingleSpecs2Core = stubEditFor(
1125
("org.specs2".g % "specs2-core".a % "3.9.3" %> "3.9.5").single
26+
)
1227

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

16-
val updateSingleTypelevelAlgebra: Update.ForArtifactId =
17-
("org.typelevel".g % "algebra".a % "3.9.4" %> "3.9.5").single
31+
val updateSingleTypelevelAlgebra =
32+
stubEditFor(("org.typelevel".g % "algebra".a % "3.9.4" %> "3.9.5").single)
1833

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

2237
test(
2338
"GroupedUpdate.from: Do not group a Update.Single if the groupId does not match the filter"
@@ -76,7 +91,11 @@ class GroupedUpdateTest extends FunSuite {
7691
assertEquals(
7792
grouped,
7893
List(
79-
Update.Grouped("typelevel", Some("update typelevel"), List(updateSingleTypelevelAlgebra))
94+
Update.Grouped(
95+
"typelevel",
96+
Some("update typelevel"),
97+
updateSingleTypelevelAlgebra.asUpdatesForArtifactId.toList
98+
)
8099
)
81100
)
82101
assertEquals(notGrouped, List.empty)
@@ -103,7 +122,9 @@ class GroupedUpdateTest extends FunSuite {
103122
Update.Grouped(
104123
"specs2",
105124
Some("update specs2"),
106-
List(updateSingleSpecs2Core, updateSingleSpecs2Scalacheck)
125+
List(updateSingleSpecs2Core, updateSingleSpecs2Scalacheck).flatMap(
126+
_.asUpdatesForArtifactId.toList
127+
)
107128
)
108129
)
109130
)
@@ -143,9 +164,15 @@ class GroupedUpdateTest extends FunSuite {
143164
Update.Grouped(
144165
"specs2",
145166
Some("update specs2"),
146-
List(updateSingleSpecs2Core, updateSingleSpecs2Scalacheck)
167+
List(updateSingleSpecs2Core, updateSingleSpecs2Scalacheck).flatMap(
168+
_.asUpdatesForArtifactId.toList
169+
)
147170
),
148-
Update.Grouped("typelevel", Some("update typelevel"), List(updateSingleTypelevelAlgebra))
171+
Update.Grouped(
172+
"typelevel",
173+
Some("update typelevel"),
174+
List(updateSingleTypelevelAlgebra).flatMap(_.asUpdatesForArtifactId.toList)
175+
)
149176
)
150177
)
151178
assertEquals(notGrouped, List(updateSingleCirceCore))

0 commit comments

Comments
 (0)