Skip to content

Commit 2b3e9c2

Browse files
authored
Merge pull request #875 from khaossolutions/fix/project-recompute-path
[gitlab_project] re-compute project attributes if path or namespace changed
2 parents 3225aaa + a1c99f1 commit 2b3e9c2

File tree

2 files changed

+54
-3
lines changed

2 files changed

+54
-3
lines changed

internal/provider/resource_gitlab_project.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"time"
99

1010
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
11+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/customdiff"
1112
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
1213
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
1314
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
@@ -388,6 +389,12 @@ var _ = registerResource("gitlab_project", func() *schema.Resource {
388389
StateContext: schema.ImportStatePassthroughContext,
389390
},
390391
Schema: resourceGitLabProjectSchema,
392+
CustomizeDiff: customdiff.All(
393+
customdiff.ComputedIf("path_with_namespace", namespaceOrPathChanged),
394+
customdiff.ComputedIf("ssh_url_to_repo", namespaceOrPathChanged),
395+
customdiff.ComputedIf("http_url_to_repo", namespaceOrPathChanged),
396+
customdiff.ComputedIf("web_url", namespaceOrPathChanged),
397+
),
391398
}
392399
})
393400

@@ -1137,3 +1144,7 @@ func flattenProjectPushRules(pushRules *gitlab.ProjectPushRules) (values []map[s
11371144
},
11381145
}
11391146
}
1147+
1148+
func namespaceOrPathChanged(ctx context.Context, d *schema.ResourceDiff, meta interface{}) bool {
1149+
return d.HasChange("namespace_id") || d.HasChange("path")
1150+
}

internal/provider/resource_gitlab_project_test.go

Lines changed: 43 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -600,24 +600,29 @@ func TestAccGitlabProject_transfer(t *testing.T) {
600600
CIForwardDeploymentEnabled: true,
601601
}
602602

603+
pathBeforeTransfer := fmt.Sprintf("foogroup-%d/foo-%d", rInt, rInt)
604+
pathAfterTransfer := fmt.Sprintf("foo2group-%d/foo-%d", rInt, rInt)
605+
603606
resource.Test(t, resource.TestCase{
604607
PreCheck: func() { testAccPreCheck(t) },
605608
ProviderFactories: providerFactories,
606609
CheckDestroy: testAccCheckGitlabProjectDestroy,
607610
Steps: []resource.TestStep{
608611
// Create a project in a group
609612
{
610-
Config: testAccGitlabProjectInGroupConfig(rInt),
613+
Config: testAccGitlabProjectTransferBetweenGroupsBefore(rInt),
611614
Check: resource.ComposeTestCheckFunc(
612615
testAccCheckGitlabProjectExists("gitlab_project.foo", &received),
616+
resource.TestCheckResourceAttrPtr("gitlab_project_variable.foo", "value", &pathBeforeTransfer),
613617
),
614618
},
615619
// Create a second group and set the transfer the project to this group
616620
{
617-
Config: testAccGitlabProjectTransferBetweenGroups(rInt),
621+
Config: testAccGitlabProjectTransferBetweenGroupsAfter(rInt),
618622
Check: resource.ComposeTestCheckFunc(
619623
testAccCheckGitlabProjectExists("gitlab_project.foo", &received),
620624
testAccCheckAggregateGitlabProject(&transferred, &received),
625+
resource.TestCheckResourceAttrPtr("gitlab_project_variable.foo", "value", &pathAfterTransfer),
621626
),
622627
},
623628
},
@@ -1117,7 +1122,35 @@ resource "gitlab_project" "foo" {
11171122
`, rInt, rInt, rInt)
11181123
}
11191124

1120-
func testAccGitlabProjectTransferBetweenGroups(rInt int) string {
1125+
func testAccGitlabProjectTransferBetweenGroupsBefore(rInt int) string {
1126+
return fmt.Sprintf(`
1127+
resource "gitlab_group" "foo" {
1128+
name = "foogroup-%d"
1129+
path = "foogroup-%d"
1130+
visibility_level = "public"
1131+
}
1132+
1133+
resource "gitlab_project" "foo" {
1134+
name = "foo-%d"
1135+
description = "Terraform acceptance tests"
1136+
namespace_id = "${gitlab_group.foo.id}"
1137+
1138+
# So that acceptance tests can be run in a gitlab organization
1139+
# with no billing
1140+
visibility_level = "public"
1141+
build_coverage_regex = "foo"
1142+
}
1143+
1144+
resource "gitlab_project_variable" "foo" {
1145+
project = "${gitlab_project.foo.id}"
1146+
1147+
key = "FOO"
1148+
value = "${gitlab_project.foo.path_with_namespace}"
1149+
}
1150+
`, rInt, rInt, rInt)
1151+
}
1152+
1153+
func testAccGitlabProjectTransferBetweenGroupsAfter(rInt int) string {
11211154
return fmt.Sprintf(`
11221155
resource "gitlab_group" "foo" {
11231156
name = "foogroup-%d"
@@ -1141,6 +1174,13 @@ resource "gitlab_project" "foo" {
11411174
visibility_level = "public"
11421175
build_coverage_regex = "foo"
11431176
}
1177+
1178+
resource "gitlab_project_variable" "foo" {
1179+
project = "${gitlab_project.foo.id}"
1180+
1181+
key = "FOO"
1182+
value = "${gitlab_project.foo.path_with_namespace}"
1183+
}
11441184
`, rInt, rInt, rInt, rInt, rInt)
11451185
}
11461186

0 commit comments

Comments
 (0)