- rename domain -> ext, cause it is not domain..

- fixed the problem with github naming, that produces 404 error;
This commit is contained in:
naudachu 2023-06-09 19:13:32 +05:00
parent ae9abd50c8
commit 636dd730a1
5 changed files with 37 additions and 10 deletions

View File

@ -11,7 +11,7 @@ import (
"sync"
"syscall"
"ticket-creator/domain"
"ticket-creator/ext"
"github.com/joho/godotenv"
"github.com/mr-linch/go-tg"
@ -87,11 +87,16 @@ func newTicketAnswer(name string) string {
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 == "" {
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)
}
@ -113,7 +118,7 @@ func pingHandler(ctx context.Context, mu *tgb.MessageUpdate) 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()
if err != nil {
@ -154,7 +159,7 @@ func workflow(name string) (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)
if err != nil {
return "", err
@ -176,7 +181,7 @@ func createRepo(name string, param uint) (string, error) {
}
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)
if cloud != nil {
return cloud.FolderPath

View File

@ -1,4 +1,4 @@
package domain
package ext
import (
"fmt"

View File

@ -1,4 +1,4 @@
package domain
package ext
import (
"time"

View File

@ -1,7 +1,9 @@
package domain
package ext
import (
"log"
"regexp"
"strings"
"time"
"github.com/imroc/req/v3"
@ -38,7 +40,26 @@ type Repo struct {
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) {
name = gitHubLikeNaming(name)
type request struct {
Name string `json:"name"`
@ -70,11 +91,12 @@ func (gb *gitbucket) NewRepo(name string) (*Repo, error) {
Perm: "admin",
}
_, err = gb.R().
resp, err = gb.R().
SetBody(&payloadPermission).
Put("/repos/naudachu/" + name + "/collaborators/apps")
if err != nil {
log.Print(resp)
return nil, err
}

View File

@ -1,4 +1,4 @@
package domain
package ext
import (
"fmt"