~jan0sch/smederee

Showing details for patch 474306da3300fb9afca9a4e8b9b630d4f2c7770d.
2022-08-04 (Thu), 1:57 PM - Jens Grassel - 474306da3300fb9afca9a4e8b9b630d4f2c7770d

darcs: change format for repo names

Summary of changes
3 files modified with 134 lines added and 8 lines removed
  • modules/hub/src/it/scala/de/smederee/hub/Generators.scala with 47 added and 0 removed lines
  • modules/hub/src/main/scala/de/smederee/hub/VcsRepository.scala with 40 added and 8 removed lines
  • modules/hub/src/test/scala/de/smederee/hub/Generators.scala with 47 added and 0 removed lines
diff -rN -u old-smederee/modules/hub/src/it/scala/de/smederee/hub/Generators.scala new-smederee/modules/hub/src/it/scala/de/smederee/hub/Generators.scala
--- old-smederee/modules/hub/src/it/scala/de/smederee/hub/Generators.scala	2025-02-03 02:11:02.732659389 +0000
+++ new-smederee/modules/hub/src/it/scala/de/smederee/hub/Generators.scala	2025-02-03 02:11:02.736659396 +0000
@@ -105,4 +105,51 @@
 
   given Arbitrary[Session] = Arbitrary(genValidSession)
 
+  val genValidVcsRepositoryName: Gen[VcsRepositoryName] = Gen
+    .nonEmptyListOf(
+      Gen.oneOf(
+        List(
+          "a",
+          "b",
+          "c",
+          "d",
+          "e",
+          "f",
+          "g",
+          "h",
+          "i",
+          "j",
+          "k",
+          "l",
+          "m",
+          "n",
+          "o",
+          "p",
+          "q",
+          "r",
+          "s",
+          "t",
+          "u",
+          "v",
+          "w",
+          "x",
+          "y",
+          "z",
+          "0",
+          "1",
+          "2",
+          "3",
+          "4",
+          "5",
+          "6",
+          "7",
+          "8",
+          "9",
+          "-",
+          "_"
+        )
+      )
+    )
+    .map(cs => VcsRepositoryName(cs.take(64).mkString))
+
 }
diff -rN -u old-smederee/modules/hub/src/main/scala/de/smederee/hub/VcsRepository.scala new-smederee/modules/hub/src/main/scala/de/smederee/hub/VcsRepository.scala
--- old-smederee/modules/hub/src/main/scala/de/smederee/hub/VcsRepository.scala	2025-02-03 02:11:02.736659396 +0000
+++ new-smederee/modules/hub/src/main/scala/de/smederee/hub/VcsRepository.scala	2025-02-03 02:11:02.736659396 +0000
@@ -9,9 +9,16 @@
 
 package de.smederee.hub
 
+import cats.data._
+import cats.syntax.all._
+
+import scala.util.matching.Regex
+
 opaque type VcsRepositoryName = String
 object VcsRepositoryName {
 
+  val Format: Regex = "^[a-z0-9][a-z0-9\\-_]{1,63}$".r
+
   /** Create an instance of VcsRepositoryName from the given String type.
     *
     * @param source
@@ -28,14 +35,39 @@
     * @return
     *   An option to the successfully converted VcsRepositoryName.
     */
-  def from(source: String): Option[VcsRepositoryName] = {
-    val filterNull = Option(source)
-    // Check for not allowed characters.
-    if (filterNull.exists(_.contains("/")) || filterNull.exists(_.contains('\u0000')))
-      None
-    else
-      filterNull
-  }
+  def from(source: String): Option[VcsRepositoryName] = validate(source).toOption
+
+  /** Validate the given string and return either the validated repository name or a list of errors.
+    *
+    * @param s
+    *   An arbitrary string which should be a repository name.
+    * @return
+    *   Either a list of errors or the validated repository name.
+    */
+  def validate(s: String): ValidatedNel[String, VcsRepositoryName] =
+    Option(s).map(_.trim.nonEmpty) match {
+      case Some(true) =>
+        val input = s.trim
+        val miniumLength =
+          if (input.length > 1)
+            input.validNel
+          else
+            "Repository name too short (min. 2 characters)!".invalidNel
+        val maximumLength =
+          if (input.length < 65)
+            input.validNel
+          else
+            "Repository name too long (max. 64 characters)!".invalidNel
+        val validFormat =
+          if (Format.matches(input))
+            input.validNel
+          else
+            "Repository name must start with a lowercase letter or number and is only allowed to contain lowercase alphanumeric characters plus .".invalidNel
+        (miniumLength, maximumLength, validFormat).mapN { case (_, _, name) =>
+          name
+        }
+      case _ => "Repository name must not be empty!".invalidNel
+    }
 }
 
 /** Data about a VCS respository.
diff -rN -u old-smederee/modules/hub/src/test/scala/de/smederee/hub/Generators.scala new-smederee/modules/hub/src/test/scala/de/smederee/hub/Generators.scala
--- old-smederee/modules/hub/src/test/scala/de/smederee/hub/Generators.scala	2025-02-03 02:11:02.736659396 +0000
+++ new-smederee/modules/hub/src/test/scala/de/smederee/hub/Generators.scala	2025-02-03 02:11:02.736659396 +0000
@@ -105,4 +105,51 @@
 
   given Arbitrary[Session] = Arbitrary(genValidSession)
 
+  val genValidVcsRepositoryName: Gen[VcsRepositoryName] = Gen
+    .nonEmptyListOf(
+      Gen.oneOf(
+        List(
+          "a",
+          "b",
+          "c",
+          "d",
+          "e",
+          "f",
+          "g",
+          "h",
+          "i",
+          "j",
+          "k",
+          "l",
+          "m",
+          "n",
+          "o",
+          "p",
+          "q",
+          "r",
+          "s",
+          "t",
+          "u",
+          "v",
+          "w",
+          "x",
+          "y",
+          "z",
+          "0",
+          "1",
+          "2",
+          "3",
+          "4",
+          "5",
+          "6",
+          "7",
+          "8",
+          "9",
+          "-",
+          "_"
+        )
+      )
+    )
+    .map(cs => VcsRepositoryName(cs.take(64).mkString))
+
 }