From 52c80095117ae7aa39b4dec481bb0247bbc233ba Mon Sep 17 00:00:00 2001 From: "Brewer, Michael (US - California)" Date: Fri, 5 Apr 2024 18:15:37 -0700 Subject: [PATCH] feat(git-clone): add support git_providers setting --- git-clone/main.test.ts | 17 +++++++++++++++-- git-clone/main.tf | 25 +++++++++++++++++++++++-- 2 files changed, 38 insertions(+), 4 deletions(-) diff --git a/git-clone/main.test.ts b/git-clone/main.test.ts index 8f31c82..1ee1c13 100644 --- a/git-clone/main.test.ts +++ b/git-clone/main.test.ts @@ -104,7 +104,7 @@ describe("git-clone", async () => { expect(state.outputs.branch_name.value).toEqual("feat/branch"); }); - it("repo_dir should match repo name with gitlab tree url", async () => { + it("gitlab tree url branch_name should match", async () => { const state = await runTerraformApply(import.meta.dir, { agent_id: "foo", base_dir: "/tmp", @@ -117,7 +117,7 @@ describe("git-clone", async () => { expect(state.outputs.branch_name.value).toEqual("feat/branch"); }); - it("repo_dir should match repo name with github tree url", async () => { + it("github tree url branch_name should match", async () => { const state = await runTerraformApply(import.meta.dir, { agent_id: "foo", base_dir: "/tmp", @@ -130,6 +130,19 @@ describe("git-clone", async () => { expect(state.outputs.branch_name.value).toEqual("feat/branch"); }); + it("handle unsupported git provider", async () => { + const url = "https://git.unknown.com/coder/coder"; + const state = await runTerraformApply(import.meta.dir, { + agent_id: "foo", + base_dir: "/tmp", + url, + }); + expect(state.outputs.repo_dir.value).toEqual("/tmp/coder"); + expect(state.outputs.clone_url.value).toEqual(url); + expect(state.outputs.web_url.value).toEqual(url); + expect(state.outputs.branch_name.value).toEqual(""); + }); + it("runs with github clone with switch to feat/branch", async () => { const state = await runTerraformApply(import.meta.dir, { agent_id: "foo", diff --git a/git-clone/main.tf b/git-clone/main.tf index f745b95..b0371c1 100644 --- a/git-clone/main.tf +++ b/git-clone/main.tf @@ -25,13 +25,34 @@ variable "agent_id" { type = string } +variable "git_providers" { + type = map(object({ + tree_path = string + })) + description = "The set of tree paths for each git provider." + default = { + "https://github.com/" = { + tree_path = "/tree/" + }, + "https://gitlab.com/" = { + tree_path = "/-/tree/" + }, + } +} + 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, "") + # Remove tree and branch name from the URL - clone_url = replace(replace(local.url, "//-/tree/.*/", ""), "//tree/.*/", "") + clone_url = local.tree_path != "" ? replace(local.url, "/${local.tree_path}.*/", "") : local.url # Extract the branch name from the URL - branch_name = replace(replace(replace(local.url, local.clone_url, ""), "/.*/-/tree//", ""), "/.*/tree//", "") + branch_name = local.tree_path != "" ? replace(replace(local.url, local.clone_url, ""), "/.*${local.tree_path}/", "") : "" # Extract the folder name from the URL folder_name = replace(basename(local.clone_url), ".git", "") # Construct the path to clone the repository