Skip to content

Commit

Permalink
[TF-9605] Add validation when configuring Registry Module Publishing
Browse files Browse the repository at this point in the history
Prevent setting tags to `true` when a branch is present for configuring
the VCSRepo.
  • Loading branch information
hashimoon committed Nov 6, 2023
1 parent a245d58 commit 0242f49
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 8 deletions.
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
62 changes: 57 additions & 5 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 @@ -301,7 +301,6 @@ func TestRegistryModuleUpdate(t *testing.T) {
}

func TestRegistryModuleUpdateWithVCSConnection(t *testing.T) {
skipUnlessBeta(t)
githubBranch := os.Getenv("GITHUB_REGISTRY_MODULE_BRANCH")
if githubBranch == "" {
githubBranch = "main"
Expand Down Expand Up @@ -363,6 +362,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 +421,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,13 +671,12 @@ 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)
})
}

func TestRegistryModulesListCommit(t *testing.T) {
skipUnlessBeta(t)
githubIdentifier := os.Getenv("GITHUB_REGISTRY_MODULE_IDENTIFIER")
if githubIdentifier == "" {
t.Skip("Export a valid GITHUB_REGISTRY_MODULE_IDENTIFIER before running this test")
Expand Down Expand Up @@ -815,6 +851,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

0 comments on commit 0242f49

Please sign in to comment.