- format Project with handler private function
This commit is contained in:
parent
716d6936c0
commit
e1acfdf308
|
|
@ -2,7 +2,6 @@ package handler
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
|
||||||
"log"
|
"log"
|
||||||
"ticket-pimp/internal/controller"
|
"ticket-pimp/internal/controller"
|
||||||
|
|
||||||
|
|
@ -80,27 +79,10 @@ func (h *router) CreateFolderHandler(nameMinLenght int) route {
|
||||||
if resp.Project == nil {
|
if resp.Project == nil {
|
||||||
result = "Надо написать имя для папки, или создать папку из проекта!"
|
result = "Надо написать имя для папки, или создать папку из проекта!"
|
||||||
} else {
|
} else {
|
||||||
result = fmt.Sprintf(
|
result = resp.Project.DiscordString() + "Errors: " + resp.Message.Error()
|
||||||
"## Project info:\n🔑 key: %s\n📂 folder: %s\n👾 project git: %s\n🚀 build git: %s\n\nErrors: %v",
|
|
||||||
resp.Project.ShortName,
|
|
||||||
resp.Project.Cloud,
|
|
||||||
resp.Project.ProjectGit,
|
|
||||||
resp.Project.BuildGit,
|
|
||||||
resp.Message,
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Sending result:
|
h.defaultFollowUp(result, s, i)
|
||||||
_, 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
|
|
||||||
}
|
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -2,7 +2,6 @@ package handler
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
|
||||||
"log"
|
"log"
|
||||||
"ticket-pimp/internal/controller"
|
"ticket-pimp/internal/controller"
|
||||||
|
|
||||||
|
|
@ -105,26 +104,9 @@ func (h *router) CreateRepoHandler(repoNameMinLength int) route {
|
||||||
if resp.Project == nil {
|
if resp.Project == nil {
|
||||||
result = resp.Message.Error()
|
result = resp.Message.Error()
|
||||||
} else {
|
} else {
|
||||||
result = fmt.Sprintf(
|
result = resp.Project.DiscordString() + "Errors: " + resp.Message.Error()
|
||||||
"## Project info:\n🔑 key: %s\n📂 folder: %s\n👾 project git: %s\n🚀 build git: %s\n\nErrors: %v",
|
|
||||||
resp.Project.ShortName,
|
|
||||||
resp.Project.Cloud,
|
|
||||||
resp.Project.ProjectGit,
|
|
||||||
resp.Project.BuildGit,
|
|
||||||
resp.Message,
|
|
||||||
)
|
|
||||||
}
|
|
||||||
// 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
|
|
||||||
}
|
}
|
||||||
|
h.defaultFollowUp(result, s, i)
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -9,6 +9,49 @@ import (
|
||||||
"github.com/bwmarrin/discordgo"
|
"github.com/bwmarrin/discordgo"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
func (h *router) GetInfo() route {
|
||||||
|
return route{
|
||||||
|
Command: discordgo.ApplicationCommand{
|
||||||
|
Name: "info",
|
||||||
|
Description: "Get project's info",
|
||||||
|
},
|
||||||
|
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 {
|
||||||
|
project, err := h.controller.GetProjectByChannelID(context.TODO(), dchan.ID)
|
||||||
|
if err != nil {
|
||||||
|
result = err.Error()
|
||||||
|
} else {
|
||||||
|
result = project.DiscordString()
|
||||||
|
if err != nil {
|
||||||
|
result += "Errors: " + err.Error()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
h.defaultFollowUp(result, s, i)
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func (h *router) InitProjectFromChannel(minLength int) route {
|
func (h *router) InitProjectFromChannel(minLength int) route {
|
||||||
const (
|
const (
|
||||||
keyOption = "key"
|
keyOption = "key"
|
||||||
|
|
@ -76,31 +119,14 @@ func (h *router) InitProjectFromChannel(minLength int) route {
|
||||||
|
|
||||||
project, err := h.controller.InitProjectInChannel(context.TODO(), i.ChannelID, option.StringValue())
|
project, err := h.controller.InitProjectInChannel(context.TODO(), i.ChannelID, option.StringValue())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
errMsg = err
|
result = fmt.Sprintf("unable to init project: %v", err)
|
||||||
} else {
|
} else {
|
||||||
result = fmt.Sprintf(
|
result = project.DiscordString() + "Errors: " + errMsg.Error()
|
||||||
"## 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:
|
h.defaultFollowUp(result, s, i)
|
||||||
_, 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
|
|
||||||
}
|
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -178,17 +204,7 @@ func (h *router) CreateTicketHandler(repoNameMinLength int) route {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Sending result:
|
h.defaultFollowUp(result, s, i)
|
||||||
_, 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
|
|
||||||
}
|
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,7 @@
|
||||||
package handler
|
package handler
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
"ticket-pimp/internal/controller"
|
"ticket-pimp/internal/controller"
|
||||||
"ticket-pimp/internal/domain"
|
"ticket-pimp/internal/domain"
|
||||||
|
|
||||||
|
|
@ -24,6 +25,7 @@ func InitRouter(wc controller.WorkflowController, conf *domain.DiscordConfig) *r
|
||||||
r.Ping(),
|
r.Ping(),
|
||||||
r.CreateTicketHandler(3),
|
r.CreateTicketHandler(3),
|
||||||
r.InitProjectFromChannel(3),
|
r.InitProjectFromChannel(3),
|
||||||
|
r.GetInfo(),
|
||||||
)
|
)
|
||||||
r.controller = wc
|
r.controller = wc
|
||||||
r.conf = conf
|
r.conf = conf
|
||||||
|
|
@ -35,3 +37,18 @@ type route struct {
|
||||||
Command discordgo.ApplicationCommand
|
Command discordgo.ApplicationCommand
|
||||||
Handler func(s *discordgo.Session, i *discordgo.InteractionCreate)
|
Handler func(s *discordgo.Session, i *discordgo.InteractionCreate)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (h *router) defaultFollowUp(answer string, s *discordgo.Session, i *discordgo.InteractionCreate) {
|
||||||
|
|
||||||
|
// Sending result:
|
||||||
|
_, err := s.FollowupMessageCreate(i.Interaction, true, &discordgo.WebhookParams{
|
||||||
|
Content: answer,
|
||||||
|
})
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
s.FollowupMessageCreate(i.Interaction, true, &discordgo.WebhookParams{
|
||||||
|
Content: fmt.Sprintf("Something went wrong: %v", err),
|
||||||
|
})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -93,6 +93,17 @@ type Project struct {
|
||||||
Cloud string `json:"cloud"`
|
Cloud string `json:"cloud"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (p *Project) DiscordString() string {
|
||||||
|
|
||||||
|
return fmt.Sprintf(
|
||||||
|
"## Project info:\n> 🔑 key: %s\n> 📂 folder: %s\n> 👾 project git: %s\n>🚀 build git: %s\n",
|
||||||
|
p.ShortName,
|
||||||
|
p.Cloud,
|
||||||
|
p.ProjectGit,
|
||||||
|
p.BuildGit,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
type ProjectID struct {
|
type ProjectID struct {
|
||||||
ID string `json:"id"`
|
ID string `json:"id"`
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue