Skip to content

Commit

Permalink
Merge branch 'main' into topic/use-git-grep-in-scanner
Browse files Browse the repository at this point in the history
  • Loading branch information
fthomas committed Nov 30, 2023
2 parents 68d374b + 1a9c3c1 commit 5bfe93a
Show file tree
Hide file tree
Showing 14 changed files with 144 additions and 120 deletions.
2 changes: 1 addition & 1 deletion .scalafmt.conf
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
version = 3.7.15
version = 3.7.17
runner.dialect = scala213
assumeStandardLibraryStripMargin = true
align.openParenCallSite = false
Expand Down
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ Thanks goes to these wonderful people for contributing to Scala Steward:
* [Ikenna Darlington Ogbajie](https://github.com/idarlington)
* [Ingar Abrahamsen](https://github.com/ingarabr)
* [Jakub Kozłowski](https://github.com/kubukoz)
* [Jamie Thompson](https://github.com/bishabosha)
* [Javier Arrieta](https://github.com/javierarrieta)
* [JCollier](https://github.com/Slakah)
* [jduval87](https://github.com/jduval87)
Expand All @@ -127,7 +128,7 @@ Thanks goes to these wonderful people for contributing to Scala Steward:
* [Leonhard Riedißer](https://github.com/L7R7)
* [Maksym Ochenashko](https://github.com/iRevive)
* [Manuel Cueto](https://github.com/manuelcueto)
* [Marco Zühlke](https://github.com/mzuehlke)
* [Marco Zühlke](https://github.com/mzuehlke)
* [Mark Canlas](https://github.com/mcanlas)
* [Mark van der Tol](https://github.com/markvandertol)
* [MaT1g3R](https://github.com/MaT1g3R)
Expand Down
5 changes: 5 additions & 0 deletions modules/core/src/main/resources/artifact-migrations.v2.conf
Original file line number Diff line number Diff line change
Expand Up @@ -994,4 +994,9 @@ changes = [
groupIdAfter = com.github.sbt
artifactIdAfter = sbt-osgi
},
{
groupIdBefore = com.iheart
groupIdAfter = io.github.play-swagger
artifactIdAfter = sbt-play-swagger
},
]
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,27 @@ import org.scalasteward.core.io.{FileAlg, ProcessAlg, WorkspaceAlg}
import org.scalasteward.core.util.Nel
import org.typelevel.log4cats.Logger

object ScalaCliAlg {
val directives =
// sourced from https://github.com/VirtusLab/scala-cli/blob/9e22d4a91ba8699ac2727d2ac3042d64abe951e1/modules/directives/src/main/scala/scala/build/preprocessing/directives/Dependency.scala#L33-L48
List(
"lib",
"libs",
"dep",
"deps",
"dependencies",
"test.dependency",
"test.dep",
"test.deps",
"test.dependencies",
"compileOnly.lib",
"compileOnly.libs",
"compileOnly.dep",
"compileOnly.deps",
"compileOnly.dependencies"
).map(alias => s"//> using $alias ")
}

final class ScalaCliAlg[F[_]](implicit
fileAlg: FileAlg[F],
gitAlg: GitAlg[F],
Expand All @@ -42,8 +63,8 @@ final class ScalaCliAlg[F[_]](implicit
override def containsBuild(buildRoot: BuildRoot): F[Boolean] = {
val buildRootPath = buildRoot.relativePath.dropWhile(Set('.', '/'))
val extensions = Set(".sc", ".scala")
gitAlg
.findFilesContaining(buildRoot.repo, "//> using lib ")
ScalaCliAlg.directives
.flatTraverse(gitAlg.findFilesContaining(buildRoot.repo, _))
.map(_.exists(path => path.startsWith(buildRootPath) && extensions.exists(path.endsWith)))
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -314,12 +314,18 @@ object NewPullRequestData {
val semverLabels =
update.on(u => semverForUpdate(u), _.updates.flatMap(semverForUpdate(_)).distinct)

val artifactMigrationsLabel = Option.when {
update.asSingleUpdates
.flatMap(_.forArtifactIds.toList)
.exists(u => u.newerGroupId.nonEmpty || u.newerArtifactId.nonEmpty)
}("artifact-migrations")
val scalafixLabel = edits.collectFirst { case _: ScalafixEdit => "scalafix-migrations" }
val oldVersionLabel = Option.when(filesWithOldVersion.nonEmpty)("old-version-remains")

List.concat(
updateTypeLabels(update),
semverLabels,
artifactMigrationsLabel,
scalafixLabel,
oldVersionLabel,
List(commitCountLabel)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ final class FileGitAlg[F[_]](config: GitCfg)(implicit

override def hasConflicts(repo: File, branch: Branch, base: Branch): F[Boolean] = {
val tryMerge = git_("merge", "--no-commit", "--no-ff", branch.name)(repo)
val abortMerge = git_("merge", "--abort")(repo).void
val abortMerge = git_("merge", "--abort")(repo).attempt.void

returnToCurrentBranch(repo) {
checkoutBranch(repo, base) >> F.guarantee(tryMerge, abortMerge).attempt.map(_.isLeft)
Expand All @@ -114,41 +114,14 @@ final class FileGitAlg[F[_]](config: GitCfg)(implicit
git("rev-parse", "--verify", branch.name)(repo)
.flatMap(lines => F.fromEither(Sha1.from(lines.mkString("").trim)))

override def mergeTheirs(repo: File, branch: Branch): F[Option[Commit]] =
for {
before <- latestSha1(repo, Branch.head)
_ <- git_("merge", "--strategy-option=theirs", sign, branch.name)(repo).void
.handleErrorWith { throwable =>
// Resolve CONFLICT (modify/delete) by deleting unmerged files:
for {
unmergedFiles <- git("diff", "--name-only", "--diff-filter=U")(repo)
_ <- Nel
.fromList(unmergedFiles.filter(_.nonEmpty))
.fold(F.raiseError[Unit](throwable))(_.traverse_(file => git_("rm", file)(repo)))
_ <- git_("commit", "--all", "--no-edit", sign)(repo)
} yield ()
}
after <- latestSha1(repo, Branch.head)
} yield Option.when(before =!= after)(Commit(after))

override def push(repo: File, branch: Branch): F[Unit] =
git_("push", "--force", "--set-upstream", "origin", branch.name)(repo).void

override def removeClone(repo: File): F[Unit] =
fileAlg.deleteForce(repo)

override def revertChanges(repo: File, base: Branch): F[Option[Commit]] = {
val range = dotdot(base, Branch.head)
git("log", "--pretty=format:%h %p", range)(repo).flatMap { commitsWithParents =>
val commitsUntilMerge = commitsWithParents.map(_.split(' ')).takeWhile(_.length === 2)
val commits = commitsUntilMerge.flatMap(_.headOption)
if (commits.isEmpty) F.pure(None)
else {
val msg = CommitMsg(s"Revert commit(s) " + commits.mkString(", "))
git_("revert" :: "--no-commit" :: commits: _*)(repo) >> commitAllIfDirty(repo, msg)
}
}
}
override def resetHard(repo: File, base: Branch): F[Unit] =
git_("reset", "--hard", base.name)(repo).void

override def setAuthor(repo: File, author: Author): F[Unit] =
for {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,14 +66,11 @@ trait GenGitAlg[F[_], Repo] {

def latestSha1(repo: Repo, branch: Branch): F[Sha1]

/** Merges `branch` into the current branch using `theirs` as merge strategy option. */
def mergeTheirs(repo: Repo, branch: Branch): F[Option[Commit]]

def push(repo: Repo, branch: Branch): F[Unit]

def removeClone(repo: Repo): F[Unit]

def revertChanges(repo: Repo, base: Branch): F[Option[Commit]]
def resetHard(repo: Repo, base: Branch): F[Unit]

def setAuthor(repo: Repo, author: Author): F[Unit]

Expand Down Expand Up @@ -152,17 +149,14 @@ trait GenGitAlg[F[_], Repo] {
override def latestSha1(repo: A, branch: Branch): F[Sha1] =
f(repo).flatMap(self.latestSha1(_, branch))

override def mergeTheirs(repo: A, branch: Branch): F[Option[Commit]] =
f(repo).flatMap(self.mergeTheirs(_, branch))

override def push(repo: A, branch: Branch): F[Unit] =
f(repo).flatMap(self.push(_, branch))

override def removeClone(repo: A): F[Unit] =
f(repo).flatMap(self.removeClone)

override def revertChanges(repo: A, base: Branch): F[Option[Commit]] =
f(repo).flatMap(self.revertChanges(_, base))
override def resetHard(repo: A, base: Branch): F[Unit] =
f(repo).flatMap(self.resetHard(_, base))

override def setAuthor(repo: A, author: Author): F[Unit] =
f(repo).flatMap(self.setAuthor(_, author))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ final class NurtureAlg[F[_]](config: ForgeCfg)(implicit
gitAlg.returnToCurrentBranch(data.repo) {
gitAlg.checkoutBranch(data.repo, data.updateBranch) >>
shouldBeUpdated(data).ifM(
ifTrue = mergeAndApplyAgain(number, data),
ifTrue = resetAndApplyAgain(number, data),
ifFalse = (Ignored: ProcessResult).pure
)
}
Expand All @@ -293,22 +293,19 @@ final class NurtureAlg[F[_]](config: ForgeCfg)(implicit
result.flatMap { case (update, msg) => logger.info(msg).as(update) }
}

private def mergeAndApplyAgain(number: PullRequestNumber, data: UpdateData): F[ProcessResult] =
private def resetAndApplyAgain(number: PullRequestNumber, data: UpdateData): F[ProcessResult] =
for {
_ <- logger.info(
s"Merge branch ${data.baseBranch.name} into ${data.updateBranch.name} and apply again"
s"Reset ${data.updateBranch.name} to ${data.baseBranch.name} and apply again"
)
maybeRevertCommit <- gitAlg.revertChanges(data.repo, data.baseBranch)
maybeMergeCommit <- gitAlg.mergeTheirs(data.repo, data.baseBranch)
_ <- gitAlg.resetHard(data.repo, data.baseBranch)
edits <- data.update.on(
update = editAlg.applyUpdate(data.repoData, _),
grouped = _.updates.flatTraverse(editAlg.applyUpdate(data.repoData, _))
)
editCommits = edits.flatMap(_.commits)
commits = maybeRevertCommit.toList ++ maybeMergeCommit.toList ++ editCommits
result <- pushCommits(data, commits)
result <- pushCommits(data, editCommits)
requestData <- preparePullRequest(data, edits)
_ <- forgeApiAlg.updatePullRequest(number: PullRequestNumber, data.repo, requestData)
} yield result

}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import org.scalasteward.core.mock.MockState.TraceEntry.{Cmd, Log}
import org.scalasteward.core.repoconfig.{BuildRootConfig, RepoConfig}
import org.scalasteward.core.scalafmt
import org.scalasteward.core.scalafmt.scalafmtConfName
import org.scalasteward.core.buildtool.scalacli.ScalaCliAlg

class BuildToolDispatcherTest extends FunSuite {
test("getDependencies") {
Expand All @@ -28,23 +29,33 @@ class BuildToolDispatcherTest extends FunSuite {
val (state, deps) =
buildToolDispatcher.getDependencies(repo, repoConfig).runSA(initial).unsafeRunSync()

val allGreps = ScalaCliAlg.directives.map { search =>
Cmd.git(
repoDir,
"grep",
"-I",
"--fixed-strings",
"--files-with-matches",
search
)
}

val expectedState = initial.copy(trace =
Vector(
Cmd("test", "-f", s"$repoDir/pom.xml"),
Cmd("test", "-f", s"$repoDir/build.sc"),
Cmd("test", "-f", s"$repoDir/build.sbt"),
Cmd.gitGrep(repoDir, "//> using lib "),
Cmd("test", "-f", s"$repoDir/mvn-build/pom.xml"),
Cmd("test", "-f", s"$repoDir/mvn-build/build.sc"),
Cmd("test", "-f", s"$repoDir/mvn-build/build.sbt"),
Cmd.gitGrep(repoDir, "//> using lib "),
Log("Get dependencies in . from sbt"),
Cmd("read", s"$repoDir/project/build.properties"),
Cmd("test", "-d", s"$repoDir/project"),
Cmd("test", "-d", s"$repoDir/project/project"),
Cmd("read", "classpath:StewardPlugin_1_0_0.scala"),
Cmd("write", s"$repoDir/project/scala-steward-StewardPlugin_1_0_0.scala"),
Cmd("write", s"$repoDir/project/project/scala-steward-StewardPlugin_1_0_0.scala"),
Cmd("test", "-f", s"$repoDir/pom.xml") +:
Cmd("test", "-f", s"$repoDir/build.sc") +:
Cmd("test", "-f", s"$repoDir/build.sbt") +:
allGreps ++:
Cmd("test", "-f", s"$repoDir/mvn-build/pom.xml") +:
Cmd("test", "-f", s"$repoDir/mvn-build/build.sc") +:
Cmd("test", "-f", s"$repoDir/mvn-build/build.sbt") +:
allGreps ++:
Log("Get dependencies in . from sbt") +:
Cmd("read", s"$repoDir/project/build.properties") +:
Cmd("test", "-d", s"$repoDir/project") +:
Cmd("test", "-d", s"$repoDir/project/project") +:
Cmd("read", "classpath:StewardPlugin_1_0_0.scala") +:
Cmd("write", s"$repoDir/project/scala-steward-StewardPlugin_1_0_0.scala") +:
Cmd("write", s"$repoDir/project/project/scala-steward-StewardPlugin_1_0_0.scala") +:
Cmd.execSandboxed(
repoDir,
"sbt",
Expand All @@ -53,26 +64,26 @@ class BuildToolDispatcherTest extends FunSuite {
"-Dsbt.supershell=false",
"-Dsbt.server.forcestart=true",
s";$crossStewardDependencies;$reloadPlugins;$stewardDependencies"
),
Cmd("rm", "-rf", s"$repoDir/project/project/scala-steward-StewardPlugin_1_0_0.scala"),
Cmd("rm", "-rf", s"$repoDir/project/scala-steward-StewardPlugin_1_0_0.scala"),
Cmd("read", s"$repoDir/$scalafmtConfName"),
Log("Get dependencies in mvn-build from Maven"),
) +:
Cmd("rm", "-rf", s"$repoDir/project/project/scala-steward-StewardPlugin_1_0_0.scala") +:
Cmd("rm", "-rf", s"$repoDir/project/scala-steward-StewardPlugin_1_0_0.scala") +:
Cmd("read", s"$repoDir/$scalafmtConfName") +:
Log("Get dependencies in mvn-build from Maven") +:
Cmd.execSandboxed(
repoDir / "mvn-build",
"mvn",
maven.args.batchMode,
maven.command.listDependencies,
maven.args.excludeTransitive
),
) +:
Cmd.execSandboxed(
repoDir / "mvn-build",
"mvn",
maven.args.batchMode,
maven.command.listRepositories
),
Cmd("read", s"$repoDir/mvn-build/$scalafmtConfName")
)
) +:
Cmd("read", s"$repoDir/mvn-build/$scalafmtConfName") +:
Vector.empty[MockState.TraceEntry]
)

assertEquals(state, expectedState)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import org.scalasteward.core.mock.MockState
import org.scalasteward.core.mock.MockState.TraceEntry.{Cmd, Log}
import org.scalasteward.core.util.Nel

import cats.syntax.parallel._

class ScalaCliAlgTest extends CatsEffectSuite {
test("containsBuild: directive in non-source file") {
val repo = Repo("user", "repo")
Expand All @@ -23,6 +25,25 @@ class ScalaCliAlgTest extends CatsEffectSuite {
assertIO(obtained, false)
}

test("containsBuild: directive with test.dep, dep, and lib") {
val repo = Repo("user", "repo")
val buildRoot = BuildRoot(repo, ".")
val repoDir = workspaceAlg.repoDir(repo).unsafeRunSync()
val fileWithUsingDirective = "project.scala"

ScalaCliAlg.directives
.map { search =>
val grepCmd =
Cmd.git(repoDir, "grep", "-I", "--fixed-strings", "--files-with-matches", search)
val initial =
MockState.empty.copy(commandOutputs = Map(grepCmd -> Right(List(fileWithUsingDirective))))
val obtained = scalaCliAlg.containsBuild(buildRoot).runA(initial)
assertIO(obtained, true)
}
.parSequence
.void
}

test("getDependencies") {
val repo = Repo("user", "repo")
val buildRoot = BuildRoot(repo, ".")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ class NewPullRequestDataTest extends FunSuite {
assertEquals(body, expected)
}

test("bodyFor() groupped update") {
test("bodyFor() grouped update") {
val update1 = ("ch.qos.logback".g % "logback-classic".a % "1.2.0" %> "1.2.3").single
val update2 = ("com.example".g % "foo".a % "1.0.0" %> "2.0.0").single
val update = Update.Grouped("my-group", Some("The PR title"), List(update1, update2))
Expand Down Expand Up @@ -515,6 +515,13 @@ class NewPullRequestDataTest extends FunSuite {
assertEquals(labels, expected)
}

test("artifact-migrations label") {
val update = ("a".g % "b".a % "1" -> "2").single.copy(newerGroupId = Some("aa".g))
val obtained = labelsFor(update)
val expected = List("library-update", "artifact-migrations", "commit-count:0")
assertEquals(obtained, expected)
}

test("oldVersionNote doesn't show version for grouped updates") {
val files = List("Readme.md", "travis.yml")
val update1 = ("a".g % "b".a % "1" -> "2").single
Expand All @@ -539,7 +546,7 @@ class NewPullRequestDataTest extends FunSuite {
)
}

test("adjustFutureUpdates for grouped udpates shows settings for each update") {
test("adjustFutureUpdates for grouped updates shows settings for each update") {
val update1 = ("a".g % "b".a % "1" -> "2").single
val update2 = ("c".g % "d".a % "1.1.0" % "test" %> "1.2.0").single
val update = Update.Grouped("my-group", None, List(update1, update2))
Expand Down
Loading

0 comments on commit 5bfe93a

Please sign in to comment.