1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/main/kotlin/de/uapcore/lightpit/vcs/VcsConnector.kt Tue Jul 18 18:05:49 2023 +0200 1.3 @@ -0,0 +1,63 @@ 1.4 +package de.uapcore.lightpit.vcs 1.5 + 1.6 +import java.nio.file.Path 1.7 +import java.util.concurrent.TimeUnit 1.8 + 1.9 +abstract class VcsConnector(protected val path: String) { 1.10 + /** 1.11 + * Invokes the VCS binary with the given [args] and returns the output on stdout. 1.12 + */ 1.13 + protected fun invokeCommand(workingDir: Path, vararg args : String): VcsConnectorResult<String> { 1.14 + return try { 1.15 + val command = mutableListOf(path) 1.16 + command.addAll(args) 1.17 + val process = ProcessBuilder(command).directory(workingDir.toFile()).start() 1.18 + val stdout = String(process.inputStream.readAllBytes(), Charsets.UTF_8) 1.19 + if (process.waitFor(30, TimeUnit.SECONDS)) { 1.20 + if (process.exitValue() == 0) { 1.21 + VcsConnectorResult.Success(stdout) 1.22 + } else { 1.23 + VcsConnectorResult.Error("VCS process did not return successfully.") 1.24 + } 1.25 + } else { 1.26 + VcsConnectorResult.Error("VCS process did not return in time.") 1.27 + } 1.28 + } catch (e: Throwable) { 1.29 + VcsConnectorResult.Error("Error during process invocation: "+e.message) 1.30 + } 1.31 + } 1.32 + 1.33 + /** 1.34 + * Takes a [commitLog] in format `::lpitref::{node}:{desc}` and parses commit references. 1.35 + * Supported references are (in this example, 47 is the issue ID): 1.36 + * - fixes #47 1.37 + * - fix #47 1.38 + * - closes #47 1.39 + * - close #47 1.40 + * - relates to #47 1.41 + */ 1.42 + protected fun parseCommitRefs(commitLog: String): List<CommitRef> = buildList { 1.43 + val marker = "::lpitref::" 1.44 + var currentHash = "" 1.45 + var currentDesc = "" 1.46 + for (line in commitLog.split("\n")) { 1.47 + // see if current line contains a new log entry 1.48 + if (line.startsWith(marker)) { 1.49 + val head = line.substring(marker.length).split(':', limit = 2) 1.50 + currentHash = head[0] 1.51 + currentDesc = head[1] 1.52 + } 1.53 + 1.54 + // skip possible preamble output 1.55 + if (currentHash.isEmpty()) continue 1.56 + 1.57 + // scan the lines for commit references 1.58 + Regex("""(?:relates to|fix(?:es)?|close(?:es)?) #(\d+)""") 1.59 + .findAll(line) 1.60 + .map { it.groupValues[1] } 1.61 + .map { it.toIntOrNull() } 1.62 + .filterNotNull() 1.63 + .forEach { commitId -> add(CommitRef(currentHash, commitId, currentDesc)) } 1.64 + } 1.65 + } 1.66 +} 1.67 \ No newline at end of file