From 0a85957c5027d75f8e4d4c26b852d7aadb492acb Mon Sep 17 00:00:00 2001 From: Tobias Trabelsi Date: Fri, 21 Apr 2023 13:23:18 +0200 Subject: [PATCH] #26 document codeberg not allowing pull mirrors fixed permission failure in gite_repository --- Makefile | 10 ++++++---- README.md | 2 +- docs/index.md | 2 +- docs/resources/repository.md | 2 ++ examples/provider.tf | 2 +- examples/provider/provider.tf | 2 +- gitea/resource_gitea_repository.go | 23 +++++++++++++++++++---- go.mod | 2 +- 8 files changed, 32 insertions(+), 13 deletions(-) diff --git a/Makefile b/Makefile index e19effd..a876a51 100644 --- a/Makefile +++ b/Makefile @@ -1,9 +1,11 @@ TEST?=./gitea GOFMT_FILES?=$$(find . -name '*.go' |grep -v vendor) +ARCH?=$$(uname -m) +KERNEL?=$$(uname -s | tr '[:upper:]' '[:lower:]') GOFMT ?= gofmt -s -VERSION = 0.12.2 +VERSION = 0.12.3 test: fmt-check go test -i $(TEST) || exit 1 @@ -34,8 +36,8 @@ 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} + @echo ~/.terraform.d/plugins/terraform.local/lerentis/gitea/${VERSION}/${KERNEL}_${ARCH}/terraform-provider-gitea_${VERSION} + @mkdir -p ~/.terraform.d/plugins/terraform.local/lerentis/gitea/${VERSION}/${KERNEL}_${ARCH} + @mv terraform-provider-gitea_${VERSION} ~/.terraform.d/plugins/terraform.local/lerentis/gitea/${VERSION}/${KERNEL}_${ARCH}/terraform-provider-gitea_${VERSION} doc: tfplugindocs diff --git a/README.md b/README.md index e3a0093..2a5bffe 100644 --- a/README.md +++ b/README.md @@ -17,7 +17,7 @@ terraform { required_providers { gitea = { source = "Lerentis/gitea" - version = "0.12.2" + version = "0.12.3" } } } diff --git a/docs/index.md b/docs/index.md index 1cb7e27..419f534 100644 --- a/docs/index.md +++ b/docs/index.md @@ -17,7 +17,7 @@ terraform { required_providers { gitea = { source = "Lerentis/gitea" - version = "0.12.2" + version = "0.12.3" } } } diff --git a/docs/resources/repository.md b/docs/resources/repository.md index 86442d3..559c018 100644 --- a/docs/resources/repository.md +++ b/docs/resources/repository.md @@ -7,6 +7,7 @@ description: |- Per default this repository will be initializiled with the provided configuration (gitignore, License etc.). If the username property is set to a organisation name, the provider will try to look if this organisation exists and create the repository under the organisation scope. Repository migrations have some properties that are not available to regular repositories. These are all prefixed with migration_. + Codeberg.org does currently not allow mirrors to be created. See FAQ Section of CodeBerg for more information: https://docs.codeberg.org/getting-started/faq/#why-am-i-not-allowed-to-set-up-an-automatic-mirror --- # gitea_repository (Resource) @@ -17,6 +18,7 @@ Per default this repository will be initializiled with the provided configuratio If the `username` property is set to a organisation name, the provider will try to look if this organisation exists and create the repository under the organisation scope. Repository migrations have some properties that are not available to regular repositories. These are all prefixed with `migration_`. +Codeberg.org does currently not allow mirrors to be created. See FAQ Section of CodeBerg for more information: https://docs.codeberg.org/getting-started/faq/#why-am-i-not-allowed-to-set-up-an-automatic-mirror ## Example Usage diff --git a/examples/provider.tf b/examples/provider.tf index b89c520..1c7dae3 100644 --- a/examples/provider.tf +++ b/examples/provider.tf @@ -2,7 +2,7 @@ terraform { required_providers { gitea = { source = "terraform.local/lerentis/gitea" - version = "0.12.2" + version = "0.12.3" } } } diff --git a/examples/provider/provider.tf b/examples/provider/provider.tf index a423efb..8362174 100644 --- a/examples/provider/provider.tf +++ b/examples/provider/provider.tf @@ -2,7 +2,7 @@ terraform { required_providers { gitea = { source = "Lerentis/gitea" - version = "0.12.2" + version = "0.12.3" } } } diff --git a/gitea/resource_gitea_repository.go b/gitea/resource_gitea_repository.go index 258c8b2..01f0212 100644 --- a/gitea/resource_gitea_repository.go +++ b/gitea/resource_gitea_repository.go @@ -1,11 +1,14 @@ package gitea import ( + "context" "errors" "fmt" "strconv" + "strings" "code.gitea.io/sdk/gitea" + "github.com/hashicorp/terraform-plugin-log/tflog" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" ) @@ -108,14 +111,20 @@ func resourceRepoCreate(d *schema.ResourceData, meta interface{}) (err error) { var repo *gitea.Repository var resp *gitea.Response - var orgRepo bool + var orgRepo, hasAdmin bool _, resp, err = client.GetOrg(d.Get(repoOwner).(string)) if resp.StatusCode == 404 { _, err := searchUserByName(client, d.Get(repoOwner).(string)) if err != nil { - return errors.New(fmt.Sprintf("Creation of repository cound not proceed as owner %s is not present in gitea", d.Get(repoOwner).(string))) + if strings.Contains(err.Error(), "could not be found") { + return errors.New(fmt.Sprintf("Creation of repository cound not proceed as owner %s is not present in gitea", d.Get(repoOwner).(string))) + } + tflog.Warn(context.Background(), "Error query for users. Assuming missing permissions and proceding with user permissions") + hasAdmin = false + } else { + hasAdmin = true } orgRepo = false } else { @@ -180,7 +189,11 @@ func resourceRepoCreate(d *schema.ResourceData, meta interface{}) (err error) { if orgRepo { repo, _, err = client.CreateOrgRepo(d.Get(repoOwner).(string), opts) } else { - repo, _, err = client.AdminCreateRepo(d.Get(repoOwner).(string), opts) + if hasAdmin { + repo, _, err = client.AdminCreateRepo(d.Get(repoOwner).(string), opts) + } else { + repo, _, err = client.CreateRepo(opts) + } } } @@ -580,6 +593,8 @@ func resourceGiteaRepository() *schema.Resource { "Per default this repository will be initializiled with the provided configuration (gitignore, License etc.).\n" + "If the `username` property is set to a organisation name, the provider will try to look if this organisation exists " + "and create the repository under the organisation scope.\n\n" + - "Repository migrations have some properties that are not available to regular repositories. These are all prefixed with `migration_`.", + "Repository migrations have some properties that are not available to regular repositories. These are all prefixed with `migration_`.\n" + + "Codeberg.org does currently not allow mirrors to be created. See FAQ Section of CodeBerg for more information: " + + "https://docs.codeberg.org/getting-started/faq/#why-am-i-not-allowed-to-set-up-an-automatic-mirror", } } diff --git a/go.mod b/go.mod index fa7eddf..4be3042 100644 --- a/go.mod +++ b/go.mod @@ -5,6 +5,7 @@ go 1.18 require ( code.gitea.io/sdk/gitea v0.15.1 github.com/hashicorp/terraform-plugin-docs v0.14.1 + github.com/hashicorp/terraform-plugin-log v0.8.0 github.com/hashicorp/terraform-plugin-sdk/v2 v2.26.1 ) @@ -35,7 +36,6 @@ require ( github.com/hashicorp/terraform-exec v0.18.1 // indirect github.com/hashicorp/terraform-json v0.16.0 // indirect github.com/hashicorp/terraform-plugin-go v0.14.3 // indirect - github.com/hashicorp/terraform-plugin-log v0.8.0 // indirect github.com/hashicorp/terraform-registry-address v0.1.0 // indirect github.com/hashicorp/terraform-svchost v0.0.0-20200729002733-f050f53b9734 // indirect github.com/hashicorp/yamux v0.0.0-20181012175058-2f1d1f20f75d // indirect