7 Commits

Author SHA1 Message Date
d6160c596b correct version in docs
All checks were successful
continuous-integration/drone/push Build is passing
continuous-integration/drone/tag Build is passing
2023-01-28 22:51:49 +01:00
9799416352 fixed org vanashing
All checks were successful
continuous-integration/drone/push Build is passing
fixed regression of  v0.11.1
2023-01-28 22:49:54 +01:00
8095c2a513 prepare next release
All checks were successful
continuous-integration/drone/push Build is passing
continuous-integration/drone/tag Build is passing
2023-01-04 19:15:00 +01:00
14c1a11417 Merge pull request #21 from martin31821/feature/deploykey
Some checks reported errors
continuous-integration/drone/push Build encountered an error
2023-01-04 19:08:30 +01:00
8a805faa72 Add docs 2023-01-04 10:10:45 +01:00
32ad02fa73 Implement deploy_key resource 2023-01-03 20:29:22 +01:00
edd03aca2f try to publish sbom with next release
All checks were successful
continuous-integration/drone/push Build is passing
2022-12-24 18:37:41 +01:00
15 changed files with 353 additions and 26 deletions

View File

@ -30,6 +30,7 @@ steps:
- name: build
image: goreleaser/goreleaser
commands:
- curl -sSfL https://raw.githubusercontent.com/anchore/syft/main/install.sh | sh -s -- -b /usr/local/bin v0.64.0
- goreleaser build --snapshot
when:
event:
@ -59,6 +60,7 @@ steps:
GPG_PRIVATE_KEY_BASE64:
from_secret: GPG_PRIVATE_KEY_BASE64
commands:
- curl -sSfL https://raw.githubusercontent.com/anchore/syft/main/install.sh | sh -s -- -b /usr/local/bin v0.64.0
- apk add gpg-agent
- gpg-agent --daemon --default-cache-ttl 7200
- echo $GPG_PRIVATE_KEY_BASE64 | base64 -d | gpg --import --batch --no-tty

View File

@ -18,26 +18,25 @@ jobs:
goreleaser:
runs-on: ubuntu-latest
steps:
-
name: Checkout
- name: Checkout
uses: actions/checkout@v3
-
name: Unshallow
- name: Unshallow
run: git fetch --prune --unshallow
-
name: Set up Go
- name: Set up Go
uses: actions/setup-go@v3
with:
go-version: 1.18
-
name: Import GPG key
- name: Import GPG key
id: import_gpg
uses: crazy-max/ghaction-import-gpg@v5.2.0
with:
gpg_private_key: ${{ secrets.GPG_PRIVATE_KEY }}
passphrase: ${{ secrets.PASSPHRASE }}
-
name: Run GoReleaser
- name: setup-syft
run: |
curl -sSfL https://raw.githubusercontent.com/anchore/syft/main/install.sh | \
sh -s -- -b /usr/local/bin v0.64.0
- name: Run GoReleaser
uses: goreleaser/goreleaser-action@v4.1.0
with:
version: latest

View File

@ -41,6 +41,8 @@ checksum:
name_template: '{{ .ProjectName }}_{{ .Version }}_manifest.json'
name_template: '{{ .ProjectName }}_{{ .Version }}_SHA256SUMS'
algorithm: sha256
sboms:
- artifacts: archive
signs:
- artifacts: checksum
args:

View File

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

View File

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

View File

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

View File

@ -0,0 +1,63 @@
---
# generated by https://github.com/hashicorp/terraform-plugin-docs
page_title: "gitea_repository_key Resource - terraform-provider-gitea"
subcategory: ""
description: |-
gitea_repository_key manages a deploy key for a single gitea_repository.
Every key needs a unique name and unique key, i.e. no key can be added twice to the same repo
---
# gitea_repository_key (Resource)
`gitea_repository_key` manages a deploy key for a single gitea_repository.
Every key needs a unique name and unique key, i.e. no key can be added twice to the same repo
## Example Usage
```terraform
terraform {
required_providers {
tls = {
source = "hashicorp/tls"
version = "4.0.4"
}
}
}
resource "tls_private_key" "example" {
type = "RSA"
rsa_bits = 4096
}
resource "gitea_repository" "example" {
name = "example"
private = true
}
resource "gitea_repository_key" "example" {
repository = gitea_repository.example.id
title = "Example Deploy Key"
read_only = true
key = tls_private_key.example.public_key_openssh
}
```
<!-- schema generated by tfplugindocs -->
## Schema
### Required
- `key` (String) Armored SSH key to add
- `repository` (Number) The ID of the repository where the deploy key belongs to
- `title` (String) Name of the deploy key
### Optional
- `read_only` (Boolean) Whether this key has read or read/write access
### Read-Only
- `id` (String) The ID of this resource.

View File

@ -101,6 +101,24 @@ 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

View File

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

View File

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

View File

@ -0,0 +1,25 @@
terraform {
required_providers {
tls = {
source = "hashicorp/tls"
version = "4.0.4"
}
}
}
resource "tls_private_key" "example" {
type = "RSA"
rsa_bits = 4096
}
resource "gitea_repository" "example" {
name = "example"
private = true
}
resource "gitea_repository_key" "example" {
repository = gitea_repository.example.id
title = "Example Deploy Key"
read_only = true
key = tls_private_key.example.public_key_openssh
}

