Skip to content

Commit 17569db

Browse files
authored
Merge pull request #422 from gitlabhq/project-push-rules
Move gitlab_project_push_rules to gitlab_project.push_rules
2 parents 4cff1e0 + fbebc75 commit 17569db

10 files changed

+538
-488
lines changed

docs/data-sources/project.md

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ The following attributes are exported:
3333
* `path_with_namespace` - The path of the repository with namespace.
3434

3535
* `namespace_id` - The namespace (group or user) of the project. Defaults to your user.
36-
See [`gitlab_group`](../r/group.html) for an example.
36+
See [`gitlab_group`](../resources/group) for an example.
3737

3838
* `description` - A description of the project.
3939

@@ -71,4 +71,36 @@ The following attributes are exported:
7171

7272
* `remove_source_branch_after_merge` - Enable `Delete source branch` option by default for all new merge requests
7373

74-
* `packages_enabled` - Enable packages repository for the project.
74+
* `packages_enabled` - Enable packages repository for the project.
75+
76+
* `push_rules` Push rules for the project (documented below).
77+
78+
## Nested Blocks
79+
80+
### push_rules
81+
82+
For information on push rules, consult the [GitLab documentation](https://docs.gitlab.com/ce/push_rules/push_rules.html#push-rules).
83+
84+
#### Attributes
85+
86+
* `author_email_regex` - All commit author emails must match this regex, e.g. `@my-company.com$`.
87+
88+
* `branch_name_regex` - All branch names must match this regex, e.g. `(feature|hotfix)\/*`.
89+
90+
* `commit_message_regex` - All commit messages must match this regex, e.g. `Fixed \d+\..*`.
91+
92+
* `commit_message_negative_regex` - No commit message is allowed to match this regex, for example `ssh\:\/\/`.
93+
94+
* `file_name_regex` - All commited filenames must not match this regex, e.g. `(jar|exe)$`.
95+
96+
* `commit_committer_check` - Users can only push commits to this repository that were committed with one of their own verified emails.
97+
98+
* `deny_delete_tag` - Deny deleting a tag.
99+
100+
* `member_check` - Restrict commits by author (email) to existing GitLab users.
101+
102+
* `prevent_secrets` - GitLab will reject any files that are likely to contain secrets.
103+
104+
* `reject_unsigned_commits` - Reject commit when it’s not signed through GPG.
105+
106+
* `max_file_size` - Maximum file size (MB).

docs/resources/project.md

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,8 @@ The following arguments are supported:
8080

8181
* `packages_enabled` - (Optional) Enable packages repository for the project.
8282

83+
* `push_rules` (Optional) Push rules for the project (documented below).
84+
8385
## Attributes Reference
8486

8587
The following additional attributes are exported:
@@ -100,6 +102,35 @@ The following additional attributes are exported:
100102

101103
* `remove_source_branch_after_merge` - Enable `Delete source branch` option by default for all new merge requests.
102104

105+
## Nested Blocks
106+
107+
### push_rules
108+
109+
For information on push rules, consult the [GitLab documentation](https://docs.gitlab.com/ce/push_rules/push_rules.html#push-rules).
110+
111+
#### Arguments
112+
113+
* `author_email_regex` - (Optional) All commit author emails must match this regex, e.g. `@my-company.com$`.
114+
115+
* `branch_name_regex` - (Optional) All branch names must match this regex, e.g. `(feature|hotfix)\/*`.
116+
117+
* `commit_message_regex` - (Optional) All commit messages must match this regex, e.g. `Fixed \d+\..*`.
118+
119+
* `commit_message_negative_regex` - (Optional) No commit message is allowed to match this regex, for example `ssh\:\/\/`.
120+
121+
* `file_name_regex` - (Optional) All commited filenames must not match this regex, e.g. `(jar|exe)$`.
122+
123+
* `commit_committer_check` - (Optional, bool) Users can only push commits to this repository that were committed with one of their own verified emails.
124+
125+
* `deny_delete_tag` - (Optional, bool) Deny deleting a tag.
126+
127+
* `member_check` - (Optional, bool) Restrict commits by author (email) to existing GitLab users.
128+
129+
* `prevent_secrets` - (Optional, bool) GitLab will reject any files that are likely to contain secrets.
130+
131+
* `reject_unsigned_commits` - (Optional, bool) Reject commit when it’s not signed through GPG.
132+
133+
* `max_file_size` - (Optional, int) Maximum file size (MB).
103134

104135
## Importing projects
105136

docs/resources/project_push_rules.md

Lines changed: 0 additions & 63 deletions
This file was deleted.

gitlab/data_source_gitlab_project.go

Lines changed: 68 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
package gitlab
22

33
import (
4+
"errors"
45
"fmt"
56
"log"
7+
"net/http"
68

79
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
810
"github.com/xanzy/go-gitlab"
@@ -19,12 +21,10 @@ func dataSourceGitlabProject() *schema.Resource {
1921
},
2022
"name": {
2123
Type: schema.TypeString,
22-
Optional: true,
2324
Computed: true,
2425
},
2526
"path": {
2627
Type: schema.TypeString,
27-
Optional: true,
2828
Computed: true,
2929
},
3030
"path_with_namespace": {
@@ -33,89 +33,125 @@ func dataSourceGitlabProject() *schema.Resource {
3333
},
3434
"description": {
3535
Type: schema.TypeString,
36-
Optional: true,
3736
Computed: true,
3837
},
3938
"default_branch": {
4039
Type: schema.TypeString,
41-
Optional: true,
4240
Computed: true,
4341
},
4442
"request_access_enabled": {
4543
Type: schema.TypeBool,
46-
Optional: true,
4744
Computed: true,
4845
},
4946
"issues_enabled": {
5047
Type: schema.TypeBool,
51-
Optional: true,
5248
Computed: true,
5349
},
5450
"merge_requests_enabled": {
5551
Type: schema.TypeBool,
56-
Optional: true,
5752
Computed: true,
5853
},
5954
"pipelines_enabled": {
6055
Type: schema.TypeBool,
61-
Optional: true,
6256
Computed: true,
6357
},
6458
"wiki_enabled": {
6559
Type: schema.TypeBool,
66-
Optional: true,
6760
Computed: true,
6861
},
6962
"snippets_enabled": {
7063
Type: schema.TypeBool,
71-
Optional: true,
7264
Computed: true,
7365
},
7466
"lfs_enabled": {
7567
Type: schema.TypeBool,
76-
Optional: true,
7768
Computed: true,
7869
},
7970
"visibility_level": {
8071
Type: schema.TypeString,
81-
Optional: true,
8272
Computed: true,
8373
},
8474
"namespace_id": {
8575
Type: schema.TypeInt,
86-
Optional: true,
8776
Computed: true,
8877
},
8978
"ssh_url_to_repo": {
9079
Type: schema.TypeString,
91-
Optional: true,
9280
Computed: true,
9381
},
9482
"http_url_to_repo": {
9583
Type: schema.TypeString,
96-
Optional: true,
9784
Computed: true,
9885
},
9986
"web_url": {
10087
Type: schema.TypeString,
101-
Optional: true,
10288
Computed: true,
10389
},
10490
"runners_token": {
10591
Type: schema.TypeString,
106-
Optional: true,
10792
Computed: true,
10893
},
10994
"archived": {
11095
Type: schema.TypeBool,
111-
Optional: true,
11296
Computed: true,
11397
},
11498
"remove_source_branch_after_merge": {
11599
Type: schema.TypeBool,
116-
Optional: true,
117100
Computed: true,
118101
},
102+
"push_rules": {
103+
Type: schema.TypeList,
104+
MaxItems: 1,
105+
Computed: true,
106+
Elem: &schema.Resource{
107+
Schema: map[string]*schema.Schema{
108+
"author_email_regex": {
109+
Type: schema.TypeString,
110+
Computed: true,
111+
},
112+
"branch_name_regex": {
113+
Type: schema.TypeString,
114+
Computed: true,
115+
},
116+
"commit_message_regex": {
117+
Type: schema.TypeString,
118+
Computed: true,
119+
},
120+
"commit_message_negative_regex": {
121+
Type: schema.TypeString,
122+
Computed: true,
123+
},
124+
"file_name_regex": {
125+
Type: schema.TypeString,
126+
Computed: true,
127+
},
128+
"commit_committer_check": {
129+
Type: schema.TypeBool,
130+
Computed: true,
131+
},
132+
"deny_delete_tag": {
133+
Type: schema.TypeBool,
134+
Computed: true,
135+
},
136+
"member_check": {
137+
Type: schema.TypeBool,
138+
Computed: true,
139+
},
140+
"prevent_secrets": {
141+
Type: schema.TypeBool,
142+
Computed: true,
143+
},
144+
"reject_unsigned_commits": {
145+
Type: schema.TypeBool,
146+
Computed: true,
147+
},
148+
"max_file_size": {
149+
Type: schema.TypeInt,
150+
Computed: true,
151+
},
152+
},
153+
},
154+
},
119155
},
120156
}
121157
}
@@ -152,5 +188,18 @@ func dataSourceGitlabProjectRead(d *schema.ResourceData, meta interface{}) error
152188
d.Set("runners_token", found.RunnersToken)
153189
d.Set("archived", found.Archived)
154190
d.Set("remove_source_branch_after_merge", found.RemoveSourceBranchAfterMerge)
191+
192+
log.Printf("[DEBUG] Reading Gitlab project %q push rules", d.Id())
193+
194+
pushRules, _, err := client.Projects.GetProjectPushRules(d.Id())
195+
var httpError *gitlab.ErrorResponse
196+
if errors.As(err, &httpError) && httpError.Response.StatusCode == http.StatusNotFound {
197+
log.Printf("[DEBUG] Failed to get push rules for project %q: %v", d.Id(), err)
198+
} else if err != nil {
199+
return fmt.Errorf("Failed to get push rules for project %q: %w", d.Id(), err)
200+
}
201+
202+
d.Set("push_rules", flattenProjectPushRules(pushRules))
203+
155204
return nil
156205
}

0 commit comments

Comments
 (0)