Sat, 22 Jul 2023 11:32:27 +0200
make vcs command timeout configurable
relates to #274
universe@279 | 1 | /* |
universe@279 | 2 | * Copyright 2023 Mike Becker. All rights reserved. |
universe@279 | 3 | * |
universe@279 | 4 | * Redistribution and use in source and binary forms, with or without |
universe@279 | 5 | * modification, are permitted provided that the following conditions are met: |
universe@279 | 6 | * |
universe@279 | 7 | * 1. Redistributions of source code must retain the above copyright |
universe@279 | 8 | * notice, this list of conditions and the following disclaimer. |
universe@279 | 9 | * |
universe@279 | 10 | * 2. Redistributions in binary form must reproduce the above copyright |
universe@279 | 11 | * notice, this list of conditions and the following disclaimer in the |
universe@279 | 12 | * documentation and/or other materials provided with the distribution. |
universe@279 | 13 | * |
universe@279 | 14 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
universe@279 | 15 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
universe@279 | 16 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
universe@279 | 17 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE |
universe@279 | 18 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
universe@279 | 19 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
universe@279 | 20 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
universe@279 | 21 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
universe@279 | 22 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
universe@279 | 23 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
universe@279 | 24 | * |
universe@279 | 25 | */ |
universe@279 | 26 | |
universe@279 | 27 | package de.uapcore.lightpit.vcs |
universe@279 | 28 | |
universe@280 | 29 | import java.nio.file.Files |
universe@280 | 30 | import kotlin.io.path.ExperimentalPathApi |
universe@280 | 31 | import kotlin.io.path.deleteRecursively |
universe@279 | 32 | |
universe@279 | 33 | /** |
universe@279 | 34 | * A connector for Mercurial repositories. |
universe@279 | 35 | * |
universe@279 | 36 | * @param path the path to the Mercurial binary |
universe@279 | 37 | */ |
universe@280 | 38 | class HgConnector(path: String) : VcsConnector(path) { |
universe@279 | 39 | |
universe@279 | 40 | /** |
universe@279 | 41 | * Checks, if the specified binary is available and executable. |
universe@279 | 42 | */ |
universe@279 | 43 | fun checkAvailability(): Boolean { |
universe@281 | 44 | return when (val versionInfo = invokeCommand("--version")) { |
universe@280 | 45 | is VcsConnectorResult.Success -> versionInfo.content.contains("Mercurial") |
universe@280 | 46 | else -> false |
universe@279 | 47 | } |
universe@279 | 48 | } |
universe@280 | 49 | |
universe@280 | 50 | /** |
universe@280 | 51 | * Reads the commit log and parses every reference to an issue. |
universe@280 | 52 | * |
universe@280 | 53 | * The [pathOrUrl] must be a valid path or URL recognized by the VCS binary. |
universe@280 | 54 | * Currently, no authentication is supported and the repository must therefore be publicly readable. |
universe@280 | 55 | */ |
universe@280 | 56 | @OptIn(ExperimentalPathApi::class) |
universe@280 | 57 | fun readCommitLog(pathOrUrl: String): VcsConnectorResult<List<CommitRef>> { |
universe@280 | 58 | val tmpDir = try { |
universe@280 | 59 | Files.createTempDirectory("lightpit-vcs-") |
universe@280 | 60 | } catch (e: Throwable) { |
universe@280 | 61 | return VcsConnectorResult.Error("Creating temporary directory for VCS connection failed: " + e.message) |
universe@280 | 62 | } |
universe@281 | 63 | val init = invokeCommand("init", workingDir = tmpDir) |
universe@280 | 64 | if (init is VcsConnectorResult.Error) { |
universe@280 | 65 | return init |
universe@280 | 66 | } |
universe@280 | 67 | |
universe@280 | 68 | val commitLogContent = when (val result = invokeCommand( |
universe@281 | 69 | "incoming", pathOrUrl, "-n", "--template", "::lpitref::{node}:{desc}\\n", |
universe@281 | 70 | workingDir = tmpDir, |
universe@281 | 71 | timeout = 60 |
universe@280 | 72 | )) { |
universe@280 | 73 | is VcsConnectorResult.Error -> return result |
universe@280 | 74 | is VcsConnectorResult.Success -> result.content |
universe@280 | 75 | } |
universe@280 | 76 | |
universe@280 | 77 | val commitRefs = parseCommitRefs(commitLogContent) |
universe@280 | 78 | |
universe@280 | 79 | tmpDir.deleteRecursively() |
universe@280 | 80 | return VcsConnectorResult.Success(commitRefs) |
universe@280 | 81 | } |
universe@279 | 82 | } |