chore(git-clone): code cleanup and use one

pull/210/head
Michael Brewer 1 year ago
parent 53cf0b5fd8
commit 402838ccac
No known key found for this signature in database
GPG Key ID: D7A137BA1254AFC7

@ -72,9 +72,8 @@ describe("git-clone", async () => {
}); });
expect(state.outputs.repo_dir.value).toEqual("/tmp/coder"); expect(state.outputs.repo_dir.value).toEqual("/tmp/coder");
expect(state.outputs.clone_url.value).toEqual(url); expect(state.outputs.clone_url.value).toEqual(url);
expect(state.outputs.web_url.value).toEqual( const https_url = "https://github.com/coder/coder.git";
"https://github.com/coder/coder.git", expect(state.outputs.web_url.value).toEqual(https_url);
);
expect(state.outputs.branch_name.value).toEqual(""); expect(state.outputs.branch_name.value).toEqual("");
}); });
@ -149,7 +148,7 @@ 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 () => { it("handle unsupported git provider configuration", async () => {
const t = async () => { const t = async () => {
await runTerraformApply(import.meta.dir, { await runTerraformApply(import.meta.dir, {
agent_id: "foo", agent_id: "foo",
@ -165,7 +164,7 @@ describe("git-clone", async () => {
expect(t).toThrow('Allowed values for provider are "github" or "gitlab".'); expect(t).toThrow('Allowed values for provider are "github" or "gitlab".');
}); });
it("handle unsupported git provider", async () => { it("handle unknown git provider url", 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, {
agent_id: "foo", agent_id: "foo",

@ -49,9 +49,9 @@ locals {
url = replace(replace(var.url, "/\\?.*/", ""), "/#.*/", "") url = replace(replace(var.url, "/\\?.*/", ""), "/#.*/", "")
# Find the git provider based on the URL and determine the tree path # Find the git provider based on the URL and determine the tree path
git_provider_key = try(coalesce([for provider in keys(var.git_providers) : provider if startswith(local.url, provider)]...), null) provider_key = try(one([for key in keys(var.git_providers) : key if startswith(local.url, key)]), null)
git_provider = try(lookup(var.git_providers, local.git_provider_key).provider, "") provider = try(lookup(var.git_providers, local.provider_key).provider, "")
tree_path = local.git_provider == "gitlab" ? "/-/tree/" : local.git_provider == "github" ? "/tree/" : "" tree_path = local.provider == "gitlab" ? "/-/tree/" : local.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