Skip to content

Commit 0ce7c19

Browse files
feat: add Emails on Push service (#607)
1 parent f9c2121 commit 0ce7c19

File tree

5 files changed

+406
-0
lines changed

5 files changed

+406
-0
lines changed
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
---
2+
# generated by https://github.com/hashicorp/terraform-plugin-docs
3+
page_title: "gitlab_service_emails_on_push Resource - terraform-provider-gitlab"
4+
subcategory: ""
5+
description: |-
6+
The gitlab_service_emails_on_push resource allows to manage the lifecycle of a project integration with Emails on Push Service.
7+
Upstream API: GitLab REST API docs https://docs.gitlab.com/ee/api/integrations.html#emails-on-push
8+
---
9+
10+
# gitlab_service_emails_on_push (Resource)
11+
12+
The `gitlab_service_emails_on_push` resource allows to manage the lifecycle of a project integration with Emails on Push Service.
13+
14+
**Upstream API**: [GitLab REST API docs](https://docs.gitlab.com/ee/api/integrations.html#emails-on-push)
15+
16+
## Example Usage
17+
18+
```terraform
19+
resource "gitlab_project" "awesome_project" {
20+
name = "awesome_project"
21+
description = "My awesome project."
22+
visibility_level = "public"
23+
}
24+
25+
resource "gitlab_service_emails_on_push" "emails" {
26+
project = gitlab_project.awesome_project.id
27+
28+
}
29+
```
30+
31+
<!-- schema generated by tfplugindocs -->
32+
## Schema
33+
34+
### Required
35+
36+
- `project` (String) ID or full-path of the project you want to activate integration on.
37+
- `recipients` (String) Emails separated by whitespace.
38+
39+
### Optional
40+
41+
- `branches_to_be_notified` (String) Branches to send notifications for. Valid options are `all`, `default`, `protected`, `default_and_protected`. Notifications are always fired for tag pushes.
42+
- `disable_diffs` (Boolean) Disable code diffs.
43+
- `push_events` (Boolean) Enable notifications for push events.
44+
- `send_from_committer_email` (Boolean) Send from committer.
45+
- `tag_push_events` (Boolean) Enable notifications for tag push events.
46+
47+
### Read-Only
48+
49+
- `active` (Boolean) Whether the integration is active.
50+
- `created_at` (String) The ISO8601 date/time that this integration was activated at in UTC.
51+
- `id` (String) The ID of this resource.
52+
- `slug` (String) The name of the integration in lowercase, shortened to 63 bytes, and with everything except 0-9 and a-z replaced with -. No leading / trailing -. Use in URLs, host names and domain names.
53+
- `title` (String) Title of the integration.
54+
- `updated_at` (String) The ISO8601 date/time that this integration was last updated at in UTC.
55+
56+
## Import
57+
58+
Import is supported using the following syntax:
59+
60+
```shell
61+
# You can import a gitlab_service_emails_on_push state using the project ID, e.g.
62+
terraform import gitlab_service_emails_on_push.emails 1
63+
```
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# You can import a gitlab_service_emails_on_push state using the project ID, e.g.
2+
terraform import gitlab_service_emails_on_push.emails 1
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
resource "gitlab_project" "awesome_project" {
2+
name = "awesome_project"
3+
description = "My awesome project."
4+
visibility_level = "public"
5+
}
6+
7+
resource "gitlab_service_emails_on_push" "emails" {
8+
project = gitlab_project.awesome_project.id
9+
10+
}
Lines changed: 183 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,183 @@
1+
package provider
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"log"
7+
"time"
8+
9+
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
10+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
11+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
12+
gitlab "github.com/xanzy/go-gitlab"
13+
)
14+
15+
var validBranchesToBeNotified = []string{
16+
"all", "default", "protected", "default_and_protected",
17+
}
18+
19+
var _ = registerResource("gitlab_service_emails_on_push", func() *schema.Resource {
20+
return &schema.Resource{
21+
Description: `The ` + "`gitlab_service_emails_on_push`" + ` resource allows to manage the lifecycle of a project integration with Emails on Push Service.
22+
23+
**Upstream API**: [GitLab REST API docs](https://docs.gitlab.com/ee/api/integrations.html#emails-on-push)`,
24+
25+
CreateContext: resourceGitlabServiceEmailsOnPushCreate,
26+
ReadContext: resourceGitlabServiceEmailsOnPushRead,
27+
UpdateContext: resourceGitlabServiceEmailsOnPushCreate,
28+
DeleteContext: resourceGitlabServiceEmailsOnPushDelete,
29+
Importer: &schema.ResourceImporter{
30+
StateContext: schema.ImportStatePassthroughContext,
31+
},
32+
33+
Schema: map[string]*schema.Schema{
34+
"project": {
35+
Description: "ID or full-path of the project you want to activate integration on.",
36+
Type: schema.TypeString,
37+
Required: true,
38+
ForceNew: true,
39+
ValidateFunc: validation.StringIsNotEmpty,
40+
},
41+
"recipients": {
42+
Description: "Emails separated by whitespace.",
43+
Type: schema.TypeString,
44+
Required: true,
45+
ValidateFunc: validation.StringIsNotEmpty,
46+
},
47+
"disable_diffs": {
48+
Description: "Disable code diffs.",
49+
Type: schema.TypeBool,
50+
Optional: true,
51+
},
52+
"send_from_committer_email": {
53+
Description: "Send from committer.",
54+
Type: schema.TypeBool,
55+
Optional: true,
56+
},
57+
"push_events": {
58+
Description: "Enable notifications for push events.",
59+
Type: schema.TypeBool,
60+
Optional: true,
61+
},
62+
"tag_push_events": {
63+
Description: "Enable notifications for tag push events.",
64+
Type: schema.TypeBool,
65+
Optional: true,
66+
},
67+
"branches_to_be_notified": {
68+
Description: fmt.Sprintf("Branches to send notifications for. Valid options are %s. Notifications are always fired for tag pushes.", renderValueListForDocs(validBranchesToBeNotified)),
69+
Type: schema.TypeString,
70+
Optional: true,
71+
Default: "all",
72+
ValidateDiagFunc: validation.ToDiagFunc(validation.StringInSlice(validBranchesToBeNotified, false)),
73+
},
74+
"title": {
75+
Description: "Title of the integration.",
76+
Type: schema.TypeString,
77+
Computed: true,
78+
},
79+
"created_at": {
80+
Description: "The ISO8601 date/time that this integration was activated at in UTC.",
81+
Type: schema.TypeString,
82+
Computed: true,
83+
},
84+
"updated_at": {
85+
Description: "The ISO8601 date/time that this integration was last updated at in UTC.",
86+
Type: schema.TypeString,
87+
Computed: true,
88+
},
89+
"slug": {
90+
Description: "The name of the integration in lowercase, shortened to 63 bytes, and with everything except 0-9 and a-z replaced with -. No leading / trailing -. Use in URLs, host names and domain names.",
91+
Type: schema.TypeString,
92+
Computed: true,
93+
},
94+
"active": {
95+
Description: "Whether the integration is active.",
96+
Type: schema.TypeBool,
97+
Computed: true,
98+
},
99+
},
100+
}
101+
})
102+
103+
func resourceGitlabServiceEmailsOnPushCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
104+
client := meta.(*gitlab.Client)
105+
106+
options := &gitlab.SetEmailsOnPushServiceOptions{
107+
Recipients: gitlab.String(d.Get("recipients").(string)),
108+
}
109+
if v, ok := d.GetOk("disable_diffs"); ok {
110+
options.DisableDiffs = gitlab.Bool(v.(bool))
111+
}
112+
if v, ok := d.GetOk("send_from_committer_email"); ok {
113+
options.SendFromCommitterEmail = gitlab.Bool(v.(bool))
114+
}
115+
if v, ok := d.GetOk("push_events"); ok {
116+
options.PushEvents = gitlab.Bool(v.(bool))
117+
}
118+
if v, ok := d.GetOk("tag_push_events"); ok {
119+
options.TagPushEvents = gitlab.Bool(v.(bool))
120+
}
121+
if v, ok := d.GetOk("branches_to_be_notified"); ok {
122+
options.BranchesToBeNotified = gitlab.String(v.(string))
123+
}
124+
125+
project := d.Get("project").(string)
126+
log.Printf("[DEBUG] create gitlab emails on push service for project %s", project)
127+
128+
_, err := client.Services.SetEmailsOnPushService(project, options, gitlab.WithContext(ctx))
129+
if err != nil {
130+
return diag.FromErr(err)
131+
}
132+
d.SetId(project)
133+
134+
return resourceGitlabServiceEmailsOnPushRead(ctx, d, meta)
135+
}
136+
137+
func resourceGitlabServiceEmailsOnPushRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
138+
client := meta.(*gitlab.Client)
139+
project := d.Id()
140+
141+
log.Printf("[DEBUG] read gitlab emails on push service for project %s", project)
142+
143+
service, _, err := client.Services.GetEmailsOnPushService(project, gitlab.WithContext(ctx))
144+
if err != nil {
145+
if is404(err) {
146+
log.Printf("[DEBUG] gitlab emails on push service not found for project %s, removing from state", project)
147+
d.SetId("")
148+
return nil
149+
}
150+
return diag.FromErr(err)
151+
}
152+
153+
d.Set("project", project)
154+
d.Set("recipients", service.Properties.Recipients)
155+
d.Set("branches_to_be_notified", service.Properties.BranchesToBeNotified)
156+
d.Set("disable_diffs", service.Properties.DisableDiffs)
157+
d.Set("push_events", service.Properties.PushEvents)
158+
d.Set("send_from_committer_email", service.Properties.SendFromCommitterEmail)
159+
d.Set("tag_push_events", service.Properties.TagPushEvents)
160+
d.Set("active", service.Active)
161+
d.Set("slug", service.Slug)
162+
d.Set("title", service.Title)
163+
d.Set("created_at", service.CreatedAt.Format(time.RFC3339))
164+
if service.UpdatedAt != nil {
165+
d.Set("updated_at", service.UpdatedAt.Format(time.RFC3339))
166+
}
167+
168+
return nil
169+
}
170+
171+
func resourceGitlabServiceEmailsOnPushDelete(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
172+
client := meta.(*gitlab.Client)
173+
project := d.Id()
174+
175+
log.Printf("[DEBUG] delete gitlab emails on push service for project %s", project)
176+
177+
_, err := client.Services.DeleteEmailsOnPushService(project, gitlab.WithContext(ctx))
178+
if err != nil {
179+
return diag.FromErr(err)
180+
}
181+
182+
return nil
183+
}

0 commit comments

Comments
 (0)