Skip to content

Commit 9f18610

Browse files
authored
Merge pull request #1204 from timofurrer/feature/datasource/gitlab_project_hook
Implement `gitlab_project_hook` and `gitlab_project_hooks` data sources
2 parents 0a76421 + 95b745a commit 9f18610

12 files changed

+482
-118
lines changed

docs/data-sources/project_hook.md

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
---
2+
# generated by https://github.com/hashicorp/terraform-plugin-docs
3+
page_title: "gitlab_project_hook Data Source - terraform-provider-gitlab"
4+
subcategory: ""
5+
description: |-
6+
The gitlab_project_hook data source allows to retrieve details about a hook in a project.
7+
Upstream API: GitLab REST API docs https://docs.gitlab.com/ee/api/projects.html#get-project-hook
8+
---
9+
10+
# gitlab_project_hook (Data Source)
11+
12+
The `gitlab_project_hook` data source allows to retrieve details about a hook in a project.
13+
14+
**Upstream API**: [GitLab REST API docs](https://docs.gitlab.com/ee/api/projects.html#get-project-hook)
15+
16+
## Example Usage
17+
18+
```terraform
19+
data "gitlab_project" "example" {
20+
id = "foo/bar/baz"
21+
}
22+
23+
data "gitlab_project_hook" "example" {
24+
project = data.gitlab_project.example.id
25+
hook_id = 1
26+
}
27+
```
28+
29+
<!-- schema generated by tfplugindocs -->
30+
## Schema
31+
32+
### Required
33+
34+
- `hook_id` (Number) The id of the project hook.
35+
- `project` (String) The name or id of the project to add the hook to.
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+
- `id` (String) The ID of this resource.
44+
- `issues_events` (Boolean) Invoke the hook for issues events.
45+
- `job_events` (Boolean) Invoke the hook for job events.
46+
- `merge_requests_events` (Boolean) Invoke the hook for merge requests.
47+
- `note_events` (Boolean) Invoke the hook for notes events.
48+
- `pipeline_events` (Boolean) Invoke the hook for pipeline events.
49+
- `project_id` (Number) The id of the project for the hook.
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+
- `tag_push_events` (Boolean) Invoke the hook for tag push events.
54+
- `token` (String) A token to present when invoking the hook. The token is not available for imported resources.
55+
- `url` (String) The url of the hook to invoke.
56+
- `wiki_page_events` (Boolean) Invoke the hook for wiki page events.
57+
58+

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+

docs/resources/project_hook.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,9 @@ resource "gitlab_project_hook" "example" {
5151

5252
### Read-Only
5353

54+
- `hook_id` (Number) The id of the project hook.
5455
- `id` (String) The ID of this resource.
56+
- `project_id` (Number) The id of the project for the hook.
5557

5658
## Import
5759

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
data "gitlab_project" "example" {
2+
id = "foo/bar/baz"
3+
}
4+
5+
data "gitlab_project_hook" "example" {
6+
project = data.gitlab_project.example.id
7+
hook_id = 1
8+
}
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: 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_project_hook", func() *schema.Resource {
13+
return &schema.Resource{
14+
Description: `The ` + "`gitlab_project_hook`" + ` data source allows to retrieve details about a hook in a project.
15+
16+
**Upstream API**: [GitLab REST API docs](https://docs.gitlab.com/ee/api/projects.html#get-project-hook)`,
17+
18+
ReadContext: dataSourceGitlabProjectHookRead,
19+
Schema: datasourceSchemaFromResourceSchema(gitlabProjectHookSchema(), []string{"project", "hook_id"}, nil),
20+
}
21+
})
22+
23+
func dataSourceGitlabProjectHookRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
24+
client := meta.(*gitlab.Client)
25+
project := d.Get("project").(string)
26+
hookID := d.Get("hook_id").(int)
27+
28+
hook, _, err := client.Projects.GetProjectHook(project, hookID, gitlab.WithContext(ctx))
29+
if err != nil {
30+
return diag.FromErr(err)
31+
}
32+
33+
d.SetId(fmt.Sprintf("%s:%d", project, hookID))
34+
stateMap := gitlabProjectHookToStateMap(project, hook)
35+
if err := setStateMapInResourceData(stateMap, d); err != nil {
36+
return diag.FromErr(err)
37+
}
38+
return nil
39+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
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 TestAccDataSourceGitlabProjectHook_basic(t *testing.T) {
14+
testProject := testAccCreateProject(t)
15+
testHook := testAccCreateProjectHooks(t, testProject.ID, 1)[0]
16+
17+
resource.ParallelTest(t, resource.TestCase{
18+
ProviderFactories: providerFactories,
19+
Steps: []resource.TestStep{
20+
{
21+
Config: fmt.Sprintf(`
22+
data "gitlab_project_hook" "this" {
23+
project = "%s"
24+
hook_id = %d
25+
}
26+
`, testProject.PathWithNamespace, testHook.ID),
27+
Check: resource.ComposeTestCheckFunc(
28+
resource.TestCheckResourceAttr("data.gitlab_project_hook.this", "hook_id", fmt.Sprintf("%d", testHook.ID)),
29+
resource.TestCheckResourceAttr("data.gitlab_project_hook.this", "project_id", fmt.Sprintf("%d", testProject.ID)),
30+
resource.TestCheckResourceAttr("data.gitlab_project_hook.this", "url", testHook.URL),
31+
),
32+
},
33+
},
34+
})
35+
}
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+
}

internal/provider/helper_test.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -322,6 +322,22 @@ func testAccAddProjectMembers(t *testing.T, pid interface{}, users []*gitlab.Use
322322
}
323323
}
324324

325+
func testAccCreateProjectHooks(t *testing.T, pid interface{}, n int) []*gitlab.ProjectHook {
326+
t.Helper()
327+
328+
var hooks []*gitlab.ProjectHook
329+
for i := 0; i < n; i++ {
330+
hook, _, err := testGitlabClient.Projects.AddProjectHook(pid, &gitlab.AddProjectHookOptions{
331+
URL: gitlab.String(fmt.Sprintf("https://%s.com", acctest.RandomWithPrefix("acctest"))),
332+
})
333+
if err != nil {
334+
t.Fatalf("could not create project hook: %v", err)
335+
}
336+
hooks = append(hooks, hook)
337+
}
338+
return hooks
339+
}
340+
325341
func testAccCreateClusterAgents(t *testing.T, pid interface{}, n int) []*gitlab.Agent {
326342
t.Helper()
327343

0 commit comments

Comments
 (0)