Enhance push command with username/password authentication (#1734)

This commit is contained in:
Eduard Heimbuch
2021-07-23 13:42:39 +02:00
committed by GitHub
parent 624605daaa
commit f52c0b07bf
8 changed files with 160 additions and 304 deletions

View File

@@ -24,9 +24,7 @@
package sonia.scm.repository.spi;
//~--- non-JDK imports --------------------------------------------------------
import com.google.common.base.Preconditions;
import com.google.common.base.Strings;
import com.google.common.collect.Iterables;
import org.eclipse.jgit.api.Git;
import org.eclipse.jgit.lib.ObjectId;
@@ -35,6 +33,7 @@ import org.eclipse.jgit.revwalk.RevCommit;
import org.eclipse.jgit.transport.PushResult;
import org.eclipse.jgit.transport.RemoteRefUpdate;
import org.eclipse.jgit.transport.ScmTransportProtocol;
import org.eclipse.jgit.transport.UsernamePasswordCredentialsProvider;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import sonia.scm.repository.GitRepositoryHandler;
@@ -44,230 +43,113 @@ import sonia.scm.repository.InternalRepositoryException;
import java.io.File;
import java.util.Collection;
//~--- JDK imports ------------------------------------------------------------
public abstract class AbstractGitPushOrPullCommand extends AbstractGitCommand {
/**
*
* @author Sebastian Sdorra
*/
public abstract class AbstractGitPushOrPullCommand extends AbstractGitCommand
{
/** Field description */
private static final String SCHEME = ScmTransportProtocol.NAME + "://";
private static final Logger LOG = LoggerFactory.getLogger(AbstractGitPushOrPullCommand.class);
/**
* the logger for AbstractGitPushOrPullCommand
*/
private static final Logger logger =
LoggerFactory.getLogger(AbstractGitPushOrPullCommand.class);
protected GitRepositoryHandler handler;
//~--- constructors ---------------------------------------------------------
/**
* Constructs ...
*
* @param handler
* @param context
*/
protected AbstractGitPushOrPullCommand(GitRepositoryHandler handler, GitContext context)
{
protected AbstractGitPushOrPullCommand(GitRepositoryHandler handler, GitContext context) {
super(context);
this.handler = handler;
}
//~--- methods --------------------------------------------------------------
protected long push(Repository source, String remoteUrl) {
protected long push(Repository source, String remoteUrl, String username, String password) {
Git git = Git.wrap(source);
org.eclipse.jgit.api.PushCommand push = git.push();
if (!Strings.isNullOrEmpty(username) && !Strings.isNullOrEmpty(password)) {
push.setCredentialsProvider(new UsernamePasswordCredentialsProvider(username, password.toCharArray()));
}
push.setPushAll().setPushTags();
push.setRemote(remoteUrl);
long counter = -1;
try
{
try {
Iterable<PushResult> results = push.call();
if (results != null)
{
if (results != null) {
counter = 0;
for (PushResult result : results)
{
for (PushResult result : results) {
counter += count(git, result);
}
}
}
catch (Exception ex)
{
} catch (Exception ex) {
throw new InternalRepositoryException(repository, "could not execute push/pull command", ex);
}
return counter;
}
//~--- get methods ----------------------------------------------------------
/**
* Method description
*
*
* @param request
*
* @return
*/
protected sonia.scm.repository.Repository getRemoteRepository(
RemoteCommandRequest request)
{
Preconditions.checkNotNull(request, "request is required");
sonia.scm.repository.Repository remoteRepository =
request.getRemoteRepository();
Preconditions.checkNotNull(remoteRepository,
"remote repository is required");
return remoteRepository;
}
/**
* Method description
*
*
* @param request
*
* @return
*/
protected String getRemoteUrl(RemoteCommandRequest request)
{
protected String getRemoteUrl(RemoteCommandRequest request) {
String url;
sonia.scm.repository.Repository remRepo = request.getRemoteRepository();
if (remRepo != null)
{
if (remRepo != null) {
url = getRemoteUrl(remRepo);
}
else if (request.getRemoteUrl() != null)
{
} else if (request.getRemoteUrl() != null) {
url = request.getRemoteUrl().toExternalForm();
}
else
{
} else {
throw new IllegalArgumentException("repository or url is required");
}
return url;
}
/**
* Method description
*
*
* @param directory
*
* @return
*/
protected String getRemoteUrl(File directory)
{
protected String getRemoteUrl(File directory) {
return SCHEME.concat(directory.getAbsolutePath());
}
/**
* Method description
*
*
* @param repository
*
* @return
*/
protected String getRemoteUrl(sonia.scm.repository.Repository repository)
{
protected String getRemoteUrl(sonia.scm.repository.Repository repository) {
return getRemoteUrl(handler.getDirectory(repository.getId()));
}
//~--- methods --------------------------------------------------------------
/**
* Method description
*
*
* @param git
* @param result
*
* @return
*/
private long count(Git git, PushResult result)
{
private long count(Git git, PushResult result) {
long counter = 0;
Collection<RemoteRefUpdate> updates = result.getRemoteUpdates();
for (RemoteRefUpdate update : updates)
{
for (RemoteRefUpdate update : updates) {
counter += count(git, update);
}
return counter;
}
/**
* Method description
*
*
* @param git
* @param update
*
* @return
*/
private long count(Git git, RemoteRefUpdate update)
{
private long count(Git git, RemoteRefUpdate update) {
long counter = 0;
if (GitUtil.isHead(update.getRemoteName()))
{
try
{
if (GitUtil.isHead(update.getRemoteName())) {
try {
org.eclipse.jgit.api.LogCommand log = git.log();
ObjectId oldId = update.getExpectedOldObjectId();
if (GitUtil.isValidObjectId(oldId))
{
if (GitUtil.isValidObjectId(oldId)) {
log.not(oldId);
}
ObjectId newId = update.getNewObjectId();
if (GitUtil.isValidObjectId(newId))
{
if (GitUtil.isValidObjectId(newId)) {
log.add(newId);
}
Iterable<RevCommit> commits = log.call();
if (commits != null)
{
if (commits != null) {
counter += Iterables.size(commits);
}
logger.trace("counting {} commits for ref update {}", counter, update);
LOG.trace("counting {} commits for ref update {}", counter, update);
} catch (Exception ex) {
LOG.error("could not count pushed/pulled changesets", ex);
}
catch (Exception ex)
{
logger.error("could not count pushed/pulled changesets", ex);
}
}
else
{
logger.debug("do not count non branch ref update {}", update);
} else {
LOG.debug("do not count non branch ref update {}", update);
}
return counter;
}
//~--- fields ---------------------------------------------------------------
/** Field description */
protected GitRepositoryHandler handler;
}

View File

@@ -71,7 +71,7 @@ public class GitPullCommand extends AbstractGitPushOrPullCommand
Repository sourceRepository = request.getRemoteRepository();
if (sourceRepository != null) {
response = pullFromScmRepository(sourceRepository);
response = pullFromScmRepository(sourceRepository, request.getUsername(), request.getPassword());
} else if (request.getRemoteUrl() != null) {
response = pullFromUrl(request);
} else {
@@ -129,7 +129,7 @@ public class GitPullCommand extends AbstractGitPushOrPullCommand
return counter;
}
private PullResponse pullFromScmRepository(Repository sourceRepository)
private PullResponse pullFromScmRepository(Repository sourceRepository, String username, String password)
throws IOException {
File sourceDirectory = handler.getDirectory(sourceRepository.getId());
@@ -150,7 +150,7 @@ public class GitPullCommand extends AbstractGitPushOrPullCommand
try (Git git = Git.open(sourceDirectory)) {
source = git.getRepository();
response = new PullResponse(push(source, getRemoteUrl(targetDirectory)));
response = new PullResponse(push(source, getRemoteUrl(targetDirectory), username, password));
} finally {
GitUtil.close(source);
}

View File

@@ -24,8 +24,6 @@
package sonia.scm.repository.spi;
//~--- non-JDK imports --------------------------------------------------------
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import sonia.scm.repository.GitRepositoryHandler;
@@ -34,54 +32,23 @@ import sonia.scm.repository.api.PushResponse;
import javax.inject.Inject;
import java.io.IOException;
//~--- JDK imports ------------------------------------------------------------
public class GitPushCommand extends AbstractGitPushOrPullCommand implements PushCommand {
/**
*
* @author Sebastian Sdorra
*/
public class GitPushCommand extends AbstractGitPushOrPullCommand
implements PushCommand
{
private static final Logger LOG = LoggerFactory.getLogger(GitPushCommand.class);
/** Field description */
private static final Logger logger =
LoggerFactory.getLogger(GitPushCommand.class);
//~--- constructors ---------------------------------------------------------
/**
* Constructs ...
*
* @param handler
* @param context
*/
@Inject
public GitPushCommand(GitRepositoryHandler handler, GitContext context) {
super(handler, context);
this.handler = handler;
}
//~--- methods --------------------------------------------------------------
/**
* Method description
*
*
* @param request
*
* @return
*
* @throws IOException
*/
@Override
public PushResponse push(PushCommandRequest request)
throws IOException
{
throws IOException {
String remoteUrl = getRemoteUrl(request);
logger.debug("push changes from {} to {}", repository, remoteUrl);
LOG.debug("push changes from {} to {}", repository, remoteUrl);
return new PushResponse(push(open(), remoteUrl));
return new PushResponse(push(open(), remoteUrl, request.getUsername(), request.getPassword()));
}
}