src/main/kotlin/de/uapcore/lightpit/types/CommitRef.kt

changeset 282
c112fad21bf6
parent 280
12b898531d1a
child 288
10324c383126
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/main/kotlin/de/uapcore/lightpit/types/CommitRef.kt	Sat Jul 22 13:04:12 2023 +0200
@@ -0,0 +1,64 @@
+/*
+ * Copyright 2023 Mike Becker. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ *
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+ * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
+ * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
+ * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
+ * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ *
+ */
+
+package de.uapcore.lightpit.types
+
+data class CommitRef(val hash: String, val issueId: Int, val message: String)
+
+/**
+ * Takes a [commitLog] in format `::lpitref::{node}:{desc}` and parses commit references.
+ * Supported references are (in this example, 47 is the issue ID):
+ *  - fixes #47
+ *  - fix #47
+ *  - closes #47
+ *  - close #47
+ *  - relates to #47
+ */
+fun parseCommitRefs(commitLog: String): List<CommitRef> = buildList {
+    val marker = "::lpitref:"
+    var currentHash = ""
+    var currentDesc = ""
+    for (line in commitLog.split("\n")) {
+        // see if current line contains a new log entry
+        if (line.startsWith(marker)) {
+            val head = line.substring(marker.length).split(':', limit = 2)
+            currentHash = head[0]
+            currentDesc = head[1]
+        }
+
+        // skip possible preamble output
+        if (currentHash.isEmpty()) continue
+
+        // scan the lines for commit references
+        Regex("""(?:relates to|fix(?:es)?|close(?:es)?) #(\d+)""")
+            .findAll(line)
+            .map { it.groupValues[1] }
+            .map { it.toIntOrNull() }
+            .filterNotNull()
+            .forEach { commitId -> add(CommitRef(currentHash, commitId, currentDesc)) }
+    }
+}
+

mercurial