- implement whole Folder's feature set
This commit is contained in:
parent
6c07f434fc
commit
3a4b461cda
|
|
@ -62,7 +62,6 @@ func run(conf domain.Config) {
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
|
|
||||||
// go func() {
|
|
||||||
opts := telegram.TelegramOptions{
|
opts := telegram.TelegramOptions{
|
||||||
// TicketsRepo: db,
|
// TicketsRepo: db,
|
||||||
GitService: gitService,
|
GitService: gitService,
|
||||||
|
|
@ -75,6 +74,4 @@ func run(conf domain.Config) {
|
||||||
log.Fatalf("telegram bot cannot be runned: %v", err)
|
log.Fatalf("telegram bot cannot be runned: %v", err)
|
||||||
defer os.Exit(1)
|
defer os.Exit(1)
|
||||||
}
|
}
|
||||||
// }()
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -5,6 +5,7 @@ import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
"ticket-pimp/internal/controller"
|
||||||
|
|
||||||
"github.com/bwmarrin/discordgo"
|
"github.com/bwmarrin/discordgo"
|
||||||
)
|
)
|
||||||
|
|
@ -30,20 +31,21 @@ func (h *router) CreateFolderHandler(nameMinLenght int) route {
|
||||||
},
|
},
|
||||||
|
|
||||||
Handler: func(s *discordgo.Session, i *discordgo.InteractionCreate) {
|
Handler: func(s *discordgo.Session, i *discordgo.InteractionCreate) {
|
||||||
|
// Моментальный ответ для избежания столкновения с протуханием токена
|
||||||
initialResponse := discordgo.InteractionResponse{
|
initialResponse := discordgo.InteractionResponse{
|
||||||
// Ignore type for now, they will be discussed in "responses"
|
|
||||||
Type: discordgo.InteractionResponseChannelMessageWithSource,
|
Type: discordgo.InteractionResponseChannelMessageWithSource,
|
||||||
Data: &discordgo.InteractionResponseData{
|
Data: &discordgo.InteractionResponseData{
|
||||||
Flags: discordgo.MessageFlagsEphemeral,
|
Flags: discordgo.MessageFlagsEphemeral,
|
||||||
Content: "..folder is going to be created",
|
Content: "Cooking your query..",
|
||||||
Title: "📂 Folder creation",
|
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
s.InteractionRespond(i.Interaction, &initialResponse)
|
s.InteractionRespond(i.Interaction, &initialResponse)
|
||||||
|
|
||||||
|
// Определение переменной для ответа
|
||||||
var result string = "unexpected result"
|
var result string = "unexpected result"
|
||||||
|
|
||||||
|
// Определение выбранных вариантов ответа
|
||||||
options := i.ApplicationCommandData().Options
|
options := i.ApplicationCommandData().Options
|
||||||
|
|
||||||
optionMap := make(map[string]*discordgo.ApplicationCommandInteractionDataOption, len(options))
|
optionMap := make(map[string]*discordgo.ApplicationCommandInteractionDataOption, len(options))
|
||||||
|
|
@ -51,39 +53,45 @@ func (h *router) CreateFolderHandler(nameMinLenght int) route {
|
||||||
optionMap[opt.Name] = opt
|
optionMap[opt.Name] = opt
|
||||||
}
|
}
|
||||||
|
|
||||||
var str string = ""
|
name, insertedValueNotNil := optionMap[nameOption]
|
||||||
|
|
||||||
dchan, err := s.Channel(i.ChannelID)
|
dchan, err := s.Channel(i.ChannelID)
|
||||||
|
|
||||||
|
// Creating request:
|
||||||
|
var req controller.FolderRequest
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf("error while identifying channel: %v", err)
|
log.Printf("error while identifying channel: %v", err)
|
||||||
}
|
} else {
|
||||||
|
|
||||||
if dchan.ParentID == strconv.Itoa(1150719794853716028) {
|
if dchan.ParentID == strconv.Itoa(1150719794853716028) {
|
||||||
log.Println("yes, channel is from `Projects`")
|
req.ChannelID = dchan.ID
|
||||||
project, err := h.controller.GetProjectByChannelID(context.TODO(), i.ChannelID)
|
if insertedValueNotNil {
|
||||||
if err != nil {
|
req.InsertedName = name.StringValue()
|
||||||
result = fmt.Sprintf("unable to retrieve project from db, error: %v", err)
|
|
||||||
} else {
|
|
||||||
switch {
|
|
||||||
case project == nil:
|
|
||||||
if option, ok := optionMap[nameOption]; ok {
|
|
||||||
str = option.StringValue()
|
|
||||||
} else {
|
|
||||||
str = "Ты, либо в проекте директорию создавай, либо имя напиши, блет!"
|
|
||||||
}
|
|
||||||
default:
|
|
||||||
str = project.ShortName
|
|
||||||
}
|
}
|
||||||
|
|
||||||
resp := h.controller.ICloud.CreateFolder(str)
|
|
||||||
if resp.ErrMessage != nil {
|
|
||||||
result = fmt.Sprintf("Command executed w/ errors: ``` %v``` \n But check this link: %s\n", resp.ErrMessage, resp.Folder.PrivateURL)
|
|
||||||
} else {
|
} else {
|
||||||
result = fmt.Sprintf("📂 Folder was created: %s", resp.Folder.PrivateURL)
|
req.ChannelID = ""
|
||||||
|
if insertedValueNotNil {
|
||||||
|
req.InsertedName = name.StringValue()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Making request:
|
||||||
|
resp := h.controller.CreateFolder(context.TODO(), req)
|
||||||
|
if resp.Project == nil {
|
||||||
|
result = "Надо написать имя для папки, или создать папку из проекта!"
|
||||||
|
} else {
|
||||||
|
result = fmt.Sprintf(
|
||||||
|
"## 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{
|
_, err = s.FollowupMessageCreate(i.Interaction, true, &discordgo.WebhookParams{
|
||||||
Content: result,
|
Content: result,
|
||||||
})
|
})
|
||||||
|
|
@ -94,7 +102,6 @@ func (h *router) CreateFolderHandler(nameMinLenght int) route {
|
||||||
})
|
})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,100 @@
|
||||||
|
package controller
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
"log"
|
||||||
|
"ticket-pimp/internal/domain"
|
||||||
|
"ticket-pimp/internal/storage/db"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/jackc/pgx/v5/pgtype"
|
||||||
|
)
|
||||||
|
|
||||||
|
type FolderRequest struct {
|
||||||
|
ChannelID string
|
||||||
|
InsertedName string
|
||||||
|
}
|
||||||
|
|
||||||
|
type ProjectResponse struct {
|
||||||
|
Project *domain.Project
|
||||||
|
Message error
|
||||||
|
}
|
||||||
|
|
||||||
|
func (wc *WorkflowController) CreateFolder(ctx context.Context, req FolderRequest) *ProjectResponse {
|
||||||
|
|
||||||
|
project, err := wc.GetProjectByChannelID(ctx, req.ChannelID)
|
||||||
|
if err != nil {
|
||||||
|
return &ProjectResponse{
|
||||||
|
Project: nil,
|
||||||
|
Message: fmt.Errorf("unable to retrieve project from db: %v", err),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
var (
|
||||||
|
name string
|
||||||
|
dbticket db.Ticket
|
||||||
|
result ProjectResponse
|
||||||
|
)
|
||||||
|
|
||||||
|
if project != nil {
|
||||||
|
switch {
|
||||||
|
case project.Cloud != "":
|
||||||
|
return &ProjectResponse{
|
||||||
|
Project: project,
|
||||||
|
Message: nil,
|
||||||
|
}
|
||||||
|
case project.ShortName != "":
|
||||||
|
name = project.ShortName
|
||||||
|
case req.InsertedName != "":
|
||||||
|
name = req.InsertedName
|
||||||
|
}
|
||||||
|
response := wc.ICloud.CreateFolder(name)
|
||||||
|
|
||||||
|
dbticket, err = wc.q.UpdateTicketFolder(
|
||||||
|
ctx,
|
||||||
|
db.UpdateTicketFolderParams{
|
||||||
|
Folder: pgtype.Text{String: response.Folder.PrivateURL, Valid: true},
|
||||||
|
UpdatedAt: pgtype.Timestamptz{Time: time.Now(), InfinityModifier: 0, Valid: true},
|
||||||
|
Channelid: pgtype.Text{String: req.ChannelID, Valid: true},
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
log.Printf("unable to scan row from db: %v", err)
|
||||||
|
return &ProjectResponse{
|
||||||
|
Project: project,
|
||||||
|
Message: fmt.Errorf("unable to update project: %v", err),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
result = ProjectResponse{
|
||||||
|
Project: &domain.Project{
|
||||||
|
ID: string(dbticket.ID),
|
||||||
|
ShortName: dbticket.Key.String,
|
||||||
|
ChannelID: dbticket.Channelid.String,
|
||||||
|
ProjectGit: dbticket.ProjectGit.String,
|
||||||
|
BuildGit: dbticket.BuildGit.String,
|
||||||
|
Cloud: dbticket.Folder.String,
|
||||||
|
},
|
||||||
|
Message: response.ErrMessage,
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if req.InsertedName != "" {
|
||||||
|
response := wc.ICloud.CreateFolder(req.InsertedName)
|
||||||
|
result = ProjectResponse{
|
||||||
|
Project: &domain.Project{
|
||||||
|
Cloud: response.Folder.PrivateURL,
|
||||||
|
},
|
||||||
|
Message: response.ErrMessage,
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
return &ProjectResponse{
|
||||||
|
Project: nil,
|
||||||
|
Message: errors.New("передано пустое имя"),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return &result
|
||||||
|
|
||||||
|
}
|
||||||
|
|
@ -78,6 +78,9 @@ func (wc *WorkflowController) GetProjectByChannelID(ctx context.Context, id stri
|
||||||
ShortName: dbTicket.Key.String,
|
ShortName: dbTicket.Key.String,
|
||||||
Name: dbTicket.Key.String,
|
Name: dbTicket.Key.String,
|
||||||
ChannelID: dbTicket.Channelid.String,
|
ChannelID: dbTicket.Channelid.String,
|
||||||
|
ProjectGit: dbTicket.ProjectGit.String,
|
||||||
|
BuildGit: dbTicket.BuildGit.String,
|
||||||
|
Cloud: dbTicket.Folder.String,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return &proj, nil
|
return &proj, nil
|
||||||
|
|
|
||||||
|
|
@ -87,6 +87,10 @@ type Project struct {
|
||||||
ShortName string `json:"shortName"`
|
ShortName string `json:"shortName"`
|
||||||
Name string `json:"name"`
|
Name string `json:"name"`
|
||||||
ChannelID string `json:"channel_id"`
|
ChannelID string `json:"channel_id"`
|
||||||
|
|
||||||
|
ProjectGit string `json:"project_git"`
|
||||||
|
BuildGit string `json:"build_git"`
|
||||||
|
Cloud string `json:"cloud"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type ProjectID struct {
|
type ProjectID struct {
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,7 @@
|
||||||
package services
|
package services
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
|
@ -82,7 +83,7 @@ func (c *Cloud) CreateFolder(name string) Response {
|
||||||
// </d:error>
|
// </d:error>
|
||||||
if strings.Contains(err.Error(), "already exists") {
|
if strings.Contains(err.Error(), "already exists") {
|
||||||
R.Folder.PrivateURL = c.BaseURL + R.Folder.PathTo
|
R.Folder.PrivateURL = c.BaseURL + R.Folder.PathTo
|
||||||
R.ErrMessage = err
|
R.ErrMessage = errors.New("guess, that folder already exists")
|
||||||
// Try to set short URL to the d entity
|
// Try to set short URL to the d entity
|
||||||
if err := c.setPrivateURL(requestPath, R.Folder); err != nil {
|
if err := c.setPrivateURL(requestPath, R.Folder); err != nil {
|
||||||
R.ErrMessage = err
|
R.ErrMessage = err
|
||||||
|
|
|
||||||
|
|
@ -215,3 +215,33 @@ func (q *Queries) UpdateTicketByID(ctx context.Context, arg UpdateTicketByIDPara
|
||||||
)
|
)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const updateTicketFolder = `-- name: UpdateTicketFolder :one
|
||||||
|
UPDATE tickets
|
||||||
|
SET folder = $1, updated_at = $2
|
||||||
|
WHERE channelID = $3
|
||||||
|
RETURNING id, key, channelid, project_git, build_git, folder, created_at, deleted_at, updated_at
|
||||||
|
`
|
||||||
|
|
||||||
|
type UpdateTicketFolderParams struct {
|
||||||
|
Folder pgtype.Text
|
||||||
|
UpdatedAt pgtype.Timestamptz
|
||||||
|
Channelid pgtype.Text
|
||||||
|
}
|
||||||
|
|
||||||
|
func (q *Queries) UpdateTicketFolder(ctx context.Context, arg UpdateTicketFolderParams) (Ticket, error) {
|
||||||
|
row := q.db.QueryRow(ctx, updateTicketFolder, arg.Folder, arg.UpdatedAt, arg.Channelid)
|
||||||
|
var i Ticket
|
||||||
|
err := row.Scan(
|
||||||
|
&i.ID,
|
||||||
|
&i.Key,
|
||||||
|
&i.Channelid,
|
||||||
|
&i.ProjectGit,
|
||||||
|
&i.BuildGit,
|
||||||
|
&i.Folder,
|
||||||
|
&i.CreatedAt,
|
||||||
|
&i.DeletedAt,
|
||||||
|
&i.UpdatedAt,
|
||||||
|
)
|
||||||
|
return i, err
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -15,6 +15,12 @@ INSERT INTO tickets (
|
||||||
)
|
)
|
||||||
RETURNING *;
|
RETURNING *;
|
||||||
|
|
||||||
|
-- name: UpdateTicketFolder :one
|
||||||
|
UPDATE tickets
|
||||||
|
SET folder = $1, updated_at = $2
|
||||||
|
WHERE channelID = $3
|
||||||
|
RETURNING *;
|
||||||
|
|
||||||
-- name: ListTickets :many
|
-- name: ListTickets :many
|
||||||
SELECT * FROM tickets WHERE deleted_at IS NULL;
|
SELECT * FROM tickets WHERE deleted_at IS NULL;
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue