added repository resource and moved repo
Some checks failed
continuous-integration/drone/push Build is failing

This commit is contained in:
Tobias Trabelsi 2022-06-04 17:17:08 +02:00
parent 077ffca4ea
commit a79db16011
Signed by: lerentis
GPG Key ID: FF0C2839718CAF2E
17 changed files with 519 additions and 16 deletions

32
.drone.yml Normal file
View File

@ -0,0 +1,32 @@
---
kind: pipeline
type: kubernetes
name: terraform-provider-gitea
platform:
os: linux
arch: amd64
steps:
- name: init
image: golang:1.18.3-alpine3.16
commands:
- "go mod download"
when:
event:
- push
- pull_request
- name: build
image: golang:1.18.3-alpine3.16
commands:
- "make build"
when:
event:
- push
- pull_request
- name: test
image: golang:1.18.3-alpine3.16
commands:
- "make test"
when:
event:
- push
- pull_request

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
.vscode

View File

@ -3,6 +3,8 @@ GOFMT_FILES?=$$(find . -name '*.go' |grep -v vendor)
GOFMT ?= gofmt -s
VERSION = 0.1.0
test: fmt-check
go test -i $(TEST) || exit 1
echo $(TEST) | \
@ -28,3 +30,10 @@ fmt-check:
echo "$${diff}"; \
exit 1; \
fi;
build:
go build -ldflags="-X 'main.Version=${VERSION}'" -o terraform-provider-gitea_${VERSION}
install: build
@echo installing to
@echo ~/.terraform.d/plugins/terraform.local/lerentis/gitea/${VERSION}/linux_amd64/terraform-provider-gitea_${VERSION}
@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}

View File

@ -2,10 +2,7 @@
Terraform Gitea Provider
# Contributing
## History
This is a fork of https://gitea.com/gitea/terraform-provider-gitea. Many thanks for the foundation of this provider
This is a fork of https://gitea.com/gitea/terraform-provider-gitea
Currently implemented build/release automations and available on
terraform registry for convenience. PRs raised here will eventually
be contributed back to the main repo if and when the CI automation
there is ready.

View File

@ -20,10 +20,6 @@ description: |-
- `name` (String)
- `username` (String)
### Optional
- `id` (String) The ID of this resource.
### Read-Only
- `clone_url` (String)
@ -34,6 +30,7 @@ description: |-
- `forks` (Number)
- `full_name` (String)
- `html_url` (String)
- `id` (String) The ID of this resource.
- `mirror` (Boolean)
- `open_issue_count` (Number)
- `permission_admin` (Boolean)

View File

