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

chore: convert interruptions to new client #218

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
66 changes: 66 additions & 0 deletions pkg/interruptions/interruption_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package interruptions
import (
"github.com/OctopusDeploy/go-octopusdeploy/v2/internal"
"github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/constants"
"github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/newclient"
"github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/services"
"github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/services/api"
"github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/users"
Expand All @@ -21,6 +22,8 @@ func NewInterruptionService(sling *sling.Sling, uriTemplate string) *Interruptio

// GetByID returns the interruption that matches the input ID. If one cannot be
// found, it returns nil and an error.
//
// Deprecated: use interruptions.GetByID()
func (s *InterruptionService) GetByID(id string) (*Interruption, error) {
if internal.IsEmpty(id) {
return nil, internal.CreateInvalidParameterError(constants.OperationGetByID, constants.ParameterID)
Expand Down Expand Up @@ -55,6 +58,8 @@ func (s *InterruptionService) GetByIDs(ids []string) ([]*Interruption, error) {

// GetAll returns all interruptions. If none can be found or an error occurs,
// it returns an empty collection.
//
// Deprecated: use interruptions.GetAll()
func (s *InterruptionService) GetAll() ([]*Interruption, error) {
items := []*Interruption{}
path, err := services.GetAllPath(s)
Expand All @@ -67,6 +72,8 @@ func (s *InterruptionService) GetAll() ([]*Interruption, error) {
}

// Submit Submits a dictionary of form values for the interruption. Only the user with responsibility for this interruption can submit this form.
//
// Deprecated: use interruptions.Submit()
func (s *InterruptionService) Submit(resource *Interruption, r *InterruptionSubmitRequest) (*Interruption, error) {
path := resource.Links[constants.LinkSubmit]

Expand All @@ -79,6 +86,8 @@ func (s *InterruptionService) Submit(resource *Interruption, r *InterruptionSubm
}

// GetResponsibility gets the User that is currently responsible for the Interruption.
//
// Deprecated: use interruptions.GetResponsibility()
func (s InterruptionService) GetResponsibility(resource *Interruption) (*users.User, error) {
path := resource.Links[constants.LinkResponsible]

Expand All @@ -90,6 +99,8 @@ func (s InterruptionService) GetResponsibility(resource *Interruption) (*users.U
}

// TakeResponsibility Allows the current user to take responsibility for this interruption. Only users in one of the responsible teams on this interruption can take responsibility for it.
//
// Deprecated: use interruptions.TakeResponsibility()
func (s InterruptionService) TakeResponsibility(resource *Interruption) (*users.User, error) {
path := resource.Links[constants.LinkResponsible]

Expand All @@ -99,3 +110,58 @@ func (s InterruptionService) TakeResponsibility(resource *Interruption) (*users.
}
return resp.(*users.User), nil
}

// --- new ---

const template = "/api/{spaceId}/interruptions{/id}{?skip,take,regarding,pendingOnly,ids}"
const submitTemplate = "/api/{spaceId}/interruptions/{id}/submit"
const responsibleTemplate = "/api/{spaceId}/interruptions/{id}/responsible"

// GetByID returns the interruption 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) (*Interruption, error) {
return newclient.GetByID[Interruption](client, template, spaceId, id)
}

// GetAll returns all interruptions. If none can be found or an error occurs,
// it returns an empty collection.
func GetAll(client newclient.Client, spaceId string) ([]*Interruption, error) {
return newclient.GetAll[Interruption](client, template, spaceId)
}

// GetResponsibility gets the User that is currently responsible for the Interruption.
func GetResponsibility(client newclient.Client, interruption *Interruption) (*users.User, error) {
return newclient.GetByID[users.User](client, responsibleTemplate, interruption.SpaceID, interruption.ID)
}

// TakeResponsibility Allows the current user to take responsibility for this interruption. Only users in one of the responsible teams on this interruption can take responsibility for it.
func TakeResponsibility(client newclient.Client, interruption *Interruption) (*users.User, error) {
return newclient.Update[users.User](client, responsibleTemplate, interruption.SpaceID, interruption.ID, new(users.User))
}

// Submit Submits a dictionary of form values for the interruption. Only the user with responsibility for this interruption can submit this form.
func Submit(client newclient.Client, interruption *Interruption, interruptionSubmitRequest *InterruptionSubmitRequest) (*Interruption, error) {
if interruptionSubmitRequest == nil {
return nil, internal.CreateInvalidParameterError(constants.OperationAPIPost, constants.ParameterResource)
}

spaceID, err := internal.GetSpaceID(interruption.SpaceID, client.GetSpaceID())
if err != nil {
return nil, err
}

path, err := client.URITemplateCache().Expand(submitTemplate, map[string]any{
"spaceId": spaceID,
"id": interruption.ID,
})
if err != nil {
return nil, err
}

res, err := newclient.Post[Interruption](client.HttpSession(), path, interruptionSubmitRequest)
if err != nil {
return nil, err
}

return res, nil
}