Added repository level hook data and resources

This commit is contained in:
Gavin Bunney
2019-10-15 12:00:51 -07:00
parent 51f1146d1d
commit 041e008176
11 changed files with 582 additions and 3 deletions

View File

@@ -0,0 +1,210 @@
package bitbucket
import (
"encoding/json"
"fmt"
"github.com/hashicorp/terraform/helper/schema"
"github.com/hashicorp/terraform/helper/validation"
"net/url"
)
type PaginatedRepositoryHooksValue struct {
Details struct {
Key string `json:"key,omitempty"`
Name string `json:"name,omitempty"`
Type string `json:"type,omitempty"`
Description string `json:"description,omitempty"`
Version string `json:"version,omitempty"`
ScopeTypes []string `json:"scopeTypes,omitempty"`
} `json:"details,omitempty"`
Enabled bool `json:"enabled,omitempty"`
Configured bool `json:"configured,omitempty"`
Scope struct {
Type string `json:"type,omitempty"`
ResourceId int `json:"resourceId,omitempty"`
} `json:"scope,omitempty"`
}
type RepositoryHook struct {
Key string
Name string
Type string
Description string
Version string
ScopeTypes []string
Enabled bool
Configured bool
ScopeType string
ScopeResourceId int
}
type PaginatedRepositoryHooks struct {
Values []PaginatedRepositoryHooksValue `json:"values,omitempty"`
Size int `json:"size,omitempty"`
Limit int `json:"limit,omitempty"`
IsLastPage bool `json:"isLastPage,omitempty"`
Start int `json:"start,omitempty"`
NextPageStart int `json:"nextPageStart,omitempty"`
}
func dataSourceRepositoryHooks() *schema.Resource {
return &schema.Resource{
Read: dataSourceRepositoryHooksRead,
Schema: map[string]*schema.Schema{
"project": {
Type: schema.TypeString,
Required: true,
},
"repository": {
Type: schema.TypeString,
Required: true,
},
"type": {
Type: schema.TypeString,
Optional: true,
ValidateFunc: validation.StringInSlice([]string{"PRE_RECEIVE", "POST_RECEIVE"}, false),
},
"hooks": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"key": {
Type: schema.TypeString,
Computed: true,
},
"name": {
Type: schema.TypeString,
Computed: true,
},
"type": {
Type: schema.TypeString,
Computed: true,
},
"description": {
Type: schema.TypeString,
Computed: true,
},
"version": {
Type: schema.TypeString,
Computed: true,
},
"scope_types": {
Type: schema.TypeList,
Elem: &schema.Schema{Type: schema.TypeString},
Computed: true,
},
"enabled": {
Type: schema.TypeBool,
Computed: true,
},
"configured": {
Type: schema.TypeBool,
Computed: true,
},
"scope_type": {
Type: schema.TypeString,
Computed: true,
},
"scope_resource_id": {
Type: schema.TypeInt,
Computed: true,
},
},
},
},
},
}
}
func dataSourceRepositoryHooksRead(d *schema.ResourceData, m interface{}) error {
hooks, err := readRepositoryHooks(m, d.Get("project").(string), d.Get("repository").(string), d.Get("type").(string))
if err != nil {
return err
}
d.SetId(d.Get("repository").(string))
var terraformHooks []interface{}
for _, hook := range hooks {
h := make(map[string]interface{})
h["key"] = hook.Key
h["name"] = hook.Name
h["type"] = hook.Type
h["description"] = hook.Description
h["version"] = hook.Version
h["scope_types"] = hook.ScopeTypes
h["enabled"] = hook.Enabled
h["configured"] = hook.Configured
h["scope_type"] = hook.ScopeType
h["scope_resource_id"] = hook.ScopeResourceId
terraformHooks = append(terraformHooks, h)
}
_ = d.Set("hooks", terraformHooks)
return nil
}
func readRepositoryHooks(m interface{}, project string, repository string, typeFilter string) ([]RepositoryHook, error) {
client := m.(*BitbucketServerProvider).BitbucketClient
resourceURL := fmt.Sprintf("/rest/api/1.0/projects/%s/repos/%s/settings/hooks",
project,
repository,
)
if typeFilter != "" {
resourceURL += "?type=" + url.QueryEscape(typeFilter)
}
var repositoryHooks PaginatedRepositoryHooks
var hooks []RepositoryHook
for {
resp, err := client.Get(resourceURL)
if err != nil {
return nil, err
}
decoder := json.NewDecoder(resp.Body)
err = decoder.Decode(&repositoryHooks)
if err != nil {
return nil, err
}
for _, hook := range repositoryHooks.Values {
h := RepositoryHook{
Key: hook.Details.Key,
Name: hook.Details.Name,
Type: hook.Details.Type,
Description: hook.Details.Description,
Version: hook.Details.Version,
ScopeTypes: hook.Details.ScopeTypes,
Enabled: hook.Enabled,
Configured: hook.Configured,
ScopeType: hook.Scope.Type,
ScopeResourceId: hook.Scope.ResourceId,
}
hooks = append(hooks, h)
}
if repositoryHooks.IsLastPage == false {
resourceURL = fmt.Sprintf("/rest/api/1.0/projects/%s/repos/%s/settings/hooks?start=%d",
project,
repository,
repositoryHooks.NextPageStart,
)
if typeFilter != "" {
resourceURL += "&type=" + url.QueryEscape(typeFilter)
}
repositoryHooks = PaginatedRepositoryHooks{}
} else {
break
}
}
return hooks, nil
}

