Search in sources :

Example 1 with GitClient

use of org.jenkinsci.plugins.gitclient.GitClient in project blueocean-plugin by jenkinsci.

the class GitCloneReadSaveRequest method save.

@Override
void save() throws IOException {
    try {
        GitClient git = cloneRepo();
        try {
            git.checkoutBranch(sourceBranch, "origin/" + sourceBranch);
        } catch (Exception e) {
            throw new RuntimeException("Branch not found: " + sourceBranch);
        }
        if (!sourceBranch.equals(branch)) {
            // git.branch(branch);
            git.checkoutBranch(branch, "origin/" + sourceBranch);
        }
        File f = new File(repositoryPath, filePath);
        // commit will fail if the contents hasn't changed
        if (!f.exists() || !Arrays.equals(FileUtils.readFileToByteArray(f), contents)) {
            FileUtils.writeByteArrayToFile(f, contents);
            git.add(filePath);
            git.commit(commitMessage);
        }
        git.push().ref(branch).to(new URIish(gitSource.getRemote())).execute();
    } catch (InterruptedException | GitException | URISyntaxException ex) {
        throw new ServiceException.UnexpectedErrorException("Unable to save " + filePath, ex);
    } finally {
        cleanupRepo();
    }
}
Also used : URIish(org.eclipse.jgit.transport.URIish) ServiceException(io.jenkins.blueocean.commons.ServiceException) GitException(hudson.plugins.git.GitException) URISyntaxException(java.net.URISyntaxException) GitClient(org.jenkinsci.plugins.gitclient.GitClient) File(java.io.File) URISyntaxException(java.net.URISyntaxException) IOException(java.io.IOException) ServiceException(io.jenkins.blueocean.commons.ServiceException) GitException(hudson.plugins.git.GitException)

Example 2 with GitClient

use of org.jenkinsci.plugins.gitclient.GitClient in project blueocean-plugin by jenkinsci.

the class GitCloneReadSaveRequest method cloneRepo.

GitClient cloneRepo() throws InterruptedException, IOException {
    EnvVars environment = new EnvVars();
    TaskListener taskListener = new LogTaskListener(Logger.getAnonymousLogger(), Level.ALL);
    String gitExe = gitTool.getGitExe();
    GitClient git = Git.with(taskListener, environment).in(repositoryPath).using(gitExe).getClient();
    git.addCredentials(gitSource.getRemote(), getCredential());
    try {
        git.clone(gitSource.getRemote(), "origin", true, null);
        log.fine("Repository " + gitSource.getRemote() + " cloned to: " + repositoryPath.getCanonicalPath());
    } catch (GitException e) {
        // check if this is an empty repository
        boolean isEmptyRepo = false;
        try {
            if (git.getRemoteReferences(gitSource.getRemote(), null, true, false).isEmpty()) {
                isEmptyRepo = true;
            }
        } catch (GitException ge) {
            // *sigh* @ this necessary hack; {@link org.jenkinsci.plugins.gitclient.CliGitAPIImpl#getRemoteReferences}
            if ("unexpected ls-remote output ".equals(ge.getMessage())) {
                // blank line, command succeeded
                isEmptyRepo = true;
            }
        // ignore other reasons
        }
        if (isEmptyRepo) {
            git.init();
            git.addRemoteUrl("origin", gitSource.getRemote());
            log.fine("Repository " + gitSource.getRemote() + " not found, created new to: " + repositoryPath.getCanonicalPath());
        } else {
            throw e;
        }
    }
    return git;
}
Also used : EnvVars(hudson.EnvVars) GitException(hudson.plugins.git.GitException) TaskListener(hudson.model.TaskListener) LogTaskListener(hudson.util.LogTaskListener) LogTaskListener(hudson.util.LogTaskListener) GitClient(org.jenkinsci.plugins.gitclient.GitClient)

Example 3 with GitClient

use of org.jenkinsci.plugins.gitclient.GitClient in project blueocean-plugin by jenkinsci.

the class GitBareRepoReadSaveRequest method cloneRepo.

