diff --git a/CHANGELOG.md b/CHANGELOG.md new file mode 100644 index 0000000..70d7e8b --- /dev/null +++ b/CHANGELOG.md @@ -0,0 +1,11 @@ +## v0.2.0 [2024-02-08] + +_What's new?_ + +- Added `create_branch`, `delete_branch` and `get_branch` pipelines. ([#10](https://github.com/turbot/flowpipe-mod-github/pull/10)) + +## v0.1.0 [2023-12-13] + +_What's new?_ + +- Added 35+ pipelines to make it easy to connect your issues, pull requests, repositories and more. For usage information and a full list of pipelines, please see [GitHub Mod for Flowpipe](https://hub.flowpipe.io/mods/turbot/github). diff --git a/README.md b/README.md index e0d1dee..b398d60 100644 --- a/README.md +++ b/README.md @@ -8,10 +8,6 @@ GitHub pipeline library for [Flowpipe](https://flowpipe.io), enabling seamless i ## Getting Started -### Requirements - -Docker daemon must be installed and running. Please see [Install Docker Engine](https://docs.docker.com/engine/install/) for more information. - ### Installation Download and install Flowpipe (https://flowpipe.io/downloads). Or use Brew: diff --git a/pipelines/branch/create_branch.fp b/pipelines/branch/create_branch.fp new file mode 100644 index 0000000..6aade3f --- /dev/null +++ b/pipelines/branch/create_branch.fp @@ -0,0 +1,63 @@ +pipeline "create_branch" { + title = "Create Branch" + description = "Creates a new branch in a specified repository." + + param "cred" { + type = string + default = "default" + description = local.cred_param_description + } + + param "repository_owner" { + type = string + description = local.repository_owner_param_description + } + + param "repository_name" { + type = string + description = local.repository_name_param_description + } + + param "branch_name" { + type = string + description = "The name of the new branch." + } + + param "source_branch" { + type = string + description = "The name of the source branch to branch off. Defaults to the default branch of the repository." + default = "main" + } + + step "http" "get_latest_commit_sha" { + method = "get" + url = "https://api.github.com/repos/${param.repository_owner}/${param.repository_name}/branches/${param.source_branch}" + request_headers = { + Authorization = "Bearer ${credential.github[param.cred].token}" + } + } + + step "http" "create_branch" { + method = "post" + url = "https://api.github.com/repos/${param.repository_owner}/${param.repository_name}/git/refs" + request_headers = { + Content-Type = "application/json" + Authorization = "Bearer ${credential.github[param.cred].token}" + } + + request_body = jsonencode({ + ref = "refs/heads/${param.branch_name}", + sha = "${step.http.get_latest_commit_sha.response_body.commit.sha}" + }) + + throw { + if = can(result.response_body.errors) + message = join(", ", flatten([for error in result.response_body.errors : error.message])) + } + } + + output "branch" { + value = step.http.create_branch + } + +} diff --git a/pipelines/branch/delete_branch.fp b/pipelines/branch/delete_branch.fp new file mode 100644 index 0000000..665459a --- /dev/null +++ b/pipelines/branch/delete_branch.fp @@ -0,0 +1,43 @@ +pipeline "delete_branch" { + title = "Delete Branch" + description = "Deletes a branch in a specified repository." + + param "cred" { + type = string + default = "default" + description = local.cred_param_description + } + + param "repository_owner" { + type = string + description = local.repository_owner_param_description + } + + param "repository_name" { + type = string + description = local.repository_name_param_description + } + + param "branch_name" { + type = string + description = "The name of the branch to delete." + } + + step "http" "delete_branch" { + method = "delete" + url = "https://api.github.com/repos/${param.repository_owner}/${param.repository_name}/git/refs/heads/${param.branch_name}" + request_headers = { + Authorization = "Bearer ${credential.github[param.cred].token}" + } + + throw { + if = can(result.response_body.errors) + message = join(", ", flatten([for error in result.response_body.errors : error.message])) + } + } + + output "branch" { + value = step.http.delete_branch + } + +} \ No newline at end of file diff --git a/pipelines/branch/get_branch.fp b/pipelines/branch/get_branch.fp new file mode 100644 index 0000000..fa42733 --- /dev/null +++ b/pipelines/branch/get_branch.fp @@ -0,0 +1,40 @@ +pipeline "get_branch" { + title = "Get branch" + description = "Get a branch in a specified repository." + + param "cred" { + type = string + default = "default" + } + + param "repository_owner" { + type = string + description = local.repository_owner_param_description + } + + param "repository_name" { + type = string + description = local.repository_name_param_description + } + + param "branch_name" { + type = string + description = "The name of the branch to check." + } + + step "http" "get_branch" { + method = "get" + url = "https://api.github.com/repos/${param.repository_owner}/${param.repository_name}/branches/${param.branch_name}" + request_headers = { + Authorization = "Bearer ${credential.github[param.cred].token}" + } + error { + ignore = true + } + } + + output "branch" { + value = step.http.get_branch + } + +} \ No newline at end of file diff --git a/pipelines/branch/tests/test_branch_operations.fp b/pipelines/branch/tests/test_branch_operations.fp new file mode 100644 index 0000000..31592c7 --- /dev/null +++ b/pipelines/branch/tests/test_branch_operations.fp @@ -0,0 +1,65 @@ +pipeline "test_branch_operations" { + title = "Test Branch Operations" + description = "Test the create_branch, get_branch, and delete_branch pipelines." + + tags = { + type = "test" + } + + param "cred" { + type = string + description = local.cred_param_description + default = "default" + } + + param "repository_owner" { + type = string + } + + param "repository_name" { + type = string + } + + param "branch_name" { + type = string + } + + step "transform" "args" { + value = { + cred = param.cred + repository_owner = param.repository_owner + repository_name = param.repository_name + branch_name = param.branch_name + } + } + + step "pipeline" "create_branch" { + pipeline = pipeline.create_branch + args = step.transform.args.value + } + + step "pipeline" "get_branch" { + depends_on = [step.pipeline.create_branch] + pipeline = pipeline.get_branch + args = step.transform.args.value + } + + step "pipeline" "delete_branch" { + depends_on = [step.pipeline.get_branch] + pipeline = pipeline.delete_branch + args = step.transform.args.value + } + + output "check_create_branch" { + value = step.pipeline.create_branch.output.branch.status_code == 201 ? "pass" : "fail" + } + + output "check_get_branch" { + value = step.pipeline.get_branch.output.branch.status_code == 200 ? "pass" : "fail" + } + + output "check_delete_branch" { + value = step.pipeline.delete_branch.output.branch.status_code == 204 ? "pass" : "fail" + } + +} \ No newline at end of file