View File

@@ -0,0 +1,101 @@
package bitbucket
import (
"fmt"
"math/rand"
"testing"
"time"
"github.com/hashicorp/terraform/helper/resource"
)
func TestAccBitbucketDataRepositoryHooks_simple(t *testing.T) {
projectKey := rand.New(rand.NewSource(time.Now().UnixNano())).Int()
config := fmt.Sprintf(`
resource "bitbucketserver_project" "test" {
key = "TEST%v"
name = "test-project-%v"
}
resource "bitbucketserver_repository" "test" {
project = bitbucketserver_project.test.key
name = "test-repo-%v"
}
data "bitbucketserver_repository_hooks" "test" {
project = bitbucketserver_project.test.key
repository = bitbucketserver_repository.test.slug
}
`, projectKey, projectKey, projectKey)
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: config,
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("data.bitbucketserver_repository_hooks.test", "hooks.#", "8"),
resource.TestCheckResourceAttr("data.bitbucketserver_repository_hooks.test", "hooks.0.key", "com.atlassian.bitbucket.server.bitbucket-bundled-hooks:all-approvers-merge-check"),
resource.TestCheckResourceAttr("data.bitbucketserver_repository_hooks.test", "hooks.0.name", "All reviewers approve"),
resource.TestCheckResourceAttr("data.bitbucketserver_repository_hooks.test", "hooks.0.type", "PRE_PULL_REQUEST_MERGE"),
resource.TestCheckResourceAttr("data.bitbucketserver_repository_hooks.test", "hooks.0.description", "Require all reviewers to approve the pull request."),
resource.TestCheckResourceAttr("data.bitbucketserver_repository_hooks.test", "hooks.0.version", "6.7.0"),
resource.TestCheckResourceAttr("data.bitbucketserver_repository_hooks.test", "hooks.0.scope_types.#", "2"),
resource.TestCheckResourceAttr("data.bitbucketserver_repository_hooks.test", "hooks.0.scope_types.0", "PROJECT"),
resource.TestCheckResourceAttr("data.bitbucketserver_repository_hooks.test", "hooks.0.scope_types.1", "REPOSITORY"),
resource.TestCheckResourceAttr("data.bitbucketserver_repository_hooks.test", "hooks.0.enabled", "false"),
resource.TestCheckResourceAttr("data.bitbucketserver_repository_hooks.test", "hooks.0.configured", "false"),
resource.TestCheckResourceAttr("data.bitbucketserver_repository_hooks.test", "hooks.0.scope_type", "PROJECT"),
resource.TestCheckResourceAttrSet("data.bitbucketserver_repository_hooks.test", "hooks.0.scope_resource_id"),
),
},
},
})
}
func TestAccBitbucketDataRepositoryHooks_type(t *testing.T) {
projectKey := rand.New(rand.NewSource(time.Now().UnixNano())).Int()
config := fmt.Sprintf(`
resource "bitbucketserver_project" "test" {
key = "TEST%v"
name = "test-project-%v"
}
resource "bitbucketserver_repository" "test" {
project = bitbucketserver_project.test.key
name = "test-repo-%v"
}
data "bitbucketserver_repository_hooks" "test" {
project = bitbucketserver_project.test.key
repository = bitbucketserver_repository.test.slug
type = "PRE_RECEIVE"
}
`, projectKey, projectKey, projectKey)
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: config,
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("data.bitbucketserver_repository_hooks.test", "hooks.#", "3"),
resource.TestCheckResourceAttr("data.bitbucketserver_repository_hooks.test", "hooks.0.key", "com.atlassian.bitbucket.server.bitbucket-bundled-hooks:force-push-hook"),
resource.TestCheckResourceAttr("data.bitbucketserver_repository_hooks.test", "hooks.0.name", "Reject Force Push"),
resource.TestCheckResourceAttr("data.bitbucketserver_repository_hooks.test", "hooks.0.type", "PRE_RECEIVE"),
resource.TestCheckResourceAttr("data.bitbucketserver_repository_hooks.test", "hooks.0.description", "Reject all force pushes (git push --force) to this repository"),
resource.TestCheckResourceAttr("data.bitbucketserver_repository_hooks.test", "hooks.0.version", "6.7.0"),
resource.TestCheckResourceAttr("data.bitbucketserver_repository_hooks.test", "hooks.0.scope_types.#", "2"),
resource.TestCheckResourceAttr("data.bitbucketserver_repository_hooks.test", "hooks.0.scope_types.0", "PROJECT"),
resource.TestCheckResourceAttr("data.bitbucketserver_repository_hooks.test", "hooks.0.scope_types.1", "REPOSITORY"),
resource.TestCheckResourceAttr("data.bitbucketserver_repository_hooks.test", "hooks.0.enabled", "false"),
resource.TestCheckResourceAttr("data.bitbucketserver_repository_hooks.test", "hooks.0.configured", "false"),
resource.TestCheckResourceAttr("data.bitbucketserver_repository_hooks.test", "hooks.0.scope_type", "PROJECT"),
resource.TestCheckResourceAttrSet("data.bitbucketserver_repository_hooks.test", "hooks.0.scope_resource_id"),
),
},
},
})
}

