* TP-49403 | parameterized slash command * TP-49403 | handeling resolve and rca params also implemented Help-Commands button * TP-49403 | using command pattern for command resolutiuon and execution * TP-49403 | made find team by name and find severity by name queries case insensitive * TP-49403 | updating help message keys
86 lines
3.1 KiB
Go
86 lines
3.1 KiB
Go
package action
|
|
|
|
import (
|
|
"fmt"
|
|
"github.com/slack-go/slack"
|
|
"github.com/slack-go/slack/socketmode"
|
|
"go.uber.org/zap"
|
|
"houston/appcontext"
|
|
"houston/internal"
|
|
"houston/logger"
|
|
"houston/pkg/slackbot"
|
|
"strings"
|
|
"time"
|
|
)
|
|
|
|
const setDescriptionActionLogTag = "[set_description_command_action]"
|
|
|
|
type SetDescriptionCommandAction struct {
|
|
socketModeClient *socketmode.Client
|
|
slackBot *slackbot.Client
|
|
}
|
|
|
|
func NewSetDescriptionCommandAction(
|
|
socketModeClient *socketmode.Client,
|
|
slackBot *slackbot.Client,
|
|
) *SetDescriptionCommandAction {
|
|
return &SetDescriptionCommandAction{
|
|
socketModeClient: socketModeClient,
|
|
slackBot: slackBot,
|
|
}
|
|
}
|
|
|
|
func (action *SetDescriptionCommandAction) 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.setDescription(cmd, strings.TrimSpace(cmd.Text[len(internal.SetDescriptionParam):]))
|
|
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", setDescriptionActionLogTag, 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 *SetDescriptionCommandAction) setDescription(cmd slack.SlashCommand, description string) error {
|
|
logger.Info(fmt.Sprintf("%s received request to update the description to %s", setDescriptionActionLogTag, description))
|
|
|
|
return executeForHoustonChannel(cmd, func() error {
|
|
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", setDescriptionActionLogTag, cmd.ChannelID, err))
|
|
return genericBackendError
|
|
}
|
|
if incidentEntity == nil {
|
|
logger.Error(fmt.Sprintf("%s no entry found for incident with channel ID: %s in DB", setDescriptionActionLogTag, cmd.ChannelID))
|
|
return genericBackendError
|
|
}
|
|
|
|
incidentEntity.Description = description
|
|
incidentEntity.UpdatedBy = cmd.UserID
|
|
incidentEntity.UpdatedAt = time.Now()
|
|
err = appcontext.GetIncidentRepo().UpdateIncident(incidentEntity)
|
|
if err != nil {
|
|
logger.Error("IncidentUpdateDescription error",
|
|
zap.String("incident_slack_channel_id", cmd.ChannelID), zap.String("channel", incidentEntity.IncidentName),
|
|
zap.String("user_id", cmd.UserID), zap.Error(err))
|
|
return fmt.Errorf("failed to update incident entity")
|
|
}
|
|
msgOption := slack.MsgOptionText(fmt.Sprintf("<@%s> *>* `houston set description to %s`", cmd.UserID, incidentEntity.Description), false)
|
|
_, _, errMessage := action.socketModeClient.PostMessage(cmd.ChannelID, msgOption)
|
|
if errMessage != nil {
|
|
logger.Error("post response failed for IncidentUpdateDescription", zap.Error(errMessage))
|
|
}
|
|
|
|
return nil
|
|
})
|
|
}
|