Search in sources :

Example 31 with VcsException

use of jetbrains.buildServer.vcs.VcsException in project teamcity-git by JetBrains.

the class LogCommandImpl method call.

@Nullable
public String call() {
    try {
        GitCommandLine cmd = getCmd();
        cmd.addParameters("log");
        if (myCommitsNumber != 0)
            cmd.addParameter("-n" + myCommitsNumber);
        if (myFormat != null)
            cmd.addParameter("--pretty=format:" + myFormat);
        cmd.addParameter(myStartPoint);
        cmd.addParameter("--");
        return CommandUtil.runCommand(cmd.stdErrExpected(false)).getStdout().trim();
    } catch (VcsException e) {
        return null;
    }
}
Also used : GitCommandLine(jetbrains.buildServer.buildTriggers.vcs.git.command.GitCommandLine) VcsException(jetbrains.buildServer.vcs.VcsException) Nullable(org.jetbrains.annotations.Nullable)

Example 32 with VcsException

use of jetbrains.buildServer.vcs.VcsException in project teamcity-git by JetBrains.

the class GitCommandLine method getPrivateKey.

@Nullable
private File getPrivateKey(@NotNull AuthSettings authSettings) throws VcsException {
    File privateKey = null;
    final boolean useSshAskPass = myCtx.isUseSshAskPass();
    try {
        switch(authSettings.getAuthMethod()) {
            case TEAMCITY_SSH_KEY:
                privateKey = getUploadedPrivateKey(authSettings);
                break;
            case PRIVATE_KEY_FILE:
                final String keyPath = authSettings.getPrivateKeyFilePath();
                if (StringUtil.isEmpty(keyPath)) {
                    throw new VcsException("Authentication method is \"" + AuthenticationMethod.PRIVATE_KEY_FILE.uiName() + "\", but no private key path provided");
                }
                final File finalPrivateKey = createTmpKeyFile();
                addPostAction(() -> FileUtil.delete(finalPrivateKey));
                privateKey = finalPrivateKey;
                FileUtil.copy(new File(keyPath), privateKey);
                break;
            case PRIVATE_KEY_DEFAULT:
                // we do not decrypt default ssh keys
                return null;
            default:
                return null;
        }
        final String passphrase = authSettings.getPassphrase();
        if (useSshAskPass) {
            withAskPassScript(passphrase, askPassPath -> {
                addEnvParam("SSH_ASKPASS", askPassPath);
                addEnvParam("DISPLAY", ":0.0");
            });
        } else {
            final KeyPair keyPair = KeyPair.load(new JSch(), privateKey.getAbsolutePath());
            OutputStream out = null;
            try {
                out = new BufferedOutputStream(new FileOutputStream(privateKey));
                if (keyPair.isEncrypted() && !keyPair.decrypt(passphrase)) {
                    throw new VcsException("Wrong SSH key passphrase");
                }
                keyPair.writePrivateKey(out, null);
            } finally {
                FileUtil.close(out);
            }
        }
        // set permissions to 600, without that ssh client rejects the key on *nix
        privateKey.setReadable(false, false);
        privateKey.setReadable(true, true);
        privateKey.setWritable(false, false);
        privateKey.setWritable(true, true);
        return privateKey;
    } catch (Exception e) {
        if (privateKey != null)
            FileUtil.delete(privateKey);
        if (e instanceof VcsException)
            throw (VcsException) e;
        throw new VcsException(e);
    }
}
Also used : KeyPair(com.jcraft.jsch.KeyPair) VcsException(jetbrains.buildServer.vcs.VcsException) LineAwareByteArrayOutputStream(jetbrains.buildServer.LineAwareByteArrayOutputStream) JSch(com.jcraft.jsch.JSch) CheckoutCanceledException(jetbrains.buildServer.buildTriggers.vcs.git.command.errors.CheckoutCanceledException) VcsException(jetbrains.buildServer.vcs.VcsException) Nullable(org.jetbrains.annotations.Nullable)

Example 33 with VcsException

use of jetbrains.buildServer.vcs.VcsException in project teamcity-git by JetBrains.

the class CommandUtil method isRecoverable.

public static boolean isRecoverable(@NotNull Exception e, AuthSettings authSettings, int attempt, int maxAttempts) {
    boolean attemptsLeft = attempt < maxAttempts;
    if (e instanceof ProcessTimeoutException || e instanceof GitExecTimeout)
        return attemptsLeft;
    if (!(e instanceof VcsException))
        return false;
    final VcsException ve = (VcsException) e;
    if (isTimeoutError(ve) || isConnectionRefused(ve) || isConnectionReset(ve))
        return attemptsLeft;
    if (isCanceledError(ve))
        return false;
    if (e instanceof GitIndexCorruptedException)
        return false;
    if (authSettings.doesTokenNeedRefresh() && attempt == 1)
        return true;
    return attemptsLeft && !isRemoteAccessError(ve);
}
Also used : GitExecTimeout(jetbrains.buildServer.buildTriggers.vcs.git.command.errors.GitExecTimeout) GitIndexCorruptedException(jetbrains.buildServer.buildTriggers.vcs.git.command.errors.GitIndexCorruptedException) VcsException(jetbrains.buildServer.vcs.VcsException) ProcessTimeoutException(jetbrains.buildServer.ProcessTimeoutException)

