Skip to content

Commit

Permalink
datasource/gitlab_group_hook: New Data Source
Browse files Browse the repository at this point in the history
Refs: #680
  • Loading branch information
timofurrer committed Aug 19, 2022
1 parent 755125a commit 76100ed
Show file tree
Hide file tree
Showing 5 changed files with 157 additions and 0 deletions.
59 changes: 59 additions & 0 deletions docs/data-sources/group_hook.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
---
# generated by https://github.com/hashicorp/terraform-plugin-docs
page_title: "gitlab_group_hook Data Source - terraform-provider-gitlab"
subcategory: ""
description: |-
The gitlab_group_hook data source allows to retrieve details about a hook in a group.
Upstream API: GitLab REST API docs https://docs.gitlab.com/ee/api/groups.html#get-group-hook
---

# gitlab_group_hook (Data Source)

The `gitlab_group_hook` data source allows to retrieve details about a hook in a group.

**Upstream API**: [GitLab REST API docs](https://docs.gitlab.com/ee/api/groups.html#get-group-hook)

## Example Usage

```terraform
data "gitlab_group" "example" {
id = "foo/bar/baz"
}
data "gitlab_group_hook" "example" {
group = data.gitlab_group.example.id
hook_id = 1
}
```

<!-- schema generated by tfplugindocs -->
## Schema

### Required

- `group` (String) The ID or full path of the group.
- `hook_id` (Number) The id of the group hook.

### Read-Only

- `confidential_issues_events` (Boolean) Invoke the hook for confidential issues events.
- `confidential_note_events` (Boolean) Invoke the hook for confidential notes events.
- `deployment_events` (Boolean) Invoke the hook for deployment events.
- `enable_ssl_verification` (Boolean) Enable ssl verification when invoking the hook.
- `group_id` (Number) The id of the group for the hook.
- `id` (String) The ID of this resource.
- `issues_events` (Boolean) Invoke the hook for issues events.
- `job_events` (Boolean) Invoke the hook for job events.
- `merge_requests_events` (Boolean) Invoke the hook for merge requests.
- `note_events` (Boolean) Invoke the hook for notes events.
- `pipeline_events` (Boolean) Invoke the hook for pipeline events.
- `push_events` (Boolean) Invoke the hook for push events.
- `push_events_branch_filter` (String) Invoke the hook for push events on matching branches only.
- `releases_events` (Boolean) Invoke the hook for releases events.
- `subgroup_events` (Boolean) Invoke the hook for subgroup events.
- `tag_push_events` (Boolean) Invoke the hook for tag push events.
- `token` (String) A token to present when invoking the hook. The token is not available for imported resources.
- `url` (String) The url of the hook to invoke.
- `wiki_page_events` (Boolean) Invoke the hook for wiki page events.


8 changes: 8 additions & 0 deletions examples/data-sources/gitlab_group_hook/data-source.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
data "gitlab_group" "example" {
id = "foo/bar/baz"
}

data "gitlab_group_hook" "example" {
group = data.gitlab_group.example.id
hook_id = 1
}
39 changes: 39 additions & 0 deletions internal/provider/data_source_gitlab_group_hook.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package provider

import (
"context"
"fmt"

"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/xanzy/go-gitlab"
)

var _ = registerDataSource("gitlab_group_hook", func() *schema.Resource {
return &schema.Resource{
Description: `The ` + "`gitlab_group_hook`" + ` data source allows to retrieve details about a hook in a group.
**Upstream API**: [GitLab REST API docs](https://docs.gitlab.com/ee/api/groups.html#get-group-hook)`,

ReadContext: dataSourceGitlabGroupHookRead,
Schema: datasourceSchemaFromResourceSchema(gitlabGroupHookSchema(), []string{"group", "hook_id"}, nil),
}
})

func dataSourceGitlabGroupHookRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
client := meta.(*gitlab.Client)
group := d.Get("group").(string)
hookID := d.Get("hook_id").(int)

hook, _, err := client.Groups.GetGroupHook(group, hookID, gitlab.WithContext(ctx))
if err != nil {
return diag.FromErr(err)
}

d.SetId(fmt.Sprintf("%s:%d", group, hookID))
stateMap := gitlabGroupHookToStateMap(group, hook)
if err := setStateMapInResourceData(stateMap, d); err != nil {
return diag.FromErr(err)
}
return nil
}
35 changes: 35 additions & 0 deletions internal/provider/data_source_gitlab_group_hook_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
//go:build acceptance
// +build acceptance

package provider

import (
"fmt"
"testing"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
)

func TestAccDataSourceGitlabGroupHook_basic(t *testing.T) {
testGroup := testAccCreateGroups(t, 1)[0]
testHook := testAccCreateGroupHooks(t, testGroup.ID, 1)[0]

resource.ParallelTest(t, resource.TestCase{
ProviderFactories: providerFactories,
Steps: []resource.TestStep{
{
Config: fmt.Sprintf(`
data "gitlab_group_hook" "this" {
group = "%s"
hook_id = %d
}
`, testGroup.FullPath, testHook.ID),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("data.gitlab_group_hook.this", "hook_id", fmt.Sprintf("%d", testHook.ID)),
resource.TestCheckResourceAttr("data.gitlab_group_hook.this", "group_id", fmt.Sprintf("%d", testGroup.ID)),
resource.TestCheckResourceAttr("data.gitlab_group_hook.this", "url", testHook.URL),
),
},
},
})
}
16 changes: 16 additions & 0 deletions internal/provider/helper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,22 @@ func testAccCreateGroups(t *testing.T, n int) []*gitlab.Group {
return groups
}

func testAccCreateGroupHooks(t *testing.T, gid interface{}, n int) []*gitlab.GroupHook {
t.Helper()

var hooks []*gitlab.GroupHook
for i := 0; i < n; i++ {
hook, _, err := testGitlabClient.Groups.AddGroupHook(gid, &gitlab.AddGroupHookOptions{
URL: gitlab.String(fmt.Sprintf("https://%s.com", acctest.RandomWithPrefix("acctest"))),
})
if err != nil {
t.Fatalf("could not create group hook: %v", err)
}
hooks = append(hooks, hook)
}
return hooks
}

// testAccCreateBranches is a test helper for creating a specified number of branches.
// It assumes the project will be destroyed at the end of the test and will not cleanup created branches.
func testAccCreateBranches(t *testing.T, project *gitlab.Project, n int) []*gitlab.Branch {
Expand Down

0 comments on commit 76100ed

Please sign in to comment.