diff --git a/git-clone/main.test.ts b/git-clone/main.test.ts index 8370e50..7a5c22e 100644 --- a/git-clone/main.test.ts +++ b/git-clone/main.test.ts @@ -38,44 +38,62 @@ describe("git-clone", async () => { }); it("repo_dir should match repo name for https", async () => { + const url = "https://github.com/coder/coder.git"; const state = await runTerraformApply(import.meta.dir, { agent_id: "foo", base_dir: "/tmp", - url: "https://github.com/coder/coder.git", + url, }); expect(state.outputs.repo_dir.value).toEqual("/tmp/coder"); - expect(state.outputs.clone_url.value).toEqual( - "https://github.com/coder/coder.git", - ); + expect(state.outputs.clone_url.value).toEqual(url); + expect(state.outputs.web_url.value).toEqual(url); expect(state.outputs.branch_name.value).toEqual(""); }); it("repo_dir should match repo name for https without .git", async () => { + const url = "https://github.com/coder/coder"; const state = await runTerraformApply(import.meta.dir, { agent_id: "foo", base_dir: "/tmp", - url: "https://github.com/coder/coder", + url, }); expect(state.outputs.repo_dir.value).toEqual("/tmp/coder"); - expect(state.outputs.clone_url.value).toEqual( - "https://github.com/coder/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("repo_dir should match repo name for ssh", async () => { + const url = "git@github.com:coder/coder.git"; const state = await runTerraformApply(import.meta.dir, { agent_id: "foo", base_dir: "/tmp", - url: "git@github.com:coder/coder.git", + url, }); expect(state.outputs.repo_dir.value).toEqual("/tmp/coder"); - expect(state.outputs.clone_url.value).toEqual( - "git@github.com:coder/coder.git", + expect(state.outputs.clone_url.value).toEqual(url); + expect(state.outputs.web_url.value).toEqual( + "https://github.com/coder/coder.git", ); expect(state.outputs.branch_name.value).toEqual(""); }); + it("branch_name should not include query string", async () => { + const state = await runTerraformApply(import.meta.dir, { + agent_id: "foo", + base_dir: "/tmp", + url: "https://gitlab.com/mike.brew/repo-tests.log/-/tree/feat/branch?ref_type=heads", + }); + expect(state.outputs.repo_dir.value).toEqual("/tmp/repo-tests.log"); + expect(state.outputs.clone_url.value).toEqual( + "https://gitlab.com/mike.brew/repo-tests.log", + ); + expect(state.outputs.web_url.value).toEqual( + "https://gitlab.com/mike.brew/repo-tests.log", + ); + expect(state.outputs.branch_name.value).toEqual("feat/branch"); + }); + it("repo_dir should match repo name with gitlab tree url", async () => { const state = await runTerraformApply(import.meta.dir, { agent_id: "foo", @@ -86,6 +104,9 @@ describe("git-clone", async () => { expect(state.outputs.clone_url.value).toEqual( "https://gitlab.com/mike.brew/repo-tests.log", ); + expect(state.outputs.web_url.value).toEqual( + "https://gitlab.com/mike.brew/repo-tests.log", + ); expect(state.outputs.branch_name.value).toEqual("feat/branch"); }); @@ -99,6 +120,9 @@ describe("git-clone", async () => { expect(state.outputs.clone_url.value).toEqual( "https://github.com/michaelbrewer/repo-tests.log", ); + expect(state.outputs.web_url.value).toEqual( + "https://github.com/michaelbrewer/repo-tests.log", + ); expect(state.outputs.branch_name.value).toEqual("feat/branch"); }); diff --git a/git-clone/main.tf b/git-clone/main.tf index 22a44c9..83510a9 100644 --- a/git-clone/main.tf +++ b/git-clone/main.tf @@ -26,9 +26,18 @@ variable "agent_id" { } locals { - clone_url = replace(replace(var.url, "//-/tree/.*/", ""), "//tree/.*/", "") - branch_name = replace(replace(replace(var.url, local.clone_url, ""), "/.*/-/tree//", ""), "/.*/tree//", "") - clone_path = var.base_dir != "" ? join("/", [var.base_dir, replace(basename(local.clone_url), ".git", "")]) : join("/", ["~", replace(basename(local.clone_url), ".git", "")]) + # Remove query parameters and branch name from the URL + url = replace(replace(var.url, "/\\?.*/", ""), "/#.*", "") + # Remove tree and branch name from the URL + clone_url = replace(replace(local.url, "//-/tree/.*/", ""), "//tree/.*/", "") + # Extract the branch name from the URL + branch_name = replace(replace(replace(local.url, local.clone_url, ""), "/.*/-/tree//", ""), "/.*/tree//", "") + # Construct the path to clone the repository + clone_path = var.base_dir != "" ? join("/", [var.base_dir, replace(basename(local.clone_url), ".git", "")]) : join("/", ["~", replace(basename(local.clone_url), ".git", "")]) + + # git@gitlab.com:mike.brew/repo-tests.log.git + # becomes https://gitlab.com/mike.brew/repo-tests.log.git + web_url = startswith(local.clone_url, "git@") ? replace(replace(local.clone_url, ":", "/"), "git@", "https://") : local.clone_url } output "repo_dir" { @@ -41,6 +50,11 @@ output "clone_url" { description = "Git repository URL" } +output "web_url" { + value = local.web_url + description = "Git https repository URL" +} + output "branch_name" { value = local.branch_name description = "Git branch"