use of org.jenkinsci.plugins.gitclient.Git in project blueocean-plugin by jenkinsci.
the class GitUtils method fetch.
public static void fetch(final Repository repo, final StandardCredentials credential) {
try (org.eclipse.jgit.api.Git git = new org.eclipse.jgit.api.Git(repo)) {
FetchCommand fetchCommand = git.fetch();
addCredential(repo, fetchCommand, credential);
fetchCommand.setRemote("origin").setRemoveDeletedRefs(true).setRefSpecs(new RefSpec("+refs/heads/*:refs/remotes/origin/*")).call();
} catch (GitAPIException ex) {
if (ex.getMessage().contains("Auth fail")) {
throw new ServiceException.UnauthorizedException("Not authorized", ex);
}
throw new RuntimeException(ex);
}
}
use of org.jenkinsci.plugins.gitclient.Git 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.jenkinsci.plugins.gitclient.Git in project blueocean-plugin by jenkinsci.
the class GitUtils method merge.
public static void merge(final Repository repo, final String localRef, final String remoteRef) {
try (org.eclipse.jgit.api.Git git = new org.eclipse.jgit.api.Git(repo)) {
CheckoutCommand checkoutCommand = git.checkout();
checkoutCommand.setCreateBranch(false);
checkoutCommand.setName(localRef);
checkoutCommand.call();
Ref mergeBranchRef = repo.exactRef(remoteRef);
MergeResult merge = git.merge().include(mergeBranchRef).setFastForward(MergeCommand.FastForwardMode.FF_ONLY).call();
if (merge.getConflicts() != null) {
throw new RuntimeException("Merge has conflicts");
}
} catch (Exception e) {
throw new ServiceException.UnexpectedErrorException("Unable to merge: " + remoteRef + " to: " + localRef, e);
}
}
use of org.jenkinsci.plugins.gitclient.Git in project blueocean-plugin by jenkinsci.
the class GitUtils method validateCredentials.
/**
* Calls 'git ls-remote -h uri' to check if git uri or supplied credentials are valid
*
* @param uri git repo uri
* @param credentials credential to use when accessing git
* @return list of Errors. Empty list means success.
*/
static List<ErrorMessage.Error> validateCredentials(@Nonnull String uri, @Nullable StandardCredentials credentials) throws GitException {
List<ErrorMessage.Error> errors = new ArrayList<>();
Git git = new Git(TaskListener.NULL, new EnvVars());
try {
GitClient gitClient = git.getClient();
if (credentials != null) {
gitClient.addCredentials(uri, credentials);
}
gitClient.getRemoteReferences(uri, null, true, false);
} catch (IOException | InterruptedException e) {
logger.error("Error running git remote-ls: " + e.getMessage(), e);
throw new ServiceException.UnexpectedErrorException("Failed to create pipeline due to unexpected error: " + e.getMessage(), e);
} catch (IllegalStateException | GitException e) {
logger.error("Error running git remote-ls: " + e.getMessage(), e);
if (credentials != null) {
// this turn very hackhish with upgrade of git plugin as there is more and more and more cause/layers before having the real cause...
if (e instanceof IllegalStateException || e.getMessage().endsWith("not authorized") || e.getMessage().endsWith("not authenticated") || (e instanceof GitException && checkCauseNotAuthenticated((GitException) e))) {
errors.add(new ErrorMessage.Error("scmConfig.credentialId", ErrorMessage.Error.ErrorCodes.INVALID.toString(), "Invalid credentialId: " + credentials.getId()));
} else {
errors.add(new ErrorMessage.Error("scmConfig.uri", ErrorMessage.Error.ErrorCodes.INVALID.toString(), e.getMessage()));
}
} else if (e.getMessage().contains("Authentication is required") || e.getMessage().contains("connection is not authenticated")) {
errors.add(new ErrorMessage.Error("scmConfig.credentialId", ErrorMessage.Error.ErrorCodes.INVALID.toString(), e.getMessage()));
} else {
errors.add(new ErrorMessage.Error("scmConfig.uri", ErrorMessage.Error.ErrorCodes.INVALID.toString(), e.getMessage()));
}
}
return errors;
}
use of org.jenkinsci.plugins.gitclient.Git 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