View File

@@ -40,6 +40,7 @@ func Provider() terraform.ResourceProvider {
"bitbucketserver_project_hooks": dataSourceProjectHooks(),
"bitbucketserver_project_permissions_groups": dataSourceProjectPermissionsGroups(),
"bitbucketserver_project_permissions_users": dataSourceProjectPermissionsUsers(),
"bitbucketserver_repository_hooks": dataSourceRepositoryHooks(),
"bitbucketserver_repository_permissions_groups": dataSourceRepositoryPermissionsGroups(),
"bitbucketserver_repository_permissions_users": dataSourceRepositoryPermissionsUsers(),
},
@@ -56,6 +57,7 @@ func Provider() terraform.ResourceProvider {
"bitbucketserver_project_permissions_group": resourceProjectPermissionsGroup(),
"bitbucketserver_project_permissions_user": resourceProjectPermissionsUser(),
"bitbucketserver_repository": resourceRepository(),
"bitbucketserver_repository_hook": resourceRepositoryHook(),
"bitbucketserver_repository_permissions_group": resourceRepositoryPermissionsGroup(),
"bitbucketserver_repository_permissions_user": resourceRepositoryPermissionsUser(),
"bitbucketserver_user": resourceUser(),

View File

@@ -76,8 +76,8 @@ func resourceProjectHookRead(d *schema.ResourceData, m interface{}) error {
if id != "" {
parts := strings.Split(id, "/")
if len(parts) == 2 {
d.Set("project", parts[0])
d.Set("hook", parts[1])
_ = d.Set("project", parts[0])
_ = d.Set("hook", parts[1])
} else {
return fmt.Errorf("incorrect ID format, should match `project/hook`")
}

View File

@@ -0,0 +1,129 @@
package bitbucket
import (
"bytes"
"encoding/json"
"fmt"
"github.com/hashicorp/terraform/helper/schema"
"strings"
)
func resourceRepositoryHook() *schema.Resource {
return &schema.Resource{
Create: resourceRepositoryHookCreate,
Update: resourceRepositoryHookUpdate,
Read: resourceRepositoryHookRead,
Delete: resourceRepositoryHookDelete,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},
Schema: map[string]*schema.Schema{
"project": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"repository": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"hook": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"settings": {
Type: schema.TypeMap,
Optional: true,
},
},
}
}
func resourceRepositoryHookUpdate(d *schema.ResourceData, m interface{}) error {
client := m.(*BitbucketServerProvider).BitbucketClient
project := d.Get("project").(string)
repository := d.Get("repository").(string)
hook := d.Get("hook").(string)
settings := d.Get("settings").(map[string]interface{})
settingsJson, err := json.Marshal(settings)
if err != nil {
return err
}
_, err = client.Put(fmt.Sprintf("/rest/api/1.0/projects/%s/repos/%s/settings/hooks/%s/enabled",
project,
repository,
hook,
), bytes.NewBuffer(settingsJson))
if err != nil {
return err
}
return resourceRepositoryHookRead(d, m)
}
func resourceRepositoryHookCreate(d *schema.ResourceData, m interface{}) error {
err := resourceRepositoryHookUpdate(d, m)
if err != nil {
return err
}
d.SetId(fmt.Sprintf("%s/%s/%s", d.Get("project").(string), d.Get("repository").(string), d.Get("hook").(string)))
return resourceRepositoryHookRead(d, m)
}
func resourceRepositoryHookRead(d *schema.ResourceData, m interface{}) error {
id := d.Id()
if id != "" {
parts := strings.Split(id, "/")
if len(parts) == 3 {
_ = d.Set("project", parts[0])
_ = d.Set("repository", parts[1])
_ = d.Set("hook", parts[2])
} else {
return fmt.Errorf("incorrect ID format, should match `project/repository/hook`")
}
}
project := d.Get("project").(string)
repository := d.Get("repository").(string)
hook := d.Get("hook").(string)
client := m.(*BitbucketServerProvider).BitbucketClient
resp, err := client.Get(fmt.Sprintf("/rest/api/1.0/projects/%s/repos/%s/settings/hooks/%s/settings",
project,
repository,
hook,
))
if err != nil {
return err
}
var settings map[string]interface{}
decoder := json.NewDecoder(resp.Body)
err = decoder.Decode(&settings)
if err != nil {
return err
}
_ = d.Set("settings", settings)
return nil
}
func resourceRepositoryHookDelete(d *schema.ResourceData, m interface{}) error {
client := m.(*BitbucketServerProvider).BitbucketClient
_, err := client.Delete(fmt.Sprintf("/rest/api/1.0/projects/%s/repos/%s/settings/hooks/%s/enabled",
d.Get("project").(string),
d.Get("repository").(string),
d.Get("hook").(string)))
return err
}

View File

@@ -0,0 +1,47 @@
package bitbucket
import (
"fmt"
"math/rand"
"testing"
"time"
"github.com/hashicorp/terraform/helper/resource"
)
func TestAccBitbucketResourceRepositoryHook_simple(t *testing.T) {
projectKey := fmt.Sprintf("TEST%v", rand.New(rand.NewSource(time.Now().UnixNano())).Int())
config := fmt.Sprintf(`
resource "bitbucketserver_project" "test" {
key = "%v"
name = "test-project-%v"
}
resource "bitbucketserver_repository" "test" {
project = bitbucketserver_project.test.key
name = "repo"
}
resource "bitbucketserver_repository_hook" "test" {
project = bitbucketserver_project.test.key
repository = bitbucketserver_repository.test.slug
hook = "com.atlassian.bitbucket.server.bitbucket-bundled-hooks:force-push-hook"
}
`, projectKey, projectKey)
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: config,
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("bitbucketserver_repository_hook.test", "project", projectKey),
resource.TestCheckResourceAttr("bitbucketserver_repository_hook.test", "repository", "repo"),
resource.TestCheckResourceAttr("bitbucketserver_repository_hook.test", "hook", "com.atlassian.bitbucket.server.bitbucket-bundled-hooks:force-push-hook"),
resource.TestCheckResourceAttr("bitbucketserver_repository_hook.test", "settings.%", "0"),
),
},
},
})
}

