feat(git-clone): easier ux

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

@ -74,7 +74,7 @@ module "git-clone" {
url = "https://github.example.com/coder/coder/tree/feat/example" url = "https://github.example.com/coder/coder/tree/feat/example"
git_providers = { git_providers = {
"https://github.example.com/" = { "https://github.example.com/" = {
tree_path = "/tree/" tree_path = "github"
} }
} }
} }
@ -103,7 +103,7 @@ module "git-clone" {
url = "https://gitlab.example.com/coder/coder/-/tree/feat/example" url = "https://gitlab.example.com/coder/coder/-/tree/feat/example"
git_providers = { git_providers = {
"https://gitlab.example.com/" = { "https://gitlab.example.com/" = {
tree_path = "/-/tree/" tree_path = "gitlab"
} }
} }
} }

@ -138,7 +138,7 @@ describe("git-clone", async () => {
git_providers: ` git_providers: `
{ {
"https://git.example.com/" = { "https://git.example.com/" = {
tree_path = "/-/tree/" provider = "gitlab"
} }
}`, }`,
}); });
@ -149,6 +149,22 @@ describe("git-clone", async () => {
expect(state.outputs.branch_name.value).toEqual("feat/example"); expect(state.outputs.branch_name.value).toEqual("feat/example");
}); });
it("handle invalid git provider configuration", async () => {
const t = async () => {
await runTerraformApply(import.meta.dir, {
agent_id: "foo",
url: "foo",
git_providers: `
{
"https://git.example.com/" = {
provider = "bitbucket"
}
}`,
});
};
expect(t).toThrow('Allowed values for provider are "github" or "gitlab".');
});
it("handle unsupported git provider", async () => { it("handle unsupported git provider", async () => {
const url = "https://git.unknown.com/coder/coder"; const url = "https://git.unknown.com/coder/coder";
const state = await runTerraformApply(import.meta.dir, { const state = await runTerraformApply(import.meta.dir, {

@ -27,27 +27,31 @@ variable "agent_id" {
variable "git_providers" { variable "git_providers" {
type = map(object({ type = map(object({
tree_path = string provider = string
})) }))
description = "The set of tree paths for each git provider." description = "The set of git provider by the base url."
default = { default = {
"https://github.com/" = { "https://github.com/" = {
tree_path = "/tree/" provider = "github"
}, },
"https://gitlab.com/" = { "https://gitlab.com/" = {
tree_path = "/-/tree/" provider = "gitlab"
}, },
} }
validation {
error_message = "Allowed values for provider are \"github\" or \"gitlab\"."
condition = alltrue([for provider in var.git_providers : contains(["github", "gitlab"], provider.provider)])
}
} }
locals { locals {
# Remove query parameters and fragments from the URL # Remove query parameters and fragments from the URL
url = replace(replace(var.url, "/\\?.*/", ""), "/#.*/", "") url = replace(replace(var.url, "/\\?.*/", ""), "/#.*/", "")
# Determine the git provider based on the URL # Find the git provider based on the URL and determine the tree path
git_provider = try(coalesce([for provider in keys(var.git_providers) : provider if startswith(local.url, provider)]...), null) git_provider_key = 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 git_provider = try(lookup(var.git_providers, local.git_provider_key).provider, "")
tree_path = try(lookup(var.git_providers, local.git_provider).tree_path, "") tree_path = local.git_provider == "gitlab" ? "/-/tree/" : local.git_provider == "github" ? "/tree/" : ""
# Remove tree and branch name from the URL # Remove tree and branch name from the URL
clone_url = local.tree_path != "" ? replace(local.url, "/${local.tree_path}.*/", "") : local.url clone_url = local.tree_path != "" ? replace(local.url, "/${local.tree_path}.*/", "") : local.url

Loading…
Cancel
Save