125 lines
4.8 KiB
Go
125 lines
4.8 KiB
Go
package action
|
|
|
|
import (
|
|
"fmt"
|
|
"houston/internal/processor/action/view"
|
|
"houston/pkg/postgres/service/incident"
|
|
"houston/pkg/postgres/service/severity"
|
|
"houston/pkg/slackbot"
|
|
"strconv"
|
|
"time"
|
|
|
|
"github.com/slack-go/slack"
|
|
"github.com/slack-go/slack/socketmode"
|
|
"go.uber.org/zap"
|
|
)
|
|
|
|
type IncidentUpdateSevertityAction struct {
|
|
client *socketmode.Client
|
|
logger *zap.Logger
|
|
severityService *severity.Service
|
|
incidentService *incident.Service
|
|
slackbotClient *slackbot.Client
|
|
}
|
|
|
|
func NewIncidentUpdateSeverityAction(client *socketmode.Client, logger *zap.Logger, incidentService *incident.Service, severityService *severity.Service, slackbotClient *slackbot.Client) *IncidentUpdateSevertityAction {
|
|
return &IncidentUpdateSevertityAction{
|
|
client: client,
|
|
logger: logger,
|
|
severityService: severityService,
|
|
incidentService: incidentService,
|
|
slackbotClient: slackbotClient,
|
|
}
|
|
}
|
|
|
|
func (isp *IncidentUpdateSevertityAction) IncidentUpdateSeverityRequestProcess(callback slack.InteractionCallback, request *socketmode.Request) {
|
|
incidentSeverity, err := isp.severityService.GetAllActiveSeverity()
|
|
if err != nil || incidentSeverity == nil {
|
|
isp.logger.Error("FindSeverityEntity error",
|
|
zap.String("incident_slack_channel_id", callback.Channel.ID), zap.String("channel", callback.Channel.Name),
|
|
zap.String("user_id", callback.User.ID), zap.Error(err))
|
|
return
|
|
}
|
|
modalRequest := view.BuildIncidentUpdateSeverityModal(callback.Channel, *incidentSeverity)
|
|
|
|
_, err = isp.client.OpenView(callback.TriggerID, modalRequest)
|
|
if err != nil {
|
|
isp.logger.Error("houston slackbot openview command failed.",
|
|
zap.String("trigger_id", callback.TriggerID), zap.String("channel_id", callback.Channel.ID), zap.Error(err))
|
|
return
|
|
}
|
|
var payload interface{}
|
|
isp.client.Ack(*request, payload)
|
|
|
|
}
|
|
|
|
func (isp *IncidentUpdateSevertityAction) IncidentUpdateSeverity(callback slack.InteractionCallback, request *socketmode.Request, channel slack.Channel, user slack.User) {
|
|
incidentEntity, err := isp.incidentService.FindIncidentByChannelId(callback.View.PrivateMetadata)
|
|
if err != nil {
|
|
isp.logger.Error("FindIncidentByChannelId error",
|
|
zap.String("incident_slack_channel_id", channel.ID), zap.String("channel", channel.Name),
|
|
zap.String("user_id", user.ID), zap.Error(err))
|
|
return
|
|
} else if incidentEntity == nil {
|
|
isp.logger.Error("IncidentEntity not found ",
|
|
zap.String("incident_slack_channel_id", callback.Channel.ID), zap.String("channel", callback.Channel.Name),
|
|
zap.String("user_id", callback.User.ID), zap.Error(err))
|
|
return
|
|
}
|
|
|
|
incidentSeverityId := buildUpdateIncidentSeverityRequest(isp.logger, callback.View.State.Values)
|
|
incidentSeverityEntity, err := isp.severityService.FindIncidentSeverityEntityById(incidentSeverityId)
|
|
if err != nil {
|
|
isp.logger.Error("FindIncidentSeverityEntityById error",
|
|
zap.String("incident_slack_channel_id", channel.ID), zap.String("channel", channel.Name),
|
|
zap.String("user_id", user.ID), zap.Error(err))
|
|
return
|
|
} else if incidentSeverityEntity == nil {
|
|
isp.logger.Error("SeverityEntity not found",
|
|
zap.String("incident_slack_channel_id", channel.ID), zap.String("channel", channel.Name),
|
|
zap.String("user_id", user.ID), zap.Error(err))
|
|
return
|
|
}
|
|
incidentEntity.SeverityId = incidentSeverityEntity.ID
|
|
incidentEntity.UpdatedBy = user.ID
|
|
incidentEntity.SeverityTat = time.Now().AddDate(0, 0, incidentSeverityEntity.Sla)
|
|
err = isp.incidentService.UpdateIncident(incidentEntity)
|
|
if err != nil {
|
|
isp.logger.Error("UpdateIncident error",
|
|
zap.String("incident_slack_channel_id", channel.ID), zap.String("channel", channel.Name),
|
|
zap.String("user_id", user.ID), zap.Error(err))
|
|
}
|
|
|
|
for _, o := range incidentSeverityEntity.SlackUserIds {
|
|
isp.slackbotClient.InviteUsersToConversation(callback.View.PrivateMetadata, o)
|
|
}
|
|
|
|
msgOption := slack.MsgOptionText(fmt.Sprintf("<@%s> > set severity to %s", user.ID, incidentSeverityEntity.Name), false)
|
|
_, _, errMessage := isp.client.PostMessage(callback.View.PrivateMetadata, msgOption)
|
|
if errMessage != nil {
|
|
isp.logger.Error("post response failed for IncidentUpdateSeverity", zap.Error(errMessage))
|
|
return
|
|
}
|
|
var payload interface{}
|
|
isp.client.Ack(*request, payload)
|
|
}
|
|
|
|
// TODO - ADD USER ACCORDING TO SEVERITY
|
|
func buildUpdateIncidentSeverityRequest(logger *zap.Logger, blockActions map[string]map[string]slack.BlockAction) int {
|
|
var requestMap = make(map[string]string, 0)
|
|
for _, actions := range blockActions {
|
|
for actionID, a := range actions {
|
|
if a.Type == "static_select" {
|
|
requestMap[actionID] = a.SelectedOption.Value
|
|
}
|
|
}
|
|
}
|
|
|
|
selectedValue := requestMap["incident_severity_modal_request"]
|
|
selectedValueInInt, err := strconv.Atoi(selectedValue)
|
|
if err != nil {
|
|
logger.Error("String conversion to int faileed in buildUpdateIncidentTypeRequest for "+selectedValue, zap.Error(err))
|
|
}
|
|
return selectedValueInInt
|
|
}
|