Skip to content

Commit 6811c20

Browse files
committed
[TF-9605] Add validation when configuring Registry Module Publishing
Prevent setting tags to `true` when a branch is present for configuring the VCSRepo.
1 parent a245d58 commit 6811c20

File tree

3 files changed

+74
-8
lines changed

3 files changed

+74
-8
lines changed

errors.go

+4-3
Original file line numberDiff line numberDiff line change
@@ -222,9 +222,10 @@ var (
222222

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

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

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

registry_module.go

+13
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,7 @@ type RegistryModuleVCSRepoOptions struct {
289289
//
290290
// **Note: This field is still in BETA and subject to change.**
291291
Branch *string `json:"branch,omitempty"`
292+
Tags *bool `json:"tags,omitempty"`
292293
}
293294

294295
type RegistryModuleVCSRepoUpdateOptions struct {
@@ -426,6 +427,12 @@ func (r *registryModules) Update(ctx context.Context, moduleID RegistryModuleID,
426427
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.")
427428
}
428429

430+
if options.VCSRepo != nil {
431+
if options.VCSRepo.Tags != nil && *options.VCSRepo.Tags && validString(options.VCSRepo.Branch) {
432+
return nil, ErrBranchMustBeEmptyWhenTagsEnabled
433+
}
434+
}
435+
429436
org := url.QueryEscape(moduleID.Organization)
430437
registryName := url.QueryEscape(string(moduleID.RegistryName))
431438
namespace := url.QueryEscape(moduleID.Namespace)
@@ -738,6 +745,12 @@ func (o RegistryModuleCreateWithVCSConnectionOptions) valid() error {
738745
}
739746
}
740747

748+
if o.VCSRepo.Tags != nil && *o.VCSRepo.Tags {
749+
if validString(o.VCSRepo.Branch) {
750+
return ErrBranchMustBeEmptyWhenTagsEnabled
751+
}
752+
}
753+
741754
return o.VCSRepo.valid()
742755
}
743756

registry_module_integration_test.go

+57-5
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ func TestRegistryModulesCreate(t *testing.T) {
160160
assert.Equal(t, *options.Provider, rm.Provider)
161161
assert.Equal(t, options.RegistryName, rm.RegistryName)
162162
assert.Equal(t, orgTest.Name, rm.Namespace)
163-
assert.Equal(t, options.NoCode, rm.NoCode)
163+
assert.Equal(t, options.NoCode, Bool(rm.NoCode))
164164

165165
assertRegistryModuleAttributes(t, rm)
166166
})
@@ -301,7 +301,6 @@ func TestRegistryModuleUpdate(t *testing.T) {
301301
}
302302

303303
func TestRegistryModuleUpdateWithVCSConnection(t *testing.T) {
304-
skipUnlessBeta(t)
305304
githubBranch := os.Getenv("GITHUB_REGISTRY_MODULE_BRANCH")
306305
if githubBranch == "" {
307306
githubBranch = "main"
@@ -363,6 +362,43 @@ func TestRegistryModuleUpdateWithVCSConnection(t *testing.T) {
363362
assert.False(t, rm.NoCode)
364363
})
365364

365+
t.Run("prevents setting the branch when using tag based publishing", func(t *testing.T) {
366+
options := RegistryModuleUpdateOptions{
367+
VCSRepo: &RegistryModuleVCSRepoUpdateOptions{
368+
Branch: String("main"),
369+
Tags: Bool(true),
370+
},
371+
}
372+
373+
_, err = client.RegistryModules.Update(ctx, RegistryModuleID{
374+
Organization: orgTest.Name,
375+
Name: rm.Name,
376+
Provider: rm.Provider,
377+
Namespace: rm.Namespace,
378+
RegistryName: rm.RegistryName,
379+
}, options)
380+
381+
assert.Error(t, err)
382+
assert.EqualError(t, err, ErrBranchMustBeEmptyWhenTagsEnabled.Error())
383+
384+
options = RegistryModuleUpdateOptions{
385+
VCSRepo: &RegistryModuleVCSRepoUpdateOptions{
386+
Branch: String(""),
387+
Tags: Bool(true),
388+
},
389+
}
390+
391+
rm, err = client.RegistryModules.Update(ctx, RegistryModuleID{
392+
Organization: orgTest.Name,
393+
Name: rm.Name,
394+
Provider: rm.Provider,
395+
Namespace: rm.Namespace,
396+
RegistryName: rm.RegistryName,
397+
}, options)
398+
399+
assert.NoError(t, err)
400+
})
401+
366402
t.Run("toggle between git tag-based and branch-based publishing", func(t *testing.T) {
367403
assert.Equal(t, rm.PublishingMechanism, PublishingMechanismTag)
368404

@@ -385,7 +421,8 @@ func TestRegistryModuleUpdateWithVCSConnection(t *testing.T) {
385421

386422
options = RegistryModuleUpdateOptions{
387423
VCSRepo: &RegistryModuleVCSRepoUpdateOptions{
388-
Tags: Bool(true),
424+
Branch: String(""),
425+
Tags: Bool(true),
389426
},
390427
}
391428
rm, err = client.RegistryModules.Update(ctx, RegistryModuleID{
@@ -634,13 +671,12 @@ func TestRegistryModulesShowVersion(t *testing.T) {
634671
rmvRead, errRead := client.RegistryModules.ReadVersion(ctx, registryModuleIDTest, *invalidVersion)
635672

636673
require.Error(t, errRead)
637-
assert.Equal(t, ErrResourceNotFound, err)
674+
assert.Equal(t, ErrResourceNotFound, errRead)
638675
assert.Empty(t, rmvRead)
639676
})
640677
}
641678

642679
func TestRegistryModulesListCommit(t *testing.T) {
643-
skipUnlessBeta(t)
644680
githubIdentifier := os.Getenv("GITHUB_REGISTRY_MODULE_IDENTIFIER")
645681
if githubIdentifier == "" {
646682
t.Skip("Export a valid GITHUB_REGISTRY_MODULE_IDENTIFIER before running this test")
@@ -815,6 +851,22 @@ func TestRegistryModulesCreateWithVCSConnection(t *testing.T) {
815851
assert.Nil(t, rm)
816852
assert.Equal(t, err, ErrRequiredDisplayIdentifier)
817853
})
854+
855+
t.Run("when tags are enabled and a branch is provided", func(t *testing.T) {
856+
options := RegistryModuleCreateWithVCSConnectionOptions{
857+
VCSRepo: &RegistryModuleVCSRepoOptions{
858+
Identifier: String(githubIdentifier),
859+
OAuthTokenID: String(oauthTokenTest.ID),
860+
DisplayIdentifier: String(githubIdentifier),
861+
Tags: Bool(true),
862+
Branch: String("main"),
863+
},
864+
}
865+
866+
rm, err := client.RegistryModules.CreateWithVCSConnection(ctx, options)
867+
assert.Nil(t, rm)
868+
assert.Equal(t, err, ErrBranchMustBeEmptyWhenTagsEnabled)
869+
})
818870
})
819871

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

0 commit comments

Comments
 (0)