From 6e35dcde60a8e009c36e1e25f07caafe44c0a0fd Mon Sep 17 00:00:00 2001 From: Bec Callow Date: Mon, 9 Dec 2024 14:30:55 +1000 Subject: [PATCH] Use the new client for project triggers and deprecate original methods --- pkg/triggers/project_trigger_service.go | 41 ++++++++ test/e2e/project_trigger_test.go | 131 ++++++++++++++++++++++++ 2 files changed, 172 insertions(+) diff --git a/pkg/triggers/project_trigger_service.go b/pkg/triggers/project_trigger_service.go index b9f5cdd4..a1c57c54 100644 --- a/pkg/triggers/project_trigger_service.go +++ b/pkg/triggers/project_trigger_service.go @@ -2,6 +2,7 @@ package triggers import ( "fmt" + "github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/newclient" "github.com/OctopusDeploy/go-octopusdeploy/v2/internal" "github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/constants" @@ -24,6 +25,8 @@ func NewProjectTriggerService(sling *sling.Sling, uriTemplate string) *ProjectTr // GetByID returns the project trigger that matches the input ID. If one cannot // be found, it returns nil and an error. +// +// Deprecated: use triggers.GetByID func (s *ProjectTriggerService) GetByID(id string) (*ProjectTrigger, error) { if internal.IsEmpty(id) { return nil, internal.CreateInvalidParameterError(constants.OperationGetByID, constants.ParameterID) @@ -59,6 +62,8 @@ func (s *ProjectTriggerService) GetByProjectID(id string) ([]*ProjectTrigger, er // GetAll returns all project triggers. If none can be found or an error // occurs, it returns an empty collection. +// +// Deprecated: use triggers.GetAll func (s *ProjectTriggerService) GetAll() ([]*ProjectTrigger, error) { path, err := services.GetPath(s) if err != nil { @@ -69,6 +74,8 @@ func (s *ProjectTriggerService) GetAll() ([]*ProjectTrigger, error) { } // Add creates a new project trigger. +// +// Deprecated: use triggers.Add func (s *ProjectTriggerService) Add(projectTrigger *ProjectTrigger) (*ProjectTrigger, error) { if projectTrigger == nil { return nil, internal.CreateInvalidParameterError(constants.OperationDelete, constants.ParameterProjectTrigger) @@ -88,6 +95,8 @@ func (s *ProjectTriggerService) Add(projectTrigger *ProjectTrigger) (*ProjectTri } // Delete will delete a project trigger. +// +// Deprecated: use triggers.Delete func (s *ProjectTriggerService) Delete(projectTrigger *ProjectTrigger) error { if projectTrigger == nil { return internal.CreateInvalidParameterError(constants.OperationDelete, constants.ParameterProjectTrigger) @@ -111,3 +120,35 @@ func (s *ProjectTriggerService) Update(projectTrigger *ProjectTrigger) (*Project return resp.(*ProjectTrigger), nil } + +// ----- New -------- + +const ( + template = "/api/{spaceId}/projecttriggers/{id}" +) + +// GetById returns the project trigger that matches the input ID. If one cannot be +// found, it returns nil and an error. +func GetById(client newclient.Client, spaceID string, ID string) (*ProjectTrigger, error) { + return newclient.GetByID[ProjectTrigger](client, template, spaceID, ID) +} + +// GetAll returns all project triggers. If an error occurs, it returns nil. +func GetAll(client newclient.Client, spaceID string) ([]*ProjectTrigger, error) { + return newclient.GetAll[ProjectTrigger](client, template, spaceID) +} + +// Add creates a new project trigger. +func Add(client newclient.Client, projectTrigger *ProjectTrigger) (*ProjectTrigger, error) { + return newclient.Add[ProjectTrigger](client, template, projectTrigger.SpaceID, projectTrigger) +} + +// DeleteById deletes a specified project trigger by ID +func DeleteById(client newclient.Client, spaceID string, ID string) error { + return newclient.DeleteByID(client, template, spaceID, ID) +} + +// Update modifies a project trigger based on the one provided as input. +func Update(client newclient.Client, projectTrigger *ProjectTrigger) (*ProjectTrigger, error) { + return newclient.Update[ProjectTrigger](client, template, projectTrigger.SpaceID, projectTrigger.ID, projectTrigger) +} diff --git a/test/e2e/project_trigger_test.go b/test/e2e/project_trigger_test.go index b2fcb5c3..b3f84145 100644 --- a/test/e2e/project_trigger_test.go +++ b/test/e2e/project_trigger_test.go @@ -166,3 +166,134 @@ func TestProjectTriggerGetAll(t *testing.T) { require.NoError(t, err) require.NotNil(t, projectTriggers) } + +// ----- new ----- + +func New_CreateTestProjectTrigger(t *testing.T, client *client.Client, project *projects.Project) *triggers.ProjectTrigger { + require.NotNil(t, project) + + if client == nil { + client = getOctopusClient() + } + require.NotNil(t, client) + + space := GetDefaultSpace(t, client) + require.NotNil(t, space) + + now := time.Now() + start := time.Date(now.Year(), now.Month(), now.Day(), 9, 0, 0, 0, time.UTC) + + action := actions.NewAutoDeployAction(createRandomBoolean()) + filter := filters.NewOnceDailyScheduledTriggerFilter([]filters.Weekday{filters.Tuesday}, start) + + projectTrigger := triggers.NewProjectTrigger(internal.GetRandomName(), internal.GetRandomName(), createRandomBoolean(), project, action, filter) + require.NotNil(t, projectTrigger) + require.NoError(t, projectTrigger.Validate()) + + createdProjectTrigger, err := client.ProjectTriggers.Add(projectTrigger) + require.NoError(t, err) + require.NotNil(t, createdProjectTrigger) + require.NotEmpty(t, createdProjectTrigger.GetID()) + + // verify the add operation was successful + projectTriggerToCompare, err := triggers.GetById(client, space.ID, createdProjectTrigger.GetID()) + require.NoError(t, err) + require.NotNil(t, projectTriggerToCompare) + AssertEqualProjectTriggers(t, createdProjectTrigger, projectTriggerToCompare) + + return createdProjectTrigger +} + +func New_DeleteTestProjectTrigger(t *testing.T, client *client.Client, projectTrigger *triggers.ProjectTrigger) { + require.NotNil(t, projectTrigger) + + if client == nil { + client = getOctopusClient() + } + require.NotNil(t, client) + + space := GetDefaultSpace(t, client) + require.NotNil(t, space) + + err := triggers.DeleteById(client, space.ID, projectTrigger.ID) + assert.NoError(t, err) + + // verify the delete operation was successful + deletedProjectTrigger, err := triggers.GetById(client, space.ID, projectTrigger.GetID()) + assert.Error(t, err) + assert.Nil(t, deletedProjectTrigger) +} + +func New_UpdateTestProjectTrigger(t *testing.T, client *client.Client, projectTrigger *triggers.ProjectTrigger) *triggers.ProjectTrigger { + require.NotNil(t, projectTrigger) + + if client == nil { + client = getOctopusClient() + } + require.NotNil(t, client) + + space := GetDefaultSpace(t, client) + require.NotNil(t, space) + + projectTrigger, err := triggers.Update(client, projectTrigger) + assert.NoError(t, err) + assert.NotNil(t, projectTrigger) + + return projectTrigger +} + +func New_TestProjectTriggerAddGetUpdateDelete(t *testing.T) { + client := getOctopusClient() + require.NotNil(t, client) + + space := GetDefaultSpace(t, client) + require.NotNil(t, space) + + lifecycle := CreateTestLifecycle(t, client) + require.NotNil(t, lifecycle) + defer DeleteTestLifecycle(t, client, lifecycle) + + projectGroup := CreateTestProjectGroup(t, client) + require.NotNil(t, projectGroup) + defer DeleteTestProjectGroup(t, client, projectGroup) + + project := CreateTestProject(t, client, space, lifecycle, projectGroup) + require.NotNil(t, project) + defer DeleteTestProject(t, client, project) + + environment := CreateTestEnvironment(t, client) + require.NotNil(t, environment) + defer DeleteTestEnvironment(t, client, environment) + + projectTrigger := New_CreateTestProjectTrigger(t, client, project) + require.NotNil(t, lifecycle) + defer New_DeleteTestProjectTrigger(t, client, projectTrigger) + + projectTrigger.Name = GetRandomName() + updatedProjectTrigger := New_UpdateTestProjectTrigger(t, client, projectTrigger) + require.NotNil(t, updatedProjectTrigger) +} + +func New_TestProjectTriggerGetAll(t *testing.T) { + client := getOctopusClient() + require.NotNil(t, client) + + space := GetDefaultSpace(t, client) + require.NotNil(t, space) + + lifecycle := CreateTestLifecycle(t, client) + require.NotNil(t, lifecycle) + defer DeleteTestLifecycle(t, client, lifecycle) + + projectGroup := CreateTestProjectGroup(t, client) + require.NotNil(t, projectGroup) + defer DeleteTestProjectGroup(t, client, projectGroup) + + project := CreateTestProject(t, client, space, lifecycle, projectGroup) + require.NotNil(t, project) + defer DeleteTestProject(t, client, project) + + projectTriggers, err := triggers.GetAll(client, space.ID) + require.NoError(t, err) + require.NotNil(t, projectTriggers) +}