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