- rename domain -> ext, cause it is not domain..
- fixed the problem with github naming, that produces 404 error;
This commit is contained in:
parent
ae9abd50c8
commit
636dd730a1
15
cmd/main.go
15
cmd/main.go
|
|
@ -11,7 +11,7 @@ import (
|
||||||
"sync"
|
"sync"
|
||||||
"syscall"
|
"syscall"
|
||||||
|
|
||||||
"ticket-creator/domain"
|
"ticket-creator/ext"
|
||||||
|
|
||||||
"github.com/joho/godotenv"
|
"github.com/joho/godotenv"
|
||||||
"github.com/mr-linch/go-tg"
|
"github.com/mr-linch/go-tg"
|
||||||
|
|
@ -87,11 +87,16 @@ func newTicketAnswer(name string) string {
|
||||||
|
|
||||||
func newRepoHandler(ctx context.Context, mu *tgb.MessageUpdate) error {
|
func newRepoHandler(ctx context.Context, mu *tgb.MessageUpdate) error {
|
||||||
|
|
||||||
str := strings.Replace(mu.Text, "/newrepo", "", 1)
|
str := strings.Replace(mu.Text, "/repo", "", 1)
|
||||||
|
str = strings.TrimSpace(str)
|
||||||
|
str = strings.Replace(str, " ", "-", -1)
|
||||||
|
|
||||||
if str == "" {
|
if str == "" {
|
||||||
return errors.New("empty command provided")
|
return errors.New("empty command provided")
|
||||||
}
|
}
|
||||||
|
|
||||||
repoStr, err := createRepo(str, 0)
|
repoStr, err := createRepo(str, 0)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return mu.Answer(errorAnswer(err.Error())).ParseMode(tg.HTML).DoVoid(ctx)
|
return mu.Answer(errorAnswer(err.Error())).ParseMode(tg.HTML).DoVoid(ctx)
|
||||||
}
|
}
|
||||||
|
|
@ -113,7 +118,7 @@ func pingHandler(ctx context.Context, mu *tgb.MessageUpdate) error {
|
||||||
}
|
}
|
||||||
|
|
||||||
func workflow(name string) (string, error) {
|
func workflow(name string) (string, error) {
|
||||||
yt := domain.NewYT(os.Getenv("YT_URL"), os.Getenv("YT_TOKEN"))
|
yt := ext.NewYT(os.Getenv("YT_URL"), os.Getenv("YT_TOKEN"))
|
||||||
|
|
||||||
projects, err := yt.GetProjects()
|
projects, err := yt.GetProjects()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
@ -154,7 +159,7 @@ func workflow(name string) (string, error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func createRepo(name string, param uint) (string, error) {
|
func createRepo(name string, param uint) (string, error) {
|
||||||
gb := domain.NewGitBucket(os.Getenv("GIT_BASE_URL"), os.Getenv("GIT_TOKEN"))
|
gb := ext.NewGitBucket(os.Getenv("GIT_BASE_URL"), os.Getenv("GIT_TOKEN"))
|
||||||
repo, err := gb.NewRepo(name)
|
repo, err := gb.NewRepo(name)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
|
|
@ -176,7 +181,7 @@ func createRepo(name string, param uint) (string, error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func createFolder(name string) string {
|
func createFolder(name string) string {
|
||||||
oc := domain.NewCloud(os.Getenv("CLOUD_BASE_URL"), os.Getenv("CLOUD_USER"), os.Getenv("CLOUD_PASS"))
|
oc := ext.NewCloud(os.Getenv("CLOUD_BASE_URL"), os.Getenv("CLOUD_USER"), os.Getenv("CLOUD_PASS"))
|
||||||
cloud, _ := oc.CreateFolder(name)
|
cloud, _ := oc.CreateFolder(name)
|
||||||
if cloud != nil {
|
if cloud != nil {
|
||||||
return cloud.FolderPath
|
return cloud.FolderPath
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,4 @@
|
||||||
package domain
|
package ext
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
|
@ -1,4 +1,4 @@
|
||||||
package domain
|
package ext
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"time"
|
"time"
|
||||||
|
|
@ -1,7 +1,9 @@
|
||||||
package domain
|
package ext
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"log"
|
"log"
|
||||||
|
"regexp"
|
||||||
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/imroc/req/v3"
|
"github.com/imroc/req/v3"
|
||||||
|
|
@ -38,7 +40,26 @@ type Repo struct {
|
||||||
SshUrl string `json:"ssh_url"`
|
SshUrl string `json:"ssh_url"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func gitHubLikeNaming(input string) string {
|
||||||
|
// Remove leading and trailing whitespace
|
||||||
|
input = strings.TrimSpace(input)
|
||||||
|
|
||||||
|
// Replace non-Latin letters with spaces
|
||||||
|
reg := regexp.MustCompile("[^a-zA-Z]+")
|
||||||
|
input = reg.ReplaceAllString(input, " ")
|
||||||
|
|
||||||
|
// Split into words and capitalize first letter of each
|
||||||
|
words := strings.Fields(input)
|
||||||
|
for i, word := range words {
|
||||||
|
words[i] = strings.ToLower(word)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Join words and return
|
||||||
|
return strings.Join(words, "-")
|
||||||
|
}
|
||||||
|
|
||||||
func (gb *gitbucket) NewRepo(name string) (*Repo, error) {
|
func (gb *gitbucket) NewRepo(name string) (*Repo, error) {
|
||||||
|
name = gitHubLikeNaming(name)
|
||||||
|
|
||||||
type request struct {
|
type request struct {
|
||||||
Name string `json:"name"`
|
Name string `json:"name"`
|
||||||
|
|
@ -70,11 +91,12 @@ func (gb *gitbucket) NewRepo(name string) (*Repo, error) {
|
||||||
Perm: "admin",
|
Perm: "admin",
|
||||||
}
|
}
|
||||||
|
|
||||||
_, err = gb.R().
|
resp, err = gb.R().
|
||||||
SetBody(&payloadPermission).
|
SetBody(&payloadPermission).
|
||||||
Put("/repos/naudachu/" + name + "/collaborators/apps")
|
Put("/repos/naudachu/" + name + "/collaborators/apps")
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
log.Print(resp)
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -1,4 +1,4 @@
|
||||||
package domain
|
package ext
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
Loading…
Reference in New Issue