- get git entity from externals;

- rework error handling;
This commit is contained in:
naudachu 2023-06-25 19:12:53 +05:00
parent 3130994e7d
commit 6be164682c
6 changed files with 97 additions and 71 deletions

View File

@ -13,12 +13,6 @@ type WorkflowController struct {
iYouTrack ext.IYouTrack iYouTrack ext.IYouTrack
} }
type IWorkflowController interface {
Workflow(name string) (string, error)
CreateRepo(name string, param uint) (string, error)
CreateFolder(name string) (*d.Folder, error)
}
func NewWorkflowController( func NewWorkflowController(
gitBaseURL, gitBaseURL,
gitToken, gitToken,
@ -35,6 +29,12 @@ func NewWorkflowController(
} }
} }
type IWorkflowController interface {
Workflow(name string) (string, error)
CreateRepo(name string) (*d.Git, error)
CreateFolder(name string) (*d.Folder, error)
}
func (wc *WorkflowController) Workflow(name string) (string, error) { func (wc *WorkflowController) Workflow(name string) (string, error) {
yt := wc.iYouTrack yt := wc.iYouTrack
@ -52,22 +52,22 @@ func (wc *WorkflowController) Workflow(name string) (string, error) {
if issue != nil { if issue != nil {
var ( var (
git, gitBuild string git, gitBuild *d.Git
cloud *d.Folder cloud *d.Folder
) )
var wg sync.WaitGroup var wg sync.WaitGroup
wg.Add(3) wg.Add(3)
go func() { go func(ref **d.Git) {
defer wg.Done() defer wg.Done()
git, _ = wc.CreateRepo(issue.Key, 0) *ref, _ = wc.CreateRepo(issue.Key)
}() }(&git)
go func() { go func(ref **d.Git) {
defer wg.Done() defer wg.Done()
gitBuild, _ = wc.CreateRepo(issue.Key+"-build", 1) *ref, _ = wc.CreateRepo(issue.Key + "-build")
}() }(&gitBuild)
go func(ref **d.Folder) { go func(ref **d.Folder) {
defer wg.Done() defer wg.Done()
@ -76,31 +76,29 @@ func (wc *WorkflowController) Workflow(name string) (string, error) {
wg.Wait() wg.Wait()
yt.UpdateIssue(issue, cloud.PrivateURL, git, gitBuild) yt.UpdateIssue(
issue,
cloud.PrivateURL,
git.HtmlUrl,
fmt.Sprintf("ssh://%s/%s.git", gitBuild.SshUrl, gitBuild.FullName))
} }
return issue.Key, nil return issue.Key, nil
} }
func (wc *WorkflowController) CreateRepo(name string, param uint) (string, error) { func (wc *WorkflowController) CreateRepo(name string) (*d.Git, error) {
//Create git repository with iGit interface; //Create git repository with iGit interface;
repo, err := wc.iGit.NewRepo(name) repo, err := wc.iGit.NewRepo(name)
if err != nil {
//Set 'apps' as collaborator to created repository; return nil, err
wc.iGit.AppsAsCollaboratorTo(repo)
// Result string formatting:
if repo != nil {
switch param {
case 0:
return repo.HtmlUrl, err
case 1:
return fmt.Sprintf("ssh://%s/%s.git", repo.SshUrl, repo.FullName), err
default:
return repo.CloneUrl, err
}
} }
return "", err //Set 'apps' as collaborator to created repository;
_, err = wc.iGit.AppsAsCollaboratorTo(repo)
if err != nil {
return nil, err
}
return repo, nil
} }
func (wc *WorkflowController) CreateFolder(name string) (*d.Folder, error) { func (wc *WorkflowController) CreateFolder(name string) (*d.Folder, error) {

View File

@ -1,11 +1,11 @@
package domain package domain
type Git struct { type Git struct {
Name string `json:"name"` Name string `json:"name"` // "poop"
FullName string `json:"full_name"` FullName string `json:"full_name"` // "developer/poop"
Private bool `json:"private"` Private bool `json:"private"`
Url string `json:"url"` Url string `json:"url"` // "http://localhost:8081/api/v3/repos/developer/poop"
CloneUrl string `json:"clone_url"` CloneUrl string `json:"clone_url"` // "http://localhost:8081/git/developer/poop.git"
HtmlUrl string `json:"Html_url"` HtmlUrl string `json:"Html_url"` // "http://localhost:8081/developer/poop"
SshUrl string `json:"ssh_url"` SshUrl string `json:"ssh_url"` // ?!
} }

View File

@ -1,6 +1,7 @@
package ext package ext
import ( import (
"fmt"
"os" "os"
"strconv" "strconv"
d "ticket-pimp/domain" d "ticket-pimp/domain"
@ -49,7 +50,7 @@ func (c *Cloud) CreateFolder(name string) (*d.Folder, error) {
cloud.PathTo = parentPath + rootDir + name cloud.PathTo = parentPath + rootDir + name
resp, err := c.R(). resp, _ := c.R().
Send("MKCOL", requestPath) Send("MKCOL", requestPath)
if resp.IsSuccessState() { if resp.IsSuccessState() {
@ -57,12 +58,12 @@ func (c *Cloud) CreateFolder(name string) (*d.Folder, error) {
cloud.PrivateURL = c.BaseURL + cloud.PathTo cloud.PrivateURL = c.BaseURL + cloud.PathTo
// Try to set short URL to the d entity // Try to set short URL to the d entity
if err = c.setPrivateURL(requestPath, &cloud); err != nil { if err := c.setPrivateURL(requestPath, &cloud); err != nil {
return &cloud, nil return &cloud, err
} }
} }
return &cloud, err return &cloud, nil
} }
func (c *Cloud) setPrivateURL(requestPath string, cloud *d.Folder) error { func (c *Cloud) setPrivateURL(requestPath string, cloud *d.Folder) error {
@ -78,22 +79,23 @@ func (c *Cloud) setPrivateURL(requestPath string, cloud *d.Folder) error {
} }
*/ */
resp, err := c.R(). resp, _ := c.R().
SetBody(payload). SetBody(payload).
Send("PROPFIND", requestPath) Send("PROPFIND", requestPath)
if err != nil { if resp.Err != nil {
return err return resp.Err
} }
id := helpers.GetFileIDFromRespBody(resp.Bytes()) id := helpers.GetFileIDFromRespBody(resp.Bytes())
if id != 0 { if id == 0 {
cloud.PrivateURL = c.BaseURL + "/f/" + strconv.Itoa(id) return fmt.Errorf("unable to get fileid")
return nil
} }
return err cloud.PrivateURL = c.BaseURL + "/f/" + strconv.Itoa(id)
return nil
} }
func (c *Cloud) ShareToExternals(cloud *d.Folder) (*d.Folder, error) { func (c *Cloud) ShareToExternals(cloud *d.Folder) (*d.Folder, error) {

View File

@ -55,31 +55,35 @@ func (gb *Git) NewRepo(name string) (*domain.Git, error) {
var git domain.Git var git domain.Git
git.Private = true git.Private = true
resp, err := gb.R(). resp, _ := gb.R().
SetBody(&payload). SetBody(&payload).
SetSuccessResult(&git). SetSuccessResult(&git).
Post("/user/repos") Post("/user/repos")
if err != nil { if resp.Err != nil {
log.Print(resp) log.Print(resp.Err)
return nil, resp.Err
} }
return &git, err return &git, nil
} }
func (gb *Git) AppsAsCollaboratorTo(git *domain.Git) (*domain.Git, error) { func (gb *Git) AppsAsCollaboratorTo(git *domain.Git) (*domain.Git, error) {
payloadPermission := permissionRequest{ payload := permissionRequest{
Perm: "admin", Perm: "admin",
} }
resp, err := gb.R(). respURL := "/repos/" + os.Getenv("GIT_USER") + "/" + git.Name + "/collaborators/apps"
SetBody(&payloadPermission).
Put("/repos/" + os.Getenv("GIT_USER") + "/" + git.Name + "/collaborators/apps")
if err != nil { resp, _ := gb.R().
log.Print(resp) SetBody(&payload).
Put(respURL)
if resp.Err != nil {
log.Print(resp.Err)
return nil, resp.Err
} }
return git, err return git, nil
} }

View File

@ -43,15 +43,15 @@ func (yt *youtrack) GetProjects() ([]d.Project, error) {
var projects []d.Project var projects []d.Project
_, err := yt.R(). resp, _ := yt.R().
EnableDump(). EnableDump().
SetQueryParam("fields", "id,name,shortName"). SetQueryParam("fields", "id,name,shortName").
SetSuccessResult(&projects). SetSuccessResult(&projects).
Get("/admin/projects") Get("/admin/projects")
// Check if the request failed; // Check if the request failed;
if err != nil { if resp.Err != nil {
return nil, fmt.Errorf("some problem with YT request. error message: %v", err) return nil, fmt.Errorf("some problem with YT request. error message: %v", resp.Err)
} }
return projects, nil return projects, nil
@ -71,15 +71,15 @@ func (yt *youtrack) CreateIssue(projectID, name string) (*d.IssueCreateRequest,
} }
// Push issue to the YT; // Push issue to the YT;
_, err := yt.R(). resp, _ := yt.R().
SetQueryParam("fields", "idReadable,id"). SetQueryParam("fields", "idReadable,id").
SetBody(&issue). SetBody(&issue).
SetSuccessResult(&issue). SetSuccessResult(&issue).
Post("/issues") Post("/issues")
// Check if the request failed; // Check if the request failed;
if err != nil { if resp.Err != nil {
return nil, fmt.Errorf("some problem with YT request. error message: %v", err) return nil, fmt.Errorf("some problem with YT request. error message: %v", resp.Err)
} }
return &issue, nil return &issue, nil
@ -109,14 +109,14 @@ func (yt *youtrack) UpdateIssue(issue *d.IssueCreateRequest, folder, git, gitBui
} }
// Push issue update to YT // Push issue update to YT
resp, err := yt.R(). resp, _ := yt.R().
SetBody(&update). SetBody(&update).
SetSuccessResult(&issue). SetSuccessResult(&issue).
Post("/issues/" + issue.Key) Post("/issues/" + issue.Key)
// Check if the request failed; // Check if the request failed;
if err != nil { if resp.Err != nil {
return nil, fmt.Errorf("some problem with YT request. error message: %v", err) return nil, fmt.Errorf("some problem with YT request. error message: %v", resp.Err)
} }
if !resp.IsSuccessState() { if !resp.IsSuccessState() {

View File

@ -6,6 +6,7 @@ import (
"fmt" "fmt"
"strings" "strings"
"ticket-pimp/controller" "ticket-pimp/controller"
d "ticket-pimp/domain"
"github.com/mr-linch/go-tg" "github.com/mr-linch/go-tg"
"github.com/mr-linch/go-tg/tgb" "github.com/mr-linch/go-tg/tgb"
@ -32,11 +33,29 @@ func (h *Handler) PingHandler(ctx context.Context, mu *tgb.MessageUpdate) error
return mu.Answer("pong").DoVoid(ctx) return mu.Answer("pong").DoVoid(ctx)
} }
func newRepoAnswer(name string) string { type git struct {
name string
url string
git string
ssh string
}
func newGit(d *d.Git) *git {
return &git{
name: d.Name,
url: d.HtmlUrl,
git: d.CloneUrl,
ssh: fmt.Sprintf("ssh://%s/%s.git", d.SshUrl, d.FullName),
}
}
// FYI: Telegram doesn't renders this hyperlink, if the url is localhost 🤷‍♂️
func (g *git) prepareAnswer() string {
return tg.HTML.Text( return tg.HTML.Text(
tg.HTML.Line( tg.HTML.Line(
"Repo ", "Repo ",
name, tg.HTML.Link(g.name, g.url),
"has been created!", "has been created!",
), ),
) )
@ -50,13 +69,16 @@ func (h *Handler) NewRepoHandler(ctx context.Context, mu *tgb.MessageUpdate) err
return errors.New("empty command provided") return errors.New("empty command provided")
} }
repoStr, err := h.workflow.CreateRepo(str, 0) var g *d.Git
g, err := h.workflow.CreateRepo(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)
} }
return mu.Answer(newRepoAnswer(repoStr)).ParseMode(tg.HTML).DoVoid(ctx) resp := newGit(g).prepareAnswer()
return mu.Answer(resp).ParseMode(tg.HTML).DoVoid(ctx)
} }
func (h *Handler) NewFolderHandler(ctx context.Context, mu *tgb.MessageUpdate) error { func (h *Handler) NewFolderHandler(ctx context.Context, mu *tgb.MessageUpdate) error {