Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[TF-9605] Add validation when configuring Registry Module Publishing #804

Merged
merged 1 commit into from
Nov 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -222,9 +222,10 @@ var (

ErrRequiredAgentPoolID = errors.New("'agent' execution mode requires an agent pool ID to be specified")

ErrRequiredAgentMode = errors.New("specifying an agent pool ID requires 'agent' execution mode")
ErrRequiredBranchWhenTestsEnabled = errors.New("VCS branch is required when enabling tests")
ErrRequiredCategory = errors.New("category is required")
ErrRequiredAgentMode = errors.New("specifying an agent pool ID requires 'agent' execution mode")
ErrRequiredBranchWhenTestsEnabled = errors.New("VCS branch is required when enabling tests")
ErrBranchMustBeEmptyWhenTagsEnabled = errors.New("VCS branch must be empty to enable tags")
ErrRequiredCategory = errors.New("category is required")

ErrRequiredDestinationType = errors.New("destination type is required")

Expand Down
13 changes: 13 additions & 0 deletions registry_module.go
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,7 @@ type RegistryModuleVCSRepoOptions struct {
//
// **Note: This field is still in BETA and subject to change.**
Branch *string `json:"branch,omitempty"`
Tags *bool `json:"tags,omitempty"`
}

type RegistryModuleVCSRepoUpdateOptions struct {
Expand Down Expand Up @@ -426,6 +427,12 @@ func (r *registryModules) Update(ctx context.Context, moduleID RegistryModuleID,
log.Println("[WARN] Support for using the NoCode field is deprecated as of release 1.22.0 and may be removed in a future version. The preferred way to update a no-code module is with the registryNoCodeModules.Update method.")
}

if options.VCSRepo != nil {
if options.VCSRepo.Tags != nil && *options.VCSRepo.Tags && validString(options.VCSRepo.Branch) {
return nil, ErrBranchMustBeEmptyWhenTagsEnabled
}
}

org := url.QueryEscape(moduleID.Organization)
registryName := url.QueryEscape(string(moduleID.RegistryName))
namespace := url.QueryEscape(moduleID.Namespace)
Expand Down Expand Up @@ -738,6 +745,12 @@ func (o RegistryModuleCreateWithVCSConnectionOptions) valid() error {
}
}

if o.VCSRepo.Tags != nil && *o.VCSRepo.Tags {
if validString(o.VCSRepo.Branch) {
return ErrBranchMustBeEmptyWhenTagsEnabled
}
}

return o.VCSRepo.valid()
}

Expand Down
60 changes: 57 additions & 3 deletions registry_module_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ func TestRegistryModulesCreate(t *testing.T) {
assert.Equal(t, *options.Provider, rm.Provider)
assert.Equal(t, options.RegistryName, rm.RegistryName)
assert.Equal(t, orgTest.Name, rm.Namespace)
assert.Equal(t, options.NoCode, rm.NoCode)
assert.Equal(t, options.NoCode, Bool(rm.NoCode))

assertRegistryModuleAttributes(t, rm)
})
Expand Down Expand Up @@ -363,6 +363,43 @@ func TestRegistryModuleUpdateWithVCSConnection(t *testing.T) {
assert.False(t, rm.NoCode)
})

t.Run("prevents setting the branch when using tag based publishing", func(t *testing.T) {
options := RegistryModuleUpdateOptions{
VCSRepo: &RegistryModuleVCSRepoUpdateOptions{
Branch: String("main"),
Tags: Bool(true),
},
}

_, err = client.RegistryModules.Update(ctx, RegistryModuleID{
Organization: orgTest.Name,
Name: rm.Name,
Provider: rm.Provider,
Namespace: rm.Namespace,
RegistryName: rm.RegistryName,
}, options)

assert.Error(t, err)
assert.EqualError(t, err, ErrBranchMustBeEmptyWhenTagsEnabled.Error())

options = RegistryModuleUpdateOptions{
VCSRepo: &RegistryModuleVCSRepoUpdateOptions{
Branch: String(""),
Tags: Bool(true),
},
}

rm, err = client.RegistryModules.Update(ctx, RegistryModuleID{
Organization: orgTest.Name,
Name: rm.Name,
Provider: rm.Provider,
Namespace: rm.Namespace,
RegistryName: rm.RegistryName,
}, options)

assert.NoError(t, err)
})

t.Run("toggle between git tag-based and branch-based publishing", func(t *testing.T) {
assert.Equal(t, rm.PublishingMechanism, PublishingMechanismTag)

Expand All @@ -385,7 +422,8 @@ func TestRegistryModuleUpdateWithVCSConnection(t *testing.T) {

options = RegistryModuleUpdateOptions{
VCSRepo: &RegistryModuleVCSRepoUpdateOptions{
Tags: Bool(true),
Branch: String(""),
Tags: Bool(true),
},
}
rm, err = client.RegistryModules.Update(ctx, RegistryModuleID{
Expand Down Expand Up @@ -634,7 +672,7 @@ func TestRegistryModulesShowVersion(t *testing.T) {
rmvRead, errRead := client.RegistryModules.ReadVersion(ctx, registryModuleIDTest, *invalidVersion)

require.Error(t, errRead)
assert.Equal(t, ErrResourceNotFound, err)
assert.Equal(t, ErrResourceNotFound, errRead)
assert.Empty(t, rmvRead)
})
}
Expand Down Expand Up @@ -815,6 +853,22 @@ func TestRegistryModulesCreateWithVCSConnection(t *testing.T) {
assert.Nil(t, rm)
assert.Equal(t, err, ErrRequiredDisplayIdentifier)
})

t.Run("when tags are enabled and a branch is provided", func(t *testing.T) {
options := RegistryModuleCreateWithVCSConnectionOptions{
VCSRepo: &RegistryModuleVCSRepoOptions{
Identifier: String(githubIdentifier),
OAuthTokenID: String(oauthTokenTest.ID),
DisplayIdentifier: String(githubIdentifier),
Tags: Bool(true),
Branch: String("main"),
},
}

rm, err := client.RegistryModules.CreateWithVCSConnection(ctx, options)
assert.Nil(t, rm)
assert.Equal(t, err, ErrBranchMustBeEmptyWhenTagsEnabled)
})
})

t.Run("without options", func(t *testing.T) {
Expand Down