~jan0sch/smederee

Showing details for patch 10a5b05d1375812cad30752b0921be25e50c0ee8.
2023-03-27 (Mon), 1:24 PM - Jens Grassel - 10a5b05d1375812cad30752b0921be25e50c0ee8

Add linking for Fixes comments in patch details.

- add a link to the patch detail url for a `Fixes: <HASH>` line
- add function `withFixesHashUrl(baseUri: Uri)` to `VcsPatchComment`
Summary of changes
3 files modified with 28 lines added and 3 lines removed
  • modules/hub/src/main/scala/de/smederee/hub/VcsRepository.scala with 17 added and 0 removed lines
  • modules/hub/src/main/twirl/de/smederee/hub/views/repositoryPatchMetadata.scala.html with 10 added and 2 removed lines
  • modules/hub/src/main/twirl/de/smederee/hub/views/showRepositoryPatch.scala.html with 1 added and 1 removed lines
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-01-31 07:54:07.274074397 +0000
+++ new-smederee/modules/hub/src/main/scala/de/smederee/hub/VcsRepository.scala	2025-01-31 07:54:07.274074397 +0000
@@ -184,6 +184,7 @@
 
 opaque type VcsPatchComment = String
 object VcsPatchComment {
+  val FixesHashMarker: Regex = "\\bFixes: (?<hash>[a-f0-9]{40})\\b".r
 
   /** Create an instance of VcsPatchComment from the given String type.
     *
@@ -205,6 +206,22 @@
 
   extension (comment: VcsPatchComment) {
     def length: Int = comment.length
+
+    /** Find trailing `Fixes: <HASH>` lines in a patch comment as described in the contributing guidelines and replace
+      * them with a link that is supposed to point where the patch details will be displayed.
+      *
+      * @param baseUri
+      *   The base URI to which the actual hash value will be appended.
+      * @return
+      *   All lines of the comment string with every line containing the patch comment with all occurrences of `Fixes:
+      *   <HASH>` replaced by `Fixes: <baseUri/HASH>`.
+      */
+    def withFixesHashUrl(baseUri: Uri): Array[String] = {
+      def replace(m: Regex.Match): String =
+        s"""Fixes: <a href="${baseUri.addSegment(m.group("hash")).toString}">${m.group("hash")}</a>"""
+      val lines = comment.toString.split("\\r?\\n")
+      lines.map(line => FixesHashMarker.replaceAllIn(line, replace))
+    }
   }
 }
 
diff -rN -u old-smederee/modules/hub/src/main/twirl/de/smederee/hub/views/repositoryPatchMetadata.scala.html new-smederee/modules/hub/src/main/twirl/de/smederee/hub/views/repositoryPatchMetadata.scala.html
--- old-smederee/modules/hub/src/main/twirl/de/smederee/hub/views/repositoryPatchMetadata.scala.html	2025-01-31 07:54:07.274074397 +0000
+++ new-smederee/modules/hub/src/main/twirl/de/smederee/hub/views/repositoryPatchMetadata.scala.html	2025-01-31 07:54:07.274074397 +0000
@@ -1,11 +1,11 @@
 @import de.smederee.hub._
 
-@(actionBaseUri: Option[Uri], patch: VcsRepositoryPatchMetadata)(implicit locale: java.util.Locale)
+@(actionBaseUri: Option[Uri], patch: VcsRepositoryPatchMetadata, linkTitle: Boolean = true)(implicit locale: java.util.Locale)
 <div class="patch">
   <div class="patch-details">
     <span class="timestamp">@Messages("repository.overview.latest-changes.timestamp", java.util.Date.from(patch.timestamp.toInstant))</span> - <span class="author">@patch.author.withoutEmail</span> - <span class="patch-hash">@patch.hash</span>
   </div>
-  @if(actionBaseUri.nonEmpty) {
+  @if(actionBaseUri.nonEmpty && linkTitle) {
     @for(uri <- actionBaseUri) {
       <h4 class="patch-name"><a href="@uri.addSegment("patch").addSegment(patch.hash.toString)" title="@Messages("repository.changes.patch.title.link", patch.hash)">@patch.name</a></h4>
     }
@@ -13,7 +13,15 @@
     <h4 class="patch-name">@patch.name</h4>
   }
   <div class="patch-comments">
+  @if(actionBaseUri.nonEmpty) {
+    @for(uri <- actionBaseUri) {
+      @defining(patch.comment.map(_.withFixesHashUrl(uri.addSegment("patch"))).getOrElse(Array.empty[String])) { lines =>
+      <pre class="latest-changes"><code>@for(line <- lines){@if(line.startsWith("Fixes:")){@Html(line)}else{@{line + "\n"}}}</code></pre>
+      }
+    }
+  } else {
     <pre class="latest-changes"><code>@patch.comment</code></pre>
+  }
   </div>
   @for(summary <- patch.summary) {
     <h5>@Messages("repository.changes.patch.summary.title")</h5>
diff -rN -u old-smederee/modules/hub/src/main/twirl/de/smederee/hub/views/showRepositoryPatch.scala.html new-smederee/modules/hub/src/main/twirl/de/smederee/hub/views/showRepositoryPatch.scala.html
--- old-smederee/modules/hub/src/main/twirl/de/smederee/hub/views/showRepositoryPatch.scala.html	2025-01-31 07:54:07.274074397 +0000
+++ new-smederee/modules/hub/src/main/twirl/de/smederee/hub/views/showRepositoryPatch.scala.html	2025-01-31 07:54:07.274074397 +0000
@@ -33,7 +33,7 @@
       <div class="pure-u-1-1 pure-u-md-1-1">
         <div class="l-box">
           @for(patch <- patch) {
-            @repositoryPatchMetadata(None, patch)
+            @repositoryPatchMetadata(actionBaseUri.some, patch, linkTitle = false)
           }
         </div>
       </div>