From 1428e2885c7c16ffa92bc081654ec6177eb75216 Mon Sep 17 00:00:00 2001 From: Rie Takahashi Date: Thu, 29 Aug 2024 15:41:32 +0100 Subject: [PATCH 1/5] feat(git-clone): custom destination folder name --- git-clone/main.tf | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/git-clone/main.tf b/git-clone/main.tf index 4af5000e..08bbb75d 100644 --- a/git-clone/main.tf +++ b/git-clone/main.tf @@ -50,6 +50,12 @@ variable "branch_name" { default = "" } +variable "dest_folder" { + description = "The destination folder to clone the repository into." + type = string + default = "" +} + locals { # Remove query parameters and fragments from the URL url = replace(replace(var.url, "/\\?.*/", ""), "/#.*/", "") @@ -64,7 +70,7 @@ locals { # Extract the branch name from the URL 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", "") + folder_name = var.dest_folder == "" ? replace(basename(local.clone_url), ".git", "") : var.dest_folder # Construct the path to clone the repository clone_path = var.base_dir != "" ? join("/", [var.base_dir, local.folder_name]) : join("/", ["~", local.folder_name]) # Construct the web URL From f0f0abe4362ea477d8f78f7d95ea5cd19bd4aa88 Mon Sep 17 00:00:00 2001 From: Rie Takahashi Date: Thu, 29 Aug 2024 15:55:22 +0100 Subject: [PATCH 2/5] chore: use dir instead of folder --- git-clone/main.tf | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/git-clone/main.tf b/git-clone/main.tf index 08bbb75d..f79bee23 100644 --- a/git-clone/main.tf +++ b/git-clone/main.tf @@ -50,7 +50,7 @@ variable "branch_name" { default = "" } -variable "dest_folder" { +variable "dest_dir" { description = "The destination folder to clone the repository into." type = string default = "" @@ -70,7 +70,7 @@ locals { # Extract the branch name from the URL 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 = var.dest_folder == "" ? replace(basename(local.clone_url), ".git", "") : var.dest_folder + folder_name = var.dest_dir == "" ? replace(basename(local.clone_url), ".git", "") : var.dest_dir # Construct the path to clone the repository clone_path = var.base_dir != "" ? join("/", [var.base_dir, local.folder_name]) : join("/", ["~", local.folder_name]) # Construct the web URL From b6baaee4c714f06c59f291a950ad176c7e739514 Mon Sep 17 00:00:00 2001 From: Rie Takahashi Date: Thu, 29 Aug 2024 15:59:51 +0100 Subject: [PATCH 3/5] chore: folder_name makes more sense as it's already in use --- git-clone/main.tf | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/git-clone/main.tf b/git-clone/main.tf index f79bee23..0295444d 100644 --- a/git-clone/main.tf +++ b/git-clone/main.tf @@ -50,7 +50,7 @@ variable "branch_name" { default = "" } -variable "dest_dir" { +variable "folder_name" { description = "The destination folder to clone the repository into." type = string default = "" @@ -70,7 +70,7 @@ locals { # Extract the branch name from the URL 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 = var.dest_dir == "" ? replace(basename(local.clone_url), ".git", "") : var.dest_dir + folder_name = var.folder_name == "" ? replace(basename(local.clone_url), ".git", "") : var.folder_name # Construct the path to clone the repository clone_path = var.base_dir != "" ? join("/", [var.base_dir, local.folder_name]) : join("/", ["~", local.folder_name]) # Construct the web URL From 61f1d7b5f91f96b7d39c213adc1190b04c684fd0 Mon Sep 17 00:00:00 2001 From: Rie Takahashi Date: Thu, 29 Aug 2024 16:52:52 +0100 Subject: [PATCH 4/5] chore: add readme for folder_name attribute --- git-clone/README.md | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/git-clone/README.md b/git-clone/README.md index 255b3f1e..5efc50ed 100644 --- a/git-clone/README.md +++ b/git-clone/README.md @@ -153,3 +153,20 @@ module "git-clone" { branch_name = "feat/example" } ``` + +## Git clone with different destination folder + +By default, the repository will be cloned into a folder matching the repository name. You can use the `folder_name` attribute to change the name of the destination folder to something else. + +For example, this will clone into the `~/projects/coder/coder-dev` folder: + +```tf +module "git-clone" { + source = "registry.coder.com/modules/git-clone/coder" + version = "1.0.12" + agent_id = coder_agent.example.id + url = "https://github.com/coder/coder" + folder_name = "coder-dev" + base_dir = "~/projects/coder" +} +``` From d064d341314f4c4b0871eb12b1b94c3b0a315249 Mon Sep 17 00:00:00 2001 From: Rie Takahashi Date: Fri, 30 Aug 2024 16:53:04 +0100 Subject: [PATCH 5/5] test(git-clone): repo_dir should match base_dir/folder_name --- git-clone/main.test.ts | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/git-clone/main.test.ts b/git-clone/main.test.ts index 87b0e4a6..9fbd2022 100644 --- a/git-clone/main.test.ts +++ b/git-clone/main.test.ts @@ -79,6 +79,22 @@ describe("git-clone", async () => { expect(state.outputs.branch_name.value).toEqual(""); }); + it("repo_dir should match base_dir/folder_name", async () => { + const url = "git@github.com:coder/coder.git"; + const state = await runTerraformApply(import.meta.dir, { + agent_id: "foo", + base_dir: "/tmp", + folder_name: "foo", + url, + }); + expect(state.outputs.repo_dir.value).toEqual("/tmp/foo"); + expect(state.outputs.folder_name.value).toEqual("foo"); + expect(state.outputs.clone_url.value).toEqual(url); + 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(""); + }); + it("branch_name should not include query string", async () => { const state = await runTerraformApply(import.meta.dir, { agent_id: "foo",