Added git credentials property setting; fixes #48

This commit is contained in:
Marcin Grzejszczak
2017-10-03 13:56:08 +02:00
parent 560825a63a
commit 0d31914a3d
4 changed files with 70 additions and 1 deletions

View File

@@ -205,6 +205,8 @@ the `spring-cloud-cli.version` to `1.0.0.RELEASE` regardless of what was set in
- `releaser.git.clone-destination-dir` - Where should the Spring Cloud Release repo get cloned to. If null defaults to a temporary directory
- `releaser.git.spring-cloud-release-git-url` - URL to Spring Cloud Release Git repository. Defaults to `https://github.com/spring-cloud/spring-cloud-release`
- `releaser.git.oauth-token` - GitHub OAuth token to be used to interact with GitHub repo.
- `releaser.git.username` - Optional Git username. If not passed keys will be used for authentication.
- `releaser.git.password` - Optional Git password. If not passed keys will be used for authentication.
- `releaser.git.number-of-checked-milestones` - In order not to iterate endlessly over milestones we introduce a threshold of milestones that
we will go through to find the matching milestone. Defaults to `10`
- `releaser.maven.build-command` - Command to be executed to build the project. Defaults to `./mvnw clean install -Pdocs`

View File

@@ -62,6 +62,16 @@ public class ReleaserProperties {
*/
private String oauthToken;
/**
* Optional Git username. If not passed keys will be used for authentication
*/
private String username;
/**
* Optional Git password. If not passed keys will be used for authentication
*/
private String password;
/**
* In order not to iterate endlessly over milestones we introduce a threshold of milestones
* that we will go through to find the matching milestone
@@ -92,6 +102,22 @@ public class ReleaserProperties {
this.oauthToken = oauthToken;
}
public String getUsername() {
return this.username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return this.password;
}
public void setPassword(String password) {
this.password = password;
}
public Integer getNumberOfCheckedMilestones() {
return this.numberOfCheckedMilestones;
}

View File

@@ -44,15 +44,19 @@ import org.eclipse.jgit.api.errors.EmtpyCommitException;
import org.eclipse.jgit.api.errors.GitAPIException;
import org.eclipse.jgit.lib.Ref;
import org.eclipse.jgit.revwalk.RevCommit;
import org.eclipse.jgit.transport.CredentialsProvider;
import org.eclipse.jgit.transport.JschConfigSessionFactory;
import org.eclipse.jgit.transport.OpenSshConfig;
import org.eclipse.jgit.transport.RefSpec;
import org.eclipse.jgit.transport.SshTransport;
import org.eclipse.jgit.transport.UsernamePasswordCredentialsProvider;
import org.eclipse.jgit.util.FS;
import org.eclipse.jgit.util.FileUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.cloud.release.internal.ReleaserProperties;
import org.springframework.util.ResourceUtils;
import org.springframework.util.StringUtils;
/**
* Abstraction over a Git repo. Can cloned repo from a given location
@@ -68,11 +72,18 @@ class GitRepo {
private final File basedir;
GitRepo(File basedir, ReleaserProperties properties) {
this.basedir = basedir;
this.gitFactory = new GitRepo.JGitFactory(properties);
}
// for tests
GitRepo(File basedir) {
this.basedir = basedir;
this.gitFactory = new GitRepo.JGitFactory();
}
// for tests
GitRepo(File basedir, GitRepo.JGitFactory factory) {
this.basedir = basedir;
this.gitFactory = factory;
@@ -311,6 +322,27 @@ class GitRepo {
}
};
private final CredentialsProvider provider;
JGitFactory(ReleaserProperties releaserProperties) {
if (StringUtils.hasText(releaserProperties.getGit().getUsername())) {
this.provider = credentialsProvider(releaserProperties);
} else {
this.provider = null;
}
}
CredentialsProvider credentialsProvider(ReleaserProperties properties) {
return new UsernamePasswordCredentialsProvider(
properties.getGit().getUsername(),
properties.getGit().getPassword());
}
// for tests
JGitFactory() {
this.provider = null;
}
private final TransportConfigCallback callback = transport -> {
if (transport instanceof SshTransport) {
SshTransport sshTransport = (SshTransport) transport;
@@ -323,7 +355,9 @@ class GitRepo {
}
PushCommand push(Git git) {
return git.push().setTransportConfigCallback(this.callback);
return git.push()
.setCredentialsProvider(this.provider)
.setTransportConfigCallback(this.callback);
}
Git open(File file) {

View File

@@ -7,16 +7,23 @@ import java.io.PrintStream;
import java.net.URISyntaxException;
import java.nio.file.Files;
import java.util.List;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicInteger;
import org.eclipse.jgit.api.CloneCommand;
import org.eclipse.jgit.api.Git;
import org.eclipse.jgit.api.errors.GitAPIException;
import org.eclipse.jgit.errors.UnsupportedCredentialItem;
import org.eclipse.jgit.lib.Ref;
import org.eclipse.jgit.revwalk.RevCommit;
import org.eclipse.jgit.transport.CredentialItem;
import org.eclipse.jgit.transport.CredentialsProvider;
import org.eclipse.jgit.transport.URIish;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
import org.springframework.cloud.release.internal.ReleaserProperties;
import org.springframework.cloud.release.internal.pom.TestUtils;
import static org.assertj.core.api.Assertions.fail;