@ -22,8 +22,8 @@ Handling [gitea oauth application](https://docs.gitea.io/en-us/oauth2-provider/)
### Read-Only
- `client_id` (String) OAuth2 client id
- `client_secret` (String, Sensitive) Oauth2 client secret
- `client_id` (String) OAuth2 Application client id
- `client_secret` (String, Sensitive) Oauth2 Application client secret
- `id` (String) The ID of this resource.

View File

@ -0,0 +1,57 @@
---
# generated by https://github.com/hashicorp/terraform-plugin-docs
page_title: "gitea_repository Resource - terraform-provider-gitea"
subcategory: ""
description: |-
Handling Repository resources
---
# gitea_repository (Resource)
Handling Repository resources
<!-- schema generated by tfplugindocs -->
## Schema
### Required
- `name` (String)
- `username` (String)
### Optional
- `allow_manual_merge` (Boolean)
- `allow_merge_commits` (Boolean)
- `allow_rebase` (Boolean)
- `allow_rebase_explicit` (Boolean)
- `allow_squash_merge` (Boolean)
- `archived` (Boolean)
- `auto_init` (Boolean)
- `autodetect_manual_merge` (Boolean)
- `default_branch` (String)
- `description` (String)
- `gitignores` (String)
- `has_issues` (Boolean)
- `has_projects` (Boolean)
- `has_pull_requests` (Boolean)
- `has_wiki` (Boolean)
- `ignore_whitespace_conflicts` (Boolean)
- `issue_labels` (String)
- `license` (String)
- `private` (Boolean)
- `readme` (String)
- `repo_template` (Boolean)
- `website` (String)
### Read-Only
- `created` (String)
- `id` (String) The ID of this resource.
- `permission_admin` (Boolean)
- `permission_pull` (Boolean)
- `permission_push` (Boolean)
- `updated` (String)

4
examples/.gitignore vendored Normal file
View File

@ -0,0 +1,4 @@
.terraform
.terraform.lock.hcl
terraform.tfstate
terraform.tfstate.backup

8
examples/main.tf Normal file
View File

@ -0,0 +1,8 @@
resource "gitea_repository" "test" {
username = "lerentis"
name = "test"
private = true
issue_labels = "Default"
license = "MIT"
gitignores = "Go"
}

13
examples/provider.tf Normal file
View File

@ -0,0 +1,13 @@
terraform {
required_providers {
gitea = {
source = "terraform.local/lerentis/gitea"
version = "0.1.0"
}
}
}
provider "gitea" {
base_url = var.gitea_url
token = var.gitea_token
}

7
examples/variables.tf Normal file
View File

@ -0,0 +1,7 @@
variable "gitea_url" {
default = "http://localhost:3000/"
}
variable "gitea_token" {
}

View File

@ -78,6 +78,7 @@ func Provider() terraform.ResourceProvider {
// "gitea_repo": resourceGiteaRepo(),
// "gitea_user": resourceGiteaUser(),
"gitea_oauth2_app": resourceGiteaOauthApp(),
"gitea_repository": resourceGiteaRepository(),
},
ConfigureFunc: providerConfigure,

View File

@ -0,0 +1,354 @@
package gitea
import (
"fmt"
"strconv"
"code.gitea.io/sdk/gitea"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
)
const (
repoOwner string = "username"
repoName string = "name"
repoDescription string = "description"
repoPrivateFlag string = "private"
repoIssueLabels string = "issue_labels"
repoAutoInit string = "auto_init"
repoTemplate string = "repo_template"
repoGitignores string = "gitignores"
repoLicense string = "license"
repoReadme string = "readme"
repoDefaultBranch string = "default_branch"
repoWebsite string = "website"
repoIssues string = "has_issues"
repoWiki string = "has_wiki"
repoPrs string = "has_pull_requests"
repoProjects string = "has_projects"
repoIgnoreWhitespace string = "ignore_whitespace_conflicts"
repoAllowMerge string = "allow_merge_commits"
repoAllowRebase string = "allow_rebase"
repoAllowRebaseMerge string = "allow_rebase_explicit"
repoAllowSquash string = "allow_squash_merge"
repoAchived string = "archived"
repoAllowManualMerge string = "allow_manual_merge"
repoAutodetectManualMerge string = "autodetect_manual_merge"
)
func resourceRepoRead(d *schema.ResourceData, meta interface{}) (err error) {
client := meta.(*gitea.Client)
id, err := strconv.ParseInt(d.Id(), 10, 64)
if err != nil {
return err
}
repo, _, err := client.GetRepoByID(id)
if err != nil {
return err
}
err = setRepoResourceData(repo, d)
return
}
func resourceRepoCreate(d *schema.ResourceData, meta interface{}) (err error) {
client := meta.(*gitea.Client)
var repo *gitea.Repository
opts := gitea.CreateRepoOption{
Name: d.Get(repoName).(string),
Description: d.Get(repoDescription).(string),
Private: d.Get(repoPrivateFlag).(bool),
IssueLabels: d.Get(repoIssueLabels).(string),
AutoInit: d.Get(repoAutoInit).(bool),
Template: d.Get(repoTemplate).(bool),
Gitignores: d.Get(repoGitignores).(string),
License: d.Get(repoLicense).(string),
Readme: d.Get(repoReadme).(string),
DefaultBranch: d.Get(repoDefaultBranch).(string),
TrustModel: "default",
}
repo, _, err = client.CreateRepo(opts)
if err != nil {
return
}
err = setRepoResourceData(repo, d)
return
}
func resourceRepoUpdate(d *schema.ResourceData, meta interface{}) (err error) {
client := meta.(*gitea.Client)
var repo *gitea.Repository
var name string = d.Get(repoName).(string)
var description string = d.Get(repoDescription).(string)
var website string = d.Get(repoDescription).(string)
var private bool = d.Get(repoPrivateFlag).(bool)
var template bool = d.Get(repoTemplate).(bool)
var hasIssues bool = d.Get(repoIssues).(bool)
var hasWiki bool = d.Get(repoWiki).(bool)
var defaultBranch string = d.Get(repoDefaultBranch).(string)
var hasPRs bool = d.Get(repoPrs).(bool)
var hasProjects bool = d.Get(repoProjects).(bool)
var ignoreWhitespaceConflicts bool = d.Get(repoIgnoreWhitespace).(bool)
var allowMerge bool = d.Get(repoAllowMerge).(bool)
var allowRebase bool = d.Get(repoAllowRebase).(bool)
var allowRebaseMerge bool = d.Get(repoAllowRebaseMerge).(bool)
var allowSquash bool = d.Get(repoAllowSquash).(bool)
var archived bool = d.Get(repoAchived).(bool)
var allowManualMerge bool = d.Get(repoAllowManualMerge).(bool)
var autodetectManualMerge bool = d.Get(repoAutodetectManualMerge).(bool)
opts := gitea.EditRepoOption{
Name: &name,
Description: &description,
Website: &website,
Private: &private,
Template: &template,
HasIssues: &hasIssues,
HasWiki: &hasWiki,
DefaultBranch: &defaultBranch,
HasPullRequests: &hasPRs,
HasProjects: &hasProjects,
IgnoreWhitespaceConflicts: &ignoreWhitespaceConflicts,
AllowMerge: &allowMerge,
AllowRebase: &allowRebase,
AllowRebaseMerge: &allowRebaseMerge,
AllowSquash: &allowSquash,
Archived: &archived,
AllowManualMerge: &allowManualMerge,
AutodetectManualMerge: &autodetectManualMerge,
}
repo, _, err = client.EditRepo(d.Get(repoOwner).(string), d.Get(repoName).(string), opts)
if err != nil {
return err
}
err = setRepoResourceData(repo, d)
return
}
func respurceRepoDelete(d *schema.ResourceData, meta interface{}) (err error) {
client := meta.(*gitea.Client)
client.DeleteRepo(d.Get(repoOwner).(string), d.Get(repoName).(string))
return
}
func setRepoResourceData(repo *gitea.Repository, d *schema.ResourceData) (err error) {
d.SetId(fmt.Sprintf("%d", repo.ID))
d.Set("name", repo.Name)
d.Set("description", repo.Description)
d.Set("full_name", repo.FullName)
d.Set("description", repo.Description)
d.Set("private", repo.Private)
d.Set("fork", repo.Fork)
d.Set("mirror", repo.Mirror)
d.Set("size", repo.Size)
d.Set("html_url", repo.HTMLURL)
d.Set("ssh_url", repo.SSHURL)
d.Set("clone_url", repo.CloneURL)
d.Set("website", repo.Website)
d.Set("stars", repo.Stars)
d.Set("forks", repo.Forks)
d.Set("watchers", repo.Watchers)
d.Set("open_issue_count", repo.OpenIssues)
d.Set("default_branch", repo.DefaultBranch)
d.Set("created", repo.Created)
d.Set("updated", repo.Updated)
d.Set("permission_admin", repo.Permissions.Admin)
d.Set("permission_push", repo.Permissions.Push)
d.Set("permission_pull", repo.Permissions.Pull)
return
}
func resourceGiteaRepository() *schema.Resource {
return &schema.Resource{
Read: resourceRepoRead,
Create: resourceRepoCreate,
Update: resourceRepoUpdate,
Delete: respurceRepoDelete,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},
Schema: map[string]*schema.Schema{
"username": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"auto_init": {
Type: schema.TypeBool,
Required: false,
Optional: true,
Default: true,
},
"repo_template": {
Type: schema.TypeBool,
Required: false,
Optional: true,
Default: false,
},
"issue_labels": {
Type: schema.TypeString,
Required: false,
Optional: true,
Default: "Default",
},
"gitignores": {
Type: schema.TypeString,
Required: false,
Optional: true,
Default: "",
},
"license": {
Type: schema.TypeString,
Required: false,
Optional: true,
Default: "",
},
"readme": {
Type: schema.TypeString,
Required: false,
Optional: true,
Default: "",
},
"description": {
Type: schema.TypeString,
Required: false,
Optional: true,
Default: "",
},
"private": {
Type: schema.TypeBool,
Required: false,
Optional: true,
Default: true,
},
"default_branch": {
Type: schema.TypeString,
Required: false,
Optional: true,
Default: "main",
},
"created": {
Type: schema.TypeString,
Computed: true,
},
"updated": {
Type: schema.TypeString,
Computed: true,
},
"permission_admin": {
Type: schema.TypeBool,
Computed: true,
},
"permission_push": {
Type: schema.TypeBool,
Computed: true,
},
"permission_pull": {
Type: schema.TypeBool,
Computed: true,
},
"website": {
Type: schema.TypeString,
Required: false,
Optional: true,
Default: "",
},
"has_issues": {
Type: schema.TypeBool,
Required: false,
Optional: true,
Default: true,
},
"has_wiki": {
Type: schema.TypeBool,
Required: false,
Optional: true,
Default: true,
},
"has_pull_requests": {
Type: schema.TypeBool,
Required: false,
Optional: true,
Default: true,
},
"has_projects": {
Type: schema.TypeBool,
Required: false,
Optional: true,
Default: true,
},
"ignore_whitespace_conflicts": {
Type: schema.TypeBool,
Required: false,
Optional: true,
Default: true,
},
"allow_merge_commits": {
Type: schema.TypeBool,
Required: false,
Optional: true,
Default: true,
},
"allow_rebase": {
Type: schema.TypeBool,
Required: false,
Optional: true,
Default: true,
},
"allow_rebase_explicit": {
Type: schema.TypeBool,
Required: false,
Optional: true,
Default: true,
},
"allow_squash_merge": {
Type: schema.TypeBool,
Required: false,
Optional: true,
Default: true,
},
"archived": {
Type: schema.TypeBool,
Required: false,
Optional: true,
Default: false,
},
"allow_manual_merge": {
Type: schema.TypeBool,
Required: false,
Optional: true,
Default: true,
},
"autodetect_manual_merge": {
Type: schema.TypeBool,
Required: false,
Optional: true,
Default: true,
},
},
Description: "Handling Repository resources",
}
}

