Files
houston-be/pkg/slack/houston/command/incident_update_title.go
Shubham Kirve b974cb6bf3 TP-0000 | Initialize houston repo (#1)
* TP-0000 | intialize houston repo

* TP-0000 | intialize houston repo
2023-03-29 00:01:17 +05:30

112 lines
4.2 KiB
Go

package command
import (
"fmt"
"houston/pkg/postgres/query"
houston "houston/pkg/slack/houston/design"
"github.com/slack-go/slack"
"github.com/slack-go/slack/socketmode"
"go.uber.org/zap"
"gorm.io/gorm"
)
type incidentUpdateTitleProcessor struct {
client *socketmode.Client
db *gorm.DB
logger *zap.Logger
}
func NewIncidentUpdateTitleProcessor(client *socketmode.Client, db *gorm.DB, logger *zap.Logger) *incidentUpdateTitleProcessor {
return &incidentUpdateTitleProcessor{
client: client,
db: db,
logger: logger,
}
}
func (itp *incidentUpdateTitleProcessor) IncidentUpdateTitleRequestProcess(callback slack.InteractionCallback, request *socketmode.Request) {
result, err := query.FindIncidentByChannelId(itp.db, callback.Channel.ID)
if err != nil {
itp.logger.Error("FindIncidentByChannelId 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
} else if result == nil {
itp.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
}
modalRequest := houston.BuildIncidentUpdateTitleModal(itp.db, callback.Channel, result.Title)
_, err = itp.client.OpenView(callback.TriggerID, modalRequest)
if err != nil {
itp.logger.Error("houston slack openview command for IncidentUpdateTitleRequestProcess failed.",
zap.String("trigger_id", callback.TriggerID), zap.String("channel_id", callback.Channel.ID), zap.Error(err))
return
}
var payload interface{}
itp.client.Ack(*request, payload)
}
func (itp *incidentUpdateTitleProcessor) IncidentUpdateTitle(callback slack.InteractionCallback, request *socketmode.Request, channel slack.Channel, user slack.User) {
incidentEntity, err := query.FindIncidentByChannelId(itp.db, callback.View.PrivateMetadata)
if err != nil {
itp.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 {
itp.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
}
incidentTitle := buildUpdateIncidentTitleRequest(callback.View.State.Values)
incidentEntity.Title = incidentTitle
incidentEntity.UpdatedBy = user.ID
err = query.UpdateIncident(itp.db, incidentEntity)
if err != nil {
itp.logger.Error("IncidentUpdateTitle error",
zap.String("incident_slack_channel_id", channel.ID), zap.String("channel", channel.Name),
zap.String("user_id", user.ID), zap.Error(err))
return
}
msgOption := slack.MsgOptionText(fmt.Sprintf("<@%s> > set title to %s", user.ID, incidentEntity.Title), false)
_, _, errMessage := itp.client.PostMessage(callback.View.PrivateMetadata, msgOption)
if errMessage != nil {
itp.logger.Error("post response failed for IncidentUpdateTitle", zap.Error(errMessage))
return
}
result, err := query.FindIncidentSeverityTeamJoin(itp.db, incidentEntity.SlackChannel)
if err != nil {
itp.logger.Error("query failed for FindIncidentSeverityTeamJoin", zap.Error(errMessage))
return
}
msgOption = slack.MsgOptionText(fmt.Sprintf("set the channel topic: %s : %s %s | %s", result.TeamsName, result.SeverityName, incidentEntity.IncidentName, incidentEntity.Title), false)
_, _, errMessage = itp.client.PostMessage(callback.View.PrivateMetadata, msgOption)
if errMessage != nil {
itp.logger.Error("post response failed for IncidentUpdateTitle", zap.Error(errMessage))
return
}
var payload interface{}
itp.client.Ack(*request, payload)
}
func buildUpdateIncidentTitleRequest(blockActions map[string]map[string]slack.BlockAction) string {
var requestMap = make(map[string]string, 0)
for _, actions := range blockActions {
for actionID, action := range actions {
if action.Type == "plain_text_input" {
requestMap[actionID] = action.Value
}
}
}
return requestMap["incident_title"]
}