Skip to content

Commit 94b8252

Browse files
committed
datasource/gitlab_project_hook: New Data Source
Closes: #1103
1 parent 6f4b341 commit 94b8252

File tree

6 files changed

+157
-1
lines changed

6 files changed

+157
-1
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+
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: 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+
}

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

internal/provider/schema_gitlab_project_hook.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ func gitlabProjectHookSchema() map[string]*schema.Schema {
1818
Computed: true,
1919
},
2020
"hook_id": {
21-
Description: "The id of the project hook",
21+
Description: "The id of the project hook.",
2222
Type: schema.TypeInt,
2323
Computed: true,
2424
},

0 commit comments

Comments
 (0)