View File

@@ -55,4 +55,3 @@ data "bitbucketserver_project_hooks" "main" {
* `configured` - Set if the hook is configured for this project
* `scope_type` - Type of scope applied for this hook, e.g. `PROJECT`
* `scope_resource_id` - Reference ID of the applied scope, e.g. `1`

View File

@@ -0,0 +1,60 @@
---
id: data_bitbucketserver_repository_hooks
title: bitbucketserver_repository_hooks
---
Retrieve a list of repository hooks and their status' for the specified repo.
## Example Usage
```hcl
data "bitbucketserver_repository_hooks" "main" {
project = "TEST"
repository = "repo1"
}
# data.bitbucketserver_repository_hooks.main.hooks = [{
# "key" = "com.atlassian.bitbucket.server.bitbucket-bundled-hooks:force-push-hook",
# "name" = "Reject Force Push",
# "type" = "PRE_RECEIVE",
# "description" = "Reject all force pushes (git push --force) to this repository",
# "version" = "6.7.0",
# "scope_types" = ["PROJECT", "REPOSITORY"],
# "enabled" = "false",
# "configured" = "false",
# "scope_type" = "REPOSITORY",
# }]
```
### Applying a Custom Filter
Find specific types of repository hooks.
```hcl
data "bitbucketserver_project_hooks" "main" {
project = "TEST"
repository = "repo1"
type = "PRE_RECEIVE"
}
```
## Argument Reference
* `project` - Required. Project Key the repository is contained within.
* `repository` - Required. Repository slug to lookup hooks for.
* `type` - Optional. Type of hook to find. Must be one of `PRE_RECEIVE`, `POST_RECEIVE`
## Attribute Reference
* `hooks` - List of maps containing:
* `key` - Unique key identifying the hook e.g. `com.atlassian.bitbucket.server.bitbucket-bundled-hooks:force-push-hook`
* `name` - Name of the hook e.g. `Reject Force Push`
* `type` - Type of the hook e.g. `PRE_RECEIVE`
* `description` - Detailed description e.g. `Reject all force pushes (git push --force) to this repository`
* `version` - Version of the hook, for system hooks this is the bitbucket version e.g. `6.7.0`
* `scope_types` - List of strings containing the scopes available for this hook, e.g. `["PROJECT", "REPOSITORY"]`
* `enabled` - Set if this hook is enabled for this project
* `configured` - Set if the hook is configured for this project
* `scope_type` - Type of scope applied for this hook, e.g. `REPOSITORY`
* `scope_resource_id` - Reference ID of the applied scope, e.g. `1`

View File

@@ -0,0 +1,23 @@
---
id: bitbucketserver_repository_hook
title: bitbucketserver_repository_hook
---
Manage a repository level hook. Extends what Bitbucket does every time a repository changes, for example when code is pushed or a pull request is merged.
## Example Usage
```hcl
resource "bitbucketserver_repository_hook" "main" {
project = "MYPROJ"
repository = "repo1"
hook = "com.atlassian.bitbucket.server.bitbucket-bundled-hooks:force-push-hook"
}
```
## Argument Reference
* `project` - Required. Project Key the repository is contained within.
* `repository` - Required. Repository slug to enable hook for.
* `hook` - Required. The hook to enable on the repository.
* `settings` - Optional. Map of values to apply as settings for the hook. Contents dependant on the individual hook settings.

View File

@@ -34,6 +34,9 @@
"data_bitbucketserver_project_permissions_users": {
"title": "bitbucketserver_project_permissions_users"
},
"data_bitbucketserver_repository_hooks": {
"title": "bitbucketserver_repository_hooks"
},
"data_bitbucketserver_repository_permissions_groups": {
"title": "bitbucketserver_repository_permissions_groups"
},
@@ -76,6 +79,9 @@
"bitbucketserver_project": {
"title": "bitbucketserver_project"
},
"bitbucketserver_repository_hook": {
"title": "bitbucketserver_repository_hook"
},
"bitbucketserver_repository_permissions_group": {
"title": "bitbucketserver_repository_permissions_group"
},

View File

@@ -14,6 +14,7 @@
"data_bitbucketserver_project_hooks",
"data_bitbucketserver_project_permissions_groups",
"data_bitbucketserver_project_permissions_users",
"data_bitbucketserver_repository_hooks",
"data_bitbucketserver_repository_permissions_groups",
"data_bitbucketserver_repository_permissions_users"
],
@@ -30,6 +31,7 @@
"bitbucketserver_project_permissions_group",
"bitbucketserver_project_permissions_user",
"bitbucketserver_repository",
"bitbucketserver_repository_hook",
"bitbucketserver_repository_permissions_group",
"bitbucketserver_repository_permissions_user",
"bitbucketserver_user",