diff --git a/git-clone/README.md b/git-clone/README.md index 7121ff8..7c9b228 100644 --- a/git-clone/README.md +++ b/git-clone/README.md @@ -74,7 +74,7 @@ module "git-clone" { url = "https://github.example.com/coder/coder/tree/feat/example" git_providers = { "https://github.example.com/" = { - tree_path = "/tree/" + tree_path = "github" } } } @@ -103,7 +103,7 @@ module "git-clone" { url = "https://gitlab.example.com/coder/coder/-/tree/feat/example" git_providers = { "https://gitlab.example.com/" = { - tree_path = "/-/tree/" + tree_path = "gitlab" } } } diff --git a/git-clone/main.test.ts b/git-clone/main.test.ts index 678766a..d66486c 100644 --- a/git-clone/main.test.ts +++ b/git-clone/main.test.ts @@ -138,7 +138,7 @@ describe("git-clone", async () => { git_providers: ` { "https://git.example.com/" = { - tree_path = "/-/tree/" + provider = "gitlab" } }`, }); @@ -149,6 +149,22 @@ describe("git-clone", async () => { expect(state.outputs.branch_name.value).toEqual("feat/example"); }); + it("handle invalid git provider configuration", async () => { + const t = async () => { + await runTerraformApply(import.meta.dir, { + agent_id: "foo", + url: "foo", + git_providers: ` + { + "https://git.example.com/" = { + provider = "bitbucket" + } + }`, + }); + }; + expect(t).toThrow('Allowed values for provider are "github" or "gitlab".'); + }); + it("handle unsupported git provider", async () => { const url = "https://git.unknown.com/coder/coder"; const state = await runTerraformApply(import.meta.dir, { diff --git a/git-clone/main.tf b/git-clone/main.tf index b0371c1..8688702 100644 --- a/git-clone/main.tf +++ b/git-clone/main.tf @@ -27,27 +27,31 @@ variable "agent_id" { variable "git_providers" { type = map(object({ - tree_path = string + provider = string })) - description = "The set of tree paths for each git provider." + description = "The set of git provider by the base url." default = { "https://github.com/" = { - tree_path = "/tree/" + provider = "github" }, "https://gitlab.com/" = { - tree_path = "/-/tree/" + provider = "gitlab" }, } + validation { + error_message = "Allowed values for provider are \"github\" or \"gitlab\"." + condition = alltrue([for provider in var.git_providers : contains(["github", "gitlab"], provider.provider)]) + } } locals { # Remove query parameters and fragments from the URL url = replace(replace(var.url, "/\\?.*/", ""), "/#.*/", "") - # Determine the git provider based on the URL - git_provider = try(coalesce([for provider in keys(var.git_providers) : provider if startswith(local.url, provider)]...), null) - # Get the tree path based on the git provider - tree_path = try(lookup(var.git_providers, local.git_provider).tree_path, "") + # Find the git provider based on the URL and determine the tree path + git_provider_key = try(coalesce([for provider in keys(var.git_providers) : provider if startswith(local.url, provider)]...), null) + git_provider = try(lookup(var.git_providers, local.git_provider_key).provider, "") + tree_path = local.git_provider == "gitlab" ? "/-/tree/" : local.git_provider == "github" ? "/tree/" : "" # Remove tree and branch name from the URL clone_url = local.tree_path != "" ? replace(local.url, "/${local.tree_path}.*/", "") : local.url