8 Commits

Author SHA1 Message Date
bc28fe186f Merge pull request #37 from yamadayutaka/feature/team_repositories
Some checks failed
continuous-integration/drone/push Build is passing
continuous-integration/drone/tag Build is failing
implement repositories attribute to gitea_team
2023-05-17 09:48:10 +02:00
19062e27f0 transparent errors
Some checks reported errors
continuous-integration/drone/push Build encountered an error
2023-05-17 09:34:11 +02:00
322ad2d3c4 add log output on error 2023-05-17 00:48:25 +00:00
9e34636c29 Bump the version from 0.12.3 to 0.13.0 2023-05-16 07:58:50 +00:00
b4b13dc3ca implement repositories attribute of gitea_team 2023-05-15 03:22:29 +00:00
e5a6132b9d Merge pull request #34 from Lerentis/dependabot/github_actions/crazy-max/ghaction-import-gpg-5.3.0
All checks were successful
continuous-integration/drone/push Build is passing
2023-05-10 10:46:32 +02:00
d71cbbd0b6 Bump crazy-max/ghaction-import-gpg from 5.2.0 to 5.3.0
Bumps [crazy-max/ghaction-import-gpg](https://github.com/crazy-max/ghaction-import-gpg) from 5.2.0 to 5.3.0.
- [Release notes](https://github.com/crazy-max/ghaction-import-gpg/releases)
- [Commits](https://github.com/crazy-max/ghaction-import-gpg/compare/v5.2.0...v5.3.0)

---
updated-dependencies:
- dependency-name: crazy-max/ghaction-import-gpg
  dependency-type: direct:production
  update-type: version-update:semver-minor
...

Signed-off-by: dependabot[bot] <support@github.com>
2023-05-08 21:01:02 +00:00
0a85957c50 #26 document codeberg not allowing pull mirrors
Some checks are pending
continuous-integration/drone/tag Build is running
continuous-integration/drone/push Build is passing
fixed permission failure in gite_repository
2023-04-21 13:23:18 +02:00
12 changed files with 162 additions and 15 deletions

View File

@ -28,7 +28,7 @@ jobs:
go-version: 1.18
- name: Import GPG key
id: import_gpg
uses: crazy-max/ghaction-import-gpg@v5.2.0
uses: crazy-max/ghaction-import-gpg@v5.3.0
with:
gpg_private_key: ${{ secrets.GPG_PRIVATE_KEY }}
passphrase: ${{ secrets.PASSPHRASE }}

View File

@ -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.13.0
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

View File

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

View File

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

View File

@ -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

View File

@ -34,6 +34,26 @@ resource "gitea_team" "test_team" {
permission = "write"
members = [gitea_user.test.username]
}
resource "gitea_repository" "test" {
username = gitea_org.test_org.name
name = "test"
private = true
issue_labels = "Default"
license = "MIT"
gitignores = "Go"
}
resource "gitea_team" "test_team_restricted" {
name = "Restricted Devs"
organisation = gitea_org.test_org.name
description = "Restricted Devs of Test Org"
permission = "write"
members = [gitea_user.test.username]
include_all_repositories = false
repositories = [gitea_repository.test.name]
}
```
<!-- schema generated by tfplugindocs -->
@ -52,6 +72,7 @@ resource "gitea_team" "test_team" {
- `members` (List of String) List of Users that should be part of this team
- `permission` (String) Permissions associated with this Team
Can be `none`, `read`, `write`, `admin` or `owner`
- `repositories` (List of String) List of Repositories that should be part of this team
- `units` (String) List of types of Repositories that should be allowed to be created from Team members.
Can be `repo.code`, `repo.issues`, `repo.ext_issues`, `repo.wiki`, `repo.pulls`, `repo.releases`, `repo.projects` and/or `repo.ext_wiki`

View File

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

View File

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

View File

@ -19,3 +19,23 @@ resource "gitea_team" "test_team" {
permission = "write"
members = [gitea_user.test.username]
}
resource "gitea_repository" "test" {
username = gitea_org.test_org.name
name = "test"
private = true
issue_labels = "Default"
license = "MIT"
gitignores = "Go"
}
resource "gitea_team" "test_team_restricted" {
name = "Restricted Devs"
organisation = gitea_org.test_org.name
description = "Restricted Devs of Test Org"
permission = "write"
members = [gitea_user.test.username]
include_all_repositories = false
repositories = [gitea_repository.test.name]
}

View File

@ -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,15 +111,21 @@ 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 {
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 {
orgRepo = true
@ -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 {
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",
}
}

View File

@ -1,6 +1,7 @@
package gitea
import (
"errors"
"fmt"
"strconv"
"strings"
@ -18,6 +19,7 @@ const (
TeamIncludeAllReposFlag string = "include_all_repositories"
TeamUnits string = "units"
TeamMembers string = "members"
TeamRepositories string = "repositories"
)
func resourceTeamRead(d *schema.ResourceData, meta interface{}) (err error) {
@ -75,12 +77,14 @@ func resourceTeamCreate(d *schema.ResourceData, meta interface{}) (err error) {
units = append(units, gitea.RepoUnitProjects)
}
includeAllRepos := d.Get(TeamIncludeAllReposFlag).(bool)
opts := gitea.CreateTeamOption{
Name: d.Get(TeamName).(string),
Description: d.Get(TeamDescription).(string),
Permission: gitea.AccessMode(d.Get(TeamPermissions).(string)),
CanCreateOrgRepo: d.Get(TeamCreateRepoFlag).(bool),
IncludesAllRepositories: d.Get(TeamIncludeAllReposFlag).(bool),
IncludesAllRepositories: includeAllRepos,
Units: units,
}
@ -101,6 +105,13 @@ func resourceTeamCreate(d *schema.ResourceData, meta interface{}) (err error) {
}
}
if !includeAllRepos {
err = setTeamRepositories(team, d, meta, false)
if err != nil {
return err
}
}
err = setTeamResourceData(team, d)
return
@ -181,6 +192,13 @@ func resourceTeamUpdate(d *schema.ResourceData, meta interface{}) (err error) {
}
}
if !includeAllRepos {
err = setTeamRepositories(team, d, meta, true)
if err != nil {
return err
}
}
team, _, _ = client.GetTeam(id)
err = setTeamResourceData(team, d)
@ -218,6 +236,7 @@ func setTeamResourceData(team *gitea.Team, d *schema.ResourceData) (err error) {
d.Set(TeamUnits, d.Get(TeamUnits).(string))
d.Set(TeamOrg, d.Get(TeamOrg).(string))
d.Set(TeamMembers, d.Get(TeamMembers))
d.Set(TeamRepositories, d.Get(TeamRepositories))
return
}
@ -290,7 +309,75 @@ func resourceGiteaTeam() *schema.Resource {
Computed: true,
Description: "List of Users that should be part of this team",
},
"repositories": {
Type: schema.TypeList,
Elem: &schema.Schema{
Type: schema.TypeString,
},
Optional: true,
Required: false,
Computed: true,
Description: "List of Repositories that should be part of this team",
},
},
Description: "`gitea_team` manages Team that are part of an organisation.",
}
}
func setTeamRepositories(team *gitea.Team, d *schema.ResourceData, meta interface{}, update bool) (err error) {
client := meta.(*gitea.Client)
org := d.Get(TeamOrg).(string)
repositories := make(map[string]bool)
for _, repo := range d.Get(TeamRepositories).([]interface{}) {
if repo != "" {
repositories[repo.(string)] = true
}
}
if update {
page := 1
for {
var existingRepositories []*gitea.Repository
existingRepositories, _, err = client.ListTeamRepositories(team.ID, gitea.ListTeamRepositoriesOptions{
ListOptions: gitea.ListOptions{
Page: page,
PageSize: 50,
},
})
if err != nil {
return errors.New(fmt.Sprintf("[ERROR] Error listeng team repositories: %s", err))
}
if len(existingRepositories) == 0 {
break
}
for _, exr := range existingRepositories {
_, exists := repositories[exr.Name]
if exists {
repositories[exr.Name] = false
} else {
_, err = client.RemoveTeamRepository(team.ID, org, exr.Name)
if err != nil {
return errors.New(fmt.Sprintf("[ERROR] Error removing team repository %q: %s", exr.Name, err))
}
}
}
page += 1
}
}
for repo, flag := range repositories {
if flag {
_, err = client.AddTeamRepository(team.ID, org, repo)
if err != nil {
return errors.New(fmt.Sprintf("[ERROR] Error adding team repository %q: %s", repo, err))
}
}
}
return
}

2
go.mod
View File

@ -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