Skip to content

Commit

Permalink
evaluate env variables in git repo URI only when the value is available
Browse files Browse the repository at this point in the history
  • Loading branch information
harikrishnan83 committed May 12, 2024
1 parent 756497b commit 0af1429
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 3 deletions.
10 changes: 7 additions & 3 deletions core/src/main/kotlin/in/specmatic/core/git/GitOperations.kt
Original file line number Diff line number Diff line change
Expand Up @@ -119,9 +119,13 @@ fun evaluateEnvVariablesInGitRepoURI(gitRepositoryURI: String, environmentVariab
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)
if (environmentVariables.containsKey(envVariable)) {
val envVariableValue = environmentVariables.getValue(envVariable)
logger.log("Evaluating $envVariable in $gitRepositoryURI")
evaluatedGitRepoUrl = evaluatedGitRepoUrl.replace("\${$envVariable}", envVariableValue)
} else {
logger.log("$envVariable in $gitRepositoryURI resembles an environment variable, but it is not available in the environment variables. Skipping evaluation.")
}
}
return evaluatedGitRepoUrl
}
Expand Down
11 changes: 11 additions & 0 deletions core/src/test/kotlin/in/specmatic/core/git/GitOperationsTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,15 @@ class GitOperationsTest {
)
assertThat(evaluatedGitRepositoryURI).isEqualTo("https://john:[email protected]/group/project.git")
}

@Test
fun shouldNotEvaluatePatternsThatResembleEnvVarsWhenTheValueIsNotAvailable() {
val gitRepositoryURI = "https://gitlab-ci-token:${'$'}{CI_JOB_TOKEN}@gitlab.com/group/${'$'}{do-not-eval}/project.git"
val evaluatedGitRepositoryURI =
evaluateEnvVariablesInGitRepoURI(
gitRepositoryURI = gitRepositoryURI,
mapOf("CI_JOB_TOKEN" to "token")
)
assertThat(evaluatedGitRepositoryURI).isEqualTo("https://gitlab-ci-token:[email protected]/group/${'$'}{do-not-eval}/project.git")
}
}

0 comments on commit 0af1429

Please sign in to comment.