Skip to content

Commit e826a32

Browse files
committed
resource/gitlab_project: fix backwards-compatibility with 14.1 regarding the squash_option. Closes #770
I gotta admit that I'm not support happy with the implementation, nor the approach in general. However, I think we agree that it's best to not break backwards compatiblity as first priority. In any case, we have #550 to discuss that :) Closes #770 Closes #776
1 parent 2dea817 commit e826a32

File tree

3 files changed

+22
-9
lines changed

3 files changed

+22
-9
lines changed

docs/resources/project.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ resource "gitlab_project" "example-two" {
7878
- **request_access_enabled** (Boolean) Allow users to request member access.
7979
- **shared_runners_enabled** (Boolean) Enable shared runners for this project.
8080
- **snippets_enabled** (Boolean) Enable snippets for the project.
81-
- **squash_option** (String) Squash commits when merge request. Valid values are `never`, `always`, `default_on`, or `default_off`. The default value is `default_off`.
81+
- **squash_option** (String) Squash commits when merge request. Valid values are `never`, `always`, `default_on`, or `default_off`. The default value is `default_off`. [GitLab >= 14.1]
8282
- **tags** (Set of String) Tags (topics) of the project.
8383
- **template_name** (String) When used without use_custom_template, name of a built-in project template. When used with use_custom_template, name of a custom project template. This option is mutually exclusive with `template_project_id`.
8484
- **template_project_id** (Number) When used with use_custom_template, project ID of a custom project template. This is preferable to using template_name since template_name may be ambiguous (enterprise edition). This option is mutually exclusive with `template_name`.

internal/provider/resource_gitlab_project.go

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ var resourceGitLabProjectSchema = map[string]*schema.Schema{
193193
Optional: true,
194194
},
195195
"squash_option": {
196-
Description: "Squash commits when merge request. Valid values are `never`, `always`, `default_on`, or `default_off`. The default value is `default_off`.",
196+
Description: "Squash commits when merge request. Valid values are `never`, `always`, `default_on`, or `default_off`. The default value is `default_off`. [GitLab >= 14.1]",
197197
Type: schema.TypeString,
198198
Optional: true,
199199
Default: "default_off",
@@ -386,7 +386,7 @@ var _ = registerResource("gitlab_project", func() *schema.Resource {
386386
}
387387
})
388388

389-
func resourceGitlabProjectSetToState(d *schema.ResourceData, project *gitlab.Project) error {
389+
func resourceGitlabProjectSetToState(client *gitlab.Client, d *schema.ResourceData, project *gitlab.Project) error {
390390
d.SetId(fmt.Sprintf("%d", project.ID))
391391
d.Set("name", project.Name)
392392
d.Set("path", project.Path)
@@ -417,7 +417,11 @@ func resourceGitlabProjectSetToState(d *schema.ResourceData, project *gitlab.Pro
417417
return err
418418
}
419419
d.Set("archived", project.Archived)
420-
d.Set("squash_option", project.SquashOption)
420+
if supportsSquashOption, err := isGitLabVersionAtLeast(client, "14.1")(); err != nil {
421+
return err
422+
} else if supportsSquashOption {
423+
d.Set("squash_option", project.SquashOption)
424+
}
421425
d.Set("remove_source_branch_after_merge", project.RemoveSourceBranchAfterMerge)
422426
d.Set("packages_enabled", project.PackagesEnabled)
423427
d.Set("pages_access_level", string(project.PagesAccessLevel))
@@ -453,7 +457,6 @@ func resourceGitlabProjectCreate(ctx context.Context, d *schema.ResourceData, me
453457
OnlyAllowMergeIfAllDiscussionsAreResolved: gitlab.Bool(d.Get("only_allow_merge_if_all_discussions_are_resolved").(bool)),
454458
AllowMergeOnSkippedPipeline: gitlab.Bool(d.Get("allow_merge_on_skipped_pipeline").(bool)),
455459
SharedRunnersEnabled: gitlab.Bool(d.Get("shared_runners_enabled").(bool)),
456-
SquashOption: stringToSquashOptionValue(d.Get("squash_option").(string)),
457460
RemoveSourceBranchAfterMerge: gitlab.Bool(d.Get("remove_source_branch_after_merge").(bool)),
458461
PackagesEnabled: gitlab.Bool(d.Get("packages_enabled").(bool)),
459462
Mirror: gitlab.Bool(d.Get("mirror").(bool)),
@@ -515,6 +518,14 @@ func resourceGitlabProjectCreate(ctx context.Context, d *schema.ResourceData, me
515518
options.CIConfigPath = gitlab.String(v.(string))
516519
}
517520

521+
if supportsSquashOption, err := isGitLabVersionAtLeast(client, "14.1")(); err != nil {
522+
return diag.FromErr(err)
523+
} else if supportsSquashOption {
524+
if v, ok := d.GetOk("squash_option"); ok {
525+
options.SquashOption = stringToSquashOptionValue(v.(string))
526+
}
527+
}
528+
518529
log.Printf("[DEBUG] create gitlab project %q", *options.Name)
519530

520531
project, _, err := client.Projects.CreateProject(options, gitlab.WithContext(ctx))
@@ -672,7 +683,7 @@ func resourceGitlabProjectRead(ctx context.Context, d *schema.ResourceData, meta
672683
return nil
673684
}
674685

675-
if err := resourceGitlabProjectSetToState(d, project); err != nil {
686+
if err := resourceGitlabProjectSetToState(client, d, project); err != nil {
676687
return diag.FromErr(err)
677688
}
678689

@@ -782,7 +793,9 @@ func resourceGitlabProjectUpdate(ctx context.Context, d *schema.ResourceData, me
782793
options.LFSEnabled = gitlab.Bool(d.Get("lfs_enabled").(bool))
783794
}
784795

785-
if d.HasChange("squash_option") {
796+
if supportsSquashOption, err := isGitLabVersionAtLeast(client, "14.1")(); err != nil {
797+
return diag.FromErr(err)
798+
} else if supportsSquashOption && d.HasChange("squash_option") {
786799
options.SquashOption = stringToSquashOptionValue(d.Get("squash_option").(string))
787800
}
788801

internal/provider/resource_gitlab_project_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -964,11 +964,11 @@ func testAccCheckAggregateGitlabProject(expected, received *gitlab.Project) reso
964964
}
965965
}
966966

967-
if err := resourceGitlabProjectSetToState(expectedData, expected); err != nil {
967+
if err := resourceGitlabProjectSetToState(testGitlabClient, expectedData, expected); err != nil {
968968
return err
969969
}
970970

971-
if err := resourceGitlabProjectSetToState(receivedData, received); err != nil {
971+
if err := resourceGitlabProjectSetToState(testGitlabClient, receivedData, received); err != nil {
972972
return err
973973
}
974974

0 commit comments

Comments
 (0)