Skip to content

Commit de7ad22

Browse files
mkjmdskiKoLiBerdependabot[bot]
authored
Add squash option (#719)
* feat: add squash_option to project resource * Bump github.com/xanzy/go-gitlab from 0.50.0 to 0.50.4 Bumps [github.com/xanzy/go-gitlab](https://github.com/xanzy/go-gitlab) from 0.50.0 to 0.50.4. - [Release notes](https://github.com/xanzy/go-gitlab/releases) - [Changelog](https://github.com/xanzy/go-gitlab/blob/master/releases_test.go) - [Commits](xanzy/go-gitlab@v0.50.0...v0.50.4) --- updated-dependencies: - dependency-name: github.com/xanzy/go-gitlab dependency-type: direct:production update-type: version-update:semver-patch ... Signed-off-by: dependabot[bot] <[email protected]> * fix: check change of SquashOption * docs: add valid options to squash_option * test: add unit test for squash_option * fix: change squash_option default value * Add data source and make test check the change in the config * add missing arguments for bump * fix * fix * conflicts * fmt * convert types * bump to v0.51.0 * fmt * fix * make linter happy * bump to 0.51.1 * fix * restore previous concept * fix test * fix test * fix? * fix typo in mapping * fix tests * add validation * fmt Co-authored-by: KoLiBer <[email protected]> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
1 parent 13a8415 commit de7ad22

File tree

4 files changed

+46
-11
lines changed

4 files changed

+46
-11
lines changed

docs/resources/project.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,8 @@ consult the [gitlab documentation](https://docs.gitlab.com/ee/user/project/repos
9191

9292
* `initialize_with_readme` - (Optional) Create main branch with first commit containing a README.md file.
9393

94+
* `squash_option` - (Optional) Squash commits when merge request. Valid values are `never`, `always`, `default_on`, or `default_off`. The default value is `default_off`.
95+
9496
* `packages_enabled` - (Optional) Enable packages repository for the project.
9597

9698
* `push_rules` (Optional) Push rules for the project (documented below).

gitlab/resource_gitlab_project.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,12 @@ var resourceGitLabProjectSchema = map[string]*schema.Schema{
162162
Type: schema.TypeBool,
163163
Optional: true,
164164
},
165+
"squash_option": {
166+
Type: schema.TypeString,
167+
Optional: true,
168+
Default: "default_off",
169+
ValidateFunc: validation.StringInSlice([]string{"never", "default_on", "always", "default_off"}, true),
170+
},
165171
"remove_source_branch_after_merge": {
166172
Type: schema.TypeBool,
167173
Optional: true,
@@ -332,6 +338,7 @@ func resourceGitlabProjectSetToState(d *schema.ResourceData, project *gitlab.Pro
332338
return err
333339
}
334340
d.Set("archived", project.Archived)
341+
d.Set("squash_option", project.SquashOption)
335342
d.Set("remove_source_branch_after_merge", project.RemoveSourceBranchAfterMerge)
336343
d.Set("packages_enabled", project.PackagesEnabled)
337344
d.Set("pages_access_level", string(project.PagesAccessLevel))
@@ -364,6 +371,7 @@ func resourceGitlabProjectCreate(d *schema.ResourceData, meta interface{}) error
364371
OnlyAllowMergeIfAllDiscussionsAreResolved: gitlab.Bool(d.Get("only_allow_merge_if_all_discussions_are_resolved").(bool)),
365372
AllowMergeOnSkippedPipeline: gitlab.Bool(d.Get("allow_merge_on_skipped_pipeline").(bool)),
366373
SharedRunnersEnabled: gitlab.Bool(d.Get("shared_runners_enabled").(bool)),
374+
SquashOption: stringToSquashOptionValue(d.Get("squash_option").(string)),
367375
RemoveSourceBranchAfterMerge: gitlab.Bool(d.Get("remove_source_branch_after_merge").(bool)),
368376
PackagesEnabled: gitlab.Bool(d.Get("packages_enabled").(bool)),
369377
Mirror: gitlab.Bool(d.Get("mirror").(bool)),
@@ -671,6 +679,10 @@ func resourceGitlabProjectUpdate(d *schema.ResourceData, meta interface{}) error
671679
options.LFSEnabled = gitlab.Bool(d.Get("lfs_enabled").(bool))
672680
}
673681

682+
if d.HasChange("squash_option") {
683+
options.SquashOption = stringToSquashOptionValue(d.Get("squash_option").(string))
684+
}
685+
674686
if d.HasChange("remove_source_branch_after_merge") {
675687
options.RemoveSourceBranchAfterMerge = gitlab.Bool(d.Get("remove_source_branch_after_merge").(bool))
676688
}

gitlab/resource_gitlab_project_test.go

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -44,12 +44,13 @@ func TestAccGitlabProject_basic(t *testing.T) {
4444
MergeMethod: gitlab.FastForwardMerge,
4545
OnlyAllowMergeIfPipelineSucceeds: true,
4646
OnlyAllowMergeIfAllDiscussionsAreResolved: true,
47-
AllowMergeOnSkippedPipeline: false,
48-
Archived: false, // needless, but let's make this explicit
49-
PackagesEnabled: true,
50-
PagesAccessLevel: gitlab.PublicAccessControl,
51-
BuildCoverageRegex: "foo",
52-
CIConfigPath: ".gitlab-ci.yml@mynamespace/myproject",
47+
SquashOption: gitlab.SquashOptionDefaultOff,
48+
AllowMergeOnSkippedPipeline: false,
49+
Archived: false, // needless, but let's make this explicit
50+
PackagesEnabled: true,
51+
PagesAccessLevel: gitlab.PublicAccessControl,
52+
BuildCoverageRegex: "foo",
53+
CIConfigPath: ".gitlab-ci.yml@mynamespace/myproject",
5354
}
5455

5556
defaultsMainBranch = defaults
@@ -89,11 +90,12 @@ func TestAccGitlabProject_basic(t *testing.T) {
8990
MergeMethod: gitlab.FastForwardMerge,
9091
OnlyAllowMergeIfPipelineSucceeds: true,
9192
OnlyAllowMergeIfAllDiscussionsAreResolved: true,
92-
AllowMergeOnSkippedPipeline: true,
93-
Archived: true,
94-
PackagesEnabled: false,
95-
PagesAccessLevel: gitlab.DisabledAccessControl,
96-
BuildCoverageRegex: "bar",
93+
SquashOption: gitlab.SquashOptionDefaultOn,
94+
AllowMergeOnSkippedPipeline: true,
95+
Archived: true,
96+
PackagesEnabled: false,
97+
PagesAccessLevel: gitlab.DisabledAccessControl,
98+
BuildCoverageRegex: "bar",
9799
}, &received),
98100
),
99101
},
@@ -362,6 +364,7 @@ func TestAccGitlabProject_willError(t *testing.T) {
362364
MergeMethod: gitlab.FastForwardMerge,
363365
OnlyAllowMergeIfPipelineSucceeds: true,
364366
OnlyAllowMergeIfAllDiscussionsAreResolved: true,
367+
SquashOption: gitlab.SquashOptionDefaultOff,
365368
PackagesEnabled: true,
366369
PagesAccessLevel: gitlab.PublicAccessControl,
367370
BuildCoverageRegex: "foo",
@@ -465,6 +468,7 @@ func TestAccGitlabProject_transfer(t *testing.T) {
465468
MergeMethod: gitlab.NoFastForwardMerge,
466469
OnlyAllowMergeIfPipelineSucceeds: false,
467470
OnlyAllowMergeIfAllDiscussionsAreResolved: false,
471+
SquashOption: gitlab.SquashOptionDefaultOff,
468472
PackagesEnabled: true,
469473
PagesAccessLevel: gitlab.PrivateAccessControl,
470474
BuildCoverageRegex: "foo",
@@ -999,6 +1003,7 @@ resource "gitlab_project" "foo" {
9991003
merge_method = "ff"
10001004
only_allow_merge_if_pipeline_succeeds = true
10011005
only_allow_merge_if_all_discussions_are_resolved = true
1006+
squash_option = "default_off"
10021007
pages_access_level = "public"
10031008
build_coverage_regex = "foo"
10041009
allow_merge_on_skipped_pipeline = false
@@ -1056,6 +1061,7 @@ resource "gitlab_project" "foo" {
10561061
merge_method = "ff"
10571062
only_allow_merge_if_pipeline_succeeds = true
10581063
only_allow_merge_if_all_discussions_are_resolved = true
1064+
squash_option = "default_on"
10591065
allow_merge_on_skipped_pipeline = true
10601066
10611067
request_access_enabled = false

gitlab/util.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,21 @@ func stringToMergeMethod(s string) *gitlab.MergeMethodValue {
143143
return &value
144144
}
145145

146+
func stringToSquashOptionValue(s string) *gitlab.SquashOptionValue {
147+
lookup := map[string]gitlab.SquashOptionValue{
148+
"never": gitlab.SquashOptionNever,
149+
"always": gitlab.SquashOptionAlways,
150+
"default_on": gitlab.SquashOptionDefaultOn,
151+
"default_off": gitlab.SquashOptionDefaultOff,
152+
}
153+
154+
value, ok := lookup[s]
155+
if !ok {
156+
return nil
157+
}
158+
return &value
159+
}
160+
146161
func stringToAccessControlValue(s string) *gitlab.AccessControlValue {
147162
lookup := map[string]gitlab.AccessControlValue{
148163
"disabled": gitlab.DisabledAccessControl,

0 commit comments

Comments
 (0)