mirror of
https://github.com/opentffoundation/opentf.git
synced 2026-03-13 10:01:08 -04:00
* provider/github: add repository_webhook resource `repository_webhook` can be used to manage webhooks for repositories. It is currently limited to organization repositories only. The changeset includes both documentation and tests. The tests are green: ``` make testacc TEST=./builtin/providers/github TESTARGS='-run=TestAccGithubRepositoryWebhook_basic' ==> Checking that code complies with gofmt requirements... go generate $(go list ./... | grep -v /terraform/vendor/) 2017/03/21 16:20:07 Generated command/internal_plugin_list.go TF_ACC=1 go test ./builtin/providers/github -v -run=TestAccGithubRepositoryWebhook_basic -timeout 120m === RUN TestAccGithubRepositoryWebhook_basic --- PASS: TestAccGithubRepositoryWebhook_basic (5.10s) PASS ok github.com/hashicorp/terraform/builtin/providers/github 5.113s ``` * provider/github: add github_organization_webhook the `github_organization_webhook` resource is similar to the `github_repository_webhook` resource, but it manages webhooks for an organization. the tests are green: ``` make testacc TEST=./builtin/providers/github TESTARGS='-run=TestAccGithubOrganizationWebhook' ==> Checking that code complies with gofmt requirements... go generate $(go list ./... | grep -v /terraform/vendor/) 2017/03/21 17:23:33 Generated command/internal_plugin_list.go TF_ACC=1 go test ./builtin/providers/github -v -run=TestAccGithubOrganizationWebhook -timeout 120m === RUN TestAccGithubOrganizationWebhook_basic --- PASS: TestAccGithubOrganizationWebhook_basic (2.09s) PASS ok github.com/hashicorp/terraform/builtin/providers/github 2.109s ```
133 lines
3.3 KiB
Go
133 lines
3.3 KiB
Go
package github
|
|
|
|
import (
|
|
"context"
|
|
"strconv"
|
|
|
|
"github.com/google/go-github/github"
|
|
"github.com/hashicorp/terraform/helper/schema"
|
|
)
|
|
|
|
func resourceGithubRepositoryWebhook() *schema.Resource {
|
|
return &schema.Resource{
|
|
Create: resourceGithubRepositoryWebhookCreate,
|
|
Read: resourceGithubRepositoryWebhookRead,
|
|
Update: resourceGithubRepositoryWebhookUpdate,
|
|
Delete: resourceGithubRepositoryWebhookDelete,
|
|
|
|
Schema: map[string]*schema.Schema{
|
|
"name": {
|
|
Type: schema.TypeString,
|
|
Required: true,
|
|
ForceNew: true,
|
|
},
|
|
"repository": {
|
|
Type: schema.TypeString,
|
|
Required: true,
|
|
ForceNew: true,
|
|
},
|
|
"events": &schema.Schema{
|
|
Type: schema.TypeSet,
|
|
Required: true,
|
|
Elem: &schema.Schema{Type: schema.TypeString},
|
|
Set: schema.HashString,
|
|
},
|
|
"configuration": {
|
|
Type: schema.TypeMap,
|
|
Optional: true,
|
|
},
|
|
"url": {
|
|
Type: schema.TypeString,
|
|
Computed: true,
|
|
},
|
|
"active": {
|
|
Type: schema.TypeBool,
|
|
Optional: true,
|
|
Default: true,
|
|
},
|
|
},
|
|
}
|
|
}
|
|
|
|
func resourceGithubRepositoryWebhookObject(d *schema.ResourceData) *github.Hook {
|
|
url := d.Get("url").(string)
|
|
active := d.Get("active").(bool)
|
|
events := []string{}
|
|
eventSet := d.Get("events").(*schema.Set)
|
|
for _, v := range eventSet.List() {
|
|
events = append(events, v.(string))
|
|
}
|
|
name := d.Get("name").(string)
|
|
|
|
hook := &github.Hook{
|
|
Name: &name,
|
|
URL: &url,
|
|
Events: events,
|
|
Active: &active,
|
|
Config: d.Get("configuration").(map[string]interface{}),
|
|
}
|
|
|
|
return hook
|
|
}
|
|
|
|
func resourceGithubRepositoryWebhookCreate(d *schema.ResourceData, meta interface{}) error {
|
|
client := meta.(*Organization).client
|
|
hk := resourceGithubRepositoryWebhookObject(d)
|
|
|
|
hook, _, err := client.Repositories.CreateHook(context.TODO(), meta.(*Organization).name, d.Get("repository").(string), hk)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
d.SetId(strconv.Itoa(*hook.ID))
|
|
|
|
return resourceGithubRepositoryWebhookRead(d, meta)
|
|
}
|
|
|
|
func resourceGithubRepositoryWebhookRead(d *schema.ResourceData, meta interface{}) error {
|
|
client := meta.(*Organization).client
|
|
hookID, _ := strconv.Atoi(d.Id())
|
|
|
|
hook, resp, err := client.Repositories.GetHook(context.TODO(), meta.(*Organization).name, d.Get("repository").(string), hookID)
|
|
if err != nil {
|
|
if resp.StatusCode == 404 {
|
|
d.SetId("")
|
|
return nil
|
|
}
|
|
return err
|
|
}
|
|
d.Set("name", hook.Name)
|
|
d.Set("url", hook.URL)
|
|
d.Set("active", hook.Active)
|
|
d.Set("events", hook.Events)
|
|
d.Set("configuration", hook.Config)
|
|
|
|
return nil
|
|
}
|
|
|
|
func resourceGithubRepositoryWebhookUpdate(d *schema.ResourceData, meta interface{}) error {
|
|
client := meta.(*Organization).client
|
|
hk := resourceGithubRepositoryWebhookObject(d)
|
|
hookID, err := strconv.Atoi(d.Id())
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
_, _, err = client.Repositories.EditHook(context.TODO(), meta.(*Organization).name, d.Get("repository").(string), hookID, hk)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
return resourceGithubRepositoryWebhookRead(d, meta)
|
|
}
|
|
|
|
func resourceGithubRepositoryWebhookDelete(d *schema.ResourceData, meta interface{}) error {
|
|
client := meta.(*Organization).client
|
|
hookID, err := strconv.Atoi(d.Id())
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
_, err = client.Repositories.DeleteHook(context.TODO(), meta.(*Organization).name, d.Get("repository").(string), hookID)
|
|
return err
|
|
}
|