Add repository webhook resource (#28)
Co-authored-by: techknowlogick <techknowlogick@noreply.gitea.com> Reviewed-on: https://gitea.com/gitea/terraform-provider-gitea/pulls/28 Reviewed-by: techknowlogick <techknowlogick@noreply.gitea.com> Co-authored-by: Petar Nikolovski <petar.nikolovski@protonmail.com> Co-committed-by: Petar Nikolovski <petar.nikolovski@protonmail.com>
This commit is contained in:
parent
f59aab3d1f
commit
683696a756
18 changed files with 290 additions and 38 deletions
1
.gitignore
vendored
1
.gitignore
vendored
|
@ -1,2 +1,3 @@
|
||||||
.vscode
|
.vscode
|
||||||
|
.idea/
|
||||||
dist/
|
dist/
|
|
@ -13,7 +13,7 @@ terraform {
|
||||||
required_providers {
|
required_providers {
|
||||||
gitea = {
|
gitea = {
|
||||||
source = "go-gitea/gitea"
|
source = "go-gitea/gitea"
|
||||||
version = "0.2.0"
|
version = "0.3.0"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -71,4 +71,3 @@ This project is licensed under the MIT License - see the [LICENSE](LICENSE) file
|
||||||
## History
|
## History
|
||||||
|
|
||||||
This codebase was created at https://gitea.com/gitea/terraform-provider-gitea, was forked by @lerentis, and then their changes were merged back into the original repo. Thank you to everyone who contributed!
|
This codebase was created at https://gitea.com/gitea/terraform-provider-gitea, was forked by @lerentis, and then their changes were merged back into the original repo. Thank you to everyone who contributed!
|
||||||
|
|
||||||
|
|
|
@ -28,5 +28,3 @@ description: |-
|
||||||
- `location` (String)
|
- `location` (String)
|
||||||
- `visibility` (String)
|
- `visibility` (String)
|
||||||
- `website` (String)
|
- `website` (String)
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -43,5 +43,3 @@ description: |-
|
||||||
- `updated` (String)
|
- `updated` (String)
|
||||||
- `watchers` (Number)
|
- `watchers` (Number)
|
||||||
- `website` (String)
|
- `website` (String)
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -29,5 +29,3 @@ description: |-
|
||||||
- `is_admin` (Boolean)
|
- `is_admin` (Boolean)
|
||||||
- `language` (String)
|
- `language` (String)
|
||||||
- `last_login` (String)
|
- `last_login` (String)
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -57,5 +57,3 @@ resource "gitea_fork" "org2_fork_of_repo1_in_org1" {
|
||||||
### Read-Only
|
### Read-Only
|
||||||
|
|
||||||
- `id` (String) The ID of this resource.
|
- `id` (String) The ID of this resource.
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -51,5 +51,3 @@ resource "gitea_git_hook" "org_repo_post_receive" {
|
||||||
### Read-Only
|
### Read-Only
|
||||||
|
|
||||||
- `id` (String) The ID of this resource.
|
- `id` (String) The ID of this resource.
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -29,5 +29,3 @@ Handling [gitea oauth application](https://docs.gitea.io/en-us/oauth2-provider/)
|
||||||
- `client_id` (String) OAuth2 Application client id
|
- `client_id` (String) OAuth2 Application client id
|
||||||
- `client_secret` (String, Sensitive) Oauth2 Application client secret
|
- `client_secret` (String, Sensitive) Oauth2 Application client secret
|
||||||
- `id` (String) The ID of this resource.
|
- `id` (String) The ID of this resource.
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -47,5 +47,3 @@ resource "gitea_repository" "org_repo" {
|
||||||
- `avatar_url` (String)
|
- `avatar_url` (String)
|
||||||
- `id` (String) The ID of this resource.
|
- `id` (String) The ID of this resource.
|
||||||
- `repos` (List of String) List of all Repositories that are part of this organisation
|
- `repos` (List of String) List of all Repositories that are part of this organisation
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -48,5 +48,3 @@ resource "gitea_public_key" "test_user_key" {
|
||||||
- `fingerprint` (String)
|
- `fingerprint` (String)
|
||||||
- `id` (String) The ID of this resource.
|
- `id` (String) The ID of this resource.
|
||||||
- `type` (String)
|
- `type` (String)
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -111,5 +111,3 @@ Need to exist in the gitea instance
|
||||||
- `permission_push` (Boolean)
|
- `permission_push` (Boolean)
|
||||||
- `ssh_url` (String)
|
- `ssh_url` (String)
|
||||||
- `updated` (String)
|
- `updated` (String)
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -59,5 +59,3 @@ resource "gitea_repository_key" "example" {
|
||||||
### Read-Only
|
### Read-Only
|
||||||
|
|
||||||
- `id` (String) The ID of this resource.
|
- `id` (String) The ID of this resource.
|
||||||
|
|
||||||
|
|
||||||
|
|
36
docs/resources/repository_webhook.md
Normal file
36
docs/resources/repository_webhook.md
Normal file
|
@ -0,0 +1,36 @@
|
||||||
|
---
|
||||||
|
# generated by https://github.com/hashicorp/terraform-plugin-docs
|
||||||
|
page_title: "gitea_repository_webhook Resource - terraform-provider-gitea"
|
||||||
|
subcategory: ""
|
||||||
|
description: |-
|
||||||
|
This resource allows you to create and manage webhooks for repositories.
|
||||||
|
---
|
||||||
|
|
||||||
|
# gitea_repository_webhook (Resource)
|
||||||
|
|
||||||
|
This resource allows you to create and manage webhooks for repositories.
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
<!-- schema generated by tfplugindocs -->
|
||||||
|
## Schema
|
||||||
|
|
||||||
|
### Required
|
||||||
|
|
||||||
|
- `active` (Boolean) Set webhook to active, e.g. `true`
|
||||||
|
- `branch_filter` (String) Set branch filter on the webhook, e.g. `"*"`
|
||||||
|
- `content_type` (String) The content type of the payload. It can be `json`, or `form`
|
||||||
|
- `events` (List of String) A list of events that will trigger the webhool, e.g. `["push"]`
|
||||||
|
- `name` (String) Repository name
|
||||||
|
- `type` (String) Webhook type, e.g. `gitea`
|
||||||
|
- `url` (String) Target URL of the webhook
|
||||||
|
- `username` (String) User name or organization name
|
||||||
|
|
||||||
|
### Optional
|
||||||
|
|
||||||
|
- `secret` (String) Webhook secret
|
||||||
|
|
||||||
|
### Read-Only
|
||||||
|
|
||||||
|
- `created_at` (String) Webhook creation timestamp
|
||||||
|
- `id` (String) The ID of this resource.
|
|
@ -79,5 +79,3 @@ Can be `repo.code`, `repo.issues`, `repo.ext_issues`, `repo.wiki`, `repo.pulls`,
|
||||||
### Read-Only
|
### Read-Only
|
||||||
|
|
||||||
- `id` (String) The ID of this resource.
|
- `id` (String) The ID of this resource.
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -63,5 +63,3 @@ output "token" {
|
||||||
- `id` (String) The ID of this resource.
|
- `id` (String) The ID of this resource.
|
||||||
- `last_eight` (String)
|
- `last_eight` (String)
|
||||||
- `token` (String, Sensitive) The actual Access Token
|
- `token` (String, Sensitive) The actual Access Token
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -56,5 +56,3 @@ resource "gitea_user" "test" {
|
||||||
### Read-Only
|
### Read-Only
|
||||||
|
|
||||||
- `id` (String) The ID of this resource.
|
- `id` (String) The ID of this resource.
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -84,6 +84,7 @@ func Provider() *schema.Provider {
|
||||||
"gitea_git_hook": resourceGiteaGitHook(),
|
"gitea_git_hook": resourceGiteaGitHook(),
|
||||||
"gitea_token": resourceGiteaToken(),
|
"gitea_token": resourceGiteaToken(),
|
||||||
"gitea_repository_key": resourceGiteaRepositoryKey(),
|
"gitea_repository_key": resourceGiteaRepositoryKey(),
|
||||||
|
"gitea_repository_webhook": resourceGiteaRepositoryWebhook(),
|
||||||
},
|
},
|
||||||
|
|
||||||
ConfigureFunc: providerConfigure,
|
ConfigureFunc: providerConfigure,
|
||||||
|
|
241
gitea/resource_gitea_repository_webhook.go
Normal file
241
gitea/resource_gitea_repository_webhook.go
Normal file
|
@ -0,0 +1,241 @@
|
||||||
|
package gitea
|
||||||
|
|
||||||
|
import (
|
||||||
|
"code.gitea.io/sdk/gitea"
|
||||||
|
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
|
||||||
|
"strconv"
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
repoWebhookUsername string = "username"
|
||||||
|
repoWebhookName string = "name"
|
||||||
|
repoWebhookType string = "type"
|
||||||
|
repoWebhookUrl string = "url"
|
||||||
|
repoWebhookContentType string = "content_type"
|
||||||
|
repoWebhookSecret string = "secret"
|
||||||
|
repoWebhookEvents string = "events"
|
||||||
|
repoWebhookBranchFilter string = "branch_filter"
|
||||||
|
repoWebhookActive string = "active"
|
||||||
|
repoWebhookCreatedAt string = "created_at"
|
||||||
|
)
|
||||||
|
|
||||||
|
func resourceRepositoryWebhookRead(d *schema.ResourceData, meta interface{}) (err error) {
|
||||||
|
client := meta.(*gitea.Client)
|
||||||
|
|
||||||
|
id, err := strconv.ParseInt(d.Id(), 10, 64)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
user := d.Get(repoWebhookUsername).(string)
|
||||||
|
repo := d.Get(repoWebhookName).(string)
|
||||||
|
|
||||||
|
hook, resp, err := client.GetRepoHook(user, repo, id)
|
||||||
|
if err != nil {
|
||||||
|
if resp.StatusCode == 404 {
|
||||||
|
d.SetId("")
|
||||||
|
return
|
||||||
|
} else {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
err = setRepositoryWebhookData(hook, d)
|
||||||
|
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
func resourceRepositoryWebhookCreate(d *schema.ResourceData, meta interface{}) (err error) {
|
||||||
|
client := meta.(*gitea.Client)
|
||||||
|
|
||||||
|
user := d.Get(repoWebhookUsername).(string)
|
||||||
|
repo := d.Get(repoWebhookName).(string)
|
||||||
|
|
||||||
|
config := map[string]string{
|
||||||
|
"url": d.Get(repoWebhookUrl).(string),
|
||||||
|
"content_type": d.Get(repoWebhookContentType).(string),
|
||||||
|
}
|
||||||
|
|
||||||
|
secret := d.Get(repoWebhookSecret).(string)
|
||||||
|
if secret != "" {
|
||||||
|
config["secret"] = secret
|
||||||
|
}
|
||||||
|
|
||||||
|
events := make([]string, 0)
|
||||||
|
for _, element := range d.Get(repoWebhookEvents).([]interface{}) {
|
||||||
|
events = append(events, element.(string))
|
||||||
|
}
|
||||||
|
|
||||||
|
hookOption := gitea.CreateHookOption{
|
||||||
|
Type: gitea.HookType(d.Get(repoWebhookType).(string)),
|
||||||
|
Config: config,
|
||||||
|
Events: events,
|
||||||
|
BranchFilter: d.Get(repoWebhookBranchFilter).(string),
|
||||||
|
Active: d.Get(repoWebhookActive).(bool),
|
||||||
|
}
|
||||||
|
|
||||||
|
hook, _, err := client.CreateRepoHook(user, repo, hookOption)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
err = setRepositoryWebhookData(hook, d)
|
||||||
|
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
func resourceRepositoryWebhookUpdate(d *schema.ResourceData, meta interface{}) (err error) {
|
||||||
|
client := meta.(*gitea.Client)
|
||||||
|
|
||||||
|
user := d.Get(repoWebhookUsername).(string)
|
||||||
|
repo := d.Get(repoWebhookName).(string)
|
||||||
|
id, err := strconv.ParseInt(d.Id(), 10, 64)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
config := map[string]string{
|
||||||
|
"url": d.Get(repoWebhookUrl).(string),
|
||||||
|
"content_type": d.Get(repoWebhookContentType).(string),
|
||||||
|
}
|
||||||
|
|
||||||
|
secret := d.Get(repoWebhookSecret).(string)
|
||||||
|
if secret != "" {
|
||||||
|
config["secret"] = secret
|
||||||
|
}
|
||||||
|
|
||||||
|
events := make([]string, 0)
|
||||||
|
for _, element := range d.Get(repoWebhookEvents).([]interface{}) {
|
||||||
|
events = append(events, element.(string))
|
||||||
|
}
|
||||||
|
|
||||||
|
active := d.Get(repoWebhookActive).(bool)
|
||||||
|
|
||||||
|
hookOption := gitea.EditHookOption{
|
||||||
|
Config: config,
|
||||||
|
Events: events,
|
||||||
|
BranchFilter: d.Get(repoWebhookBranchFilter).(string),
|
||||||
|
Active: &active,
|
||||||
|
}
|
||||||
|
|
||||||
|
_, err = client.EditRepoHook(user, repo, id, hookOption)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
hook, _, err := client.GetRepoHook(user, repo, id)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
err = setRepositoryWebhookData(hook, d)
|
||||||
|
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
func resourceRepositoryWebhookDelete(d *schema.ResourceData, meta interface{}) (err error) {
|
||||||
|
client := meta.(*gitea.Client)
|
||||||
|
|
||||||
|
user := d.Get(repoWebhookUsername).(string)
|
||||||
|
repo := d.Get(repoWebhookName).(string)
|
||||||
|
id, err := strconv.ParseInt(d.Id(), 10, 64)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
_, err = client.DeleteRepoHook(user, repo, id)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
func setRepositoryWebhookData(hook *gitea.Hook, d *schema.ResourceData) (err error) {
|
||||||
|
d.SetId(strconv.FormatInt(hook.ID, 10))
|
||||||
|
|
||||||
|
d.Set(repoWebhookUsername, d.Get(repoWebhookUsername).(string))
|
||||||
|
d.Set(repoWebhookName, d.Get(repoWebhookName).(string))
|
||||||
|
d.Set(repoWebhookType, d.Get(repoWebhookType).(string))
|
||||||
|
d.Set(repoWebhookUrl, d.Get(repoWebhookUrl).(string))
|
||||||
|
d.Set(repoWebhookContentType, d.Get(repoWebhookContentType).(string))
|
||||||
|
|
||||||
|
secret := d.Get(repoWebhookSecret).(string)
|
||||||
|
if secret != "" {
|
||||||
|
d.Set(repoWebhookSecret, secret)
|
||||||
|
}
|
||||||
|
|
||||||
|
d.Set(repoWebhookEvents, d.Get(repoWebhookEvents))
|
||||||
|
d.Set(repoWebhookBranchFilter, d.Get(repoWebhookBranchFilter).(string))
|
||||||
|
d.Set(repoWebhookActive, d.Get(repoWebhookActive).(bool))
|
||||||
|
d.Set(repoWebhookCreatedAt, hook.Created)
|
||||||
|
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
func resourceGiteaRepositoryWebhook() *schema.Resource {
|
||||||
|
return &schema.Resource{
|
||||||
|
Read: resourceRepositoryWebhookRead,
|
||||||
|
Create: resourceRepositoryWebhookCreate,
|
||||||
|
Update: resourceRepositoryWebhookUpdate,
|
||||||
|
Delete: resourceRepositoryWebhookDelete,
|
||||||
|
Schema: map[string]*schema.Schema{
|
||||||
|
"username": {
|
||||||
|
Type: schema.TypeString,
|
||||||
|
Required: true,
|
||||||
|
ForceNew: true,
|
||||||
|
Description: "User name or organization name",
|
||||||
|
},
|
||||||
|
"name": {
|
||||||
|
Type: schema.TypeString,
|
||||||
|
Required: true,
|
||||||
|
ForceNew: true,
|
||||||
|
Description: "Repository name",
|
||||||
|
},
|
||||||
|
"type": {
|
||||||
|
Type: schema.TypeString,
|
||||||
|
Required: true,
|
||||||
|
Description: "Webhook type, e.g. `gitea`",
|
||||||
|
},
|
||||||
|
"url": {
|
||||||
|
Type: schema.TypeString,
|
||||||
|
Required: true,
|
||||||
|
Description: "Target URL of the webhook",
|
||||||
|
},
|
||||||
|
"content_type": {
|
||||||
|
Type: schema.TypeString,
|
||||||
|
Required: true,
|
||||||
|
Description: "The content type of the payload. It can be `json`, or `form`",
|
||||||
|
},
|
||||||
|
"secret": {
|
||||||
|
Type: schema.TypeString,
|
||||||
|
Optional: true,
|
||||||
|
Description: "Webhook secret",
|
||||||
|
},
|
||||||
|
"events": {
|
||||||
|
Type: schema.TypeList,
|
||||||
|
Elem: &schema.Schema{
|
||||||
|
Type: schema.TypeString,
|
||||||
|
},
|
||||||
|
Required: true,
|
||||||
|
Description: "A list of events that will trigger the webhool, e.g. `[\"push\"]`",
|
||||||
|
},
|
||||||
|
"branch_filter": {
|
||||||
|
Type: schema.TypeString,
|
||||||
|
Required: true,
|
||||||
|
Description: "Set branch filter on the webhook, e.g. `\"*\"`",
|
||||||
|
},
|
||||||
|
"active": {
|
||||||
|
Type: schema.TypeBool,
|
||||||
|
Required: true,
|
||||||
|
Description: "Set webhook to active, e.g. `true`",
|
||||||
|
},
|
||||||
|
"created_at": {
|
||||||
|
Type: schema.TypeString,
|
||||||
|
Computed: true,
|
||||||
|
Description: "Webhook creation timestamp",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
Description: "This resource allows you to create and manage webhooks for repositories.",
|
||||||
|
}
|
||||||
|
}
|
Loading…
Add table
Reference in a new issue