This commit is contained in:
naudachu 2023-06-09 17:30:10 +05:00
parent 90d17515c3
commit ae9abd50c8
3 changed files with 72 additions and 46 deletions

View File

@ -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'

View File

@ -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,7 +51,17 @@ 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 {
Message(newTicketHandler, tgb.TextHasPrefix("/new")).
Message(pingHandler, tgb.Command("ping")).
Message(newRepoHandler, tgb.TextHasPrefix("/repo"))
return tgb.NewPoller(
router,
client,
).Run(ctx)
}
func newTicketHandler(ctx context.Context, mu *tgb.MessageUpdate) error {
str := strings.Replace(mu.Text, "/new", "", 1)
if str == "" {
@ -72,16 +72,44 @@ func run(ctx context.Context) error {
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"))
return mu.Answer(newTicketAnswer(issueKeyStr)).ParseMode(tg.HTML).DoVoid(ctx)
}
return tgb.NewPoller(
router,
client,
).Run(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) {
@ -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 {

View File

@ -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
}