Skip to content

Commit 6d609e7

Browse files
author
Julien Pivotto
committed
2 parents 9815e1c + ef7c88a commit 6d609e7

File tree

4 files changed

+165
-65
lines changed

4 files changed

+165
-65
lines changed

gitlab/resource_gitlab_project.go

Lines changed: 40 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,22 @@ func resourceGitlabProject() *schema.Resource {
7676
ValidateFunc: validation.StringInSlice([]string{"private", "internal", "public"}, true),
7777
Default: "private",
7878
},
79-
79+
"merge_method": {
80+
Type: schema.TypeString,
81+
Optional: true,
82+
ValidateFunc: validation.StringInSlice([]string{"merge", "rebase_merge", "ff"}, true),
83+
Default: "merge",
84+
},
85+
"only_allow_merge_if_pipeline_succeeds": {
86+
Type: schema.TypeBool,
87+
Optional: true,
88+
Default: false,
89+
},
90+
"only_allow_merge_if_all_discussions_are_resolved": {
91+
Type: schema.TypeBool,
92+
Optional: true,
93+
Default: false,
94+
},
8095
"ssh_url_to_repo": {
8196
Type: schema.TypeString,
8297
Computed: true,
@@ -130,8 +145,10 @@ func resourceGitlabProjectSetToState(d *schema.ResourceData, project *gitlab.Pro
130145
d.Set("wiki_enabled", project.WikiEnabled)
131146
d.Set("snippets_enabled", project.SnippetsEnabled)
132147
d.Set("visibility_level", string(project.Visibility))
148+
d.Set("merge_method", string(project.MergeMethod))
149+
d.Set("only_allow_merge_if_pipeline_succeeds", project.OnlyAllowMergeIfPipelineSucceeds)
150+
d.Set("only_allow_merge_if_all_discussions_are_resolved", project.OnlyAllowMergeIfAllDiscussionsAreResolved)
133151
d.Set("namespace_id", project.Namespace.ID)
134-
135152
d.Set("ssh_url_to_repo", project.SSHURLToRepo)
136153
d.Set("http_url_to_repo", project.HTTPURLToRepo)
137154
d.Set("web_url", project.WebURL)
@@ -142,12 +159,15 @@ func resourceGitlabProjectSetToState(d *schema.ResourceData, project *gitlab.Pro
142159
func resourceGitlabProjectCreate(d *schema.ResourceData, meta interface{}) error {
143160
client := meta.(*gitlab.Client)
144161
options := &gitlab.CreateProjectOptions{
145-
Name: gitlab.String(d.Get("name").(string)),
146-
IssuesEnabled: gitlab.Bool(d.Get("issues_enabled").(bool)),
147-
MergeRequestsEnabled: gitlab.Bool(d.Get("merge_requests_enabled").(bool)),
148-
WikiEnabled: gitlab.Bool(d.Get("wiki_enabled").(bool)),
149-
SnippetsEnabled: gitlab.Bool(d.Get("snippets_enabled").(bool)),
150-
Visibility: stringToVisibilityLevel(d.Get("visibility_level").(string)),
162+
Name: gitlab.String(d.Get("name").(string)),
163+
IssuesEnabled: gitlab.Bool(d.Get("issues_enabled").(bool)),
164+
MergeRequestsEnabled: gitlab.Bool(d.Get("merge_requests_enabled").(bool)),
165+
WikiEnabled: gitlab.Bool(d.Get("wiki_enabled").(bool)),
166+
SnippetsEnabled: gitlab.Bool(d.Get("snippets_enabled").(bool)),
167+
Visibility: stringToVisibilityLevel(d.Get("visibility_level").(string)),
168+
MergeMethod: stringToMergeMethod(d.Get("merge_method").(string)),
169+
OnlyAllowMergeIfPipelineSucceeds: gitlab.Bool(d.Get("only_allow_merge_if_pipeline_succeeds").(bool)),
170+
OnlyAllowMergeIfAllDiscussionsAreResolved: gitlab.Bool(d.Get("only_allow_merge_if_all_discussions_are_resolved").(bool)),
151171
}
152172

153173
if v, ok := d.GetOk("path"); ok {
@@ -229,6 +249,18 @@ func resourceGitlabProjectUpdate(d *schema.ResourceData, meta interface{}) error
229249
options.Visibility = stringToVisibilityLevel(d.Get("visibility_level").(string))
230250
}
231251

252+
if d.HasChange("merge_method") {
253+
options.MergeMethod = stringToMergeMethod(d.Get("merge_method").(string))
254+
}
255+
256+
if d.HasChange("only_allow_merge_if_pipeline_succeeds") {
257+
options.OnlyAllowMergeIfPipelineSucceeds = gitlab.Bool(d.Get("only_allow_merge_if_pipeline_succeeds").(bool))
258+
}
259+
260+
if d.HasChange("only_allow_merge_if_all_discussions_are_resolved") {
261+
options.OnlyAllowMergeIfAllDiscussionsAreResolved = gitlab.Bool(d.Get("only_allow_merge_if_all_discussions_are_resolved").(bool))
262+
}
263+
232264
if d.HasChange("issues_enabled") {
233265
options.IssuesEnabled = gitlab.Bool(d.Get("issues_enabled").(bool))
234266
}

gitlab/resource_gitlab_project_test.go

Lines changed: 100 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,17 @@ func TestAccGitlabProject_basic(t *testing.T) {
2525
Check: resource.ComposeTestCheckFunc(
2626
testAccCheckGitlabProjectExists("gitlab_project.foo", &project),
2727
testAccCheckGitlabProjectAttributes(&project, &testAccGitlabProjectExpectedAttributes{
28-
Name: fmt.Sprintf("foo-%d", rInt),
29-
Path: fmt.Sprintf("foo.%d", rInt),
30-
Description: "Terraform acceptance tests",
31-
IssuesEnabled: true,
32-
MergeRequestsEnabled: true,
33-
WikiEnabled: true,
34-
SnippetsEnabled: true,
35-
Visibility: gitlab.PublicVisibility,
28+
Name: fmt.Sprintf("foo-%d", rInt),
29+
Path: fmt.Sprintf("foo.%d", rInt),
30+
Description: "Terraform acceptance tests",
31+
IssuesEnabled: true,
32+
MergeRequestsEnabled: true,
33+
WikiEnabled: true,
34+
SnippetsEnabled: true,
35+
Visibility: gitlab.PublicVisibility,
36+
MergeMethod: gitlab.FastForwardMerge,
37+
OnlyAllowMergeIfPipelineSucceeds: true,
38+
OnlyAllowMergeIfAllDiscussionsAreResolved: true,
3639
}),
3740
),
3841
},
@@ -42,27 +45,33 @@ func TestAccGitlabProject_basic(t *testing.T) {
4245
Check: resource.ComposeTestCheckFunc(
4346
testAccCheckGitlabProjectExists("gitlab_project.foo", &project),
4447
testAccCheckGitlabProjectAttributes(&project, &testAccGitlabProjectExpectedAttributes{
45-
Name: fmt.Sprintf("foo-%d", rInt),
46-
Path: fmt.Sprintf("foo.%d", rInt),
47-
Description: "Terraform acceptance tests!",
48-
Visibility: gitlab.PublicVisibility,
48+
Name: fmt.Sprintf("foo-%d", rInt),
49+
Path: fmt.Sprintf("foo.%d", rInt),
50+
Description: "Terraform acceptance tests!",
51+
Visibility: gitlab.PublicVisibility,
52+
MergeMethod: gitlab.FastForwardMerge,
53+
OnlyAllowMergeIfPipelineSucceeds: true,
54+
OnlyAllowMergeIfAllDiscussionsAreResolved: true,
4955
}),
5056
),
5157
},
52-
//Update the project to turn the features on again
58+
// Update the project to turn the features on again
5359
{
5460
Config: testAccGitlabProjectConfig(rInt),
5561
Check: resource.ComposeTestCheckFunc(
5662
testAccCheckGitlabProjectExists("gitlab_project.foo", &project),
5763
testAccCheckGitlabProjectAttributes(&project, &testAccGitlabProjectExpectedAttributes{
58-
Name: fmt.Sprintf("foo-%d", rInt),
59-
Path: fmt.Sprintf("foo.%d", rInt),
60-
Description: "Terraform acceptance tests",
61-
IssuesEnabled: true,
62-
MergeRequestsEnabled: true,
63-
WikiEnabled: true,
64-
SnippetsEnabled: true,
65-
Visibility: gitlab.PublicVisibility,
64+
Name: fmt.Sprintf("foo-%d", rInt),
65+
Path: fmt.Sprintf("foo.%d", rInt),
66+
Description: "Terraform acceptance tests",
67+
IssuesEnabled: true,
68+
MergeRequestsEnabled: true,
69+
WikiEnabled: true,
70+
SnippetsEnabled: true,
71+
Visibility: gitlab.PublicVisibility,
72+
MergeMethod: gitlab.FastForwardMerge,
73+
OnlyAllowMergeIfPipelineSucceeds: true,
74+
OnlyAllowMergeIfAllDiscussionsAreResolved: true,
6675
}),
6776
),
6877
},
@@ -72,14 +81,17 @@ func TestAccGitlabProject_basic(t *testing.T) {
7281
Check: resource.ComposeTestCheckFunc(
7382
testAccCheckGitlabProjectExists("gitlab_project.foo", &project),
7483
testAccCheckGitlabProjectAttributes(&project, &testAccGitlabProjectExpectedAttributes{
75-
Name: fmt.Sprintf("foo-%d", rInt),
76-
Path: fmt.Sprintf("foo.%d", rInt),
77-
Description: "Terraform acceptance tests",
78-
IssuesEnabled: true,
79-
MergeRequestsEnabled: true,
80-
WikiEnabled: true,
81-
SnippetsEnabled: true,
82-
Visibility: gitlab.PublicVisibility,
84+
Name: fmt.Sprintf("foo-%d", rInt),
85+
Path: fmt.Sprintf("foo.%d", rInt),
86+
Description: "Terraform acceptance tests",
87+
IssuesEnabled: true,
88+
MergeRequestsEnabled: true,
89+
WikiEnabled: true,
90+
SnippetsEnabled: true,
91+
Visibility: gitlab.PublicVisibility,
92+
MergeMethod: gitlab.FastForwardMerge,
93+
OnlyAllowMergeIfPipelineSucceeds: false,
94+
OnlyAllowMergeIfAllDiscussionsAreResolved: false,
8395
SharedWithGroups: []struct {
8496
GroupID int
8597
GroupName string
@@ -94,14 +106,17 @@ func TestAccGitlabProject_basic(t *testing.T) {
94106
Check: resource.ComposeTestCheckFunc(
95107
testAccCheckGitlabProjectExists("gitlab_project.foo", &project),
96108
testAccCheckGitlabProjectAttributes(&project, &testAccGitlabProjectExpectedAttributes{
97-
Name: fmt.Sprintf("foo-%d", rInt),
98-
Path: fmt.Sprintf("foo.%d", rInt),
99-
Description: "Terraform acceptance tests",
100-
IssuesEnabled: true,
101-
MergeRequestsEnabled: true,
102-
WikiEnabled: true,
103-
SnippetsEnabled: true,
104-
Visibility: gitlab.PublicVisibility,
109+
Name: fmt.Sprintf("foo-%d", rInt),
110+
Path: fmt.Sprintf("foo.%d", rInt),
111+
Description: "Terraform acceptance tests",
112+
IssuesEnabled: true,
113+
MergeRequestsEnabled: true,
114+
WikiEnabled: true,
115+
SnippetsEnabled: true,
116+
Visibility: gitlab.PublicVisibility,
117+
MergeMethod: gitlab.FastForwardMerge,
118+
OnlyAllowMergeIfPipelineSucceeds: false,
119+
OnlyAllowMergeIfAllDiscussionsAreResolved: false,
105120
SharedWithGroups: []struct {
106121
GroupID int
107122
GroupName string
@@ -116,14 +131,17 @@ func TestAccGitlabProject_basic(t *testing.T) {
116131
Check: resource.ComposeTestCheckFunc(
117132
testAccCheckGitlabProjectExists("gitlab_project.foo", &project),
118133
testAccCheckGitlabProjectAttributes(&project, &testAccGitlabProjectExpectedAttributes{
119-
Name: fmt.Sprintf("foo-%d", rInt),
120-
Path: fmt.Sprintf("foo.%d", rInt),
121-
Description: "Terraform acceptance tests",
122-
IssuesEnabled: true,
123-
MergeRequestsEnabled: true,
124-
WikiEnabled: true,
125-
SnippetsEnabled: true,
126-
Visibility: gitlab.PublicVisibility,
134+
Name: fmt.Sprintf("foo-%d", rInt),
135+
Path: fmt.Sprintf("foo.%d", rInt),
136+
Description: "Terraform acceptance tests",
137+
IssuesEnabled: true,
138+
MergeRequestsEnabled: true,
139+
WikiEnabled: true,
140+
SnippetsEnabled: true,
141+
Visibility: gitlab.PublicVisibility,
142+
MergeMethod: gitlab.FastForwardMerge,
143+
OnlyAllowMergeIfPipelineSucceeds: true,
144+
OnlyAllowMergeIfAllDiscussionsAreResolved: true,
127145
SharedWithGroups: []struct {
128146
GroupID int
129147
GroupName string
@@ -199,16 +217,19 @@ func testAccCheckGitlabProjectExists(n string, project *gitlab.Project) resource
199217
}
200218

201219
type testAccGitlabProjectExpectedAttributes struct {
202-
Name string
203-
Path string
204-
Description string
205-
DefaultBranch string
206-
IssuesEnabled bool
207-
MergeRequestsEnabled bool
208-
WikiEnabled bool
209-
SnippetsEnabled bool
210-
Visibility gitlab.VisibilityValue
211-
SharedWithGroups []struct {
220+
Name string
221+
Path string
222+
Description string
223+
DefaultBranch string
224+
IssuesEnabled bool
225+
MergeRequestsEnabled bool
226+
WikiEnabled bool
227+
SnippetsEnabled bool
228+
Visibility gitlab.VisibilityValue
229+
MergeMethod gitlab.MergeMethodValue
230+
OnlyAllowMergeIfPipelineSucceeds bool
231+
OnlyAllowMergeIfAllDiscussionsAreResolved bool
232+
SharedWithGroups []struct {
212233
GroupID int
213234
GroupName string
214235
GroupAccessLevel int
@@ -264,6 +285,18 @@ func testAccCheckGitlabProjectAttributes(project *gitlab.Project, want *testAccG
264285
return fmt.Errorf("got shared with groups: %v; want %v", project.SharedWithGroups, want.SharedWithGroups)
265286
}
266287

288+
if project.MergeMethod != want.MergeMethod {
289+
return fmt.Errorf("got merge_method %q; want %q", project.MergeMethod, want.MergeMethod)
290+
}
291+
292+
if project.OnlyAllowMergeIfPipelineSucceeds != want.OnlyAllowMergeIfPipelineSucceeds {
293+
return fmt.Errorf("got only_allow_merge_if_pipeline_succeeds %t; want %t", project.OnlyAllowMergeIfPipelineSucceeds, want.OnlyAllowMergeIfPipelineSucceeds)
294+
}
295+
296+
if project.OnlyAllowMergeIfAllDiscussionsAreResolved != want.OnlyAllowMergeIfAllDiscussionsAreResolved {
297+
return fmt.Errorf("got only_allow_merge_if_all_discussions_are_resolved %t; want %t", project.OnlyAllowMergeIfAllDiscussionsAreResolved, want.OnlyAllowMergeIfAllDiscussionsAreResolved)
298+
}
299+
267300
return nil
268301
}
269302
}
@@ -320,6 +353,9 @@ resource "gitlab_project" "foo" {
320353
# So that acceptance tests can be run in a gitlab organization
321354
# with no billing
322355
visibility_level = "public"
356+
merge_method = "ff"
357+
only_allow_merge_if_pipeline_succeeds = true
358+
only_allow_merge_if_all_discussions_are_resolved = true
323359
}
324360
`, rInt, rInt)
325361
}
@@ -334,6 +370,9 @@ resource "gitlab_project" "foo" {
334370
# So that acceptance tests can be run in a gitlab organization
335371
# with no billing
336372
visibility_level = "public"
373+
merge_method = "ff"
374+
only_allow_merge_if_pipeline_succeeds = true
375+
only_allow_merge_if_all_discussions_are_resolved = true
337376
338377
issues_enabled = false
339378
merge_requests_enabled = false
@@ -350,6 +389,9 @@ resource "gitlab_project" "foo" {
350389
path = "foo.%d"
351390
description = "Terraform acceptance tests"
352391
visibility_level = "public"
392+
merge_method = "ff"
393+
only_allow_merge_if_pipeline_succeeds = false
394+
only_allow_merge_if_all_discussions_are_resolved = false
353395
354396
shared_with_groups = [
355397
{
@@ -375,6 +417,9 @@ resource "gitlab_project" "foo" {
375417
path = "foo.%d"
376418
description = "Terraform acceptance tests"
377419
visibility_level = "public"
420+
merge_method = "ff"
421+
only_allow_merge_if_pipeline_succeeds = false
422+
only_allow_merge_if_all_discussions_are_resolved = false
378423
379424
shared_with_groups = [
380425
{

gitlab/util.go

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,12 @@ package gitlab
33
import (
44
"fmt"
55

6-
"github.com/hashicorp/terraform/helper/schema"
7-
"github.com/xanzy/go-gitlab"
86
"regexp"
97
"strings"
108
"time"
9+
10+
"github.com/hashicorp/terraform/helper/schema"
11+
"github.com/xanzy/go-gitlab"
1112
)
1213

1314
var accessLevelNameToValue = map[string]gitlab.AccessLevelValue{
@@ -76,6 +77,20 @@ func stringToVisibilityLevel(s string) *gitlab.VisibilityValue {
7677
return &value
7778
}
7879

80+
func stringToMergeMethod(s string) *gitlab.MergeMethodValue {
81+
lookup := map[string]gitlab.MergeMethodValue{
82+
"merge": gitlab.NoFastForwardMerge,
83+
"ff": gitlab.FastForwardMerge,
84+
"rebase_merge": gitlab.RebaseMerge,
85+
}
86+
87+
value, ok := lookup[s]
88+
if !ok {
89+
return nil
90+
}
91+
return &value
92+
}
93+
7994
func StringIsGitlabVariableName() schema.SchemaValidateFunc {
8095
return func(v interface{}, k string) (s []string, es []error) {
8196
value, ok := v.(string)

website/docs/r/project.html.markdown

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,14 @@ The following arguments are supported:
5050
Valid values are `private`, `internal`, `public`.
5151
Repositories are created as private by default.
5252

53+
* `merge_method` - (Optional) Set to `ff` to create fast-forward merges
54+
Valid values are `merge`, `rebase_merge`, `ff`
55+
Repositories are created with `merge` by default
56+
57+
* `only_allow_merge_if_pipeline_succeeds` - (Optional) Set to true if you want allow merges only if a pipeline succeeds.
58+
59+
* `only_allow_merge_if_all_discussions_are_resolved` - (Optional) Set to true if you want allow merges only if all discussions are resolved.
60+
5361
* `shared_with_groups` - (Optional) Enable sharing the project with a list of groups (maps).
5462
* `group_id` - (Required) Group id of the group you want to share the project with.
5563
* `group_access_level` - (Optional) Group's sharing permissions. See [group members permission][group_members_permissions] for more info.

0 commit comments

Comments
 (0)