4
go.mod
View File

@ -1,6 +1,6 @@
module code.gitea.io/terraform-provider-gitea
module git.uploadfilter24.eu/terraform-provider-gitea
go 1.17
go 1.18
require (
code.gitea.io/sdk/gitea v0.15.1

1
go.sum
View File

@ -261,7 +261,6 @@ github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/
github.com/ulikunitz/xz v0.5.5 h1:pFrO0lVpTBXLpYw+pnLj6TbvHuyjXMfjGeCwSqCVwok=
github.com/ulikunitz/xz v0.5.5/go.mod h1:2bypXElzHzzJZwzH67Y6wb67pO62Rzfn7BSiF4ABRW8=
github.com/vmihailenco/msgpack v3.3.3+incompatible/go.mod h1:fy3FlTQTDXWkZ7Bh6AcGMlsjHatGryHQYUTf1ShIgkk=
github.com/vmihailenco/msgpack v4.0.1+incompatible h1:RMF1enSPeKTlXrXdOcqjFUElywVZjjC6pqse21bKbEU=
github.com/vmihailenco/msgpack v4.0.1+incompatible/go.mod h1:fy3FlTQTDXWkZ7Bh6AcGMlsjHatGryHQYUTf1ShIgkk=
github.com/vmihailenco/msgpack/v4 v4.3.12 h1:07s4sz9IReOgdikxLTKNbBdqDMLsjPKXwvCazn8G65U=
github.com/vmihailenco/msgpack/v4 v4.3.12/go.mod h1:gborTTJjAo/GWTqqRjrLCn9pgNN+NXzzngzBKDPIqw4=

View File

@ -1,10 +1,12 @@
package main // import "src.techknowlogick.com/terraform-provider-gitea"
import (
"code.gitea.io/terraform-provider-gitea/gitea"
"git.uploadfilter24.eu/terraform-provider-gitea/gitea"
"github.com/hashicorp/terraform-plugin-sdk/plugin"
)
var Version = "development"
func main() {
plugin.Serve(&plugin.ServeOpts{
ProviderFunc: gitea.Provider})

View File

@ -0,0 +1,22 @@
version: "3"
networks:
gitea:
external: false
services:
server:
image: gitea/gitea:1.16.8
container_name: gitea
environment:
- USER_UID=1000
- USER_GID=1000
restart: always
networks:
- gitea
volumes:
- /etc/timezone:/etc/timezone:ro
- /etc/localtime:/etc/localtime:ro
ports:
- "3000:3000"
- "222:22"