Skip to content

Commit

Permalink
Merge pull request #37 from iLert/fix/alert-actions-teams
Browse files Browse the repository at this point in the history
Fix/alert actions teams
  • Loading branch information
STLVRTX authored Feb 20, 2024
2 parents 85f07b7 + 5f34214 commit 5ad941c
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 20 deletions.
7 changes: 6 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
# Changelog

# 20.02.2024, Version 3.6.1

- fix backwards compatibility with alert actions v2 changes [#37](https://github.com/iLert/ilert-go/pull/37)
- ensures existing scripts using one alert source with no team explicitly set to use legacy api without breaking

# 01.02.2024, Version 3.6.0

- apply alert actions v2 changes in [#29](https://github.com/iLert/ilert-go/pull/29)
- add alertSources and teams fields, deprecate alertSourceIds

# 12.01.2024, Version 3.5.0

- add Telegram as alert action type in [#35](https://github.com/iLert/ilert-go/pull/35)
- add Telegram as alert action type in [#36](https://github.com/iLert/ilert-go/pull/36)

# 05.01.2024, Version 3.4.1

Expand Down
56 changes: 39 additions & 17 deletions alert_action.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,28 +10,28 @@ import (

// AlertAction definition https://api.ilert.com/api-docs/#tag/Alert-Actions
type AlertAction struct {
ID string `json:"id,omitempty"`
Name string `json:"name"`
AlertSourceIDs []int64 `json:"alertSourceIds,omitempty"` // @deprecated
AlertSources []AlertSource `json:"alertSources,omitempty"`
ConnectorID string `json:"connectorId,omitempty"`
ConnectorType string `json:"connectorType"`
TriggerMode string `json:"triggerMode"`
DelaySec int `json:"delaySec,omitempty"` // between 0 and 7200, only allowed with triggerType 'alert-escalation-ended'
TriggerTypes []string `json:"triggerTypes,omitempty"`
CreatedAt string `json:"createdAt,omitempty"` // date time string in ISO 8601
UpdatedAt string `json:"updatedAt,omitempty"` // date time string in ISO 8601
Params interface{} `json:"params"`
AlertFilter *AlertFilter `json:"alertFilter,omitempty"`
Teams []TeamShort `json:"teams,omitempty"`
ID string `json:"id,omitempty"`
Name string `json:"name"`
AlertSourceIDs []int64 `json:"alertSourceIds,omitempty"` // @deprecated
AlertSources *[]AlertSource `json:"alertSources,omitempty"`
ConnectorID string `json:"connectorId,omitempty"`
ConnectorType string `json:"connectorType"`
TriggerMode string `json:"triggerMode"`
DelaySec int `json:"delaySec,omitempty"` // between 0 and 7200, only allowed with triggerType 'alert-escalation-ended'
TriggerTypes []string `json:"triggerTypes,omitempty"`
CreatedAt string `json:"createdAt,omitempty"` // date time string in ISO 8601
UpdatedAt string `json:"updatedAt,omitempty"` // date time string in ISO 8601
Params interface{} `json:"params"`
AlertFilter *AlertFilter `json:"alertFilter,omitempty"`
Teams *[]TeamShort `json:"teams,omitempty"`
}

// AlertActionOutput definition https://api.ilert.com/api-docs/#tag/Alert-Actions
type AlertActionOutput struct {
ID string `json:"id"`
Name string `json:"name"`
AlertSourceIDs []int64 `json:"alertSourceIds,omitempty"` // @deprecated
AlertSources []AlertSource `json:"alertSources,omitempty"`
AlertSources *[]AlertSource `json:"alertSources,omitempty"`
ConnectorID string `json:"connectorId"`
ConnectorType string `json:"connectorType"`
TriggerMode string `json:"triggerMode"`
Expand All @@ -41,7 +41,7 @@ type AlertActionOutput struct {
UpdatedAt string `json:"updatedAt"` // date time string in ISO 8601
Params *AlertActionOutputParams `json:"params"`
AlertFilter *AlertFilter `json:"alertFilter,omitempty"`
Teams []TeamShort `json:"teams,omitempty"`
Teams *[]TeamShort `json:"teams,omitempty"`
}

// AlertActionOutputParams definition
Expand Down Expand Up @@ -404,6 +404,15 @@ func (c *Client) CreateAlertAction(input *CreateAlertActionInput) (*CreateAlertA
if input.AlertAction == nil {
return nil, errors.New("alert action input is required")
}
if input.AlertAction.AlertSources != nil && len(*input.AlertAction.AlertSources) == 1 && (input.AlertAction.Teams == nil || len(*input.AlertAction.Teams) == 0) {
sourceId := (*input.AlertAction.AlertSources)[0].ID

// manually set fields to ensure backwards compatibility with api v1
input.AlertAction.AlertSourceIDs = []int64{sourceId}
input.AlertAction.AlertSources = nil
input.AlertAction.Teams = nil
}

resp, err := c.httpClient.R().SetBody(input.AlertAction).Post(apiRoutes.alertActions)
if err != nil {
return nil, err
Expand All @@ -425,6 +434,7 @@ func (c *Client) CreateAlertAction(input *CreateAlertActionInput) (*CreateAlertA
type GetAlertActionInput struct {
_ struct{}
AlertActionID *string
Version *int
}

// GetAlertActionOutput represents the output of a GetAlertAction operation.
Expand All @@ -441,8 +451,12 @@ func (c *Client) GetAlertAction(input *GetAlertActionInput) (*GetAlertActionOutp
if input.AlertActionID == nil {
return nil, errors.New("alert action id is required")
}
q := url.Values{}
if input.Version != nil {
q.Add("version", strconv.Itoa(*input.Version))
}

resp, err := c.httpClient.R().Get(fmt.Sprintf("%s/%s", apiRoutes.alertActions, *input.AlertActionID))
resp, err := c.httpClient.R().Get(fmt.Sprintf("%s/%s?%s", apiRoutes.alertActions, *input.AlertActionID, q.Encode()))
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -571,6 +585,14 @@ func (c *Client) UpdateAlertAction(input *UpdateAlertActionInput) (*UpdateAlertA
if input.AlertActionID == nil {
return nil, errors.New("alert action id is required")
}
if input.AlertAction.AlertSources != nil && len(*input.AlertAction.AlertSources) == 1 && (input.AlertAction.Teams == nil || len(*input.AlertAction.Teams) == 0) {
sourceId := (*input.AlertAction.AlertSources)[0].ID

// manually set fields to ensure backwards compatibility with api v1
input.AlertAction.AlertSourceIDs = []int64{sourceId}
input.AlertAction.AlertSources = nil
input.AlertAction.Teams = nil
}

resp, err := c.httpClient.R().SetBody(input.AlertAction).Put(fmt.Sprintf("%s/%s", apiRoutes.alertActions, *input.AlertActionID))
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion examples/alert_action/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ func main() {
ConnectorID: rcr.Connector.ID,
TriggerMode: ilert.AlertActionTriggerModes.Automatic,
TriggerTypes: ilert.AlertActionTriggerTypesAll,
AlertSources: []ilert.AlertSource{*ras.AlertSource},
AlertSources: &[]ilert.AlertSource{*ras.AlertSource},
Params: ilert.AlertActionParamsGithub{
Owner: "my-org",
Repository: "my-repo",
Expand Down
2 changes: 1 addition & 1 deletion version.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package ilert

// Version package version
const Version = "v3.6.0"
const Version = "v3.6.1"

0 comments on commit 5ad941c

Please sign in to comment.