- positive cases with create repo for a project;

This commit is contained in:
naudachu 2023-11-06 16:46:56 +05:00
parent ecad950f54
commit f8bccf0135
21 changed files with 307 additions and 306 deletions

View File

@ -8,15 +8,14 @@ import (
"os/signal"
"syscall"
"ticket-pimp/internal/controller"
"ticket-pimp/internal/domain"
"ticket-pimp/internal/services"
"ticket-pimp/discord"
"ticket-pimp/telegram"
ticketDB "ticket-pimp/internal/storage/db/tickets"
"github.com/jackc/pgx/v5"
"github.com/jackc/pgx/v5/pgxpool"
)
func main() {
@ -26,37 +25,12 @@ func main() {
// test(config)
}
// func test(conf domain.Config) {
// ctx, cancel := signal.NotifyContext(context.Background(), os.Interrupt, os.Kill, syscall.SIGTERM)
// defer cancel()
// conn, err := pgxpool.New(ctx, fmt.Sprintf(
// "postgresql://%s:%s@%s:%s/%s",
// conf.DB.User,
// conf.DB.Pass,
// conf.DB.Host,
// conf.DB.Port,
// conf.DB.Name,
// ))
// if err != nil {
// log.Fatalf("DB connection failed: %v", err)
// }
// q := configDB.New(conn)
// appController := controller.NewAppConfig(q)
// ticketConfig, err := appController.NewKey(ctx)
// _ = ticketConfig
// }
func run(conf domain.Config) {
ctx, cancel := signal.NotifyContext(context.Background(), os.Interrupt, os.Kill, syscall.SIGTERM)
defer cancel()
// -- DB connection init -- START
conn, err := pgx.Connect(
conn, err := pgxpool.New(
ctx,
fmt.Sprintf(
"postgresql://%s:%s@%s:%s/%s",
@ -65,21 +39,24 @@ func run(conf domain.Config) {
if err != nil {
log.Fatalf("DB connection failed: %v", err)
}
defer conn.Close(ctx)
// -- DB connection init -- END
ticketsRepo := ticketDB.New(conn)
gitService := services.NewGit(conf.Git)
cloudService := services.NewCloud(conf.Cloud)
codeService := services.NewCodaClient(conf.Coda)
// Инициализация контроллера:
controller := controller.NewWorkflowController(
gitService,
cloudService,
codeService,
conn,
)
go func() {
opts := discord.DiscordOptions{
TicketsRepo: ticketsRepo,
GitService: gitService,
CloudService: cloudService,
Coda: codeService,
AppConfig: &conf,
Controller: controller,
AppConfig: &conf,
}
if err := discord.Run(conf, opts); err != nil {
log.Fatalf("discord bot cannot be runned: %v", err)
@ -88,7 +65,7 @@ func run(conf domain.Config) {
// go func() {
opts := telegram.TelegramOptions{
TicketsRepo: ticketsRepo,
// TicketsRepo: db,
GitService: gitService,
CloudService: cloudService,
Coda: codeService,

View File

@ -6,10 +6,8 @@ import (
"os"
"os/signal"
"ticket-pimp/discord/handler"
"ticket-pimp/internal/controller"
"ticket-pimp/internal/domain"
"ticket-pimp/internal/services"
tickets "ticket-pimp/internal/storage/db/tickets"
"github.com/bwmarrin/discordgo"
)
@ -23,11 +21,8 @@ func initBotWith(token string) (*discordgo.Session, error) {
}
type DiscordOptions struct {
TicketsRepo *tickets.Queries
GitService *services.Git
CloudService *services.Cloud
Coda *services.Coda
AppConfig *domain.Config
AppConfig *domain.Config
Controller *controller.WorkflowController
}
func Run(conf domain.Config, opts DiscordOptions) error {
@ -38,7 +33,7 @@ func Run(conf domain.Config, opts DiscordOptions) error {
return err
}
router := handler.InitRouter(opts.GitService)
router := handler.InitRouter(*opts.Controller)
commandHandlers := map[string]func(s *discordgo.Session, i *discordgo.InteractionCreate){}
for _, handler := range router.Routes {

View File

@ -1,29 +1,30 @@
package handler
import (
"context"
"fmt"
"log"
"ticket-pimp/internal/controller"
"ticket-pimp/internal/domain"
"ticket-pimp/internal/services"
"github.com/bwmarrin/discordgo"
)
type router struct {
Routes []route
git services.IGit
Routes []route
controller controller.WorkflowController
}
// Подключение роутов к Discord боту
func InitRouter(gitService services.IGit) *router {
func InitRouter(wc controller.WorkflowController) *router {
var r router
r.Routes = append(
r.Routes,
r.CreateRepoHandler(3),
r.CreateTicketHandler(3),
)
r.git = gitService
r.controller = wc
return &r
}
@ -64,12 +65,13 @@ func (h *router) CreateRepoHandler(repoNameMinLength int) route {
Type: discordgo.ApplicationCommandOptionString,
Name: "repo_name",
Description: "Type the repository's name",
Required: true,
Required: false,
MinLength: &repoNameMinLength,
},
},
},
Handler: func(s *discordgo.Session, i *discordgo.InteractionCreate) {
var result string
// Access options in the order provided by the user.
options := i.ApplicationCommandData().Options
@ -81,26 +83,37 @@ func (h *router) CreateRepoHandler(repoNameMinLength int) route {
var str string = ""
if option, ok := optionMap["repo_name"]; ok {
str = option.StringValue()
project, err := h.controller.GetProjectByChannelID(context.TODO(), i.ChannelID)
if err != nil {
result = fmt.Sprintf("unable to retrieve project from db, error: %v", err)
} else {
var suffix string
if option, ok := optionMap["repo_type"]; ok {
switch option.Value {
case projectRepo:
suffix = ""
case buildRepo:
str += "-build"
suffix = "-build"
}
}
}
var g *domain.Git
var result string
if option, ok := optionMap["repo_name"]; ok {
str = option.StringValue()
} else {
str = project.ShortName
}
g, err := h.git.CreateRepo(str)
if err != nil {
result = fmt.Sprintf("error while repo creation: %v", err)
} else {
result = "🚀 " + g.HtmlUrl + " was created"
str = str + suffix
var g *domain.Git
g, err := h.controller.IGit.CreateRepo(str)
// g, err := h.git.CreateRepo(str)
if err != nil {
result = fmt.Sprintf("error while repo creation: %v", err)
} else {
result = "🚀 " + g.HtmlUrl + " was created"
}
}
discerr := s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
@ -144,28 +157,32 @@ func (h *router) CreateTicketHandler(repoNameMinLength int) route {
if option, ok := optionMap["project_name"]; ok {
dchan, err := s.GuildChannelCreate(i.GuildID, option.StringValue(), discordgo.ChannelTypeGuildText)
if err != nil {
log.Printf("chan creation problem: %v\n", err)
}
result = fmt.Sprintf("chan creation problem: %v\n", err)
} else {
p, err := h.controller.ProjectCreate(context.TODO(), domain.Project{
ChannelID: dchan.ID,
})
if err != nil {
result = fmt.Sprintf("unable to create project: %v\n", err)
} else {
edit := discordgo.ChannelEdit{
Name: p.ShortName,
ParentID: "1150719794853716028",
}
// permissions := discordgo.PermissionOverwrite{
// ID: i.User.ID,
// Type: 1,
// //Deny: 0,
// Allow: 1,
// }
edit := discordgo.ChannelEdit{
// PermissionOverwrites: []*discordgo.PermissionOverwrite{&permissions}, // [ ] Как сделать приватный канал то???
ParentID: "1150719794853716028",
dchan, err = s.ChannelEdit(dchan.ID, &edit)
if err != nil {
result = fmt.Sprintf("channel created, but unable to edit: %v\n", err)
} else {
_, err = s.ChannelMessageSend(dchan.ID, "Hello!")
if err != nil {
log.Printf("message send problem: %v\n", err)
}
result = dchan.ID
}
}
}
dchan, err = s.ChannelEdit(dchan.ID, &edit)
if err != nil {
log.Printf("chan editing problem: %v\n", err)
}
_, err = s.ChannelMessageSend(dchan.ID, "Hello!")
if err != nil {
log.Printf("message send problem: %v\n", err)
}
result = dchan.ID
}
s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{

View File

@ -8,38 +8,36 @@ import (
"sync"
"ticket-pimp/internal/domain"
"ticket-pimp/internal/services"
db "ticket-pimp/internal/storage/db/tickets"
"time"
"ticket-pimp/internal/storage/db"
"github.com/jackc/pgx/v5/pgtype"
"github.com/jackc/pgx/v5/pgxpool"
)
type WorkflowController struct {
iGit services.IGit
iCloud services.ICloud
iCoda services.ICoda
db *db.Queries
IGit services.IGit
ICloud services.ICloud
ICoda services.ICoda
pool *pgxpool.Pool
q *db.Queries
}
func NewWorkflowController(
git services.IGit,
cloud services.ICloud,
coda services.ICoda,
db *db.Queries,
pool *pgxpool.Pool,
) *WorkflowController {
return &WorkflowController{
iGit: git,
iCloud: cloud,
iCoda: coda,
db: db,
IGit: git,
ICloud: cloud,
ICoda: coda,
pool: pool,
q: db.New(pool),
}
}
type IWorkflowController interface {
Workflow(name, key, id string) (string, error)
}
func (wc *WorkflowController) Workflow(name, key, id string) (string, error) {
func (wc *WorkflowController) FullProjectInit(name, key, id string) (string, error) {
appKey := fmt.Sprintf("%s-%s", key, id)
@ -53,17 +51,17 @@ func (wc *WorkflowController) Workflow(name, key, id string) (string, error) {
go func(ref **domain.Git) {
defer wg.Done()
*ref, _ = wc.iGit.CreateRepo(appKey)
*ref, _ = wc.IGit.CreateRepo(appKey)
}(&git)
go func(ref **domain.Git) {
defer wg.Done()
*ref, _ = wc.iGit.CreateRepo(appKey + "-build")
*ref, _ = wc.IGit.CreateRepo(appKey + "-build")
}(&gitBuild)
go func(ref **domain.Folder) {
defer wg.Done()
*ref, _ = wc.iCloud.CreateFolder(appKey)
*ref, _ = wc.ICloud.CreateFolder(appKey)
}(&cloud)
wg.Wait()
@ -89,19 +87,16 @@ func (wc *WorkflowController) Workflow(name, key, id string) (string, error) {
}
ctx := context.TODO()
insertedTicket, err := wc.db.CreateTicket(ctx, db.CreateTicketParams{
Key: pgtype.Text{String: appKey, Valid: true},
ProjectGit: pgtype.Text{String: gitResult, Valid: true},
BuildGit: pgtype.Text{String: gitBuildResult, Valid: true},
Folder: pgtype.Text{String: cloudResult, Valid: true},
CreatedAt: pgtype.Timestamptz{Time: time.Now(), InfinityModifier: 0, Valid: true},
insertedTicket, err := wc.q.CreateTicket(ctx, db.CreateTicketParams{
Key: pgtype.Text{String: appKey, Valid: true},
Channelid: pgtype.Text{},
})
if err != nil {
log.Fatal(err)
}
log.Print(insertedTicket)
wc.iCoda.CreateApp(domain.CodaApplication{
wc.ICoda.CreateApp(domain.CodaApplication{
ID: appKey,
Summary: strings.TrimSpace(name),
Git: gitResult,

View File

@ -0,0 +1,84 @@
package controller
import (
"context"
"fmt"
"ticket-pimp/internal/domain"
"ticket-pimp/internal/storage/db"
"github.com/jackc/pgx/v5"
"github.com/jackc/pgx/v5/pgtype"
)
func (wc *WorkflowController) Get(ctx context.Context) (*domain.ApplicationConfig, error) {
c, err := wc.q.GetConfig(ctx)
return &domain.ApplicationConfig{
Key: c.TicketKey.String,
ID: int(c.TicketID.Int32),
}, err
}
func (wc *WorkflowController) NewKey(ctx context.Context) (*domain.ApplicationConfig, error) {
c, err := wc.q.SetNewConfig(ctx)
return &domain.ApplicationConfig{
Key: c.TicketKey.String,
ID: int(c.TicketID.Int32),
}, err
}
func (wc *WorkflowController) ProjectCreate(ctx context.Context, project domain.Project) (*domain.Project, error) {
tx, err := wc.pool.Begin(ctx)
if err != nil {
return nil, err
}
defer tx.Rollback(ctx)
qtx := wc.q.WithTx(tx)
appconfig, err := qtx.SetNewConfig(ctx)
if err != nil {
return nil, err
}
project.ShortName = fmt.Sprintf(
"%s-%d",
appconfig.TicketKey.String,
appconfig.TicketID.Int32,
)
project.ID = string(appconfig.TicketID.Int32)
projectRow, err := qtx.CreateTicket(ctx, db.CreateTicketParams{
Key: pgtype.Text{String: project.ShortName, Valid: true},
Channelid: pgtype.Text{String: project.ChannelID, Valid: true},
})
if err != nil {
tx.Rollback(ctx)
return nil, err
} else {
tx.Commit(ctx)
fmt.Println(projectRow)
}
return &project, nil
}
func (wc *WorkflowController) GetProjectByChannelID(ctx context.Context, id string) (*domain.Project, error) {
var proj domain.Project
dbTicket, err := wc.q.GetTicketByChannelID(ctx, pgtype.Text{String: id, Valid: true})
if err != nil {
if err == pgx.ErrNoRows {
return nil, nil
}
return nil, err
} else {
proj = domain.Project{
ID: string(dbTicket.ID),
ShortName: dbTicket.Key.String,
Name: dbTicket.Key.String,
ChannelID: dbTicket.Channelid.String,
}
}
return &proj, nil
}

View File

@ -3,7 +3,7 @@ package controller
import (
"context"
"ticket-pimp/internal/domain"
db "ticket-pimp/internal/storage/db/config"
db "ticket-pimp/internal/storage/db"
)
type IConfigController interface {
@ -20,19 +20,3 @@ func NewAppConfig(db *db.Queries) AppConfig {
db: db,
}
}
func (ac *AppConfig) Get(ctx context.Context) (*domain.ApplicationConfig, error) {
c, err := ac.db.GetConfig(ctx)
return &domain.ApplicationConfig{
Key: c.TicketKey.String,
ID: int(c.TicketID.Int32),
}, err
}
func (ac *AppConfig) NewKey(ctx context.Context) (*domain.ApplicationConfig, error) {
c, err := ac.db.SetNewConfig(ctx)
return &domain.ApplicationConfig{
Key: c.TicketKey.String,
ID: int(c.TicketID.Int32),
}, err
}

View File

@ -86,6 +86,7 @@ type Project struct {
ID string `json:"id"`
ShortName string `json:"shortName"`
Name string `json:"name"`
ChannelID string `json:"channel_id"`
}
type ProjectID struct {

View File

@ -1,35 +0,0 @@
// Code generated by sqlc. DO NOT EDIT.
// versions:
// sqlc v1.23.0
// source: config.sql
package db
import (
"context"
)
const getConfig = `-- name: GetConfig :one
SELECT ticket_key, ticket_id
FROM appconfig
`
func (q *Queries) GetConfig(ctx context.Context) (Appconfig, error) {
row := q.db.QueryRow(ctx, getConfig)
var i Appconfig
err := row.Scan(&i.TicketKey, &i.TicketID)
return i, err
}
const setNewConfig = `-- name: SetNewConfig :one
UPDATE appconfig
SET ticket_id = ticket_id + 1
RETURNING ticket_key, ticket_id
`
func (q *Queries) SetNewConfig(ctx context.Context) (Appconfig, error) {
row := q.db.QueryRow(ctx, setNewConfig)
var i Appconfig
err := row.Scan(&i.TicketKey, &i.TicketID)
return i, err
}

View File

@ -1,14 +0,0 @@
// Code generated by sqlc. DO NOT EDIT.
// versions:
// sqlc v1.23.0
package db
import (
"github.com/jackc/pgx/v5/pgtype"
)
type Appconfig struct {
TicketKey pgtype.Text
TicketID pgtype.Int4
}

View File

@ -8,9 +8,15 @@ import (
"github.com/jackc/pgx/v5/pgtype"
)
type Appconfig struct {
TicketKey pgtype.Text
TicketID pgtype.Int4
}
type Ticket struct {
ID int32
Key pgtype.Text
Channelid pgtype.Text
ProjectGit pgtype.Text
BuildGit pgtype.Text
Folder pgtype.Text

View File

@ -1,7 +1,7 @@
// Code generated by sqlc. DO NOT EDIT.
// versions:
// sqlc v1.23.0
// source: tickets.sql
// source: queries.sql
package db
@ -13,33 +13,25 @@ import (
const createTicket = `-- name: CreateTicket :one
INSERT INTO tickets (
key, project_git, build_git, folder, created_at
key, channelID
) VALUES (
$1, $2, $3, $4, $5
$1, $2
)
RETURNING id, key, project_git, build_git, folder, created_at, deleted_at, updated_at
RETURNING id, key, channelid, project_git, build_git, folder, created_at, deleted_at, updated_at
`
type CreateTicketParams struct {
Key pgtype.Text
ProjectGit pgtype.Text
BuildGit pgtype.Text
Folder pgtype.Text
CreatedAt pgtype.Timestamptz
Key pgtype.Text
Channelid pgtype.Text
}
func (q *Queries) CreateTicket(ctx context.Context, arg CreateTicketParams) (Ticket, error) {
row := q.db.QueryRow(ctx, createTicket,
arg.Key,
arg.ProjectGit,
arg.BuildGit,
arg.Folder,
arg.CreatedAt,
)
row := q.db.QueryRow(ctx, createTicket, arg.Key, arg.Channelid)
var i Ticket
err := row.Scan(
&i.ID,
&i.Key,
&i.Channelid,
&i.ProjectGit,
&i.BuildGit,
&i.Folder,
@ -68,8 +60,41 @@ func (q *Queries) DeleteTicketByKey(ctx context.Context, key pgtype.Text) error
return err
}
const getConfig = `-- name: GetConfig :one
SELECT ticket_key, ticket_id
FROM appconfig
`
func (q *Queries) GetConfig(ctx context.Context) (Appconfig, error) {
row := q.db.QueryRow(ctx, getConfig)
var i Appconfig
err := row.Scan(&i.TicketKey, &i.TicketID)
return i, err
}
const getTicketByChannelID = `-- name: GetTicketByChannelID :one
SELECT id, key, channelid, project_git, build_git, folder, created_at, deleted_at, updated_at FROM tickets WHERE channelID = $1
`
func (q *Queries) GetTicketByChannelID(ctx context.Context, channelid pgtype.Text) (Ticket, error) {
row := q.db.QueryRow(ctx, getTicketByChannelID, 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
}
const getTicketByID = `-- name: GetTicketByID :one
SELECT id, key, project_git, build_git, folder, created_at, deleted_at, updated_at FROM tickets WHERE id = $1
SELECT id, key, channelid, project_git, build_git, folder, created_at, deleted_at, updated_at FROM tickets WHERE id = $1
`
func (q *Queries) GetTicketByID(ctx context.Context, id int32) (Ticket, error) {
@ -78,6 +103,7 @@ func (q *Queries) GetTicketByID(ctx context.Context, id int32) (Ticket, error) {
err := row.Scan(
&i.ID,
&i.Key,
&i.Channelid,
&i.ProjectGit,
&i.BuildGit,
&i.Folder,
@ -89,7 +115,7 @@ func (q *Queries) GetTicketByID(ctx context.Context, id int32) (Ticket, error) {
}
const listTickets = `-- name: ListTickets :many
SELECT id, key, project_git, build_git, folder, created_at, deleted_at, updated_at FROM tickets WHERE deleted_at IS NULL
SELECT id, key, channelid, project_git, build_git, folder, created_at, deleted_at, updated_at FROM tickets WHERE deleted_at IS NULL
`
func (q *Queries) ListTickets(ctx context.Context) ([]Ticket, error) {
@ -104,6 +130,7 @@ func (q *Queries) ListTickets(ctx context.Context) ([]Ticket, error) {
if err := rows.Scan(
&i.ID,
&i.Key,
&i.Channelid,
&i.ProjectGit,
&i.BuildGit,
&i.Folder,
@ -122,7 +149,7 @@ func (q *Queries) ListTickets(ctx context.Context) ([]Ticket, error) {
}
const listTicketsWithDeleted = `-- name: ListTicketsWithDeleted :many
SELECT id, key, project_git, build_git, folder, created_at, deleted_at, updated_at FROM tickets
SELECT id, key, channelid, project_git, build_git, folder, created_at, deleted_at, updated_at FROM tickets
`
func (q *Queries) ListTicketsWithDeleted(ctx context.Context) ([]Ticket, error) {
@ -137,6 +164,7 @@ func (q *Queries) ListTicketsWithDeleted(ctx context.Context) ([]Ticket, error)
if err := rows.Scan(
&i.ID,
&i.Key,
&i.Channelid,
&i.ProjectGit,
&i.BuildGit,
&i.Folder,
@ -154,6 +182,19 @@ func (q *Queries) ListTicketsWithDeleted(ctx context.Context) ([]Ticket, error)
return items, nil
}
const setNewConfig = `-- name: SetNewConfig :one
UPDATE appconfig
SET ticket_id = ticket_id + 1
RETURNING ticket_key, ticket_id
`
func (q *Queries) SetNewConfig(ctx context.Context) (Appconfig, error) {
row := q.db.QueryRow(ctx, setNewConfig)
var i Appconfig
err := row.Scan(&i.TicketKey, &i.TicketID)
return i, err
}
const updateTicketByID = `-- name: UpdateTicketByID :exec
UPDATE tickets SET project_git = $1, build_git = $2, folder = $3 WHERE id = $4
`

View File

@ -1,32 +0,0 @@
// Code generated by sqlc. DO NOT EDIT.
// versions:
// sqlc v1.23.0
package db
import (
"context"
"github.com/jackc/pgx/v5"
"github.com/jackc/pgx/v5/pgconn"
)
type DBTX interface {
Exec(context.Context, string, ...interface{}) (pgconn.CommandTag, error)
Query(context.Context, string, ...interface{}) (pgx.Rows, error)
QueryRow(context.Context, string, ...interface{}) pgx.Row
}
func New(db DBTX) *Queries {
return &Queries{db: db}
}
type Queries struct {
db DBTX
}
func (q *Queries) WithTx(tx pgx.Tx) *Queries {
return &Queries{
db: tx,
}
}

View File

@ -2,6 +2,7 @@
CREATE TABLE tickets (
id SERIAL PRIMARY KEY,
key VARCHAR(10),
channelID VARCHAR(255),
project_git VARCHAR(255),
build_git VARCHAR(255),
folder VARCHAR(255),

View File

@ -4,5 +4,8 @@ CREATE TABLE appconfig (
ticket_id INT
);
-- +migrate Up
INSERT INTO appconfig (ticket_key, ticket_id) VALUES ('DAP', 100);
-- +migrate Down
DROP TABLE appconfig;

View File

@ -1,18 +1,10 @@
version: "2"
sql:
- engine: "postgresql"
queries: "sqlc/tickets.sql"
schema: "migrate/0001_init_tickets.sql"
queries: "sqlc/queries.sql"
schema: "migrate"
gen:
go:
package: "db"
sql_package: "pgx/v5"
out: "db/tickets"
- engine: "postgresql"
queries: "sqlc/config.sql"
schema: "migrate/0002_init_config.sql"
gen:
go:
package: "db"
sql_package: "pgx/v5"
out: "db/config"
out: "db"

View File

@ -1,8 +0,0 @@
-- name: GetConfig :one
SELECT ticket_key, ticket_id
FROM appconfig;
-- name: SetNewConfig :one
UPDATE appconfig
SET ticket_id = ticket_id + 1
RETURNING *;

View File

@ -1,8 +1,17 @@
-- name: GetConfig :one
SELECT ticket_key, ticket_id
FROM appconfig;
-- name: SetNewConfig :one
UPDATE appconfig
SET ticket_id = ticket_id + 1
RETURNING *;
-- name: CreateTicket :one
INSERT INTO tickets (
key, project_git, build_git, folder, created_at
key, channelID
) VALUES (
$1, $2, $3, $4, $5
$1, $2
)
RETURNING *;
@ -15,6 +24,9 @@ SELECT * FROM tickets;
-- name: GetTicketByID :one
SELECT * FROM tickets WHERE id = $1;
-- name: GetTicketByChannelID :one
SELECT * FROM tickets WHERE channelID = $1;
-- name: UpdateTicketByID :exec
UPDATE tickets SET project_git = $1, build_git = $2, folder = $3 WHERE id = $4;

View File

@ -1,48 +1,38 @@
package handler
import (
"context"
"errors"
"strconv"
"strings"
// func (h *Handler) DevelopmentTaskHandler(ctx context.Context, mu *tgb.MessageUpdate) error {
"github.com/mr-linch/go-tg"
"github.com/mr-linch/go-tg/tgb"
)
// str := strings.Replace(mu.Text, "/new", "", 1)
func (h *Handler) DevelopmentTaskHandler(ctx context.Context, mu *tgb.MessageUpdate) error {
// if str == "" {
// return errors.New("empty command provided")
// }
str := strings.Replace(mu.Text, "/new", "", 1)
// issueKeyStr, err := h.workflow.Workflow(str, h.key, h.id)
if str == "" {
return errors.New("empty command provided")
}
// if err != nil {
// answer := errorAnswer(err.Error())
// h.LogMessage(ctx, mu, answer)
// return mu.Answer(answer).ParseMode(tg.HTML).DoVoid(ctx)
// }
issueKeyStr, err := h.workflow.Workflow(str, h.key, h.id)
// i, err := strconv.Atoi(h.id)
// if err != nil {
// return errors.New("problem with conversion id to int")
// }
// h.id = strconv.Itoa(i + 1)
if err != nil {
answer := errorAnswer(err.Error())
h.LogMessage(ctx, mu, answer)
return mu.Answer(answer).ParseMode(tg.HTML).DoVoid(ctx)
}
// answer := newTicketAnswer(issueKeyStr)
// h.LogMessage(ctx, mu, answer)
// return mu.Answer(answer).ParseMode(tg.HTML).DoVoid(ctx)
// }
i, err := strconv.Atoi(h.id)
if err != nil {
return errors.New("problem with conversion id to int")
}
h.id = strconv.Itoa(i + 1)
answer := newTicketAnswer(issueKeyStr)
h.LogMessage(ctx, mu, answer)
return mu.Answer(answer).ParseMode(tg.HTML).DoVoid(ctx)
}
func newTicketAnswer(name string) string {
return tg.HTML.Text(
tg.HTML.Line(
"🤘 Ticket ",
name,
" has been created!",
),
)
}
// func newTicketAnswer(name string) string {
// return tg.HTML.Text(
// tg.HTML.Line(
// "🤘 Ticket ",
// name,
// " has been created!",
// ),
// )
// }

View File

@ -1,33 +1,26 @@
package handler
import (
"ticket-pimp/internal/controller"
"ticket-pimp/internal/services"
tickets "ticket-pimp/internal/storage/db/tickets"
)
type Handler struct {
workflow controller.IWorkflowController
git services.IGit
cloud services.ICloud
coda services.ICoda
key string
id string
db *tickets.Queries
git services.IGit
cloud services.ICloud
coda services.ICoda
key string
id string
}
func NewHandler(
git services.IGit,
cloud services.ICloud,
coda services.ICoda,
db *tickets.Queries,
) *Handler {
return &Handler{
workflow: controller.NewWorkflowController(git, cloud, coda, db),
git: git,
cloud: cloud,
coda: coda,
db: db,
git: git,
cloud: cloud,
coda: coda,
}
}

View File

@ -10,7 +10,7 @@ import (
"github.com/mr-linch/go-tg"
"github.com/mr-linch/go-tg/tgb"
tickets "ticket-pimp/internal/storage/db/tickets"
tickets "ticket-pimp/internal/storage/db"
)
type TelegramOptions struct {
@ -35,13 +35,12 @@ func Run(ctx context.Context, opts TelegramOptions) error {
opts.GitService,
opts.CloudService,
opts.Coda,
opts.TicketsRepo,
)
router := tgb.NewRouter().
Message(h.Init, tgb.Command("init")).
Message(h.PingHandler, tgb.Command("ping")).
Message(h.DevelopmentTaskHandler, tgb.TextHasPrefix("/new")).
// Message(h.DevelopmentTaskHandler, tgb.TextHasPrefix("/new")).
Message(h.NewRepoHandler, tgb.TextHasPrefix("/repo")).
Message(h.NewFolderHandler, tgb.TextHasPrefix("/folder")).
Message(h.FarmTaskHandler, tgb.TextHasPrefix("/task"))