test(git-clone): add missing fragment test

pull/210/head
Brewer, Michael (US - California) 1 year ago
parent 118b93eb7f
commit 741d656a89
No known key found for this signature in database
GPG Key ID: 83CCF3ACE4DF618E

@ -85,12 +85,22 @@ describe("git-clone", async () => {
url: "https://gitlab.com/mike.brew/repo-tests.log/-/tree/feat/branch?ref_type=heads", 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.repo_dir.value).toEqual("/tmp/repo-tests.log");
expect(state.outputs.clone_url.value).toEqual( const https_url = "https://gitlab.com/mike.brew/repo-tests.log";
"https://gitlab.com/mike.brew/repo-tests.log", expect(state.outputs.clone_url.value).toEqual(https_url);
); expect(state.outputs.web_url.value).toEqual(https_url);
expect(state.outputs.web_url.value).toEqual( expect(state.outputs.branch_name.value).toEqual("feat/branch");
"https://gitlab.com/mike.brew/repo-tests.log", });
);
it("branch_name should not include fragments", 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#name",
});
expect(state.outputs.repo_dir.value).toEqual("/tmp/repo-tests.log");
const https_url = "https://gitlab.com/mike.brew/repo-tests.log";
expect(state.outputs.clone_url.value).toEqual(https_url);
expect(state.outputs.web_url.value).toEqual(https_url);
expect(state.outputs.branch_name.value).toEqual("feat/branch"); expect(state.outputs.branch_name.value).toEqual("feat/branch");
}); });
@ -101,12 +111,9 @@ describe("git-clone", async () => {
url: "https://gitlab.com/mike.brew/repo-tests.log/-/tree/feat/branch", url: "https://gitlab.com/mike.brew/repo-tests.log/-/tree/feat/branch",
}); });
expect(state.outputs.repo_dir.value).toEqual("/tmp/repo-tests.log"); expect(state.outputs.repo_dir.value).toEqual("/tmp/repo-tests.log");
expect(state.outputs.clone_url.value).toEqual( const https_url = "https://gitlab.com/mike.brew/repo-tests.log";
"https://gitlab.com/mike.brew/repo-tests.log", expect(state.outputs.clone_url.value).toEqual(https_url);
); expect(state.outputs.web_url.value).toEqual(https_url);
expect(state.outputs.web_url.value).toEqual(
"https://gitlab.com/mike.brew/repo-tests.log",
);
expect(state.outputs.branch_name.value).toEqual("feat/branch"); expect(state.outputs.branch_name.value).toEqual("feat/branch");
}); });
@ -117,12 +124,9 @@ describe("git-clone", async () => {
url: "https://github.com/michaelbrewer/repo-tests.log/tree/feat/branch", url: "https://github.com/michaelbrewer/repo-tests.log/tree/feat/branch",
}); });
expect(state.outputs.repo_dir.value).toEqual("/tmp/repo-tests.log"); expect(state.outputs.repo_dir.value).toEqual("/tmp/repo-tests.log");
expect(state.outputs.clone_url.value).toEqual( const https_url = "https://github.com/michaelbrewer/repo-tests.log";
"https://github.com/michaelbrewer/repo-tests.log", expect(state.outputs.clone_url.value).toEqual(https_url);
); expect(state.outputs.web_url.value).toEqual(https_url);
expect(state.outputs.web_url.value).toEqual(
"https://github.com/michaelbrewer/repo-tests.log",
);
expect(state.outputs.branch_name.value).toEqual("feat/branch"); expect(state.outputs.branch_name.value).toEqual("feat/branch");
}); });

@ -26,17 +26,17 @@ variable "agent_id" {
} }
locals { locals {
# Remove query parameters and branch name from the URL # Remove query parameters and fragments from the URL
url = replace(replace(var.url, "/\\?.*/", ""), "/#.*", "") url = replace(replace(var.url, "/\\?.*/", ""), "/#.*", "")
# Remove tree and branch name from the URL # Remove tree and branch name from the URL
clone_url = replace(replace(local.url, "//-/tree/.*/", ""), "//tree/.*/", "") clone_url = replace(replace(local.url, "//-/tree/.*/", ""), "//tree/.*/", "")
# Extract the branch name from the URL # Extract the branch name from the URL
branch_name = replace(replace(replace(local.url, local.clone_url, ""), "/.*/-/tree//", ""), "/.*/tree//", "") branch_name = replace(replace(replace(local.url, local.clone_url, ""), "/.*/-/tree//", ""), "/.*/tree//", "")
# Extract the folder name from the URL
folder_name = replace(basename(local.clone_url), ".git", "")
# Construct the path to clone the repository # 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", "")]) clone_path = var.base_dir != "" ? join("/", [var.base_dir, local.folder_name]) : join("/", ["~", local.folder_name])
# Construct the web URL
# 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 web_url = startswith(local.clone_url, "git@") ? replace(replace(local.clone_url, ":", "/"), "git@", "https://") : local.clone_url
} }
@ -52,12 +52,12 @@ output "clone_url" {
output "web_url" { output "web_url" {
value = local.web_url value = local.web_url
description = "Git https repository URL" description = "Git https repository URL (may be invalid for unsupported providers)"
} }
output "branch_name" { output "branch_name" {
value = local.branch_name value = local.branch_name
description = "Git branch" description = "Git branch name (may be empty)"
} }
resource "coder_script" "git_clone" { resource "coder_script" "git_clone" {

Loading…
Cancel
Save