From 4d1cc2743744a921569ae2235ef702636f76411c Mon Sep 17 00:00:00 2001 From: Sven Herrmann Date: Tue, 6 Aug 2024 10:49:46 +0200 Subject: [PATCH 1/3] refactor --- .scala-steward.conf | 5 ++ .scalafix.conf | 21 +++++- .scalafmt.conf | 9 ++- build.sbt | 38 ++++++----- .../thatscalaguy/skunkcrypt/CryptCodecs.scala | 20 +++--- .../thatscalaguy/skunkcrypt/MainSuite.scala | 65 +++++++++---------- 6 files changed, 96 insertions(+), 62 deletions(-) create mode 100644 .scala-steward.conf diff --git a/.scala-steward.conf b/.scala-steward.conf new file mode 100644 index 0000000..47beab5 --- /dev/null +++ b/.scala-steward.conf @@ -0,0 +1,5 @@ +pullRequests.grouping = [ + { name = "patches", "title" = "Patch updates", "filter" = [{"version" = "patch"}] }, + { name = "minor_major", "title" = "Minor/major updates", "filter" = [{"version" = "minor"}, {"version" = "major"}] }, + { name = "all", title = "Dependency updates", "filter" = [{"group" = "*"}] } +] \ No newline at end of file diff --git a/.scalafix.conf b/.scalafix.conf index 8c1468a..d142bde 100644 --- a/.scalafix.conf +++ b/.scalafix.conf @@ -2,5 +2,22 @@ rules = [ OrganizeImports ] -OrganizeImports.removeUnused = false -OrganizeImports.targetDialect = Scala3 \ No newline at end of file +OrganizeImports { + blankLines = Auto + coalesceToWildcardImportThreshold = null + expandRelative = false + groupExplicitlyImportedImplicitsSeparately = true + groupedImports = AggressiveMerge + groups = [ + "cats" + "cats.effect" + "nats4cats" + "*" + "re:(javax?|scala)\\." + ] + importSelectorsOrder = Ascii + importsOrder = Ascii + preset = DEFAULT +} + +OrganizeImports.removeUnused = false \ No newline at end of file diff --git a/.scalafmt.conf b/.scalafmt.conf index dcf013c..c5eb8f8 100644 --- a/.scalafmt.conf +++ b/.scalafmt.conf @@ -1,5 +1,12 @@ -version = 3.7.1 +version = 3.8.3 runner.dialect = scala213source3 +style = defaultWithAlign +maxColumn = 160 +includeCurlyBraceInSelectChains = false +newlines.penalizeSingleSelectMultiArgList = false +project.git = true +project.excludeFilters = ["target/"] + fileOverride { "glob:**/scala-3/**" { diff --git a/build.sbt b/build.sbt index 63dcfe0..ef676e9 100644 --- a/build.sbt +++ b/build.sbt @@ -1,10 +1,21 @@ +lazy val V = new { + val Scala213 = "2.13.14" + val Scala3 = "3.3.3" + val Skunk = "1.0.0-M7" + val Cats = "2.12.0" + val CatsEffect = "3.5.4" + val Munit = "1.0.0" + val MunitCatsEffect = "2.0.0" + val Testcontainers = "0.41.4" +} + // https://typelevel.org/sbt-typelevel/faq.html#what-is-a-base-version-anyway ThisBuild / tlBaseVersion := "0.0" // your current series x.y -ThisBuild / organization := "de.thatscalaguy" +ThisBuild / organization := "de.thatscalaguy" ThisBuild / organizationName := "ThatScalaGuy" -ThisBuild / startYear := Some(2024) -ThisBuild / licenses := Seq(License.Apache2) +ThisBuild / startYear := Some(2024) +ThisBuild / licenses := Seq(License.Apache2) ThisBuild / developers := List( // your GitHub handle and name tlGitHubDev("ThatScalaGuy", "Sven Herrmann") @@ -16,13 +27,10 @@ ThisBuild / tlSonatypeUseLegacyHost := false // publish website from this branch ThisBuild / tlSitePublishBranch := Some("main") -val Scala213 = "2.13.14" -val Scala3 = "3.3.3" - -ThisBuild / crossScalaVersions := Seq(Scala213, Scala3) -ThisBuild / scalaVersion := Scala213 // the default Scala +ThisBuild / crossScalaVersions := Seq(V.Scala213, V.Scala3) +ThisBuild / scalaVersion := V.Scala213 // the default Scala -Test / fork := true +Test / fork := true Test / classLoaderLayeringStrategy := ClassLoaderLayeringStrategy.Flat lazy val root = (project in file(".")) @@ -34,12 +42,12 @@ lazy val core = project .settings( name := "skunk-crypt", libraryDependencies ++= Seq( - "org.typelevel" %% "cats-core" % "2.12.0", - "org.typelevel" %% "cats-effect" % "3.5.4", - "org.tpolecat" %% "skunk-core" % "1.0.0-M7", - "org.scalameta" %% "munit" % "1.0.0" % Test, - "org.typelevel" %% "munit-cats-effect" % "2.0.0" % Test, - "com.dimafeng" %% "testcontainers-scala-munit" % "0.41.4" % Test + "org.typelevel" %% "cats-core" % V.Cats % "provided", + "org.typelevel" %% "cats-effect" % V.CatsEffect % "provided", + "org.tpolecat" %% "skunk-core" % V.Skunk % "provided", + "org.scalameta" %% "munit" % "1.0.0" % Test, + "org.typelevel" %% "munit-cats-effect" % "2.0.0" % Test, + "com.dimafeng" %% "testcontainers-scala-munit" % "0.41.4" % Test ) ) diff --git a/core/src/main/scala/de/thatscalaguy/skunkcrypt/CryptCodecs.scala b/core/src/main/scala/de/thatscalaguy/skunkcrypt/CryptCodecs.scala index 363dfd1..1515064 100644 --- a/core/src/main/scala/de/thatscalaguy/skunkcrypt/CryptCodecs.scala +++ b/core/src/main/scala/de/thatscalaguy/skunkcrypt/CryptCodecs.scala @@ -16,19 +16,19 @@ package de.thatscalaguy.skunkcrypt -import cats.syntax.all.* +import cats.syntax.all._ + import skunk.Codec import skunk.codec.numeric.safe import skunk.data.Type import java.security.SecureRandom import java.util.Base64 -import javax.crypto.Cipher -import javax.crypto.SecretKey import javax.crypto.spec.GCMParameterSpec +import javax.crypto.{Cipher, SecretKey} trait CryptCodecs { - val GCM_IV_LENGTH = 12 + val GCM_IV_LENGTH = 12 val GCM_TAG_LENGTH = 16 private[skunkcrypt] def encrypt(implicit c: CryptContext): String => String @@ -40,8 +40,8 @@ trait CryptCodecs { value: String ) = { val secretKey = secretKeys.last - val ivSpec = new GCMParameterSpec(GCM_TAG_LENGTH * 8, iv) - val cipher = Cipher.getInstance("AES/GCM/NoPadding") + val ivSpec = new GCMParameterSpec(GCM_TAG_LENGTH * 8, iv) + val cipher = Cipher.getInstance("AES/GCM/NoPadding") cipher.init(Cipher.ENCRYPT_MODE, secretKey, ivSpec) val encryptedBytes = cipher.doFinal(value.getBytes()) Base64.getEncoder.encodeToString( @@ -56,8 +56,8 @@ trait CryptCodecs { encrypted: String ) = { val encryptedBytes = Base64.getDecoder.decode(encrypted) - val ivSpec = new GCMParameterSpec(GCM_TAG_LENGTH * 8, iv) - val cipher = Cipher.getInstance("AES/GCM/NoPadding") + val ivSpec = new GCMParameterSpec(GCM_TAG_LENGTH * 8, iv) + val cipher = Cipher.getInstance("AES/GCM/NoPadding") cipher.init(Cipher.DECRYPT_MODE, secretKey, ivSpec) new String(cipher.doFinal(encryptedBytes)) } @@ -113,7 +113,7 @@ object crypt extends CryptCodecs { def decrypt(implicit c: CryptContext) = value => value.split("\\.").toList match { case iv :: keyIndex :: encrypted :: Nil => - val ivBytes = Base64.getDecoder.decode(iv) + val ivBytes = Base64.getDecoder.decode(iv) val secretKey = c.secretKeys(keyIndex.toInt) genDecrypt(ivBytes, secretKey, encrypted) case _ => throw new Exception("Invalid input") @@ -130,7 +130,7 @@ object cryptd extends CryptCodecs { def decrypt(implicit c: CryptContext) = value => value.split("\\.").toList match { case iv :: keyIndex :: encrypted :: Nil => - val ivBytes = Base64.getDecoder.decode(iv) + val ivBytes = Base64.getDecoder.decode(iv) val secretKey = c.secretKeys(keyIndex.toInt) genDecrypt(ivBytes, secretKey, encrypted) case _ => throw new Exception("Invalid input") diff --git a/core/src/test/scala/de/thatscalaguy/skunkcrypt/MainSuite.scala b/core/src/test/scala/de/thatscalaguy/skunkcrypt/MainSuite.scala index ff117dc..70af8cc 100644 --- a/core/src/test/scala/de/thatscalaguy/skunkcrypt/MainSuite.scala +++ b/core/src/test/scala/de/thatscalaguy/skunkcrypt/MainSuite.scala @@ -17,16 +17,16 @@ package de.thatscalaguy.skunkcrypt import cats.effect.IO + import com.dimafeng.testcontainers.GenericContainer import com.dimafeng.testcontainers.munit.TestContainerForAll import munit.CatsEffectSuite import org.testcontainers.containers.wait.strategy.Wait +import skunk._ +import skunk.codec.all._ +import skunk.implicits._ + import org.typelevel.otel4s.trace.Tracer.Implicits.noop -import skunk.* -import skunk.SSL -import skunk.Session -import skunk.codec.all.* -import skunk.implicits.* class MainSuite extends CatsEffectSuite with TestContainerForAll { override val containerDef = GenericContainer.Def( @@ -52,13 +52,11 @@ class MainSuite extends CatsEffectSuite with TestContainerForAll { session( containers.asInstanceOf[GenericContainer].container.getMappedPort(5432) - ) - .use { session => - session.execute( - sql"CREATE TABLE test (string TEXT, numbers TEXT)".command - ) - } - .void + ).use { session => + session.execute( + sql"CREATE TABLE test (string TEXT, numbers TEXT)".command + ) + }.void .unsafeRunSync() } @@ -70,30 +68,29 @@ class MainSuite extends CatsEffectSuite with TestContainerForAll { "c0e5c54c2a40c95b40d6e837a9c147d4cd7cadeccc555e679efed48f726a5fef" ) .get - session(database.container.getMappedPort(5432)) - .use { session => - for { - // _ <- session.execute( - // sql"INSERT INTO test (string, numbers) VALUES (${text}, ${crypt.int4})".command - // )("hpc3AZ+t1m7mDBf2.e11YUVCQUkPdytj441OjImPhnElN+wSOLL7liXcB+TeRbrsESuGdidbndfu3", 123) - _ <- session.execute( - sql"INSERT INTO test (string, numbers) VALUES (${cryptd.text}, ${cryptd.int4})".command - )(("Hello", 123)) - _ <- session - .execute( - sql"SELECT * FROM test".query(cryptd.text ~ text) - ) - .map(_.foreach(println)) + session(database.container.getMappedPort(5432)).use { session => + for { + // _ <- session.execute( + // sql"INSERT INTO test (string, numbers) VALUES (${text}, ${crypt.int4})".command + // )("hpc3AZ+t1m7mDBf2.e11YUVCQUkPdytj441OjImPhnElN+wSOLL7liXcB+TeRbrsESuGdidbndfu3", 123) + _ <- session.execute( + sql"INSERT INTO test (string, numbers) VALUES (${cryptd.text}, ${cryptd.int4})".command + )(("Hello", 123)) + _ <- session + .execute( + sql"SELECT * FROM test".query(cryptd.text ~ text) + ) + .map(_.foreach(println)) - _ <- session - .execute( - sql"SELECT * FROM test" - .query(cryptd.text ~ cryptd.int4) - ) - .map(_.foreach(println)) - } yield () + _ <- session + .execute( + sql"SELECT * FROM test" + .query(cryptd.text ~ cryptd.int4) + ) + .map(_.foreach(println)) + } yield () - } + } .unsafeRunSync() } } From b59aad6ebca72afa9246104a8c5b774cfaa1dc2b Mon Sep 17 00:00:00 2001 From: Sven Herrmann Date: Tue, 6 Aug 2024 10:59:15 +0200 Subject: [PATCH 2/3] refactor: Add validation for Steward config file --- .github/workflows/ci.yml | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index da30828..7b9523a 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -205,6 +205,31 @@ jobs: modules-ignore: root_2.13 root_3 docs_2.13 docs_3 configs-ignore: test scala-tool scala-doc-tool test-internal + validate-steward: + name: Validate Steward Config + strategy: + matrix: + os: [ubuntu-latest] + java: [temurin@11] + runs-on: ${{ matrix.os }} + steps: + - name: Checkout current branch (fast) + uses: actions/checkout@v4 + + - name: Setup Java (temurin@11) + id: setup-java-temurin-11 + if: matrix.java == 'temurin@11' + uses: actions/setup-java@v4 + with: + distribution: temurin + java-version: 11 + + - uses: coursier/setup-action@v1 + with: + apps: scala-steward + + - run: scala-steward validate-repo-config .scala-steward.conf + site: name: Generate Site strategy: From adce785cb5e16e573728318adc6083a1aa1ad808 Mon Sep 17 00:00:00 2001 From: Sven Herrmann Date: Tue, 6 Aug 2024 11:06:42 +0200 Subject: [PATCH 3/3] refactor: Update sbt plugins versions --- project/plugins.sbt | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/project/plugins.sbt b/project/plugins.sbt index 2c1b0c8..1ab507f 100644 --- a/project/plugins.sbt +++ b/project/plugins.sbt @@ -1,5 +1,5 @@ -addSbtPlugin("org.typelevel" % "sbt-typelevel" % "0.7.2") -addSbtPlugin("org.typelevel" % "sbt-typelevel-site" % "0.7.2") +addSbtPlugin("org.typelevel" % "sbt-typelevel" % "0.7.2") +addSbtPlugin("org.typelevel" % "sbt-typelevel-site" % "0.7.2") addSbtPlugin("org.typelevel" % "sbt-typelevel-scalafix" % "0.7.2") -addSbtPlugin("org.scala-js" % "sbt-scalajs" % "1.16.0") -addSbtPlugin("org.scalameta" % "sbt-mdoc" % "2.5.4") +addSbtPlugin("org.scala-js" % "sbt-scalajs" % "1.16.0") +addSbtPlugin("org.scalameta" % "sbt-mdoc" % "2.5.4")