Example 34 with VcsException

use of jetbrains.buildServer.vcs.VcsException in project teamcity-git by JetBrains.

the class CommandUtil method commandFailed.

@SuppressWarnings({ "ThrowableResultOfMethodCallIgnored" })
private static void commandFailed(final String cmdName, final ExecResult res) throws VcsException {
    Throwable exception = res.getException();
    String stderr = res.getStderr().trim();
    String stdout = res.getStdout().trim();
    int exitCode = res.getExitCode();
    String message = cmdName + " command failed." + (exitCode != 0 ? "\nexit code: " + exitCode : "") + (!StringUtil.isEmpty(stdout) ? "\n" + "stdout: " + stdout : "") + (!StringUtil.isEmpty(stderr) ? "\n" + "stderr: " + stderr : "");
    if (exception != null) {
        message += "\nexception: ";
        message += exception.getClass().getName();
        String exceptionMessage = exception.getMessage();
        if (!StringUtil.isEmpty(exceptionMessage))
            message += ": " + exceptionMessage;
    }
    if (exception != null && isImportant(exception)) {
        Writer stackWriter = new StringWriter();
        exception.printStackTrace(new PrintWriter(stackWriter));
        message += "\n" + stackWriter;
    }
    if (exception != null)
        throw new VcsException(message, exception);
    throw new VcsException(message);
}
Also used : VcsException(jetbrains.buildServer.vcs.VcsException)

Example 35 with VcsException

use of jetbrains.buildServer.vcs.VcsException in project teamcity-git by JetBrains.

the class GitCommandLine method getUploadedPrivateKey.

@NotNull
private File getUploadedPrivateKey(@NotNull AuthSettings authSettings) throws Exception {
    final String keyId = authSettings.getTeamCitySshKeyId();
    final VcsRoot root = authSettings.getRoot();
    if (keyId == null || root == null || mySshKeyManager == null) {
        final String msg = "Failed to locate uploaded SSH key " + keyId + " for vcs root %s " + (mySshKeyManager == null ? ": null ssh key manager" : "");
        Loggers.VCS.warn(String.format(msg, LogUtil.describe(root)));
        throw new VcsException(String.format(msg, root == null ? null : root.getName()));
    }
    final TeamCitySshKey key = mySshKeyManager.getKey(root);
    if (key == null) {
        throw new VcsException("Failed to locate uploaded SSH key " + keyId + " for vcs root " + root.getName());
    }
    final File privateKey = createTmpKeyFile();
    addPostAction(() -> FileUtil.delete(privateKey));
    FileUtil.writeFileAndReportErrors(privateKey, new String(key.getPrivateKey()));
    return privateKey;
}
Also used : VcsException(jetbrains.buildServer.vcs.VcsException) VcsRoot(jetbrains.buildServer.vcs.VcsRoot) TeamCitySshKey(jetbrains.buildServer.ssh.TeamCitySshKey) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

VcsException (jetbrains.buildServer.vcs.VcsException)79 File (java.io.File)34 NotNull (org.jetbrains.annotations.NotNull)22 GitTestUtil.dataFile (jetbrains.buildServer.buildTriggers.vcs.git.tests.GitTestUtil.dataFile)13 IOException (java.io.IOException)12 TestFor (jetbrains.buildServer.util.TestFor)11 GeneralCommandLine (com.intellij.execution.configurations.GeneralCommandLine)10 Method (java.lang.reflect.Method)10 ExecResult (jetbrains.buildServer.ExecResult)10 VcsRootImpl (jetbrains.buildServer.vcs.impl.VcsRootImpl)10 AfterMethod (org.testng.annotations.AfterMethod)10 BeforeMethod (org.testng.annotations.BeforeMethod)10 VcsRoot (jetbrains.buildServer.vcs.VcsRoot)9 Repository (org.eclipse.jgit.lib.Repository)9 URIish (org.eclipse.jgit.transport.URIish)9 Nullable (org.jetbrains.annotations.Nullable)8 SimpleCommandLineProcessRunner (jetbrains.buildServer.SimpleCommandLineProcessRunner)5 GitExec (jetbrains.buildServer.buildTriggers.vcs.git.command.GitExec)5 CheckoutRules (jetbrains.buildServer.vcs.CheckoutRules)5 RefSpec (org.eclipse.jgit.transport.RefSpec)5