- clean architecture implemeted!

This commit is contained in:
naudachu 2023-06-14 22:21:58 +05:00
parent 0e6585b683
commit ad1beb66dd
6 changed files with 166 additions and 64 deletions

View File

@ -16,7 +16,7 @@ import (
func main() { func main() {
log.Print("started") log.Print("started")
env(".env") env(".dev.env")
ctx := context.Background() ctx := context.Background()
ctx, cancel := signal.NotifyContext(ctx, os.Interrupt, os.Kill, syscall.SIGTERM) ctx, cancel := signal.NotifyContext(ctx, os.Interrupt, os.Kill, syscall.SIGTERM)
@ -36,12 +36,24 @@ func env(envFilePath string) {
} }
func run(ctx context.Context) error { func run(ctx context.Context) error {
client := tg.New(os.Getenv("TG_API")) client := tg.New(os.Getenv("TG_API"))
h := handler.NewHandler(
os.Getenv("GIT_BASE_URL"),
os.Getenv("GIT_TOKEN"),
os.Getenv("CLOUD_BASE_URL"),
os.Getenv("CLOUD_USER"),
os.Getenv("CLOUD_PASS"),
os.Getenv("YT_URL"),
os.Getenv("YT_TOKEN"),
)
router := tgb.NewRouter(). router := tgb.NewRouter().
Message(handler.NewTicketHandler, tgb.TextHasPrefix("/new")). Message(h.NewTicketHandler, tgb.TextHasPrefix("/new")).
Message(handler.PingHandler, tgb.Command("ping")). Message(h.PingHandler, tgb.Command("ping")).
Message(handler.NewRepoHandler, tgb.TextHasPrefix("/repo")) Message(h.NewRepoHandler, tgb.TextHasPrefix("/repo")).
Message(h.NewFolderHandler, tgb.TextHasPrefix("/folder"))
return tgb.NewPoller( return tgb.NewPoller(
router, router,

View File

@ -2,13 +2,40 @@ package controller
import ( import (
"fmt" "fmt"
"os"
"sync" "sync"
"ticket-pimp/ext" "ticket-pimp/ext"
) )
func Workflow(name string) (string, error) { type WorkflowController struct {
yt := ext.NewYT(os.Getenv("YT_URL"), os.Getenv("YT_TOKEN")) iGit ext.IGit
iCloud ext.ICloud
iYouTrack ext.IYouTrack
}
type IWorkflowController interface {
Workflow(name string) (string, error)
CreateRepo(name string, param uint) (string, error)
CreateFolder(name string) (string, string, error)
}
func NewWorkflowController(
gitBaseURL,
gitToken,
cloudBaseURL,
cloudAuthUser,
cloudAuthPass,
ytBaseURL,
ytToken string,
) *WorkflowController {
return &WorkflowController{
iGit: ext.NewGit(gitBaseURL, gitToken),
iCloud: ext.NewCloud(cloudBaseURL, cloudAuthUser, cloudAuthPass),
iYouTrack: ext.NewYT(ytBaseURL, ytToken),
}
}
func (wc *WorkflowController) Workflow(name string) (string, error) {
yt := wc.iYouTrack
projects, err := yt.GetProjects() projects, err := yt.GetProjects()
@ -16,7 +43,7 @@ func Workflow(name string) (string, error) {
return "", err return "", err
} }
issue, err := yt.CreateIssue(projects[0].ID, name) issue, err := yt.CreateIssue(projects[1].ID, name)
if err != nil { if err != nil {
return "", err return "", err
@ -32,17 +59,17 @@ func Workflow(name string) (string, error) {
go func() { go func() {
defer wg.Done() defer wg.Done()
git, _ = CreateRepo(issue.Key, 0) git, _ = wc.CreateRepo(issue.Key, 0)
}() }()
go func() { go func() {
defer wg.Done() defer wg.Done()
gitBuild, _ = CreateRepo(issue.Key+"-build", 1) gitBuild, _ = wc.CreateRepo(issue.Key+"-build", 1)
}() }()
go func() { go func() {
defer wg.Done() defer wg.Done()
folder = CreateFolder(issue.Key + " - " + issue.Summary) _, folder, _ = wc.CreateFolder(issue.Key + " - " + issue.Summary)
}() }()
wg.Wait() wg.Wait()
@ -52,10 +79,12 @@ func Workflow(name string) (string, error) {
return issue.Key, nil return issue.Key, nil
} }
func CreateRepo(name string, param uint) (string, error) { func (wc *WorkflowController) CreateRepo(name string, param uint) (string, error) {
gb := ext.NewGit(os.Getenv("GIT_BASE_URL"), os.Getenv("GIT_TOKEN")) //Create git repository with iGit interface;
repo, err := gb.NewRepo(name) repo, err := wc.iGit.NewRepo(name)
gb.AppsAsCollaboratorTo(repo)
//Set 'apps' as collaborator to created repository;
wc.iGit.AppsAsCollaboratorTo(repo)
// Result string formatting: // Result string formatting:
if repo != nil { if repo != nil {
@ -72,12 +101,13 @@ func CreateRepo(name string, param uint) (string, error) {
return "", err return "", err
} }
func CreateFolder(name string) string { func (wc *WorkflowController) CreateFolder(name string) (string, string, error) {
oc := ext.NewCloud(os.Getenv("CLOUD_BASE_URL"), os.Getenv("CLOUD_USER"), os.Getenv("CLOUD_PASS"))
cloud, _ := oc.CreateFolder(name) //Create ownCloud folder w/ iCloud interface;
if cloud != nil { cloud, err := wc.iCloud.CreateFolder(name)
return cloud.FolderPath if cloud == nil {
return "", "", err
} }
return "no-folder"
return cloud.FolderName, cloud.FolderPath, err
} }

View File

@ -2,27 +2,40 @@ package ext
import ( import (
"os" "os"
"ticket-pimp/helpers"
"time" "time"
) )
func NewCloud(base, user, pass string) *Client { type Cloud struct {
//[ ] out in separate domain struct
FolderName string
FolderPath string
*Client
}
type ICloud interface {
CreateFolder(name string) (*Cloud, error)
}
func NewCloud(base, user, pass string) *Cloud {
client := NewClient(). client := NewClient().
SetTimeout(5*time.Second). SetTimeout(5*time.Second).
SetCommonBasicAuth(user, pass). SetCommonBasicAuth(user, pass).
SetBaseURL(base) SetBaseURL(base)
return &Client{ return &Cloud{
client, FolderName: "",
FolderPath: "",
Client: &Client{
client,
},
} }
} }
type Cloud struct { func (c *Cloud) CreateFolder(name string) (*Cloud, error) {
FolderName string
FolderPath string
}
func (c *Client) CreateFolder(name string) (*Cloud, error) { name = helpers.GitNaming(name)
cloud := Cloud{ cloud := Cloud{
FolderName: name, FolderName: name,
@ -36,6 +49,7 @@ func (c *Client) CreateFolder(name string) (*Cloud, error) {
if resp.IsSuccessState() { if resp.IsSuccessState() {
cloud.FolderPath = c.BaseURL + os.Getenv("FOLDER_PATH") + name cloud.FolderPath = c.BaseURL + os.Getenv("FOLDER_PATH") + name
} }
return &cloud, err return &cloud, err

View File

@ -13,6 +13,11 @@ type Git struct {
*domain.Git *domain.Git
} }
type IGit interface {
NewRepo(string) (*domain.Git, error)
AppsAsCollaboratorTo(*domain.Git) (*domain.Git, error)
}
func NewGit(base, token string) *Git { func NewGit(base, token string) *Git {
headers := map[string]string{ headers := map[string]string{
"Accept": "application/vnd.github+json", "Accept": "application/vnd.github+json",
@ -63,7 +68,6 @@ func (gb *Git) NewRepo(name string) (*domain.Git, error) {
SetBody(&payload). SetBody(&payload).
SetSuccessResult(&git). SetSuccessResult(&git).
Post("/user/repos") Post("/user/repos")
//Post("/orgs/apps/repos")
if err != nil { if err != nil {
log.Print(resp) log.Print(resp)
@ -72,7 +76,7 @@ func (gb *Git) NewRepo(name string) (*domain.Git, error) {
return &git, err return &git, err
} }
func (gb *Client) AppsAsCollaboratorTo(git *domain.Git) (*domain.Git, error) { func (gb *Git) AppsAsCollaboratorTo(git *domain.Git) (*domain.Git, error) {
payloadPermission := permissionRequest{ payloadPermission := permissionRequest{
Perm: "admin", Perm: "admin",
} }

View File

@ -12,6 +12,12 @@ type youtrack struct {
*req.Client *req.Client
} }
type IYouTrack interface {
GetProjects() ([]Project, error)
CreateIssue(projectID, name string) (*IssueCreateRequest, error)
UpdateIssue(issue *IssueCreateRequest, folder, git, gitBuild string) (*IssueUpdateRequest, error)
}
func NewYT(base, token string) *youtrack { func NewYT(base, token string) *youtrack {
headers := map[string]string{ headers := map[string]string{
"Accept": "application/json", "Accept": "application/json",

View File

@ -11,6 +11,73 @@ import (
"github.com/mr-linch/go-tg/tgb" "github.com/mr-linch/go-tg/tgb"
) )
type Handler struct {
workflow controller.IWorkflowController
}
func NewHandler(gitBaseURL, gitToken, cloudBaseURL, cloudAuthUser, cloudAuthPass, ytBaseURL, ytToken string) *Handler {
return &Handler{
workflow: controller.NewWorkflowController(gitBaseURL, gitToken, cloudBaseURL, cloudAuthUser, cloudAuthPass, ytBaseURL, ytToken),
}
}
func (h *Handler) PingHandler(ctx context.Context, mu *tgb.MessageUpdate) error {
return mu.Answer("pong").DoVoid(ctx)
}
func newRepoAnswer(name string) string {
return tg.HTML.Text(
tg.HTML.Line(
"Repo ",
name,
"has been created!",
),
)
}
func (h *Handler) NewRepoHandler(ctx context.Context, mu *tgb.MessageUpdate) error {
str := strings.Replace(mu.Text, "/repo", "", 1)
if str == "" {
return errors.New("empty command provided")
}
repoStr, err := h.workflow.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 newFolderAnswer(name string) string {
return tg.HTML.Text(
tg.HTML.Line(
"Folder ",
name,
"has been created!",
),
)
}
func (h *Handler) NewFolderHandler(ctx context.Context, mu *tgb.MessageUpdate) error {
str := strings.Replace(mu.Text, "/folder", "", 1)
if str == "" {
return errors.New("empty command provided")
}
nameStr, _, err := h.workflow.CreateFolder(str)
if err != nil {
return mu.Answer(errorAnswer(err.Error())).ParseMode(tg.HTML).DoVoid(ctx)
}
return mu.Answer(newFolderAnswer(nameStr)).ParseMode(tg.HTML).DoVoid(ctx)
}
func errorAnswer(errorMsg string) string { func errorAnswer(errorMsg string) string {
return tg.HTML.Text( return tg.HTML.Text(
tg.HTML.Line( tg.HTML.Line(
@ -19,7 +86,7 @@ func errorAnswer(errorMsg string) string {
) )
} }
func NewTicketHandler(ctx context.Context, mu *tgb.MessageUpdate) error { func (h *Handler) NewTicketHandler(ctx context.Context, mu *tgb.MessageUpdate) error {
str := strings.Replace(mu.Text, "/new", "", 1) str := strings.Replace(mu.Text, "/new", "", 1)
@ -27,7 +94,7 @@ func NewTicketHandler(ctx context.Context, mu *tgb.MessageUpdate) error {
return errors.New("empty command provided") return errors.New("empty command provided")
} }
issueKeyStr, err := controller.Workflow(str) issueKeyStr, err := h.workflow.Workflow(str)
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)
@ -45,34 +112,3 @@ func newTicketAnswer(name string) string {
), ),
) )
} }
func NewRepoHandler(ctx context.Context, mu *tgb.MessageUpdate) error {
str := strings.Replace(mu.Text, "/repo", "", 1)
if str == "" {
return errors.New("empty command provided")
}
repoStr, err := controller.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)
}