bugfix/#12-import-of-org-not-working #13

Merged
lerentis merged 4 commits from bugfix/#12-import-of-org-not-working into main 2022-08-13 21:02:28 +00:00
8 changed files with 55 additions and 16 deletions

View File

@ -15,13 +15,24 @@ steps:
- push
- pull_request
- tag
- name: build-dev
image: golang:1.18.3-alpine3.16
commands:
- "apk add --update --no-cache make"
- "make build"
when:
event:
- push
resources:
limits:
cpu: 1000
memory: 1024MiB
- name: build
image: goreleaser/goreleaser
commands:
- goreleaser build --snapshot
when:
event:
- push
- pull_request
resources:
limits:

View File

@ -3,7 +3,7 @@ GOFMT_FILES?=$$(find . -name '*.go' |grep -v vendor)
GOFMT ?= gofmt -s
VERSION = 0.7.1
VERSION = 0.7.2
test: fmt-check
go test -i $(TEST) || exit 1

View File

@ -17,7 +17,7 @@ terraform {
required_providers {
gitea = {
source = "Lerentis/gitea"
version = "0.7.1"
version = "0.7.2"
}
}
}

View File

@ -17,7 +17,7 @@ terraform {
required_providers {
gitea = {
source = "Lerentis/gitea"
version = "0.3.0"
version = "0.7.2"
}
}
}

View File

@ -18,7 +18,8 @@ resource "gitea_repository" "mirror" {
}
resource "gitea_org" "test_org" {
name = "test-org"
name = "test-org"
description = "test description"
}
resource "gitea_repository" "org_repo" {

View File

@ -2,7 +2,7 @@ terraform {
required_providers {
gitea = {
source = "terraform.local/lerentis/gitea"
version = "0.7.1"
version = "0.7.2"
}
}
}

View File

@ -2,7 +2,7 @@ terraform {
required_providers {
gitea = {
source = "Lerentis/gitea"
version = "0.3.0"
version = "0.7.2"
}
}
}

View File

@ -2,6 +2,7 @@ package gitea
import (
"fmt"
"strconv"
"code.gitea.io/sdk/gitea"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
@ -17,21 +18,47 @@ const (
RepoAdminChangeTeamAccess string = "repo_admin_change_team_access"
)
// might come in handy if we want to stick to numeric IDs
func searchOrgByClientId(c *gitea.Client, id int64) (res *gitea.Organization, err error) {
page := 1
for {
orgs, _, err := c.AdminListOrgs(gitea.AdminListOrgsOptions{
ListOptions: gitea.ListOptions{
Page: page,
PageSize: 50,
},
})
if err != nil {
return nil, err
}
if len(orgs) == 0 {
return nil, fmt.Errorf("Organisation with ID %d could not be found", id)
}
for _, org := range orgs {
if org.ID == id {
return org, nil
}
}
page += 1
}
}
func resourceOrgRead(d *schema.ResourceData, meta interface{}) (err error) {
client := meta.(*gitea.Client)
var org *gitea.Organization
var resp *gitea.Response
org, resp, err = client.GetOrg(d.Get(orgName).(string))
id, err := strconv.ParseInt(d.Id(), 10, 64)
org, err = searchOrgByClientId(client, id)
if err != nil {
if resp.StatusCode == 404 {
d.SetId("")
return nil
} else {
return err
}
return err
}
err = setOrgResourceData(org, d)
@ -133,7 +160,7 @@ func resourceGiteaOrg() *schema.Resource {
Update: resourceOrgUpdate,
Delete: resourceOrgDelete,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
StateContext: schema.ImportStatePassthroughContext,
},
Schema: map[string]*schema.Schema{
"name": {