Search in sources :

Example 31 with RemoteRefUpdate

use of org.eclipse.jgit.transport.RemoteRefUpdate in project blueocean-plugin by jenkinsci.

the class GitUtils method validatePushAccess.

/**
 *  Attempts to push to a non-existent branch to validate the user actually has push access
 *
 * @param repo local repository
 * @param remoteUrl git repo url
 * @param credential credential to use when accessing git
 */
public static void validatePushAccess(@Nonnull Repository repo, @Nonnull String remoteUrl, @Nullable StandardCredentials credential) throws GitException {
    try (org.eclipse.jgit.api.Git git = new org.eclipse.jgit.api.Git(repo)) {
        // we need to perform an actual push, so we try a deletion of a very-unlikely-to-exist branch
        // which needs to have push permissions in order to get a 'branch not found' message
        String pushSpec = ":this-branch-is-only-to-test-if-jenkins-has-push-access";
        PushCommand pushCommand = git.push();
        addCredential(repo, pushCommand, credential);
        Iterable<PushResult> resultIterable = pushCommand.setRefSpecs(new RefSpec(pushSpec)).setRemote(remoteUrl).setDryRun(// we only want to test
        true).call();
        PushResult result = resultIterable.iterator().next();
        if (result.getRemoteUpdates().isEmpty()) {
            System.out.println("No remote updates occurred");
        } else {
            for (RemoteRefUpdate update : result.getRemoteUpdates()) {
                if (!RemoteRefUpdate.Status.NON_EXISTING.equals(update.getStatus()) && !RemoteRefUpdate.Status.OK.equals(update.getStatus())) {
                    throw new ServiceException.UnexpectedErrorException("Expected non-existent ref but got: " + update.getStatus().name() + ": " + update.getMessage());
                }
            }
        }
    } catch (GitAPIException e) {
        if (e.getMessage().toLowerCase().contains("auth")) {
            throw new ServiceException.UnauthorizedException(e.getMessage(), e);
        }
        throw new ServiceException.UnexpectedErrorException("Unable to access and push to: " + remoteUrl + " - " + e.getMessage(), e);
    }
}
Also used : RemoteRefUpdate(org.eclipse.jgit.transport.RemoteRefUpdate) PushResult(org.eclipse.jgit.transport.PushResult) PushCommand(org.eclipse.jgit.api.PushCommand) GitAPIException(org.eclipse.jgit.api.errors.GitAPIException) Git(org.jenkinsci.plugins.gitclient.Git) RefSpec(org.eclipse.jgit.transport.RefSpec) ServiceException(io.jenkins.blueocean.commons.ServiceException)

Example 32 with RemoteRefUpdate

use of org.eclipse.jgit.transport.RemoteRefUpdate in project blueocean-plugin by jenkinsci.

the class GitUtils method push.

public static void push(String remoteUrl, Repository repo, StandardCredentials credential, String localBranchRef, String remoteBranchRef) {
    try (org.eclipse.jgit.api.Git git = new org.eclipse.jgit.api.Git(repo)) {
        String pushSpec = "+" + localBranchRef + ":" + remoteBranchRef;
        PushCommand pushCommand = git.push();
        addCredential(repo, pushCommand, credential);
        Iterable<PushResult> resultIterable = pushCommand.setRefSpecs(new RefSpec(pushSpec)).setRemote(remoteUrl).call();
        PushResult result = resultIterable.iterator().next();
        if (result.getRemoteUpdates().isEmpty()) {
            throw new RuntimeException("No remote updates occurred");
        } else {
            for (RemoteRefUpdate update : result.getRemoteUpdates()) {
                if (!RemoteRefUpdate.Status.OK.equals(update.getStatus())) {
                    throw new ServiceException.UnexpectedErrorException("Remote update failed: " + update.getStatus().name() + ": " + update.getMessage());
                }
            }
        }
    } catch (GitAPIException e) {
        if (e.getMessage().toLowerCase().contains("auth")) {
            throw new ServiceException.UnauthorizedException(e.getMessage(), e);
        }
        throw new ServiceException.UnexpectedErrorException("Unable to save and push to: " + remoteUrl + " - " + e.getMessage(), e);
    }
}
Also used : RemoteRefUpdate(org.eclipse.jgit.transport.RemoteRefUpdate) PushResult(org.eclipse.jgit.transport.PushResult) PushCommand(org.eclipse.jgit.api.PushCommand) GitAPIException(org.eclipse.jgit.api.errors.GitAPIException) Git(org.jenkinsci.plugins.gitclient.Git) RefSpec(org.eclipse.jgit.transport.RefSpec) ServiceException(io.jenkins.blueocean.commons.ServiceException)

Aggregations

RemoteRefUpdate (org.eclipse.jgit.transport.RemoteRefUpdate)32 PushResult (org.eclipse.jgit.transport.PushResult)28 Test (org.junit.Test)15 File (java.io.File)13 CloneCommand (org.eclipse.jgit.api.CloneCommand)13 Git (org.eclipse.jgit.api.Git)12 BufferedWriter (java.io.BufferedWriter)11 FileOutputStream (java.io.FileOutputStream)11 OutputStreamWriter (java.io.OutputStreamWriter)11 Date (java.util.Date)11 RepositoryModel (com.gitblit.models.RepositoryModel)10 UsernamePasswordCredentialsProvider (org.eclipse.jgit.transport.UsernamePasswordCredentialsProvider)9 RevCommit (org.eclipse.jgit.revwalk.RevCommit)7 AbstractDaemonTest (com.google.gerrit.acceptance.AbstractDaemonTest)6 RefSpec (org.eclipse.jgit.transport.RefSpec)6 GitAPIException (org.eclipse.jgit.api.errors.GitAPIException)5 UserModel (com.gitblit.models.UserModel)4 CredentialsProvider (org.eclipse.jgit.transport.CredentialsProvider)4 Status (org.eclipse.jgit.transport.RemoteRefUpdate.Status)4 PushCommand (org.eclipse.jgit.api.PushCommand)3