feat(git-clone): add support for tree git clone url

pull/210/head
Michael Brewer 1 year ago
parent 5476f819ce
commit 6b1fdbecc4
No known key found for this signature in database
GPG Key ID: D7A137BA1254AFC7

@ -14,6 +14,71 @@ describe("git-clone", async () => {
url: "foo",
});
it("repo_dir should match repo name for https", async () => {
const state = await runTerraformApply(import.meta.dir, {
agent_id: "foo",
base_dir: "/tmp",
url: "https://github.com/coder/coder.git",
});
expect(state.outputs.repo_dir.value).toEqual("/tmp/coder");
expect(state.outputs.clone_url.value).toEqual(
"https://github.com/coder/coder.git",
);
expect(state.outputs.branch_name.value).toEqual("");
});
it("repo_dir should match repo name for https without .git", async () => {
const state = await runTerraformApply(import.meta.dir, {
agent_id: "foo",
base_dir: "/tmp",
url: "https://github.com/coder/coder",
});
expect(state.outputs.repo_dir.value).toEqual("/tmp/coder");
expect(state.outputs.clone_url.value).toEqual(
"https://github.com/coder/coder",
);
expect(state.outputs.branch_name.value).toEqual("");
});
it("repo_dir should match repo name for ssh", async () => {
const state = await runTerraformApply(import.meta.dir, {
agent_id: "foo",
base_dir: "/tmp",
url: "git@github.com:coder/coder.git",
});
expect(state.outputs.repo_dir.value).toEqual("/tmp/coder");
expect(state.outputs.clone_url.value).toEqual(
"git@github.com:coder/coder.git",
);
expect(state.outputs.branch_name.value).toEqual("");
});
it("repo_dir should match repo name with gitlab tree url", 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",
});
expect(state.outputs.repo_dir.value).toEqual("/tmp/repo-tests.log");
expect(state.outputs.clone_url.value).toEqual(
"https://gitlab.com/mike.brew/repo-tests.log",
);
expect(state.outputs.branch_name.value).toEqual("feat/branch");
});
it("repo_dir should match repo name with github tree url", async () => {
const state = await runTerraformApply(import.meta.dir, {
agent_id: "foo",
base_dir: "/tmp",
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.clone_url.value).toEqual(
"https://github.com/michaelbrewer/repo-tests.log",
);
expect(state.outputs.branch_name.value).toEqual("feat/branch");
});
it("fails without git", async () => {
const state = await runTerraformApply(import.meta.dir, {
agent_id: "foo",

@ -26,7 +26,9 @@ variable "agent_id" {
}
locals {
clone_path = var.base_dir != "" ? join("/", [var.base_dir, replace(basename(var.url), ".git", "")]) : join("/", ["~", replace(basename(var.url), ".git", "")])
clone_url = replace(replace(var.url, "//-/tree/.*/", ""), "//tree/.*/", "")
branch_name = replace(replace(replace(var.url, local.clone_url, ""), "/.*/-/tree//", ""), "/.*/tree//", "")
clone_path = var.base_dir != "" ? join("/", [var.base_dir, replace(basename(local.clone_url), ".git", "")]) : join("/", ["~", replace(basename(local.clone_url), ".git", "")])
}
output "repo_dir" {
@ -34,11 +36,22 @@ output "repo_dir" {
description = "Full path of cloned repo directory"
}
output "clone_url" {
value = local.clone_url
description = "Git repository URL"
}
output "branch_name" {
value = local.branch_name
description = "Git branch"
}
resource "coder_script" "git_clone" {
agent_id = var.agent_id
script = templatefile("${path.module}/run.sh", {
CLONE_PATH = local.clone_path
REPO_URL : var.url,
CLONE_PATH = local.clone_path,
REPO_URL : local.clone_url,
BRANCH_NAME : local.branch_name,
})
display_name = "Git Clone"
icon = "/icon/git.svg"

@ -2,6 +2,7 @@
REPO_URL="${REPO_URL}"
CLONE_PATH="${CLONE_PATH}"
BRANCH_NAME="${BRANCH_NAME}"
# Expand home if it's specified!
CLONE_PATH="$${CLONE_PATH/#\~/$${HOME}}"
@ -35,6 +36,20 @@ fi
if [ -z "$(ls -A "$CLONE_PATH")" ]; then
echo "Cloning $REPO_URL to $CLONE_PATH..."
git clone "$REPO_URL" "$CLONE_PATH"
# Return the exit code of the last command
exit_code=$?
if [ $exit_code -ne 0 ]; then
exit $exit_code
fi
# If BRANCH_NAME is set of a non-blank value, switch to that branch
if [ -n "$BRANCH_NAME" ]; then
echo "Switch to branch $BRANCH_NAME..."
cd "$CLONE_PATH" || exit 1
git switch "$BRANCH_NAME"
cd - || exit 1
fi
else
echo "$CLONE_PATH already exists and isn't empty, skipping clone!"
exit 0

Loading…
Cancel
Save