From 480994bfa9188e64268b52723ecc6763f445f75e Mon Sep 17 00:00:00 2001 From: Judd Wilcox Date: Sat, 1 Oct 2022 06:56:10 -0500 Subject: [PATCH] implement gitea_fork --- Makefile | 4 +- README.md | 2 +- docs/resources/fork.md | 31 ++++++++++ gitea/provider.go | 7 ++- gitea/resource_gitea_fork.go | 107 +++++++++++++++++++++++++++++++++++ 5 files changed, 145 insertions(+), 6 deletions(-) create mode 100644 docs/resources/fork.md create mode 100644 gitea/resource_gitea_fork.go diff --git a/Makefile b/Makefile index 621ddb1..c867723 100644 --- a/Makefile +++ b/Makefile @@ -3,7 +3,7 @@ GOFMT_FILES?=$$(find . -name '*.go' |grep -v vendor) GOFMT ?= gofmt -s -VERSION = 0.8.0 +VERSION = 0.9.0 test: fmt-check go test -i $(TEST) || exit 1 @@ -38,4 +38,4 @@ install: build @mkdir -p ~/.terraform.d/plugins/terraform.local/lerentis/gitea/${VERSION}/linux_amd64 @mv terraform-provider-gitea_${VERSION} ~/.terraform.d/plugins/terraform.local/lerentis/gitea/${VERSION}/linux_amd64/terraform-provider-gitea_${VERSION} doc: - tfplugindocs \ No newline at end of file + tfplugindocs diff --git a/README.md b/README.md index 610582a..8ab4365 100644 --- a/README.md +++ b/README.md @@ -17,7 +17,7 @@ terraform { required_providers { gitea = { source = "Lerentis/gitea" - version = "0.8.0" + version = "0.9.0" } } } diff --git a/docs/resources/fork.md b/docs/resources/fork.md new file mode 100644 index 0000000..e96181f --- /dev/null +++ b/docs/resources/fork.md @@ -0,0 +1,31 @@ +--- +# generated by https://github.com/hashicorp/terraform-plugin-docs +page_title: "gitea_fork Resource - terraform-provider-gitea" +subcategory: "" +description: |- + gitea_fork manages repository fork +--- + +# gitea_fork (Resource) + +`gitea_fork` manages repository fork + + + + +## Schema + +### Required + +- `owner` (String) The owner or owning organization of the repository to fork +- `repo` (String) The name of the repository to fork + +### Optional + +- `organization` (String) The organization that owns the forked repo + +### Read-Only + +- `id` (String) The ID of this resource. + + diff --git a/gitea/provider.go b/gitea/provider.go index 6c5c806..7a9152d 100644 --- a/gitea/provider.go +++ b/gitea/provider.go @@ -72,12 +72,13 @@ func Provider() *schema.Provider { }, ResourcesMap: map[string]*schema.Resource{ - "gitea_org": resourceGiteaOrg(), - // "gitea_team": resourceGiteaTeam(), - // "gitea_repo": resourceGiteaRepo(), + "gitea_org": resourceGiteaOrg(), + // "gitea_team": resourceGiteaTeam(), + // "gitea_repo": resourceGiteaRepo(), "gitea_user": resourceGiteaUser(), "gitea_oauth2_app": resourceGiteaOauthApp(), "gitea_repository": resourceGiteaRepository(), + "gitea_fork": resourceGiteaFork(), "gitea_public_key": resourceGiteaPublicKey(), "gitea_team": resourceGiteaTeam(), "gitea_git_hook": resourceGiteaGitHook(), diff --git a/gitea/resource_gitea_fork.go b/gitea/resource_gitea_fork.go new file mode 100644 index 0000000..91b5f96 --- /dev/null +++ b/gitea/resource_gitea_fork.go @@ -0,0 +1,107 @@ +package gitea + +import ( + "fmt" + "strconv" + "code.gitea.io/sdk/gitea" + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" +) + +const ( + forkOwner string = "owner" + forkRepo string = "repo" + forkOrganization string = "organization" +) + +func resourceForkCreate(d *schema.ResourceData, meta interface{}) (err error) { + client := meta.(*gitea.Client) + + var opts gitea.CreateForkOption + var org string + org = d.Get(forkOrganization).(string) + if org != "" { + opts.Organization = &org + } + + repo, _, err := client.CreateFork(d.Get(forkOwner).(string), + d.Get(forkRepo).(string), + opts) + if err == nil { + err = setForkResourceData(repo, d) + } + return err +} + +func resourceForkRead(d *schema.ResourceData, meta interface{}) (err error) { + client := meta.(*gitea.Client) + + id, err := strconv.ParseInt(d.Id(), 10, 64) + var resp *gitea.Response + + if err != nil { + return err + } + + repo, resp, err := client.GetRepoByID(id) + + if err != nil { + if resp.StatusCode == 404 { + d.SetId("") + return nil + } else { + return err + } + } + + err = setForkResourceData(repo, d) + + return +} + +func resourceForkDelete(d *schema.ResourceData, meta interface{}) (err error) { + client := meta.(*gitea.Client) + + client.DeleteRepo(d.Get(forkOrganization).(string), d.Get(forkRepo).(string)) + + return +} + +func setForkResourceData(repo *gitea.Repository, d *schema.ResourceData) (err error) { + + d.SetId(fmt.Sprintf("%d", repo.ID)) + + return +} + +func resourceGiteaFork() *schema.Resource { + return &schema.Resource{ + Read: resourceForkRead, + Create: resourceForkCreate, + Delete: resourceForkDelete, + Importer: &schema.ResourceImporter{ + StateContext: schema.ImportStatePassthroughContext, + }, + Schema: map[string]*schema.Schema{ + "owner": { + Type: schema.TypeString, + Required: true, + ForceNew: true, + Description: "The owner or owning organization of the repository to fork", + }, + "repo": { + Type: schema.TypeString, + Required: true, + ForceNew: true, + Description: "The name of the repository to fork", + }, + "organization": { + Type: schema.TypeString, + Required: false, + Optional: true, + ForceNew: true, + Description: "The organization that owns the forked repo", + }, + }, + Description: "`gitea_fork` manages repository fork", + } +}