- add init old project handler
This commit is contained in:
parent
4e2ec953f8
commit
716d6936c0
|
|
@ -61,7 +61,7 @@ func (h *router) CreateFolderHandler(nameMinLenght int) route {
|
||||||
log.Printf("error while identifying channel: %v", err)
|
log.Printf("error while identifying channel: %v", err)
|
||||||
} else {
|
} else {
|
||||||
|
|
||||||
if dchan.ParentID == h.conf.ProjectsChannelID {
|
if dchan.ParentID == h.conf.IsProjectChannel {
|
||||||
req.ChannelID = dchan.ID
|
req.ChannelID = dchan.ID
|
||||||
if insertedValueNotNil {
|
if insertedValueNotNil {
|
||||||
req.InsertedName = name.StringValue()
|
req.InsertedName = name.StringValue()
|
||||||
|
|
|
||||||
|
|
@ -87,7 +87,7 @@ func (h *router) CreateRepoHandler(repoNameMinLength int) route {
|
||||||
log.Printf("error while identifying channel: %v", err)
|
log.Printf("error while identifying channel: %v", err)
|
||||||
} else {
|
} else {
|
||||||
|
|
||||||
if dchan.ParentID == h.conf.ProjectsChannelID {
|
if dchan.ParentID == h.conf.IsProjectChannel {
|
||||||
req.ChannelID = dchan.ID
|
req.ChannelID = dchan.ID
|
||||||
if insertedValueNotNil {
|
if insertedValueNotNil {
|
||||||
req.InsertedName = name.StringValue()
|
req.InsertedName = name.StringValue()
|
||||||
|
|
|
||||||
|
|
@ -9,6 +9,102 @@ import (
|
||||||
"github.com/bwmarrin/discordgo"
|
"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 {
|
func (h *router) CreateTicketHandler(repoNameMinLength int) route {
|
||||||
return route{
|
return route{
|
||||||
Command: discordgo.ApplicationCommand{
|
Command: discordgo.ApplicationCommand{
|
||||||
|
|
|
||||||
|
|
@ -23,6 +23,7 @@ func InitRouter(wc controller.WorkflowController, conf *domain.DiscordConfig) *r
|
||||||
r.CreateFolderHandler(3),
|
r.CreateFolderHandler(3),
|
||||||
r.Ping(),
|
r.Ping(),
|
||||||
r.CreateTicketHandler(3),
|
r.CreateTicketHandler(3),
|
||||||
|
r.InitProjectFromChannel(3),
|
||||||
)
|
)
|
||||||
r.controller = wc
|
r.controller = wc
|
||||||
r.conf = conf
|
r.conf = conf
|
||||||
|
|
|
||||||
|
|
@ -85,3 +85,34 @@ func (wc *WorkflowController) GetProjectByChannelID(ctx context.Context, id stri
|
||||||
}
|
}
|
||||||
return &proj, nil
|
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
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -48,8 +48,8 @@ type TelegramConfig struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
type DiscordConfig struct {
|
type DiscordConfig struct {
|
||||||
Token string
|
Token string
|
||||||
ProjectsChannelID string
|
IsProjectChannel string
|
||||||
}
|
}
|
||||||
|
|
||||||
type ApplicationConfig struct {
|
type ApplicationConfig struct {
|
||||||
|
|
@ -93,8 +93,8 @@ func InitConfig(envFilePath string) Config {
|
||||||
Token: os.Getenv("TG_API"),
|
Token: os.Getenv("TG_API"),
|
||||||
},
|
},
|
||||||
Discord: DiscordConfig{
|
Discord: DiscordConfig{
|
||||||
Token: os.Getenv("DISCORD_TOKEN"),
|
Token: os.Getenv("DISCORD_TOKEN"),
|
||||||
ProjectsChannelID: os.Getenv("PROJECTS_CHANNEL_GROUP"),
|
IsProjectChannel: os.Getenv("PROJECTS_CHANNEL_GROUP"),
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue