Merge pull request #8 from juddowilcox/implement-forks
Some checks reported errors
continuous-integration/drone/push Build encountered an error
Some checks reported errors
continuous-integration/drone/push Build encountered an error
Co-authored-by: Judd Wilcox <Judd.Wilcox@nokia.com>
This commit is contained in:
commit
f3c0793a88
2
Makefile
2
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
|
||||
|
@ -17,7 +17,7 @@ terraform {
|
||||
required_providers {
|
||||
gitea = {
|
||||
source = "Lerentis/gitea"
|
||||
version = "0.8.0"
|
||||
version = "0.9.0"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
31
docs/resources/fork.md
Normal file
31
docs/resources/fork.md
Normal file
@ -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 generated by tfplugindocs -->
|
||||
## 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.
|
||||
|
||||
|
23
examples/resources/gitea_fork/resource.tf
Normal file
23
examples/resources/gitea_fork/resource.tf
Normal file
@ -0,0 +1,23 @@
|
||||
resource "gitea_org" "org1" {
|
||||
name = "org1"
|
||||
}
|
||||
|
||||
resource "gitea_org" "org2" {
|
||||
name = "org2"
|
||||
}
|
||||
|
||||
resource "gitea_repository" "repo1_in_org1" {
|
||||
username = gitea_org.org1.name
|
||||
name = "repo1-in-org1"
|
||||
}
|
||||
|
||||
resource "gitea_fork" "user_fork_of_repo1_in_org1" {
|
||||
owner = gitea_org.org1.name
|
||||
repo = gitea_repository.repo1_in_org1.name
|
||||
}
|
||||
|
||||
resource "gitea_fork" "org2_fork_of_repo1_in_org1" {
|
||||
owner = gitea_org.org1.name
|
||||
repo = gitea_repository.repo1_in_org1.name
|
||||
organization = gitea_org.org2.name
|
||||
}
|
@ -78,6 +78,7 @@ func Provider() *schema.Provider {
|
||||
"gitea_user": resourceGiteaUser(),
|
||||
"gitea_oauth2_app": resourceGiteaOauthApp(),
|
||||
"gitea_repository": resourceGiteaRepository(),
|
||||
"gitea_fork": resourceGiteaFork(),
|
||||
"gitea_public_key": resourceGiteaPublicKey(),
|
||||
"gitea_team": resourceGiteaTeam(),
|
||||
"gitea_git_hook": resourceGiteaGitHook(),
|
||||
|
107
gitea/resource_gitea_fork.go
Normal file
107
gitea/resource_gitea_fork.go
Normal file
@ -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",
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user