Skip to content

Commit

Permalink
Use the new client for project triggers and deprecate original methods
Browse files Browse the repository at this point in the history
  • Loading branch information
bec-callow-oct committed Dec 9, 2024
1 parent 7bd692c commit 6e35dcd
Show file tree
Hide file tree
Showing 2 changed files with 172 additions and 0 deletions.
41 changes: 41 additions & 0 deletions pkg/triggers/project_trigger_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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)
Expand Down Expand Up @@ -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 {
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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)
}
131 changes: 131 additions & 0 deletions test/e2e/project_trigger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

0 comments on commit 6e35dcd

Please sign in to comment.