GitClient cloneRepo() throws InterruptedException, IOException {
    EnvVars environment = new EnvVars();
    TaskListener taskListener = new LogTaskListener(Logger.getAnonymousLogger(), Level.ALL);
    String gitExe = gitTool.getGitExe();
    GitClient git = org.jenkinsci.plugins.gitclient.Git.with(taskListener, environment).in(repositoryPath).using(gitExe).getClient();
    git.addCredentials(gitSource.getRemote(), getCredential());
    try {
        git.clone(gitSource.getRemote(), "origin", true, null);
        log.fine("Repository " + gitSource.getRemote() + " cloned to: " + repositoryPath.getCanonicalPath());
    } catch (GitException e) {
        // check if this is an empty repository
        boolean isEmptyRepo = false;
        try {
            if (git.getRemoteReferences(gitSource.getRemote(), null, true, false).isEmpty()) {
                isEmptyRepo = true;
            }
        } catch (GitException ge) {
            // *sigh* @ this necessary hack; {@link org.jenkinsci.plugins.gitclient.CliGitAPIImpl#getRemoteReferences}
            if ("unexpected ls-remote output ".equals(ge.getMessage())) {
                // blank line, command succeeded
                isEmptyRepo = true;
            }
        // ignore other reasons
        }
        if (isEmptyRepo) {
            git.init();
            git.addRemoteUrl("origin", gitSource.getRemote());
            log.fine("Repository " + gitSource.getRemote() + " not found, created new to: " + repositoryPath.getCanonicalPath());
        } else {
            throw e;
        }
    }
    return git;
}
Also used : EnvVars(hudson.EnvVars) GitException(hudson.plugins.git.GitException) LogTaskListener(hudson.util.LogTaskListener) TaskListener(hudson.model.TaskListener) LogTaskListener(hudson.util.LogTaskListener) GitClient(org.jenkinsci.plugins.gitclient.GitClient)

Example 4 with GitClient

use of org.jenkinsci.plugins.gitclient.GitClient in project blueocean-plugin by jenkinsci.

the class GitCacheCloneReadSaveRequest method invokeOnScm.

<T> T invokeOnScm(final GitSCMFileSystem.FSFunction<T> function) throws IOException {
    try {
        GitSCMFileSystem fs = getFilesystem();
        if (fs == null) {
            // Fall back to a git clone if we can't get the repository filesystem
            GitCloneReadSaveRequest gitClone = new GitCloneReadSaveRequest(gitSource, branch, commitMessage, sourceBranch, filePath, contents);
            GitClient git = gitClone.cloneRepo();
            try {
                return git.withRepository(new RepositoryCallbackToFSFunctionAdapter<>(function));
            } finally {
                gitClone.cleanupRepo();
            }
        }
        return fs.invoke(function);
    } catch (InterruptedException ex) {
        throw new ServiceException.UnexpectedErrorException("Unable to save " + filePath, ex);
    }
}
Also used : ServiceException(io.jenkins.blueocean.commons.ServiceException) GitSCMFileSystem(jenkins.plugins.git.GitSCMFileSystem) GitClient(org.jenkinsci.plugins.gitclient.GitClient)

Example 5 with GitClient

use of org.jenkinsci.plugins.gitclient.GitClient in project blueocean-plugin by jenkinsci.

the class GitCloneReadSaveRequest method read.

@Override
byte[] read() throws IOException {
    try {
        GitClient git = cloneRepo();
        try {
            // thank you test for how to use something...
            // https://github.com/jenkinsci/git-client-plugin/blob/master/src/test/java/org/jenkinsci/plugins/gitclient/GitClientTest.java#L1108
            git.checkoutBranch(branch, "origin/" + branch);
        } catch (Exception e) {
            throw new RuntimeException("Branch not found: " + branch);
        }
        File f = new File(repositoryPath, filePath);
        if (f.canRead()) {
            return FileUtils.readFileToByteArray(f);
        }
        return null;
    } catch (InterruptedException ex) {
        throw new ServiceException.UnexpectedErrorException("Unable to read " + filePath, ex);
    } finally {
        cleanupRepo();
    }
}
Also used : ServiceException(io.jenkins.blueocean.commons.ServiceException) GitClient(org.jenkinsci.plugins.gitclient.GitClient) File(java.io.File) URISyntaxException(java.net.URISyntaxException) IOException(java.io.IOException) ServiceException(io.jenkins.blueocean.commons.ServiceException) GitException(hudson.plugins.git.GitException)

Aggregations

GitClient (org.jenkinsci.plugins.gitclient.GitClient)6 GitException (hudson.plugins.git.GitException)5 ServiceException (io.jenkins.blueocean.commons.ServiceException)4 EnvVars (hudson.EnvVars)3 IOException (java.io.IOException)3 TaskListener (hudson.model.TaskListener)2 LogTaskListener (hudson.util.LogTaskListener)2 File (java.io.File)2 URISyntaxException (java.net.URISyntaxException)2 ErrorMessage (io.jenkins.blueocean.commons.ErrorMessage)1 ArrayList (java.util.ArrayList)1 GitSCMFileSystem (jenkins.plugins.git.GitSCMFileSystem)1 URIish (org.eclipse.jgit.transport.URIish)1 Git (org.jenkinsci.plugins.gitclient.Git)1