Search in sources :

Example 56 with RemoteRefUpdate

use of org.eclipse.jgit.transport.RemoteRefUpdate in project gerrit by GerritCodeReview.

the class SubmitRequirementsValidationIT method assertOkStatus.

public void assertOkStatus(PushResult result) {
    RemoteRefUpdate refUpdate = result.getRemoteUpdate(RefNames.REFS_CONFIG);
    assertThat(refUpdate).isNotNull();
    assertWithMessage(getMessage(result, refUpdate)).that(refUpdate.getStatus()).isEqualTo(Status.OK);
}
Also used : RemoteRefUpdate(org.eclipse.jgit.transport.RemoteRefUpdate)

Example 57 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 58 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)58 PushResult (org.eclipse.jgit.transport.PushResult)43 Test (org.junit.Test)19 File (java.io.File)15 Git (org.eclipse.jgit.api.Git)15 CloneCommand (org.eclipse.jgit.api.CloneCommand)14 URIish (org.eclipse.jgit.transport.URIish)13 BufferedWriter (java.io.BufferedWriter)11 FileOutputStream (java.io.FileOutputStream)11 OutputStreamWriter (java.io.OutputStreamWriter)11 Date (java.util.Date)11 RefSpec (org.eclipse.jgit.transport.RefSpec)11 RepositoryModel (com.gitblit.models.RepositoryModel)10 RevCommit (org.eclipse.jgit.revwalk.RevCommit)10 UsernamePasswordCredentialsProvider (org.eclipse.jgit.transport.UsernamePasswordCredentialsProvider)10 PushOperationSpecification (org.eclipse.egit.core.op.PushOperationSpecification)9 AbstractDaemonTest (com.google.gerrit.acceptance.AbstractDaemonTest)8 ArrayList (java.util.ArrayList)8 PushOperation (org.eclipse.egit.core.op.PushOperation)7 IOException (java.io.IOException)6