From 37c78787597fce19587d4ea58f2527aed0be1ce5 Mon Sep 17 00:00:00 2001 From: Marcin Grzejszczak Date: Tue, 3 Jul 2018 08:32:03 +0200 Subject: [PATCH] Fixing the lack of picking of creds from the GIT plugins; fixes gh-678 --- .../stubrunner/ContractProjectUpdater.java | 4 +- .../cloud/contract/stubrunner/GitRepo.java | 6 +- .../ContractProjectUpdaterTest.java | 60 +++++++++++++++++++ 3 files changed, 67 insertions(+), 3 deletions(-) diff --git a/spring-cloud-contract-stub-runner/src/main/java/org/springframework/cloud/contract/stubrunner/ContractProjectUpdater.java b/spring-cloud-contract-stub-runner/src/main/java/org/springframework/cloud/contract/stubrunner/ContractProjectUpdater.java index 6c27dbafac..f9b4878590 100644 --- a/spring-cloud-contract-stub-runner/src/main/java/org/springframework/cloud/contract/stubrunner/ContractProjectUpdater.java +++ b/spring-cloud-contract-stub-runner/src/main/java/org/springframework/cloud/contract/stubrunner/ContractProjectUpdater.java @@ -63,8 +63,10 @@ public class ContractProjectUpdater { public void updateContractProject(String projectName, Path rootStubsFolder) { File clonedRepo = this.gitContractsRepo .clonedRepo(this.stubRunnerOptions.stubRepositoryRoot); + GitStubDownloaderProperties properties = new GitStubDownloaderProperties( + this.stubRunnerOptions.stubRepositoryRoot, this.stubRunnerOptions); copyStubs(projectName, rootStubsFolder, clonedRepo); - GitRepo gitRepo = new GitRepo(clonedRepo); + GitRepo gitRepo = new GitRepo(clonedRepo, properties); String msg = StubRunnerPropertyUtils.getProperty(this.stubRunnerOptions.getProperties(), GIT_COMMIT_MESSAGE); GitRepo.CommitResult commit = gitRepo diff --git a/spring-cloud-contract-stub-runner/src/main/java/org/springframework/cloud/contract/stubrunner/GitRepo.java b/spring-cloud-contract-stub-runner/src/main/java/org/springframework/cloud/contract/stubrunner/GitRepo.java index d90aaa71ed..1588e30cad 100644 --- a/spring-cloud-contract-stub-runner/src/main/java/org/springframework/cloud/contract/stubrunner/GitRepo.java +++ b/spring-cloud-contract-stub-runner/src/main/java/org/springframework/cloud/contract/stubrunner/GitRepo.java @@ -69,7 +69,7 @@ class GitRepo { private static final Logger log = LoggerFactory.getLogger(GitRepo.class); - private final JGitFactory gitFactory; + final JGitFactory gitFactory; private final File basedir; @@ -79,12 +79,14 @@ class GitRepo { } // for tests + @Deprecated GitRepo(File basedir) { this.basedir = basedir; this.gitFactory = new JGitFactory(); } // for tests + @Deprecated GitRepo(File basedir, JGitFactory factory) { this.basedir = basedir; this.gitFactory = factory; @@ -330,7 +332,7 @@ class GitRepo { } }; - private final CredentialsProvider provider; + final CredentialsProvider provider; JGitFactory(GitStubDownloaderProperties properties) { if (org.springframework.util.StringUtils.hasText(properties.username)) { diff --git a/spring-cloud-contract-stub-runner/src/test/java/org/springframework/cloud/contract/stubrunner/ContractProjectUpdaterTest.java b/spring-cloud-contract-stub-runner/src/test/java/org/springframework/cloud/contract/stubrunner/ContractProjectUpdaterTest.java index 7cf6e526cc..2392696061 100644 --- a/spring-cloud-contract-stub-runner/src/test/java/org/springframework/cloud/contract/stubrunner/ContractProjectUpdaterTest.java +++ b/spring-cloud-contract-stub-runner/src/test/java/org/springframework/cloud/contract/stubrunner/ContractProjectUpdaterTest.java @@ -17,13 +17,17 @@ package org.springframework.cloud.contract.stubrunner; import java.io.File; +import java.util.HashMap; import org.assertj.core.api.BDDAssertions; import org.eclipse.jgit.api.Git; import org.eclipse.jgit.api.ResetCommand; import org.eclipse.jgit.revwalk.RevCommit; +import org.eclipse.jgit.transport.UsernamePasswordCredentialsProvider; import org.junit.Before; +import org.junit.Rule; import org.junit.Test; +import org.springframework.boot.test.rule.OutputCapture; import org.springframework.cloud.contract.stubrunner.spring.StubRunnerProperties; import static org.assertj.core.api.BDDAssertions.then; @@ -38,6 +42,8 @@ public class ContractProjectUpdaterTest extends AbstractGitTest { GitRepo gitRepo; File origin; + @Rule public OutputCapture outputCapture = new OutputCapture(); + @Before public void setup() throws Exception { GitContractsRepo.CACHED_LOCATIONS.clear(); @@ -69,6 +75,60 @@ public class ContractProjectUpdaterTest extends AbstractGitTest { git.reset().setMode(ResetCommand.ResetType.HARD).call(); } BDDAssertions.then(new File(this.project, "META-INF/com.example/hello-world/0.0.2/mappings/someMapping.json")).exists(); + BDDAssertions.then(gitRepo.gitFactory.provider).isNull(); + BDDAssertions.then(outputCapture.toString()).contains("No custom credentials provider will be set"); + } + + @Test + public void should_push_changes_to_current_branch_using_credentials() throws Exception { + StubRunnerOptions options = new StubRunnerOptionsBuilder() + .withStubRepositoryRoot("file://" + this.project.getAbsolutePath() + "/") + .withStubsMode(StubRunnerProperties.StubsMode.REMOTE) + .withProperties(new HashMap() { + { + put("git.username", "foo"); + put("git.password", "bar"); + } + } ) + .build(); + ContractProjectUpdater updater = new ContractProjectUpdater(options); + File stubs = new File(GitRepoTests.class.getResource("/git_samples/sample_stubs").toURI()); + + updater.updateContractProject("hello-world", stubs.toPath()); + + // project, not origin, cause we're making one more clone of the local copy + try(Git git = openGitProject(this.project)) { + RevCommit revCommit = git.log().call().iterator().next(); + then(revCommit.getShortMessage()).isEqualTo("Updating project [hello-world] with stubs"); + // I have no idea but the file gets deleted after pushing + git.reset().setMode(ResetCommand.ResetType.HARD).call(); + } + BDDAssertions.then(new File(this.project, "META-INF/com.example/hello-world/0.0.2/mappings/someMapping.json")).exists(); + BDDAssertions.then(outputCapture.toString()).contains("Passed username and password - will set a custom credentials provider"); + } + + @Test + public void should_push_changes_to_current_branch_using_root_credentials() throws Exception { + StubRunnerOptions options = new StubRunnerOptionsBuilder() + .withStubRepositoryRoot("file://" + this.project.getAbsolutePath() + "/") + .withStubsMode(StubRunnerProperties.StubsMode.REMOTE) + .withUsername("foo") + .withPassword("bar") + .build(); + ContractProjectUpdater updater = new ContractProjectUpdater(options); + File stubs = new File(GitRepoTests.class.getResource("/git_samples/sample_stubs").toURI()); + + updater.updateContractProject("hello-world", stubs.toPath()); + + // project, not origin, cause we're making one more clone of the local copy + try(Git git = openGitProject(this.project)) { + RevCommit revCommit = git.log().call().iterator().next(); + then(revCommit.getShortMessage()).isEqualTo("Updating project [hello-world] with stubs"); + // I have no idea but the file gets deleted after pushing + git.reset().setMode(ResetCommand.ResetType.HARD).call(); + } + BDDAssertions.then(new File(this.project, "META-INF/com.example/hello-world/0.0.2/mappings/someMapping.json")).exists(); + BDDAssertions.then(outputCapture.toString()).contains("Passed username and password - will set a custom credentials provider"); } @Test