From 402838ccac4ffc92d726050c53b5b13cbb09a45f Mon Sep 17 00:00:00 2001 From: Michael Brewer Date: Sat, 6 Apr 2024 09:26:18 -0700 Subject: [PATCH] chore(git-clone): code cleanup and use one --- git-clone/main.test.ts | 9 ++++----- git-clone/main.tf | 6 +++--- 2 files changed, 7 insertions(+), 8 deletions(-) diff --git a/git-clone/main.test.ts b/git-clone/main.test.ts index d66486c..e7fffa4 100644 --- a/git-clone/main.test.ts +++ b/git-clone/main.test.ts @@ -72,9 +72,8 @@ describe("git-clone", async () => { }); expect(state.outputs.repo_dir.value).toEqual("/tmp/coder"); expect(state.outputs.clone_url.value).toEqual(url); - expect(state.outputs.web_url.value).toEqual( - "https://github.com/coder/coder.git", - ); + const https_url = "https://github.com/coder/coder.git"; + expect(state.outputs.web_url.value).toEqual(https_url); expect(state.outputs.branch_name.value).toEqual(""); }); @@ -149,7 +148,7 @@ describe("git-clone", async () => { expect(state.outputs.branch_name.value).toEqual("feat/example"); }); - it("handle invalid git provider configuration", async () => { + it("handle unsupported git provider configuration", async () => { const t = async () => { await runTerraformApply(import.meta.dir, { agent_id: "foo", @@ -165,7 +164,7 @@ describe("git-clone", async () => { expect(t).toThrow('Allowed values for provider are "github" or "gitlab".'); }); - it("handle unsupported git provider", async () => { + it("handle unknown git provider url", async () => { const url = "https://git.unknown.com/coder/coder"; const state = await runTerraformApply(import.meta.dir, { agent_id: "foo", diff --git a/git-clone/main.tf b/git-clone/main.tf index 8688702..b29979c 100644 --- a/git-clone/main.tf +++ b/git-clone/main.tf @@ -49,9 +49,9 @@ locals { url = replace(replace(var.url, "/\\?.*/", ""), "/#.*/", "") # 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/" : "" + provider_key = try(one([for key in keys(var.git_providers) : key if startswith(local.url, key)]), null) + provider = try(lookup(var.git_providers, local.provider_key).provider, "") + tree_path = local.provider == "gitlab" ? "/-/tree/" : local.provider == "github" ? "/tree/" : "" # Remove tree and branch name from the URL clone_url = local.tree_path != "" ? replace(local.url, "/${local.tree_path}.*/", "") : local.url