From 97994163528935d3c44aebcf6be45d86bfcbaa67 Mon Sep 17 00:00:00 2001 From: Tobias Trabelsi Date: Sat, 28 Jan 2023 22:49:54 +0100 Subject: [PATCH] fixed org vanashing fixed regression of v0.11.1 --- Makefile | 2 +- README.md | 2 +- docs/index.md | 2 +- examples/main.tf | 20 +++++++++++- examples/provider.tf | 2 +- examples/provider/provider.tf | 2 +- gitea/resource_gitea_organisation.go | 3 +- gitea/resource_gitea_repository.go | 49 +++++++++++++++++++++++++++- 8 files changed, 74 insertions(+), 8 deletions(-) diff --git a/Makefile b/Makefile index ff74bdc..6f3fb75 100644 --- a/Makefile +++ b/Makefile @@ -3,7 +3,7 @@ GOFMT_FILES?=$$(find . -name '*.go' |grep -v vendor) GOFMT ?= gofmt -s -VERSION = 0.12.0 +VERSION = 0.12.1 test: fmt-check go test -i $(TEST) || exit 1 diff --git a/README.md b/README.md index c51c1ef..981b732 100644 --- a/README.md +++ b/README.md @@ -17,7 +17,7 @@ terraform { required_providers { gitea = { source = "Lerentis/gitea" - version = "0.12.0" + version = "0.12.1" } } } diff --git a/docs/index.md b/docs/index.md index 40087d4..4b94117 100644 --- a/docs/index.md +++ b/docs/index.md @@ -17,7 +17,7 @@ terraform { required_providers { gitea = { source = "Lerentis/gitea" - version = "0.11.1" + version = "0.11.2" } } } diff --git a/examples/main.tf b/examples/main.tf index 2cefae2..87a0af3 100644 --- a/examples/main.tf +++ b/examples/main.tf @@ -101,7 +101,25 @@ resource "gitea_token" "test_token" { name = "test-token" } +resource "gitea_repository" "test_existing_user" { + username = "testuser2" + name = "testExistingUser" + private = true + issue_labels = "Default" + license = "MIT" + gitignores = "Go" +} + +//resource "gitea_repository" "test_bs_user" { +// username = "manualTest" +// name = "testBullshitUser" +// private = true +// issue_labels = "Default" +// license = "MIT" +// gitignores = "Go" +//} + output "token" { value = resource.gitea_token.test_token.token sensitive = true -} \ No newline at end of file +} diff --git a/examples/provider.tf b/examples/provider.tf index c26374e..c3e5fe7 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.0" + version = "0.12.1" } } } diff --git a/examples/provider/provider.tf b/examples/provider/provider.tf index 8b26bc4..2a90246 100644 --- a/examples/provider/provider.tf +++ b/examples/provider/provider.tf @@ -2,7 +2,7 @@ terraform { required_providers { gitea = { source = "Lerentis/gitea" - version = "0.11.1" + version = "0.11.2" } } } diff --git a/gitea/resource_gitea_organisation.go b/gitea/resource_gitea_organisation.go index f8b941d..ba1552b 100644 --- a/gitea/resource_gitea_organisation.go +++ b/gitea/resource_gitea_organisation.go @@ -58,7 +58,8 @@ func resourceOrgRead(d *schema.ResourceData, meta interface{}) (err error) { org, err = searchOrgByClientId(client, id) if err != nil { - return err + d.SetId("") + return nil } err = setOrgResourceData(org, d) diff --git a/gitea/resource_gitea_repository.go b/gitea/resource_gitea_repository.go index 7a0220a..258c8b2 100644 --- a/gitea/resource_gitea_repository.go +++ b/gitea/resource_gitea_repository.go @@ -1,6 +1,7 @@ package gitea import ( + "errors" "fmt" "strconv" @@ -48,6 +49,34 @@ const ( migrationLFSEndpoint string = "migration_lfs_endpoint" ) +func searchUserByName(c *gitea.Client, name string) (res *gitea.User, err error) { + page := 1 + + for { + users, _, err := c.AdminListUsers(gitea.AdminListUsersOptions{ + ListOptions: gitea.ListOptions{ + Page: page, + PageSize: 50, + }, + }) + if err != nil { + return nil, err + } + + if len(users) == 0 { + return nil, fmt.Errorf("User with name %s could not be found", name) + } + + for _, user := range users { + if user.UserName == name { + return user, nil + } + } + + page += 1 + } +} + func resourceRepoRead(d *schema.ResourceData, meta interface{}) (err error) { client := meta.(*gitea.Client) @@ -78,6 +107,20 @@ func resourceRepoCreate(d *schema.ResourceData, meta interface{}) (err error) { client := meta.(*gitea.Client) var repo *gitea.Repository + var resp *gitea.Response + var orgRepo 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))) + } + orgRepo = false + } else { + orgRepo = true + } if (d.Get(repoMirror)).(bool) { @@ -134,7 +177,11 @@ func resourceRepoCreate(d *schema.ResourceData, meta interface{}) (err error) { TrustModel: "default", } - repo, _, err = client.CreateOrgRepo(d.Get(repoOwner).(string), opts) + if orgRepo { + repo, _, err = client.CreateOrgRepo(d.Get(repoOwner).(string), opts) + } else { + repo, _, err = client.AdminCreateRepo(d.Get(repoOwner).(string), opts) + } } if err != nil {