Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add: Add authentication support! && fix: Force Update when sone one force pull to remote git repository #242

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@
import java.util.List;
import java.util.Properties;

import org.eclipse.jgit.api.RebaseCommand.Operation;
import org.eclipse.jgit.transport.CredentialsProvider;

/**
* Note: use {@link GitConfigurationSourceBuilder} for building instances of this class.
* <p>
Expand All @@ -55,6 +58,7 @@ class GitConfigurationSource implements ConfigurationSource, Closeable {
private final BranchResolver branchResolver;
private final PathResolver pathResolver;
private final ConfigFilesProvider configFilesProvider;
private final CredentialsProvider credentialsProvider; //@wjw_add
private final PropertiesProviderSelector propertiesProviderSelector;
private final String repositoryURI;
private final Path tmpPath;
Expand All @@ -76,15 +80,17 @@ class GitConfigurationSource implements ConfigurationSource, Closeable {
* @param branchResolver {@link BranchResolver} used for extracting git branch from an {@link Environment}
* @param pathResolver {@link PathResolver} used for extracting git path from an {@link Environment}
* @param configFilesProvider {@link ConfigFilesProvider} used for determining which files in repository should be read
* @param configFilesProvider add git authentication support!
* @param propertiesProviderSelector selector used for choosing {@link PropertiesProvider} based on a configuration file extension
* as config files
*/
GitConfigurationSource(String repositoryURI, Path tmpPath, String tmpRepoPrefix, BranchResolver branchResolver,
PathResolver pathResolver, ConfigFilesProvider configFilesProvider,
PathResolver pathResolver, ConfigFilesProvider configFilesProvider, CredentialsProvider credentialsProvider,
PropertiesProviderSelector propertiesProviderSelector) {
this.branchResolver = requireNonNull(branchResolver);
this.pathResolver = requireNonNull(pathResolver);
this.configFilesProvider = requireNonNull(configFilesProvider);
this.credentialsProvider = credentialsProvider; //@wjw_add
this.propertiesProviderSelector = requireNonNull(propertiesProviderSelector);
this.repositoryURI = requireNonNull(repositoryURI);
this.tmpPath = requireNonNull(tmpPath);
Expand All @@ -104,7 +110,11 @@ public Properties getConfiguration(Environment environment) {
try {
checkoutToBranch(branchResolver.getBranchNameFor(environment));
} catch (GitAPIException e) {
throw new MissingEnvironmentException(environment.getName(), e);
try { //@wjw_add Force Update when sone one force pull to remote git repository
clonedRepo.rebase().setOperation(Operation.SKIP).call();
} catch (GitAPIException e1) {
throw new MissingEnvironmentException(environment.getName(), e);
}
}

Properties properties = new Properties();
Expand Down Expand Up @@ -147,6 +157,7 @@ public void init() {
try {
clonedRepo = Git.cloneRepository()
.setURI(repositoryURI)
.setCredentialsProvider(credentialsProvider)
.setDirectory(clonedRepoPath.toFile())
.call();
} catch (GitAPIException e) {
Expand All @@ -156,13 +167,17 @@ public void init() {
initialized = true;
}

private void reload() {
public void reload() {
try {
LOG.debug("Reloading configuration by pulling changes");
clonedRepo.pull().call();
clonedRepo.pull().setCredentialsProvider(credentialsProvider).call();
} catch (GitAPIException e) {
initialized = false;
throw new IllegalStateException("Unable to pull from remote repository", e);
try { //@wjw_add Force Update when sone one force pull to remote git repository
clonedRepo.rebase().setOperation(Operation.SKIP).call();
} catch (GitAPIException e1) {
initialized = false;
throw new IllegalStateException("Unable to pull from remote repository", e);
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@
import java.nio.file.Path;
import java.nio.file.Paths;

import org.eclipse.jgit.transport.CredentialsProvider;


/**
* Builder for {@link GitConfigurationSource}.
*/
Expand All @@ -37,6 +40,7 @@ public class GitConfigurationSourceBuilder {
private String tmpRepoPrefix;
private ConfigFilesProvider configFilesProvider;
private PropertiesProviderSelector propertiesProviderSelector;
private CredentialsProvider credentialsProvider; //@wjw_add

/**
* Construct {@link GitConfigurationSource}s builder
Expand Down Expand Up @@ -129,14 +133,24 @@ public GitConfigurationSourceBuilder withConfigFilesProvider(ConfigFilesProvider
return this;
}

/**
* for Authenticate With JGit
* @param credentialsProvider
* @return
*/
public GitConfigurationSourceBuilder withCredentialsProvider(CredentialsProvider credentialsProvider) {
this.credentialsProvider = credentialsProvider;
return this;
}

/**
* Build a {@link GitConfigurationSource} using this builder's configuration
*
* @return new {@link GitConfigurationSource}
*/
public GitConfigurationSource build() {
return new GitConfigurationSource(repositoryURI, tmpPath, tmpRepoPrefix, branchResolver, pathResolver,
configFilesProvider, propertiesProviderSelector);
configFilesProvider, credentialsProvider, propertiesProviderSelector);
}

@Override
Expand Down