Skip to content

Commit

Permalink
[SCM-919] Fix codecheck violations in JGit provider
Browse files Browse the repository at this point in the history
This closes #89
  • Loading branch information
cquoss authored and michael-o committed Feb 8, 2019
1 parent 04b16dc commit 2a133a5
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,81 +25,106 @@
import org.apache.maven.scm.log.ScmLogger;
import org.apache.maven.scm.provider.git.repository.GitScmProviderRepository;
import org.eclipse.jgit.api.TransportConfigCallback;
import org.eclipse.jgit.transport.*;
import org.eclipse.jgit.transport.JschConfigSessionFactory;
import org.eclipse.jgit.transport.OpenSshConfig;
import org.eclipse.jgit.transport.SshSessionFactory;
import org.eclipse.jgit.transport.SshTransport;
import org.eclipse.jgit.transport.Transport;
import org.eclipse.jgit.util.FS;
import org.eclipse.jgit.util.StringUtils;

/**
* Implementation of {@link TransportConfigCallback} which adds
* a public/private key identity to ssh URLs if configured.
*/
public class JGitTransportConfigCallback implements TransportConfigCallback {
public class JGitTransportConfigCallback implements TransportConfigCallback
{
private SshSessionFactory sshSessionFactory = null;

public JGitTransportConfigCallback(GitScmProviderRepository repo, ScmLogger logger) {
if (repo.getFetchInfo().getProtocol().equals("ssh")) {
if (!StringUtils.isEmptyOrNull(repo.getPrivateKey()) && repo.getPassphrase() == null) {
logger.debug("using private key with passphrase: " + repo.getPrivateKey());
sshSessionFactory = new UnprotectedPrivateKeySessionFactory(repo);
} else if (!StringUtils.isEmptyOrNull(repo.getPrivateKey()) && repo.getPassphrase() != null) {
logger.debug("using private key: " + repo.getPrivateKey());
sshSessionFactory = new ProtectedPrivateKeyFileSessionFactory(repo);
} else {
public JGitTransportConfigCallback( GitScmProviderRepository repo, ScmLogger logger )
{
if ( repo.getFetchInfo().getProtocol().equals( "ssh" ) )
{
if ( !StringUtils.isEmptyOrNull( repo.getPrivateKey() ) && repo.getPassphrase() == null )
{
logger.debug( "using private key: " + repo.getPrivateKey() );
sshSessionFactory = new UnprotectedPrivateKeySessionFactory( repo );
}
else if ( !StringUtils.isEmptyOrNull( repo.getPrivateKey() ) && repo.getPassphrase() != null )
{
logger.debug( "using private key with passphrase: " + repo.getPrivateKey() );
sshSessionFactory = new ProtectedPrivateKeyFileSessionFactory( repo );
}
else
{
sshSessionFactory = new SimpleSessionFactory();
}
}
}

@Override
public void configure(Transport transport) {
if (transport instanceof SshTransport) {
public void configure( Transport transport )
{
if ( transport instanceof SshTransport )
{
SshTransport sshTransport = (SshTransport) transport;
sshTransport.setSshSessionFactory(sshSessionFactory);
sshTransport.setSshSessionFactory( sshSessionFactory );
}
}

static private class SimpleSessionFactory extends JschConfigSessionFactory {
private static class SimpleSessionFactory extends JschConfigSessionFactory
{
@Override
protected void configure(OpenSshConfig.Host host, Session session) {
protected void configure( OpenSshConfig.Host host, Session session )
{
}
}

static private abstract class PrivateKeySessionFactory extends SimpleSessionFactory {
private abstract static class PrivateKeySessionFactory extends SimpleSessionFactory
{
private final GitScmProviderRepository repo;

public GitScmProviderRepository getRepo() {
GitScmProviderRepository getRepo()
{
return repo;
}

public PrivateKeySessionFactory(GitScmProviderRepository repo) {
PrivateKeySessionFactory( GitScmProviderRepository repo )
{
this.repo = repo;
}
}

static private class UnprotectedPrivateKeySessionFactory extends PrivateKeySessionFactory {
private static class UnprotectedPrivateKeySessionFactory extends PrivateKeySessionFactory
{

public UnprotectedPrivateKeySessionFactory(GitScmProviderRepository repo) {
super(repo);
UnprotectedPrivateKeySessionFactory( GitScmProviderRepository repo )
{
super( repo );
}

@Override
protected JSch createDefaultJSch(FS fs) throws JSchException {
JSch defaultJSch = super.createDefaultJSch(fs);
defaultJSch.addIdentity(getRepo().getPrivateKey());
protected JSch createDefaultJSch( FS fs ) throws JSchException
{
JSch defaultJSch = super.createDefaultJSch( fs );
defaultJSch.addIdentity( getRepo().getPrivateKey() );
return defaultJSch;
}
}

static private class ProtectedPrivateKeyFileSessionFactory extends PrivateKeySessionFactory {
private static class ProtectedPrivateKeyFileSessionFactory extends PrivateKeySessionFactory
{

public ProtectedPrivateKeyFileSessionFactory(GitScmProviderRepository repo) {
super(repo);
ProtectedPrivateKeyFileSessionFactory( GitScmProviderRepository repo )
{
super( repo );
}

@Override
protected JSch createDefaultJSch(FS fs) throws JSchException {
JSch defaultJSch = super.createDefaultJSch(fs);
defaultJSch.addIdentity(getRepo().getPrivateKey(), getRepo().getPassphrase());
protected JSch createDefaultJSch( FS fs ) throws JSchException
{
JSch defaultJSch = super.createDefaultJSch( fs );
defaultJSch.addIdentity( getRepo().getPrivateKey(), getRepo().getPassphrase() );
return defaultJSch;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -188,8 +188,8 @@ public static Iterable<PushResult> push( ScmLogger logger, Git git, GitScmProvid
throws GitAPIException, InvalidRemoteException, TransportException
{
CredentialsProvider credentials = JGitUtils.prepareSession( logger, git, repo );
PushCommand command = git.push().setRefSpecs(refSpec).setCredentialsProvider(credentials)
.setTransportConfigCallback(new JGitTransportConfigCallback(repo, logger));
PushCommand command = git.push().setRefSpecs( refSpec ).setCredentialsProvider( credentials )
.setTransportConfigCallback( new JGitTransportConfigCallback( repo, logger ) );

Iterable<PushResult> pushResultList = command.call();
for ( PushResult pushResult : pushResultList )
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,11 @@
import org.apache.maven.scm.provider.git.jgit.command.remoteinfo.JGitRemoteInfoCommand;
import org.apache.maven.scm.provider.git.repository.GitScmProviderRepository;
import org.codehaus.plexus.util.StringUtils;
import org.eclipse.jgit.api.*;
import org.eclipse.jgit.api.CloneCommand;
import org.eclipse.jgit.api.FetchCommand;
import org.eclipse.jgit.api.Git;
import org.eclipse.jgit.api.PullCommand;
import org.eclipse.jgit.api.TransportConfigCallback;
import org.eclipse.jgit.lib.Constants;
import org.eclipse.jgit.lib.ProgressMonitor;
import org.eclipse.jgit.revwalk.RevCommit;
Expand Down Expand Up @@ -113,8 +117,8 @@ protected CheckOutScmResult executeCheckOutCommand( ScmProviderRepository repo,
command.setCredentialsProvider( credentials ).setBranch( branch ).setDirectory( fileSet.getBasedir() );

TransportConfigCallback transportConfigCallback = new JGitTransportConfigCallback(
(GitScmProviderRepository) repo, getLogger());
command.setTransportConfigCallback(transportConfigCallback);
(GitScmProviderRepository) repo, getLogger() );
command.setTransportConfigCallback( transportConfigCallback );

command.setProgressMonitor( monitor );
git = command.call();
Expand All @@ -136,7 +140,7 @@ protected CheckOutScmResult executeCheckOutCommand( ScmProviderRepository repo,
// git repo exists, so we must git-pull the changes
CredentialsProvider credentials = JGitUtils.prepareSession( getLogger(), git, repository );
TransportConfigCallback transportConfigCallback = new JGitTransportConfigCallback(
(GitScmProviderRepository) repo, getLogger());
(GitScmProviderRepository) repo, getLogger() );

if ( version != null && StringUtils.isNotEmpty( version.getName() ) && ( version instanceof ScmTag ) )
{
Expand All @@ -146,16 +150,18 @@ protected CheckOutScmResult executeCheckOutCommand( ScmProviderRepository repo,
// In fact, a tag in git may be in multiple branches. This occurs if
// you create a branch after the tag has been created
getLogger().debug( "fetch..." );
FetchCommand command = git.fetch().setCredentialsProvider(credentials).setProgressMonitor(monitor);
command.setTransportConfigCallback(transportConfigCallback);
FetchCommand command = git.fetch().setCredentialsProvider( credentials )
.setProgressMonitor( monitor );
command.setTransportConfigCallback( transportConfigCallback );
command.call();

}
else
{
getLogger().debug( "pull..." );
PullCommand command = git.pull().setCredentialsProvider(credentials).setProgressMonitor(monitor);
command.setTransportConfigCallback(transportConfigCallback);
PullCommand command = git.pull().setCredentialsProvider( credentials )
.setProgressMonitor( monitor );
command.setTransportConfigCallback( transportConfigCallback );
command.call();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ protected ListScmResult executeListCommand( ScmProviderRepository repo, ScmFileS
List<ScmFile> list = new ArrayList<ScmFile>();
Collection<Ref> lsResult = git.lsRemote().setCredentialsProvider( credentials )
.setTransportConfigCallback(
new JGitTransportConfigCallback((GitScmProviderRepository) repo, getLogger()))
new JGitTransportConfigCallback( (GitScmProviderRepository) repo, getLogger() ) )
.call();
for ( Ref ref : lsResult )
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public RemoteInfoScmResult executeRemoteInfoCommand( ScmProviderRepository repos

LsRemoteCommand lsCommand =
git.lsRemote().setRemote( repo.getPushUrl() ).setCredentialsProvider( credentials )
.setTransportConfigCallback(new JGitTransportConfigCallback(repo, getLogger()));
.setTransportConfigCallback( new JGitTransportConfigCallback( repo, getLogger() ) );

Map<String, String> tag = new HashMap<String, String>();
Collection<Ref> allTags = lsCommand.setHeads( false ).setTags( true ).call();
Expand Down

0 comments on commit 2a133a5

Please sign in to comment.