From 95c1c72771eb06a3298cb972ca5b30e3d7a5573d Mon Sep 17 00:00:00 2001 From: chandresh pancholi Date: Wed, 3 May 2023 12:54:59 +0530 Subject: [PATCH] TP-12345|adding slack topic while creating channel (#51) * TP-12345|adding slack topic while creating channel * TP-12345|create incident request api update --- cmd/app/handler/team_handler.go | 2 -- .../start_incident_modal_submission_action.go | 4 ++++ model/severity/severity.go | 15 ++++++++++++ model/team/team.go | 13 +++++++++++ pkg/slackbot/channel.go | 11 +++++++++ service/incident_service.go | 23 +++++++++++++++---- service/request/create_incident.go | 10 ++++---- 7 files changed, 66 insertions(+), 12 deletions(-) diff --git a/cmd/app/handler/team_handler.go b/cmd/app/handler/team_handler.go index 8c46f6e..2690627 100644 --- a/cmd/app/handler/team_handler.go +++ b/cmd/app/handler/team_handler.go @@ -10,14 +10,12 @@ import ( ) type teamHandler struct { - gin *gin.Engine logger *zap.Logger db *gorm.DB } func NewTeamHandler(gin *gin.Engine, logger *zap.Logger, db *gorm.DB) *teamHandler { return &teamHandler{ - gin: gin, logger: logger, db: db, } diff --git a/internal/processor/action/start_incident_modal_submission_action.go b/internal/processor/action/start_incident_modal_submission_action.go index 3ee5adb..23071a4 100644 --- a/internal/processor/action/start_incident_modal_submission_action.go +++ b/internal/processor/action/start_incident_modal_submission_action.go @@ -64,6 +64,10 @@ func (cip *CreateIncidentAction) CreateIncidentModalCommandProcessing(callback s return } + topic := fmt.Sprintf("%s-%s(%s) Incident-%d | %s", teamEntity.Name, severityEntity.Name, severityEntity.Description, incidentEntity.ID, incidentEntity.Title) + + cip.slackbotClient.SetChannelTopic(*channelID, topic) + go func() { // Post incident summary to Blaze Group channel and incident channel _, err := cip.postIncidentSummary(callback.View.PrivateMetadata, *channelID, incidentEntity, teamEntity, diff --git a/model/severity/severity.go b/model/severity/severity.go index 2eb8df8..f43d05e 100644 --- a/model/severity/severity.go +++ b/model/severity/severity.go @@ -61,6 +61,21 @@ func (s *Repository) FindSeverityById(severityId uint) (*SeverityEntity, error) return &severityEntity, nil } +func (s *Repository) FindSeverityByName(severityName string) (*SeverityEntity, error) { + var severityEntity SeverityEntity + + result := s.gormClient.Find(&severityEntity, "name = ?", severityName) + if result.Error != nil { + return nil, result.Error + } + + if result.RowsAffected == 0 { + return nil, nil + } + + return &severityEntity, nil +} + func (s *Repository) Update(severityEntity *SeverityEntity) error { result := s.gormClient.Updates(severityEntity) if result.Error != nil { diff --git a/model/team/team.go b/model/team/team.go index 971c8bb..03b1733 100644 --- a/model/team/team.go +++ b/model/team/team.go @@ -42,6 +42,19 @@ func (r *Repository) FindTeamById(teamId uint) (*TeamEntity, error) { return &teamEntity, nil } +func (r *Repository) FindTeamByTeamName(teamName string) (*TeamEntity, error) { + var teamEntity TeamEntity + + result := r.gormClient.Find(&teamEntity, "name = ?", teamName) + if result.Error != nil { + return nil, result.Error + } else if result.RowsAffected == 0 { + return nil, nil + } + + return &teamEntity, nil +} + func (r *Repository) UpdateTeam(teamEntity *TeamEntity) error { result := r.gormClient.Updates(teamEntity) if result.Error != nil { diff --git a/pkg/slackbot/channel.go b/pkg/slackbot/channel.go index d01aa30..5928971 100644 --- a/pkg/slackbot/channel.go +++ b/pkg/slackbot/channel.go @@ -37,6 +37,17 @@ func (c *Client) CreateChannel(channelName string) (string, error) { return channel.ID, nil } +func (c *Client) SetChannelTopic(channelId, topic string) (string, error) { + channel, err := c.socketModeClient.SetTopicOfConversation(channelId, topic) + if err != nil { + c.logger.Error("set topic on slack channel failed", zap.String("channel_id", channelId), zap.Error(err)) + return "", err + } + + c.logger.Info("set topic on slack channel successful", zap.String("channel_id", channelId), zap.String("channel_id", channel.ID)) + return "", nil +} + func (c *Client) InviteUsersToConversation(channelId string, userId ...string) { _, err := c.socketModeClient.InviteUsersToConversation(channelId, userId...) if err != nil { diff --git a/service/incident_service.go b/service/incident_service.go index 3c74cd0..8079fb6 100644 --- a/service/incident_service.go +++ b/service/incident_service.go @@ -286,7 +286,10 @@ func (i *incidentService) CreateIncident(c *gin.Context) { return } - var incidentRequest = buildCreateIncidentRequest(createIncRequest) + incidentRequest, err := i.buildCreateIncidentRequest(createIncRequest) + if err != nil { + c.JSON(http.StatusBadRequest, err) + } // Save the incident to the database incidentEntity, err := i.incidentRepository.CreateIncident(incidentRequest) @@ -434,18 +437,28 @@ func (i *incidentService) createSlackChannel(incidentEntity *incident.IncidentEn return &channelID, nil } -func buildCreateIncidentRequest(createIncRequest request.CreateIncidentRequest) *incident.CreateIncidentRequest { +func (i *incidentService) buildCreateIncidentRequest(createIncRequest request.CreateIncidentRequest) (*incident.CreateIncidentRequest, error) { var createIncidentRequest incident.CreateIncidentRequest + teamEntity, err := i.teamRepository.FindTeamByTeamName(createIncRequest.TeamName) + if err != nil { + return nil, err + } + + severityEntity, err := i.severityRepository.FindSeverityByName(createIncRequest.SeverityName) + if err != nil { + return nil, err + } + createIncidentRequest.Title = createIncRequest.Title createIncidentRequest.Description = createIncRequest.Description - createIncidentRequest.Severity = createIncRequest.SeverityId - createIncidentRequest.TeamId = createIncRequest.TeamId + createIncidentRequest.Severity = fmt.Sprintf("%d", severityEntity.ID) + createIncidentRequest.TeamId = fmt.Sprintf("%d", teamEntity.ID) createIncidentRequest.Status = incident.Investigating createIncidentRequest.CreatedBy = createIncRequest.CreatedBy createIncidentRequest.UpdatedBy = createIncRequest.CreatedBy createIncidentRequest.StartTime = time.Now() createIncidentRequest.EnableReminder = false - return &createIncidentRequest + return &createIncidentRequest, nil } func (i *incidentService) UpdateIncident(c *gin.Context) { diff --git a/service/request/create_incident.go b/service/request/create_incident.go index 8515e8b..aa6d73d 100644 --- a/service/request/create_incident.go +++ b/service/request/create_incident.go @@ -1,9 +1,9 @@ package service type CreateIncidentRequest struct { - Title string `gorm:"column:title"` - Description string `gorm:"column:description"` - SeverityId string `gorm:"column:severityId"` - TeamId string `gorm:"column:teamId"` - CreatedBy string `gorm:"column:createdBy"` + Title string `gorm:"column:title"` + Description string `gorm:"column:description"` + SeverityName string `gorm:"column:severityName"` + TeamName string `gorm:"column:teamName"` + CreatedBy string `gorm:"column:createdBy"` }