Skip to content

Commit

Permalink
evaluating env var in git repo URL before clone command
Browse files Browse the repository at this point in the history
  • Loading branch information
harikrishnan83 committed May 12, 2024
1 parent 17ec079 commit 1bfc2af
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 3 deletions.
19 changes: 18 additions & 1 deletion core/src/main/kotlin/in/specmatic/core/git/GitOperations.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand All @@ -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, String>): 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<String>): Value? {
if (json !is JSONObjectValue)
return null
Expand Down
2 changes: 1 addition & 1 deletion core/src/main/kotlin/in/specmatic/core/git/SystemGit.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
33 changes: 33 additions & 0 deletions core/src/test/kotlin/in/specmatic/core/git/GitOperationsTest.kt
Original file line number Diff line number Diff line change
@@ -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<String, String>())
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:[email protected]/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:[email protected]/group/project.git")
}
}
2 changes: 1 addition & 1 deletion version.properties
Original file line number Diff line number Diff line change
@@ -1 +1 @@
version=1.3.17
version=1.3.18

0 comments on commit 1bfc2af

Please sign in to comment.