diff --git a/cmd/.env.dev b/cmd/.env.dev deleted file mode 100644 index edbb086..0000000 --- a/cmd/.env.dev +++ /dev/null @@ -1,11 +0,0 @@ -CLOUD_BASE_URL = 'http://82.151.222.22:7000' -CLOUD_USER = 'naudachu' -CLOUD_PASS = '123456' - -GIT_BASE_URL = 'http://82.151.222.22:7001/api/v3' -GIT_TOKEN = '7bd9d60cf7b9e78a4f2f1aea4734fbfa1052e419' - -YT_URL = 'https://marlerino.youtrack.cloud/api' -YT_TOKEN = 'perm:bmF1ZGFjaHU=.NTYtMQ==.4TVHQx65u4EKnCGjadeMB1NMAmSHSL' - -TG_API = '6002875059:AAFp1ZR9Y68oaqSL6vTNQdhrVgcM_yHouCY' \ No newline at end of file diff --git a/cmd/main.go b/cmd/main.go index 28682b9..7c8377a 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -39,16 +39,6 @@ func env(envFilePath string) { } } -func answer(name string) string { - return tg.HTML.Text( - tg.HTML.Line( - "🤘 Ticket ", - tg.HTML.Link(name, fmt.Sprintf("https://marlerino.youtrack.cloud/issue/%s", name)), - "has been created!", - ), - ) -} - func errorAnswer(errorMsg string) string { return tg.HTML.Text( tg.HTML.Line( @@ -61,22 +51,9 @@ func run(ctx context.Context) error { client := tg.New(os.Getenv("TG_API")) router := tgb.NewRouter(). - Message(func(ctx context.Context, mu *tgb.MessageUpdate) error { - - str := strings.Replace(mu.Text, "/new", "", 1) - if str == "" { - return errors.New("empty command provided") - } - issueKeyStr, err := workflow(str) - if err != nil { - return mu.Answer(errorAnswer(err.Error())).ParseMode(tg.HTML).DoVoid(ctx) - } - - return mu.Answer(answer(issueKeyStr)).ParseMode(tg.HTML).DoVoid(ctx) - }, tgb.TextHasPrefix("/new")). - Message(func(ctx context.Context, mu *tgb.MessageUpdate) error { - return mu.Answer("pong").DoVoid(ctx) - }, tgb.Command("ping")) + Message(newTicketHandler, tgb.TextHasPrefix("/new")). + Message(pingHandler, tgb.Command("ping")). + Message(newRepoHandler, tgb.TextHasPrefix("/repo")) return tgb.NewPoller( router, @@ -84,6 +61,57 @@ func run(ctx context.Context) error { ).Run(ctx) } +func newTicketHandler(ctx context.Context, mu *tgb.MessageUpdate) error { + + str := strings.Replace(mu.Text, "/new", "", 1) + if str == "" { + return errors.New("empty command provided") + } + issueKeyStr, err := workflow(str) + if err != nil { + return mu.Answer(errorAnswer(err.Error())).ParseMode(tg.HTML).DoVoid(ctx) + } + + return mu.Answer(newTicketAnswer(issueKeyStr)).ParseMode(tg.HTML).DoVoid(ctx) +} + +func newTicketAnswer(name string) string { + return tg.HTML.Text( + tg.HTML.Line( + "🤘 Ticket ", + tg.HTML.Link(name, fmt.Sprintf("https://marlerino.youtrack.cloud/issue/%s", name)), + "has been created!", + ), + ) +} + +func newRepoHandler(ctx context.Context, mu *tgb.MessageUpdate) error { + + str := strings.Replace(mu.Text, "/newrepo", "", 1) + if str == "" { + return errors.New("empty command provided") + } + repoStr, err := createRepo(str, 0) + if err != nil { + return mu.Answer(errorAnswer(err.Error())).ParseMode(tg.HTML).DoVoid(ctx) + } + + return mu.Answer(newRepoAnswer(repoStr)).ParseMode(tg.HTML).DoVoid(ctx) +} +func newRepoAnswer(name string) string { + return tg.HTML.Text( + tg.HTML.Line( + "Repo ", + name, + "has been created!", + ), + ) +} + +func pingHandler(ctx context.Context, mu *tgb.MessageUpdate) error { + return mu.Answer("pong").DoVoid(ctx) +} + func workflow(name string) (string, error) { yt := domain.NewYT(os.Getenv("YT_URL"), os.Getenv("YT_TOKEN")) @@ -106,12 +134,12 @@ func workflow(name string) (string, error) { go func() { defer wg.Done() - git = createRepo(issue.Key, 0) + git, _ = createRepo(issue.Key, 0) }() go func() { defer wg.Done() - gitBuild = createRepo(issue.Key+"-build", 1) + gitBuild, _ = createRepo(issue.Key+"-build", 1) }() go func() { defer wg.Done() @@ -119,25 +147,32 @@ func workflow(name string) (string, error) { }() wg.Wait() + yt.UpdateIssue(issue, folder, git, gitBuild) } return issue.Key, nil } -func createRepo(name string, param uint) string { +func createRepo(name string, param uint) (string, error) { gb := domain.NewGitBucket(os.Getenv("GIT_BASE_URL"), os.Getenv("GIT_TOKEN")) - repo, _ := gb.NewRepo(name) + repo, err := gb.NewRepo(name) + if err != nil { + return "", err + } + + // Result string formatting: if repo != nil { switch param { case 0: - return repo.HtmlUrl + return repo.HtmlUrl, nil case 1: - return fmt.Sprintf("ssh://%s/%s.git", repo.SshUrl, repo.FullName) + return fmt.Sprintf("ssh://%s/%s.git", repo.SshUrl, repo.FullName), nil default: - return repo.CloneUrl + return repo.CloneUrl, nil } } - return "no-repo" + + return "", err } func createFolder(name string) string { diff --git a/domain/git.go b/domain/git.go index b1232c0..cb8f07c 100644 --- a/domain/git.go +++ b/domain/git.go @@ -1,6 +1,7 @@ package domain import ( + "log" "time" "github.com/imroc/req/v3" @@ -51,12 +52,13 @@ func (gb *gitbucket) NewRepo(name string) (*Repo, error) { var git Repo - _, err := gb.R(). + resp, err := gb.R(). SetBody(&payload). SetSuccessResult(&git). Post("/user/repos") if err != nil { + log.Print(resp) return nil, err }