View File

@ -83,6 +83,7 @@ func Provider() *schema.Provider {
"gitea_team": resourceGiteaTeam(),
"gitea_git_hook": resourceGiteaGitHook(),
"gitea_token": resourceGiteaToken(),
"gitea_repository_key": resourceGiteaRepositoryKey(),
},
ConfigureFunc: providerConfigure,

View File

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

View File

@ -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",
}
if orgRepo {
repo, _, err = client.CreateOrgRepo(d.Get(repoOwner).(string), opts)
} else {
repo, _, err = client.AdminCreateRepo(d.Get(repoOwner).(string), opts)
}
}
if err != nil {

View File

@ -0,0 +1,169 @@
package gitea
import (
"fmt"
"strconv"
"strings"
"code.gitea.io/sdk/gitea"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)
const (
deployKeyRepoId string = "repository"
deployKeyName string = "title"
deployKeyKey string = "key"
deployKeyReadOnly string = "read_only"
)
func resourceRepoKeyIdParts(d *schema.ResourceData) (bool, int64, int64, error) {
parts := strings.Split(d.Id(), "/")
if len(parts) != 2 {
return false, 0, 0, nil
}
repoId, err := strconv.ParseInt(parts[0], 10, 64)
if err != nil {
return false, 0, 0, err
}
keyId, err := strconv.ParseInt(parts[1], 10, 64)
if err != nil {
return false, 0, 0, err
}
return true, repoId, keyId, err
}
func resourceRepoKeyRead(d *schema.ResourceData, meta interface{}) (err error) {
client := meta.(*gitea.Client)
hasId, repoId, keyId, err := resourceRepoKeyIdParts(d)
if err != nil {
return err
}
if !hasId {
d.SetId("")
return nil
}
repo, resp, err := client.GetRepoByID(repoId)
if err != nil {
if resp.StatusCode == 404 {
d.SetId("")
return nil
} else {
return err
}
}
key, resp, err := client.GetDeployKey(repo.Owner.UserName, repo.Name, keyId)
if err != nil {
if resp.StatusCode == 404 {
d.SetId("")
return nil
} else {
return err
}
}
err = setRepoKeyResourceData(key, repoId, d)
return
}
func resourceRepoKeyCreate(d *schema.ResourceData, meta interface{}) (err error) {
client := meta.(*gitea.Client)
repo, _, err := client.GetRepoByID(int64(d.Get(deployKeyRepoId).(int)))
if err != nil {
return err
}
dk, _, err := client.CreateDeployKey(repo.Owner.UserName, repo.Name, gitea.CreateKeyOption{
Title: d.Get(deployKeyName).(string),
ReadOnly: d.Get(deployKeyReadOnly).(bool),
Key: d.Get(deployKeyKey).(string),
})
if err != nil {
return err
}
setRepoKeyResourceData(dk, repo.ID, d)
return nil
}
func respurceRepoKeyDelete(d *schema.ResourceData, meta interface{}) (err error) {
client := meta.(*gitea.Client)
hasId, repoId, keyId, err := resourceRepoKeyIdParts(d)
if err != nil {
return err
}
if !hasId {
d.SetId("")
return nil
}
repo, resp, err := client.GetRepoByID(repoId)
if err != nil {
if resp.StatusCode == 404 {
d.SetId("")
return nil
}
return err
}
client.DeleteDeployKey(repo.Owner.UserName, repo.Name, keyId)
return nil
}
func setRepoKeyResourceData(dk *gitea.DeployKey, repoId int64, d *schema.ResourceData) (err error) {
d.SetId(fmt.Sprintf("%d/%d", repoId, dk.ID))
d.Set(deployKeyRepoId, repoId)
d.Set(deployKeyReadOnly, dk.ReadOnly)
d.Set(deployKeyKey, dk.Key)
d.Set(deployKeyName, dk.Title)
return
}
func resourceGiteaRepositoryKey() *schema.Resource {
return &schema.Resource{
Read: resourceRepoKeyRead,
Create: resourceRepoKeyCreate,
Delete: respurceRepoKeyDelete,
Importer: &schema.ResourceImporter{
StateContext: schema.ImportStatePassthroughContext,
},
Schema: map[string]*schema.Schema{
deployKeyRepoId: {
Type: schema.TypeInt,
Required: true,
ForceNew: true,
Description: "The ID of the repository where the deploy key belongs to",
},
deployKeyKey: {
Type: schema.TypeString,
Required: true,
ForceNew: true,
Description: "Armored SSH key to add",
},
deployKeyReadOnly: {
Type: schema.TypeBool,
Required: false,
Optional: true,
Default: true,
ForceNew: true,
Description: "Whether this key has read or read/write access",
},
deployKeyName: {
Type: schema.TypeString,
Required: true,
ForceNew: true,
Description: "Name of the deploy key",
},
},
Description: "`gitea_repository_key` manages a deploy key for a single gitea_repository.\n\n" +
"Every key needs a unique name and unique key, i.e. no key can be added twice to the same repo",
}
}