diff --git a/git-clone/README.md b/git-clone/README.md index 216b93b..a8dd312 100644 --- a/git-clone/README.md +++ b/git-clone/README.md @@ -137,3 +137,19 @@ module "git-clone" { } } ``` + +## Git clone with branch_name set + +Alternatively, you can set the `branch_name` attribute to clone a specific branch. + +For example, to clone the `feat/example` branch: + +```tf +module "git-clone" { + source = "registry.coder.com/modules/git-clone/coder" + version = "1.0.11" + agent_id = coder_agent.example.id + url = "https://github.com/coder/coder" + branch_name = "feat/example" +} +``` diff --git a/git-clone/main.test.ts b/git-clone/main.test.ts index c7d6e81..87b0e4a 100644 --- a/git-clone/main.test.ts +++ b/git-clone/main.test.ts @@ -207,4 +207,25 @@ describe("git-clone", async () => { "Cloning https://gitlab.com/mike.brew/repo-tests.log to ~/repo-tests.log on branch feat/branch...", ]); }); + + it("runs with github clone with branch_name set to feat/branch", async () => { + const url = "https://github.com/michaelbrewer/repo-tests.log"; + const branch_name = "feat/branch"; + const state = await runTerraformApply(import.meta.dir, { + agent_id: "foo", + url, + branch_name, + }); + expect(state.outputs.repo_dir.value).toEqual("~/repo-tests.log"); + expect(state.outputs.clone_url.value).toEqual(url); + expect(state.outputs.web_url.value).toEqual(url); + expect(state.outputs.branch_name.value).toEqual(branch_name); + + const output = await executeScriptInContainer(state, "alpine/git"); + expect(output.exitCode).toBe(0); + expect(output.stdout).toEqual([ + "Creating directory ~/repo-tests.log...", + "Cloning https://github.com/michaelbrewer/repo-tests.log to ~/repo-tests.log on branch feat/branch...", + ]); + }); }); diff --git a/git-clone/main.tf b/git-clone/main.tf index 9563f51..4af5000 100644 --- a/git-clone/main.tf +++ b/git-clone/main.tf @@ -44,6 +44,12 @@ variable "git_providers" { } } +variable "branch_name" { + description = "The branch name to clone. If not provided, the default branch will be cloned." + type = string + default = "" +} + locals { # Remove query parameters and fragments from the URL url = replace(replace(var.url, "/\\?.*/", ""), "/#.*/", "") @@ -54,9 +60,9 @@ locals { 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 + clone_url = var.branch_name == "" && local.tree_path != "" ? replace(local.url, "/${local.tree_path}.*/", "") : local.url # Extract the branch name from the URL - branch_name = local.tree_path != "" ? replace(replace(local.url, local.clone_url, ""), "/.*${local.tree_path}/", "") : "" + branch_name = var.branch_name == "" && local.tree_path != "" ? replace(replace(local.url, local.clone_url, ""), "/.*${local.tree_path}/", "") : var.branch_name # Extract the folder name from the URL folder_name = replace(basename(local.clone_url), ".git", "") # Construct the path to clone the repository