src/main/kotlin/de/uapcore/lightpit/vcs/VcsConnector.kt

changeset 282
c112fad21bf6
parent 281
c15b9555ecf3
child 283
ea6181255423
     1.1 --- a/src/main/kotlin/de/uapcore/lightpit/vcs/VcsConnector.kt	Sat Jul 22 11:32:27 2023 +0200
     1.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.3 @@ -1,67 +0,0 @@
     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(
    1.14 -        vararg args: String,
    1.15 -        workingDir: Path = Path.of("."),
    1.16 -        timeout: Long = 30L
    1.17 -    ): VcsConnectorResult<String> {
    1.18 -        return try {
    1.19 -            val command = mutableListOf(path)
    1.20 -            command.addAll(args)
    1.21 -            val process = ProcessBuilder(command).directory(workingDir.toFile()).start()
    1.22 -            val stdout = String(process.inputStream.readAllBytes(), Charsets.UTF_8)
    1.23 -            if (process.waitFor(timeout, TimeUnit.SECONDS)) {
    1.24 -                if (process.exitValue() == 0) {
    1.25 -                    VcsConnectorResult.Success(stdout)
    1.26 -                } else {
    1.27 -                    VcsConnectorResult.Error("VCS process did not return successfully.")
    1.28 -                }
    1.29 -            } else {
    1.30 -                VcsConnectorResult.Error("VCS process did not return in time.")
    1.31 -            }
    1.32 -        } catch (e: Throwable) {
    1.33 -            VcsConnectorResult.Error("Error during process invocation: "+e.message)
    1.34 -        }
    1.35 -    }
    1.36 -
    1.37 -    /**
    1.38 -     * Takes a [commitLog] in format `::lpitref::{node}:{desc}` and parses commit references.
    1.39 -     * Supported references are (in this example, 47 is the issue ID):
    1.40 -     *  - fixes #47
    1.41 -     *  - fix #47
    1.42 -     *  - closes #47
    1.43 -     *  - close #47
    1.44 -     *  - relates to #47
    1.45 -     */
    1.46 -    protected fun parseCommitRefs(commitLog: String): List<CommitRef> = buildList {
    1.47 -        val marker = "::lpitref::"
    1.48 -        var currentHash = ""
    1.49 -        var currentDesc = ""
    1.50 -        for (line in commitLog.split("\n")) {
    1.51 -            // see if current line contains a new log entry
    1.52 -            if (line.startsWith(marker)) {
    1.53 -                val head = line.substring(marker.length).split(':', limit = 2)
    1.54 -                currentHash = head[0]
    1.55 -                currentDesc = head[1]
    1.56 -            }
    1.57 -
    1.58 -            // skip possible preamble output
    1.59 -            if (currentHash.isEmpty()) continue
    1.60 -
    1.61 -            // scan the lines for commit references
    1.62 -            Regex("""(?:relates to|fix(?:es)?|close(?:es)?) #(\d+)""")
    1.63 -                .findAll(line)
    1.64 -                .map { it.groupValues[1] }
    1.65 -                .map { it.toIntOrNull() }
    1.66 -                .filterNotNull()
    1.67 -                .forEach { commitId -> add(CommitRef(currentHash, commitId, currentDesc)) }
    1.68 -        }
    1.69 -    }
    1.70 -}
    1.71 \ No newline at end of file

mercurial