diff --git a/core/src/main/kotlin/in/specmatic/core/git/GitOperations.kt b/core/src/main/kotlin/in/specmatic/core/git/GitOperations.kt index 844c7890c..2b33eab51 100644 --- a/core/src/main/kotlin/in/specmatic/core/git/GitOperations.kt +++ b/core/src/main/kotlin/in/specmatic/core/git/GitOperations.kt @@ -84,9 +84,11 @@ private fun jgitClone(gitRepositoryURI: String, cloneDirectory: File) { try { HttpTransport.setConnectionFactory(InsecureHttpConnectionFactory()) + var evaluatedGitRepoURI = evaluateEnvVariablesInGitRepoURI(gitRepositoryURI, System.getenv()) + val cloneCommand = Git.cloneRepository().apply { setTransportConfigCallback(getTransportCallingCallback()) - setURI(gitRepositoryURI) + setURI(evaluatedGitRepoURI) setDirectory(cloneDirectory) } @@ -104,12 +106,27 @@ private fun jgitClone(gitRepositoryURI: String, cloneDirectory: File) { } logger.log("Cloning: $gitRepositoryURI -> ${cloneDirectory.canonicalPath}") + cloneCommand.call() } finally { HttpTransport.setConnectionFactory(preservedConnectionFactory) } } +fun evaluateEnvVariablesInGitRepoURI(gitRepositoryURI: String, environmentVariables: Map): String { + logger.log("Evaluating any environment variables in $gitRepositoryURI") + var evaluatedGitRepoUrl = gitRepositoryURI + val envVariableRegex = Regex("\\$\\{([^}]+)}") + val envVariableMatches = envVariableRegex.findAll(gitRepositoryURI) + envVariableMatches.forEach { matchResult -> + val envVariable = matchResult.groupValues[1] + val envVariableValue = environmentVariables.getValue(envVariable) + logger.log("Evaluating $envVariable in $gitRepositoryURI") + evaluatedGitRepoUrl = evaluatedGitRepoUrl.replace("\${$envVariable}", envVariableValue) + } + return evaluatedGitRepoUrl +} + fun loadFromPath(json: Value?, path: List): Value? { if (json !is JSONObjectValue) return null diff --git a/core/src/main/kotlin/in/specmatic/core/git/SystemGit.kt b/core/src/main/kotlin/in/specmatic/core/git/SystemGit.kt index 73e7ebb5e..a86cfd5f3 100644 --- a/core/src/main/kotlin/in/specmatic/core/git/SystemGit.kt +++ b/core/src/main/kotlin/in/specmatic/core/git/SystemGit.kt @@ -48,7 +48,7 @@ class SystemGit(override val workingDirectory: String = ".", private val prefix: override fun checkout(branchName: String): SystemGit = this.also { execute(Configuration.gitCommand, "checkout", branchName) } override fun merge(branchName: String): SystemGit = this.also { execute(Configuration.gitCommand, "merge", branchName) } override fun clone(gitRepositoryURI: String, cloneDirectory: File): SystemGit = - this.also { executeWithAuth("clone", gitRepositoryURI, cloneDirectory.absolutePath) } + this.also { executeWithAuth("clone", evaluateEnvVariablesInGitRepoURI(gitRepositoryURI, System.getenv()), cloneDirectory.absolutePath) } override fun exists(treeish: String, relativePath: String): Boolean { return try { show(treeish, relativePath) diff --git a/core/src/test/kotlin/in/specmatic/core/git/GitOperationsTest.kt b/core/src/test/kotlin/in/specmatic/core/git/GitOperationsTest.kt new file mode 100644 index 000000000..d4dc4c925 --- /dev/null +++ b/core/src/test/kotlin/in/specmatic/core/git/GitOperationsTest.kt @@ -0,0 +1,33 @@ +package `in`.specmatic.core.git + +import org.assertj.core.api.AssertionsForClassTypes.assertThat +import org.junit.jupiter.api.Test + +class GitOperationsTest { + @Test + fun shouldNotChangeGitRepoUrlWhenThereAreNoEnvVariables() { + val gitRepositoryURI = "https://gitlab.com/group/project.git" + val evaluatedGitRepositoryURI = + evaluateEnvVariablesInGitRepoURI(gitRepositoryURI = gitRepositoryURI, emptyMap()) + assertThat(evaluatedGitRepositoryURI).isEqualTo(gitRepositoryURI) + } + + @Test + fun shouldChangeGitRepoUrlWhenThereIsOneEnvVariable() { + val gitRepositoryURI = "https://gitlab-ci-token:${'$'}{CI_JOB_TOKEN}@gitlab.com/group/project.git" + val evaluatedGitRepositoryURI = + evaluateEnvVariablesInGitRepoURI(gitRepositoryURI = gitRepositoryURI, mapOf("CI_JOB_TOKEN" to "token")) + assertThat(evaluatedGitRepositoryURI).isEqualTo("https://gitlab-ci-token:token@gitlab.com/group/project.git") + } + + @Test + fun shouldChangeGitRepoUrlWhenThereAreMultipleEnvVariable() { + val gitRepositoryURI = "https://${'$'}{USER_NAME}:${'$'}{PASSWORD}@gitlab.com/group/project.git" + val evaluatedGitRepositoryURI = + evaluateEnvVariablesInGitRepoURI( + gitRepositoryURI = gitRepositoryURI, + mapOf("USER_NAME" to "john", "PASSWORD" to "password") + ) + assertThat(evaluatedGitRepositoryURI).isEqualTo("https://john:password@gitlab.com/group/project.git") + } +} \ No newline at end of file diff --git a/version.properties b/version.properties index 20f003866..9b15ea6d9 100644 --- a/version.properties +++ b/version.properties @@ -1 +1 @@ -version=1.3.17 +version=1.3.18