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);
}
}
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);
}
}
Aggregations