|
1 package de.uapcore.lightpit.vcs |
|
2 |
|
3 import java.nio.file.Path |
|
4 import java.util.concurrent.TimeUnit |
|
5 |
|
6 abstract class VcsConnector(protected val path: String) { |
|
7 /** |
|
8 * Invokes the VCS binary with the given [args] and returns the output on stdout. |
|
9 */ |
|
10 protected fun invokeCommand(workingDir: Path, vararg args : String): VcsConnectorResult<String> { |
|
11 return try { |
|
12 val command = mutableListOf(path) |
|
13 command.addAll(args) |
|
14 val process = ProcessBuilder(command).directory(workingDir.toFile()).start() |
|
15 val stdout = String(process.inputStream.readAllBytes(), Charsets.UTF_8) |
|
16 if (process.waitFor(30, TimeUnit.SECONDS)) { |
|
17 if (process.exitValue() == 0) { |
|
18 VcsConnectorResult.Success(stdout) |
|
19 } else { |
|
20 VcsConnectorResult.Error("VCS process did not return successfully.") |
|
21 } |
|
22 } else { |
|
23 VcsConnectorResult.Error("VCS process did not return in time.") |
|
24 } |
|
25 } catch (e: Throwable) { |
|
26 VcsConnectorResult.Error("Error during process invocation: "+e.message) |
|
27 } |
|
28 } |
|
29 |
|
30 /** |
|
31 * Takes a [commitLog] in format `::lpitref::{node}:{desc}` and parses commit references. |
|
32 * Supported references are (in this example, 47 is the issue ID): |
|
33 * - fixes #47 |
|
34 * - fix #47 |
|
35 * - closes #47 |
|
36 * - close #47 |
|
37 * - relates to #47 |
|
38 */ |
|
39 protected fun parseCommitRefs(commitLog: String): List<CommitRef> = buildList { |
|
40 val marker = "::lpitref::" |
|
41 var currentHash = "" |
|
42 var currentDesc = "" |
|
43 for (line in commitLog.split("\n")) { |
|
44 // see if current line contains a new log entry |
|
45 if (line.startsWith(marker)) { |
|
46 val head = line.substring(marker.length).split(':', limit = 2) |
|
47 currentHash = head[0] |
|
48 currentDesc = head[1] |
|
49 } |
|
50 |
|
51 // skip possible preamble output |
|
52 if (currentHash.isEmpty()) continue |
|
53 |
|
54 // scan the lines for commit references |
|
55 Regex("""(?:relates to|fix(?:es)?|close(?:es)?) #(\d+)""") |
|
56 .findAll(line) |
|
57 .map { it.groupValues[1] } |
|
58 .map { it.toIntOrNull() } |
|
59 .filterNotNull() |
|
60 .forEach { commitId -> add(CommitRef(currentHash, commitId, currentDesc)) } |
|
61 } |
|
62 } |
|
63 } |