Files
houston-be/internal/processor/action/set_status_command_action.go
Ajay Devarakonda 392f434e46 TP-53262 | Fixed incident status from slash command processor and incident update status popup (#353)
* TP-38709 | Merging the changes to master on the logfix

* TP-53262 | Fixed set status to investigating from incident action drop down and from slash command resolver

* TP-53262 | Resolved pr comment

* TP-53262 | Resolved pr comment

* TP-53262 | Resolved pr comment
2024-01-17 17:42:51 +05:30

123 lines
4.6 KiB
Go

package action
import (
"fmt"
"github.com/slack-go/slack"
"github.com/slack-go/slack/socketmode"
"go.uber.org/zap"
"houston/appcontext"
"houston/common/util"
"houston/internal"
"houston/logger"
"houston/model/incident"
"houston/pkg/slackbot"
"strings"
"time"
)
const setStatusActionLogTag = "[set_status_command_action]"
type SetStatusCommandAction struct {
socketModeClient *socketmode.Client
slackBot *slackbot.Client
}
func NewSetStatusCommandAction(
socketModeClient *socketmode.Client,
slackBot *slackbot.Client,
) *SetStatusCommandAction {
return &SetStatusCommandAction{
socketModeClient: socketModeClient,
slackBot: slackBot,
}
}
func (action *SetStatusCommandAction) PerformAction(evt *socketmode.Event) {
cmd, ok := evt.Data.(slack.SlashCommand)
logger.Info("processing houston command", zap.Any("payload", cmd))
if !ok {
logger.Error("event data to slash command conversion failed", zap.Any("data", evt))
return
}
err := action.setStatus(cmd, strings.TrimSpace(cmd.Text[len(internal.SetStatusParam):]))
if err != nil {
err := appcontext.GetSlackService().PostEphemeralByChannelID(err.Error(), cmd.UserID, false, cmd.ChannelID)
if err != nil {
logger.Error(fmt.Sprintf("%s failed to post ephemeral for create incident error. %+v", setStatusActionLogTag, err))
}
}
action.socketModeClient.Ack(*evt.Request)
}
// todo: this method has to be removed and usage has to be replaced with update incident V2 once update incident refactor goes live.
func (action *SetStatusCommandAction) setStatus(cmd slack.SlashCommand, status string) error {
logger.Info(fmt.Sprintf("%s received request to update the status to %s", setStatusActionLogTag, status))
return executeForHoustonChannel(cmd, func() error {
if strings.TrimSpace(strings.ToLower(status)) == strings.ToLower(incident.Resolved) {
message := "Please resolve using `/houston resolve` command."
err := appcontext.GetSlackService().PostEphemeralByChannelID(message, cmd.UserID, false, cmd.ChannelID)
if err != nil {
logger.Error(fmt.Sprintf("%s failed to post ephemeral for invalid slash command param. %+v", setStatusActionLogTag, err))
}
} else if strings.TrimSpace(strings.ToLower(status)) == strings.ToLower(incident.Duplicated) {
message := "Please duplicate using `/houston` command and select `Mark incident as duplicate` action from drop down."
err := appcontext.GetSlackService().PostEphemeralByChannelID(message, cmd.UserID, false, cmd.ChannelID)
if err != nil {
logger.Error(fmt.Sprintf("%s failed to post ephemeral for invalid slash command param. %+v", setStatusActionLogTag, err))
}
} else {
incidentEntity, err := appcontext.GetIncidentService().GetIncidentByChannelID(cmd.ChannelID)
if err != nil {
logger.Error(fmt.Sprintf("%s failed to fetch incident entity with channel ID: %s. %+v", setStatusActionLogTag, cmd.ChannelID, err))
return genericBackendError
}
if incidentEntity == nil {
logger.Error(fmt.Sprintf("%s no entry found for incident with channel ID: %s in DB", setStatusActionLogTag, cmd.ChannelID))
return genericBackendError
}
incidentStatusEntity, err := appcontext.GetIncidentRepo().GetIncidentStatusByStatusName(status)
if err != nil {
logger.Error(fmt.Sprintf("%s error in finding incident status for status name %s. %+v", setStatusActionLogTag, status, err))
return genericBackendError
}
if incidentStatusEntity == nil {
logger.Error(fmt.Sprintf("%s no entity found or status name %s", setStatusActionLogTag, status))
return fmt.Errorf("%s is not a valid status", status)
}
statusID := incidentStatusEntity.ID
incidentEntity.Status = statusID
incidentEntity.UpdatedBy = cmd.UserID
incidentEntity.UpdatedAt = time.Now()
err = appcontext.GetIncidentRepo().UpdateIncident(incidentEntity)
if err != nil {
logger.Error("UpdateIncident error",
zap.String("incident_slack_channel_id", cmd.ChannelID), zap.String("channel", incidentEntity.IncidentName),
zap.String("user_id", cmd.UserID), zap.Error(err))
}
go func() {
errMessage := util.PostIncidentStatusUpdateMessage(cmd.UserID, incidentStatusEntity.Name, cmd.ChannelID, action.socketModeClient)
if errMessage != nil {
logger.Error("post response failed for IncidentUpdateStatus", zap.Error(errMessage))
return
}
msgUpdate := NewIncidentChannelMessageUpdateAction(
action.socketModeClient,
appcontext.GetIncidentRepo(),
appcontext.GetTeamRepo(),
appcontext.GetSeverityRepo(),
)
msgUpdate.ProcessAction(incidentEntity.SlackChannel)
}()
}
return nil
})
}