Skip to content

Commit bce0b77

Browse files
committed
datasource/gitlab_group_hook: New Data Source
Refs: #680
1 parent e3d5787 commit bce0b77

File tree

5 files changed

+159
-0
lines changed

5 files changed

+159
-0
lines changed

docs/data-sources/group_hook.md

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
---
2+
# generated by https://github.com/hashicorp/terraform-plugin-docs
3+
page_title: "gitlab_group_hook Data Source - terraform-provider-gitlab"
4+
subcategory: ""
5+
description: |-
6+
The gitlab_group_hook data source allows to retrieve details about a hook in a group.
7+
Upstream API: GitLab REST API docs https://docs.gitlab.com/ee/api/groups.html#get-group-hook
8+
---
9+
10+
# gitlab_group_hook (Data Source)
11+
12+
The `gitlab_group_hook` data source allows to retrieve details about a hook in a group.
13+
14+
**Upstream API**: [GitLab REST API docs](https://docs.gitlab.com/ee/api/groups.html#get-group-hook)
15+
16+
## Example Usage
17+
18+
```terraform
19+
data "gitlab_group" "example" {
20+
id = "foo/bar/baz"
21+
}
22+
23+
data "gitlab_group_hook" "example" {
24+
group = data.gitlab_group.example.id
25+
hook_id = 1
26+
}
27+
```
28+
29+
<!-- schema generated by tfplugindocs -->
30+
## Schema
31+
32+
### Required
33+
34+
- `group` (String) The ID or full path of the group.
35+
- `hook_id` (Number) The id of the group hook.
36+
37+
### Read-Only
38+
39+
- `confidential_issues_events` (Boolean) Invoke the hook for confidential issues events.
40+
- `confidential_note_events` (Boolean) Invoke the hook for confidential notes events.
41+
- `deployment_events` (Boolean) Invoke the hook for deployment events.
42+
- `enable_ssl_verification` (Boolean) Enable ssl verification when invoking the hook.
43+
- `group_id` (Number) The id of the group for the hook.
44+
- `id` (String) The ID of this resource.
45+
- `issues_events` (Boolean) Invoke the hook for issues events.
46+
- `job_events` (Boolean) Invoke the hook for job events.
47+
- `merge_requests_events` (Boolean) Invoke the hook for merge requests.
48+
- `note_events` (Boolean) Invoke the hook for notes events.
49+
- `pipeline_events` (Boolean) Invoke the hook for pipeline events.
50+
- `push_events` (Boolean) Invoke the hook for push events.
51+
- `push_events_branch_filter` (String) Invoke the hook for push events on matching branches only.
52+
- `releases_events` (Boolean) Invoke the hook for releases events.
53+
- `subgroup_events` (Boolean) Invoke the hook for subgroup events.
54+
- `tag_push_events` (Boolean) Invoke the hook for tag push events.
55+
- `token` (String) A token to present when invoking the hook. The token is not available for imported resources.
56+
- `url` (String) The url of the hook to invoke.
57+
- `wiki_page_events` (Boolean) Invoke the hook for wiki page events.
58+
59+
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
data "gitlab_group" "example" {
2+
id = "foo/bar/baz"
3+
}
4+
5+
data "gitlab_group_hook" "example" {
6+
group = data.gitlab_group.example.id
7+
hook_id = 1
8+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package provider
2+
3+
import (
4+
"context"
5+
"fmt"
6+
7+
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
8+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
9+
"github.com/xanzy/go-gitlab"
10+
)
11+
12+
var _ = registerDataSource("gitlab_group_hook", func() *schema.Resource {
13+
return &schema.Resource{
14+
Description: `The ` + "`gitlab_group_hook`" + ` data source allows to retrieve details about a hook in a group.
15+
16+
**Upstream API**: [GitLab REST API docs](https://docs.gitlab.com/ee/api/groups.html#get-group-hook)`,
17+
18+
ReadContext: dataSourceGitlabGroupHookRead,
19+
Schema: datasourceSchemaFromResourceSchema(gitlabGroupHookSchema(), []string{"group", "hook_id"}, nil),
20+
}
21+
})
22+
23+
func dataSourceGitlabGroupHookRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
24+
client := meta.(*gitlab.Client)
25+
group := d.Get("group").(string)
26+
hookID := d.Get("hook_id").(int)
27+
28+
hook, _, err := client.Groups.GetGroupHook(group, hookID, gitlab.WithContext(ctx))
29+
if err != nil {
30+
return diag.FromErr(err)
31+
}
32+
33+
d.SetId(fmt.Sprintf("%s:%d", group, hookID))
34+
stateMap := gitlabGroupHookToStateMap(group, hook)
35+
if err := setStateMapInResourceData(stateMap, d); err != nil {
36+
return diag.FromErr(err)
37+
}
38+
return nil
39+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
//go:build acceptance
2+
// +build acceptance
3+
4+
package provider
5+
6+
import (
7+
"fmt"
8+
"testing"
9+
10+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
11+
)
12+
13+
func TestAccDataSourceGitlabGroupHook_basic(t *testing.T) {
14+
testAccCheckEE(t)
15+
16+
testGroup := testAccCreateGroups(t, 1)[0]
17+
testHook := testAccCreateGroupHooks(t, testGroup.ID, 1)[0]
18+
19+
resource.ParallelTest(t, resource.TestCase{
20+
ProviderFactories: providerFactories,
21+
Steps: []resource.TestStep{
22+
{
23+
Config: fmt.Sprintf(`
24+
data "gitlab_group_hook" "this" {
25+
group = "%s"
26+
hook_id = %d
27+
}
28+
`, testGroup.FullPath, testHook.ID),
29+
Check: resource.ComposeTestCheckFunc(
30+
resource.TestCheckResourceAttr("data.gitlab_group_hook.this", "hook_id", fmt.Sprintf("%d", testHook.ID)),
31+
resource.TestCheckResourceAttr("data.gitlab_group_hook.this", "group_id", fmt.Sprintf("%d", testGroup.ID)),
32+
resource.TestCheckResourceAttr("data.gitlab_group_hook.this", "url", testHook.URL),
33+
),
34+
},
35+
},
36+
})
37+
}

internal/provider/helper_test.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,22 @@ func testAccCreateSubGroups(t *testing.T, parentGroup *gitlab.Group, n int) []*g
249249
return groups
250250
}
251251

252+
func testAccCreateGroupHooks(t *testing.T, gid interface{}, n int) []*gitlab.GroupHook {
253+
t.Helper()
254+
255+
var hooks []*gitlab.GroupHook
256+
for i := 0; i < n; i++ {
257+
hook, _, err := testGitlabClient.Groups.AddGroupHook(gid, &gitlab.AddGroupHookOptions{
258+
URL: gitlab.String(fmt.Sprintf("https://%s.com", acctest.RandomWithPrefix("acctest"))),
259+
})
260+
if err != nil {
261+
t.Fatalf("could not create group hook: %v", err)
262+
}
263+
hooks = append(hooks, hook)
264+
}
265+
return hooks
266+
}
267+
252268
// testAccCreateBranches is a test helper for creating a specified number of branches.
253269
// It assumes the project will be destroyed at the end of the test and will not cleanup created branches.
254270
func testAccCreateBranches(t *testing.T, project *gitlab.Project, n int) []*gitlab.Branch {

0 commit comments

Comments
 (0)