Skip to content

Commit 95b745a

Browse files
committed
datasource/gitlab_project_hooks: New Data Source
Closes: #1103
1 parent 94b8252 commit 95b745a

File tree

4 files changed

+175
-0
lines changed

4 files changed

+175
-0
lines changed

docs/data-sources/project_hooks.md

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
---
2+
# generated by https://github.com/hashicorp/terraform-plugin-docs
3+
page_title: "gitlab_project_hooks Data Source - terraform-provider-gitlab"
4+
subcategory: ""
5+
description: |-
6+
The gitlab_project_hooks data source allows to retrieve details about hooks in a project.
7+
Upstream API: GitLab REST API docs https://docs.gitlab.com/ee/api/projects.html#list-project-hooks
8+
---
9+
10+
# gitlab_project_hooks (Data Source)
11+
12+
The `gitlab_project_hooks` data source allows to retrieve details about hooks in a project.
13+
14+
**Upstream API**: [GitLab REST API docs](https://docs.gitlab.com/ee/api/projects.html#list-project-hooks)
15+
16+
## Example Usage
17+
18+
```terraform
19+
data "gitlab_project" "example" {
20+
id = "foo/bar/baz"
21+
}
22+
23+
data "gitlab_project_hooks" "examples" {
24+
project = data.gitlab_project.example.id
25+
}
26+
```
27+
28+
<!-- schema generated by tfplugindocs -->
29+
## Schema
30+
31+
### Required
32+
33+
- `project` (String) The name or id of the project.
34+
35+
### Read-Only
36+
37+
- `hooks` (List of Object) The list of hooks. (see [below for nested schema](#nestedatt--hooks))
38+
- `id` (String) The ID of this resource.
39+
40+
<a id="nestedatt--hooks"></a>
41+
### Nested Schema for `hooks`
42+
43+
Read-Only:
44+
45+
- `confidential_issues_events` (Boolean)
46+
- `confidential_note_events` (Boolean)
47+
- `deployment_events` (Boolean)
48+
- `enable_ssl_verification` (Boolean)
49+
- `hook_id` (Number)
50+
- `issues_events` (Boolean)
51+
- `job_events` (Boolean)
52+
- `merge_requests_events` (Boolean)
53+
- `note_events` (Boolean)
54+
- `pipeline_events` (Boolean)
55+
- `project` (String)
56+
- `project_id` (Number)
57+
- `push_events` (Boolean)
58+
- `push_events_branch_filter` (String)
59+
- `releases_events` (Boolean)
60+
- `tag_push_events` (Boolean)
61+
- `token` (String)
62+
- `url` (String)
63+
- `wiki_page_events` (Boolean)
64+
65+
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
data "gitlab_project" "example" {
2+
id = "foo/bar/baz"
3+
}
4+
5+
data "gitlab_project_hooks" "examples" {
6+
project = data.gitlab_project.example.id
7+
}
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
package provider
2+
3+
import (
4+
"context"
5+
6+
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
7+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
8+
"github.com/xanzy/go-gitlab"
9+
)
10+
11+
var _ = registerDataSource("gitlab_project_hooks", func() *schema.Resource {
12+
return &schema.Resource{
13+
Description: `The ` + "`gitlab_project_hooks`" + ` data source allows to retrieve details about hooks in a project.
14+
15+
**Upstream API**: [GitLab REST API docs](https://docs.gitlab.com/ee/api/projects.html#list-project-hooks)`,
16+
17+
ReadContext: dataSourceGitlabProjectHooksRead,
18+
Schema: map[string]*schema.Schema{
19+
"project": {
20+
Description: "The name or id of the project.",
21+
Type: schema.TypeString,
22+
Required: true,
23+
},
24+
"hooks": {
25+
Description: "The list of hooks.",
26+
Type: schema.TypeList,
27+
Computed: true,
28+
Elem: &schema.Resource{
29+
Schema: datasourceSchemaFromResourceSchema(gitlabProjectHookSchema(), nil, nil),
30+
},
31+
},
32+
},
33+
}
34+
})
35+
36+
func dataSourceGitlabProjectHooksRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
37+
client := meta.(*gitlab.Client)
38+
39+
project := d.Get("project").(string)
40+
options := gitlab.ListProjectHooksOptions{
41+
PerPage: 20,
42+
Page: 1,
43+
}
44+
45+
var hooks []*gitlab.ProjectHook
46+
for options.Page != 0 {
47+
paginatedHooks, resp, err := client.Projects.ListProjectHooks(project, &options, gitlab.WithContext(ctx))
48+
if err != nil {
49+
return diag.FromErr(err)
50+
}
51+
52+
hooks = append(hooks, paginatedHooks...)
53+
options.Page = resp.NextPage
54+
}
55+
56+
d.SetId(project)
57+
if err := d.Set("hooks", flattenGitlabProjectHooks(project, hooks)); err != nil {
58+
return diag.Errorf("failed to set hooks to state: %v", err)
59+
}
60+
61+
return nil
62+
}
63+
64+
func flattenGitlabProjectHooks(project string, hooks []*gitlab.ProjectHook) (values []map[string]interface{}) {
65+
for _, hook := range hooks {
66+
values = append(values, gitlabProjectHookToStateMap(project, hook))
67+
}
68+
return values
69+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
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 TestAccDataSourceGitlabProjectHooks_basic(t *testing.T) {
14+
testProject := testAccCreateProject(t)
15+
testHooks := testAccCreateProjectHooks(t, testProject.ID, 25)
16+
17+
resource.ParallelTest(t, resource.TestCase{
18+
ProviderFactories: providerFactories,
19+
Steps: []resource.TestStep{
20+
{
21+
Config: fmt.Sprintf(`
22+
data "gitlab_project_hooks" "this" {
23+
project = "%s"
24+
}
25+
`, testProject.PathWithNamespace),
26+
Check: resource.ComposeTestCheckFunc(
27+
resource.TestCheckResourceAttr("data.gitlab_project_hooks.this", "hooks.#", fmt.Sprintf("%d", len(testHooks))),
28+
resource.TestCheckResourceAttr("data.gitlab_project_hooks.this", "hooks.0.url", testHooks[0].URL),
29+
resource.TestCheckResourceAttr("data.gitlab_project_hooks.this", "hooks.1.url", testHooks[1].URL),
30+
),
31+
},
32+
},
33+
})
34+
}

0 commit comments

Comments
 (0)