feat(git-clone): add support git_providers setting

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

@ -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",

@ -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

Loading…
Cancel
Save