Skip to content

Commit 35529c7

Browse files
committed
resource/gitlab_project: Fix project creation when default branch protection is disabled on instance-level
Closes: #1127
1 parent 89d8c48 commit 35529c7

File tree

2 files changed

+109
-4
lines changed

2 files changed

+109
-4
lines changed

internal/provider/resource_gitlab_project.go

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1084,7 +1084,7 @@ func resourceGitlabProjectCreate(ctx context.Context, d *schema.ResourceData, me
10841084
// disabled (`default_branch_protection = 0`) then we don't have to wait for one.
10851085
waitForDefaultBranchProtection, err := expectDefaultBranchProtection(ctx, client, project)
10861086
if err != nil {
1087-
return diag.Errorf("Failed to fetch group the project %d is owned by: %+v", project.ID, err)
1087+
return diag.Errorf("Failed to discover if branch protection is enabled by default or not for project %d: %+v", project.ID, err)
10881088
}
10891089

10901090
if waitForDefaultBranchProtection {
@@ -1766,6 +1766,7 @@ func namespaceOrPathChanged(ctx context.Context, d *schema.ResourceDiff, meta in
17661766
}
17671767

17681768
func expectDefaultBranchProtection(ctx context.Context, client *gitlab.Client, project *gitlab.Project) (bool, error) {
1769+
// If the project is part of a group it may have default branch protection disabled for its projects
17691770
if project.Namespace.Kind == "group" {
17701771
group, _, err := client.Groups.GetGroup(project.Namespace.ID, nil, gitlab.WithContext(ctx))
17711772
if err != nil {
@@ -1775,7 +1776,11 @@ func expectDefaultBranchProtection(ctx context.Context, client *gitlab.Client, p
17751776
return group.DefaultBranchProtection != 0, nil
17761777
}
17771778

1778-
// projects which are not assigned to a group can't have a "no branch protection" default,
1779-
// thus, we always expect a default branch protection.
1780-
return true, nil
1779+
// // If the project is not part of a group it may have default branch protection disabled because of the instance-wide application settings
1780+
settings, _, err := client.Settings.GetSettings(nil, gitlab.WithContext(ctx))
1781+
if err != nil {
1782+
return false, err
1783+
}
1784+
1785+
return settings.DefaultBranchProtection != 0, nil
17811786
}

internal/provider/resource_gitlab_project_test.go

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -844,6 +844,13 @@ resource "gitlab_project" "foo" {
844844
},
845845
),
846846
},
847+
// Verify Import
848+
{
849+
ResourceName: "gitlab_project.foo",
850+
ImportState: true,
851+
ImportStateVerify: true,
852+
ImportStateVerifyIgnore: []string{"initialize_with_readme"},
853+
},
847854
},
848855
})
849856
}
@@ -883,6 +890,99 @@ func TestAccGitlabProject_CreateProjectInUserNamespace(t *testing.T) {
883890
})
884891
}
885892

893+
func TestAccGitlabProject_InstanceBranchProtectionDisabled(t *testing.T) {
894+
rInt := acctest.RandInt()
895+
896+
resource.Test(t, resource.TestCase{
897+
ProviderFactories: providerFactories,
898+
CheckDestroy: testAccCheckGitlabProjectDestroy,
899+
Steps: []resource.TestStep{
900+
{
901+
PreConfig: func() {
902+
settings, _, err := testGitlabClient.Settings.GetSettings()
903+
if err != nil {
904+
t.Fatalf("failed to get settings: %v", err)
905+
}
906+
t.Cleanup(func() {
907+
if _, _, err := testGitlabClient.Settings.UpdateSettings(&gitlab.UpdateSettingsOptions{DefaultBranchProtection: gitlab.Int(settings.DefaultBranchProtection)}); err != nil {
908+
t.Fatalf("failed to update instance-wide default branch protection setting to default: %v", err)
909+
}
910+
})
911+
912+
if _, _, err := testGitlabClient.Settings.UpdateSettings(&gitlab.UpdateSettingsOptions{DefaultBranchProtection: gitlab.Int(0)}); err != nil {
913+
t.Fatalf("failed to update instance-wide default branch protection setting: %v", err)
914+
}
915+
},
916+
Config: ` `, // requires a space for empty config
917+
},
918+
// Without explicit default branch
919+
{
920+
Config: fmt.Sprintf(`
921+
resource "gitlab_project" "foo" {
922+
name = "foo-%d"
923+
description = "Terraform acceptance tests"
924+
visibility_level = "public"
925+
initialize_with_readme = true
926+
}
927+
`, rInt),
928+
},
929+
// Verify Import
930+
{
931+
ResourceName: "gitlab_project.foo",
932+
ImportState: true,
933+
ImportStateVerify: true,
934+
ImportStateVerifyIgnore: []string{"initialize_with_readme"},
935+
},
936+
// Force a destroy for the project so that it can be recreated as the same resource
937+
{
938+
Config: ` `, // requires a space for empty config
939+
},
940+
// With explicit default branch set to instance-wide default
941+
{
942+
Config: fmt.Sprintf(`
943+
resource "gitlab_project" "foo" {
944+
name = "foo-%d"
945+
description = "Terraform acceptance tests"
946+
visibility_level = "public"
947+
default_branch = "main"
948+
initialize_with_readme = true
949+
}
950+
`, rInt),
951+
},
952+
// Verify Import
953+
{
954+
ResourceName: "gitlab_project.foo",
955+
ImportState: true,
956+
ImportStateVerify: true,
957+
ImportStateVerifyIgnore: []string{"initialize_with_readme"},
958+
},
959+
// Force a destroy for the project so that it can be recreated as the same resource
960+
{
961+
Config: ` `, // requires a space for empty config
962+
},
963+
// With custom default branch
964+
{
965+
Config: fmt.Sprintf(`
966+
resource "gitlab_project" "foo" {
967+
name = "foo-%d-custom-default-branch"
968+
description = "Terraform acceptance tests"
969+
visibility_level = "public"
970+
default_branch = "foobar-non-default-branch"
971+
initialize_with_readme = true
972+
}
973+
`, rInt),
974+
},
975+
// Verify Import
976+
{
977+
ResourceName: "gitlab_project.foo",
978+
ImportState: true,
979+
ImportStateVerify: true,
980+
ImportStateVerifyIgnore: []string{"initialize_with_readme"},
981+
},
982+
},
983+
})
984+
}
985+
886986
type testAccGitlabProjectMirroredExpectedAttributes struct {
887987
Mirror bool
888988
MirrorTriggerBuilds bool

0 commit comments

Comments
 (0)