From 716d6936c0ae03830d35bc6474237dd81cc60ad6 Mon Sep 17 00:00:00 2001 From: naudachu Date: Fri, 10 Nov 2023 16:59:08 +0500 Subject: [PATCH] - add init old project handler --- client/discord/handler/handle_folder.go | 2 +- client/discord/handler/handle_git.go | 2 +- client/discord/handler/handle_ticket.go | 96 +++++++++++++++++++++++++ client/discord/handler/handler.go | 1 + internal/controller/control_project.go | 31 ++++++++ internal/domain/config.go | 8 +-- 6 files changed, 134 insertions(+), 6 deletions(-) diff --git a/client/discord/handler/handle_folder.go b/client/discord/handler/handle_folder.go index ce0b830..f83b127 100644 --- a/client/discord/handler/handle_folder.go +++ b/client/discord/handler/handle_folder.go @@ -61,7 +61,7 @@ func (h *router) CreateFolderHandler(nameMinLenght int) route { log.Printf("error while identifying channel: %v", err) } else { - if dchan.ParentID == h.conf.ProjectsChannelID { + if dchan.ParentID == h.conf.IsProjectChannel { req.ChannelID = dchan.ID if insertedValueNotNil { req.InsertedName = name.StringValue() diff --git a/client/discord/handler/handle_git.go b/client/discord/handler/handle_git.go index 3013070..66be11c 100644 --- a/client/discord/handler/handle_git.go +++ b/client/discord/handler/handle_git.go @@ -87,7 +87,7 @@ func (h *router) CreateRepoHandler(repoNameMinLength int) route { log.Printf("error while identifying channel: %v", err) } else { - if dchan.ParentID == h.conf.ProjectsChannelID { + if dchan.ParentID == h.conf.IsProjectChannel { req.ChannelID = dchan.ID if insertedValueNotNil { req.InsertedName = name.StringValue() diff --git a/client/discord/handler/handle_ticket.go b/client/discord/handler/handle_ticket.go index b9b8b92..0be9fde 100644 --- a/client/discord/handler/handle_ticket.go +++ b/client/discord/handler/handle_ticket.go @@ -9,6 +9,102 @@ import ( "github.com/bwmarrin/discordgo" ) +func (h *router) InitProjectFromChannel(minLength int) route { + const ( + keyOption = "key" + ) + return route{ + Command: discordgo.ApplicationCommand{ + Name: "init_project", + Description: "Connect project with Coda ID", + Options: []*discordgo.ApplicationCommandOption{ + { + Type: discordgo.ApplicationCommandOptionString, + Name: keyOption, + Description: "Project's key from Coda.io", + Required: true, + MinLength: &minLength, + }, + }, + }, + Handler: func(s *discordgo.Session, i *discordgo.InteractionCreate) { + + // Моментальный ответ для избежания столкновения с протуханием токена + initialResponse := discordgo.InteractionResponse{ + Type: discordgo.InteractionResponseChannelMessageWithSource, + Data: &discordgo.InteractionResponseData{ + Flags: discordgo.MessageFlagsEphemeral, + Content: "👩‍🍳 Cooking your query..", + }, + } + + s.InteractionRespond(i.Interaction, &initialResponse) + + var result string + + // Get channel from the request + dchan, err := s.Channel(i.ChannelID) + if err != nil { + result = "unable to get channel from the message" + } else { + if dchan.ParentID != h.conf.IsProjectChannel { + // Sending result: + _, err := s.FollowupMessageCreate(i.Interaction, true, &discordgo.WebhookParams{ + Content: "This channel is not at the project's group", + }) + + if err != nil { + s.FollowupMessageCreate(i.Interaction, true, &discordgo.WebhookParams{ + Content: fmt.Sprintf("Something went wrong: %v", err), + }) + return + } + return + } + + // Access options in the order provided by the user. + options := i.ApplicationCommandData().Options + + // Or convert the slice into a map + optionMap := make(map[string]*discordgo.ApplicationCommandInteractionDataOption, len(options)) + for _, opt := range options { + optionMap[opt.Name] = opt + } + + if option, ok := optionMap[keyOption]; ok { + var errMsg error = nil + + project, err := h.controller.InitProjectInChannel(context.TODO(), i.ChannelID, option.StringValue()) + if err != nil { + errMsg = err + } else { + result = fmt.Sprintf( + "## Project info:\n🔑 key: %s\n📂 folder: %s\n👾 project git: %s\n🚀 build git: %s\n\nErrors: %v", + project.ShortName, + project.Cloud, + project.ProjectGit, + project.BuildGit, + errMsg, + ) + } + } + } + + // Sending result: + _, err = s.FollowupMessageCreate(i.Interaction, true, &discordgo.WebhookParams{ + Content: result, + }) + + if err != nil { + s.FollowupMessageCreate(i.Interaction, true, &discordgo.WebhookParams{ + Content: fmt.Sprintf("Something went wrong: %v", err), + }) + return + } + }, + } +} + func (h *router) CreateTicketHandler(repoNameMinLength int) route { return route{ Command: discordgo.ApplicationCommand{ diff --git a/client/discord/handler/handler.go b/client/discord/handler/handler.go index 305e107..f7d6a18 100644 --- a/client/discord/handler/handler.go +++ b/client/discord/handler/handler.go @@ -23,6 +23,7 @@ func InitRouter(wc controller.WorkflowController, conf *domain.DiscordConfig) *r r.CreateFolderHandler(3), r.Ping(), r.CreateTicketHandler(3), + r.InitProjectFromChannel(3), ) r.controller = wc r.conf = conf diff --git a/internal/controller/control_project.go b/internal/controller/control_project.go index 54094f3..bc95ff6 100644 --- a/internal/controller/control_project.go +++ b/internal/controller/control_project.go @@ -85,3 +85,34 @@ func (wc *WorkflowController) GetProjectByChannelID(ctx context.Context, id stri } return &proj, nil } + +func (wc *WorkflowController) InitProjectInChannel(ctx context.Context, channelID string, key string) (*domain.Project, error) { + dbTicket, err := wc.q.GetTicketByChannelID(ctx, pgtype.Text{String: channelID, Valid: true}) + if err == pgx.ErrNoRows { + // [ ] Логика инициализации проекта + dbTicket, err = wc.q.CreateTicket( + ctx, + db.CreateTicketParams{ + Key: pgtype.Text{String: key, Valid: true}, + Channelid: pgtype.Text{String: channelID, Valid: true}, + }, + ) + if err != nil { + return nil, err + } + } else { + if err != nil { + return nil, err + } + } + + return &domain.Project{ + ID: string(dbTicket.ID), + ShortName: dbTicket.Key.String, + Name: dbTicket.Key.String, + ChannelID: dbTicket.Channelid.String, + ProjectGit: dbTicket.ProjectGit.String, + BuildGit: dbTicket.BuildGit.String, + Cloud: dbTicket.Folder.String, + }, nil +} diff --git a/internal/domain/config.go b/internal/domain/config.go index f91cf65..fb02231 100644 --- a/internal/domain/config.go +++ b/internal/domain/config.go @@ -48,8 +48,8 @@ type TelegramConfig struct { } type DiscordConfig struct { - Token string - ProjectsChannelID string + Token string + IsProjectChannel string } type ApplicationConfig struct { @@ -93,8 +93,8 @@ func InitConfig(envFilePath string) Config { Token: os.Getenv("TG_API"), }, Discord: DiscordConfig{ - Token: os.Getenv("DISCORD_TOKEN"), - ProjectsChannelID: os.Getenv("PROJECTS_CHANNEL_GROUP"), + Token: os.Getenv("DISCORD_TOKEN"), + IsProjectChannel: os.Getenv("PROJECTS_CHANNEL_GROUP"), }, } }