Skip to content

Commit

Permalink
Fix project persistence settings (#844)
Browse files Browse the repository at this point in the history
* fix: Project persistence settings error when protected_branches is not provided

* fix: Project persistence settings basepath should be set with a default(".octopus") if not provided
  • Loading branch information
mik-ky authored Dec 24, 2024
1 parent 6643d95 commit 857bb8f
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 7 deletions.
15 changes: 14 additions & 1 deletion octopusdeploy_framework/resource_project_expand.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package octopusdeploy_framework
import (
"context"
"fmt"
"net/url"

"github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/actiontemplates"
"github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/core"
"github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/credentials"
Expand All @@ -11,7 +13,6 @@ import (
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/hashicorp/terraform-plugin-framework/types/basetypes"
"github.com/hashicorp/terraform-plugin-log/tflog"
"net/url"
)

func expandProject(ctx context.Context, model projectResourceModel) *projects.Project {
Expand Down Expand Up @@ -143,6 +144,10 @@ func expandGitLibraryPersistenceSettings(ctx context.Context, model gitLibraryPe
var protectedBranches []string
model.ProtectedBranches.ElementsAs(ctx, &protectedBranches, false)

if protectedBranches == nil {
protectedBranches = []string{}
}

return projects.NewGitPersistenceSettings(
model.BasePath.ValueString(),
credentials.NewReference(model.GitCredentialID.ValueString()),
Expand All @@ -157,6 +162,10 @@ func expandGitUsernamePasswordPersistenceSettings(ctx context.Context, model git
var protectedBranches []string
model.ProtectedBranches.ElementsAs(ctx, &protectedBranches, false)

if protectedBranches == nil {
protectedBranches = []string{}
}

return projects.NewGitPersistenceSettings(
model.BasePath.ValueString(),
credentials.NewUsernamePassword(
Expand All @@ -174,6 +183,10 @@ func expandGitAnonymousPersistenceSettings(ctx context.Context, model gitAnonymo
var protectedBranches []string
model.ProtectedBranches.ElementsAs(ctx, &protectedBranches, false)

if protectedBranches == nil {
protectedBranches = []string{}
}

return projects.NewGitPersistenceSettings(
model.BasePath.ValueString(),
credentials.NewAnonymous(),
Expand Down
13 changes: 7 additions & 6 deletions octopusdeploy_framework/schemas/project.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/hashicorp/terraform-plugin-framework/resource/schema/listplanmodifier"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/objectplanmodifier"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/planmodifier"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/setplanmodifier"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/stringplanmodifier"
"github.com/hashicorp/terraform-plugin-framework/types"
)
Expand Down Expand Up @@ -77,9 +78,9 @@ func (p ProjectSchema) GetResourceSchema() resourceSchema.Schema {
NestedObject: resourceSchema.NestedBlockObject{
Attributes: map[string]resourceSchema.Attribute{
"url": util.ResourceString().Required().Description("The URL associated with these version control settings.").Build(),
"base_path": util.ResourceString().Optional().Description("The base path associated with these version control settings.").Build(),
"base_path": util.ResourceString().Optional().Computed().Default(".octopus").Description("The base path associated with these version control settings.").Build(),
"default_branch": util.ResourceString().Optional().Description("The default branch associated with these version control settings.").Build(),
"protected_branches": util.ResourceSet(types.StringType).Optional().Description("A list of protected branch patterns.").Build(),
"protected_branches": util.ResourceSet(types.StringType).Optional().Computed().PlanModifiers(setplanmodifier.UseStateForUnknown()).Description("A list of protected branch patterns.").Build(),
},
},
Description: "Provides Git-related persistence settings for a version-controlled project.",
Expand All @@ -89,9 +90,9 @@ func (p ProjectSchema) GetResourceSchema() resourceSchema.Schema {
Attributes: map[string]resourceSchema.Attribute{
"git_credential_id": util.ResourceString().Required().Build(),
"url": util.ResourceString().Required().Description("The URL associated with these version control settings.").Build(),
"base_path": util.ResourceString().Optional().Description("The base path associated with these version control settings.").Build(),
"base_path": util.ResourceString().Optional().Computed().Default(".octopus").Description("The base path associated with these version control settings.").Build(),
"default_branch": util.ResourceString().Optional().Description("The default branch associated with these version control settings.").Build(),
"protected_branches": util.ResourceSet(types.StringType).Optional().Description("A list of protected branch patterns.").Build(),
"protected_branches": util.ResourceSet(types.StringType).Optional().Computed().PlanModifiers(setplanmodifier.UseStateForUnknown()).Description("A list of protected branch patterns.").Build(),
},
},
Description: "Provides Git-related persistence settings for a version-controlled project.",
Expand All @@ -102,9 +103,9 @@ func (p ProjectSchema) GetResourceSchema() resourceSchema.Schema {
"url": util.ResourceString().Required().Description("The URL associated with these version control settings.").Build(),
"username": util.ResourceString().Required().Description("The username for the Git credential.").Build(),
"password": util.ResourceString().Sensitive().Required().Description("The password for the Git credential").Build(), //util.GetPasswordResourceSchema(false),
"base_path": util.ResourceString().Optional().Description("The base path associated with these version control settings.").Build(),
"base_path": util.ResourceString().Optional().Computed().Default(".octopus").Description("The base path associated with these version control settings.").Build(),
"default_branch": util.ResourceString().Optional().Description("The default branch associated with these version control settings.").Build(),
"protected_branches": util.ResourceSet(types.StringType).Optional().Description("A list of protected branch patterns.").Build(),
"protected_branches": util.ResourceSet(types.StringType).Optional().Computed().PlanModifiers(setplanmodifier.UseStateForUnknown()).Description("A list of protected branch patterns.").Build(),
},
},
Description: "Provides Git-related persistence settings for a version-controlled project.",
Expand Down

0 comments on commit 857bb8f

Please sign in to comment.