- 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
}
type IWorkflowController interface {
Workflow(name string) (string, error)
CreateRepo(name string, param uint) (string, error)
CreateFolder(name string) (*d.Folder, error)
}
func NewWorkflowController(
gitBaseURL,
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) {
yt := wc.iYouTrack
@ -52,22 +52,22 @@ func (wc *WorkflowController) Workflow(name string) (string, error) {
if issue != nil {
var (
git, gitBuild string
git, gitBuild *d.Git
cloud *d.Folder
)
var wg sync.WaitGroup
wg.Add(3)
go func() {
go func(ref **d.Git) {
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()
gitBuild, _ = wc.CreateRepo(issue.Key+"-build", 1)
}()
*ref, _ = wc.CreateRepo(issue.Key + "-build")
}(&gitBuild)
go func(ref **d.Folder) {
defer wg.Done()
@ -76,31 +76,29 @@ func (wc *WorkflowController) Workflow(name string) (string, error) {
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
}
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;
repo, err := wc.iGit.NewRepo(name)
if err != nil {
return nil, err
}
//Set 'apps' as collaborator to created repository;
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
}
_, err = wc.iGit.AppsAsCollaboratorTo(repo)
if err != nil {
return nil, err
}
return "", err
return repo, nil
}
func (wc *WorkflowController) CreateFolder(name string) (*d.Folder, error) {

View File

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

View File

@ -1,6 +1,7 @@
package ext
import (
"fmt"
"os"
"strconv"
d "ticket-pimp/domain"
@ -49,7 +50,7 @@ func (c *Cloud) CreateFolder(name string) (*d.Folder, error) {
cloud.PathTo = parentPath + rootDir + name
resp, err := c.R().
resp, _ := c.R().
Send("MKCOL", requestPath)
if resp.IsSuccessState() {
@ -57,12 +58,12 @@ func (c *Cloud) CreateFolder(name string) (*d.Folder, error) {
cloud.PrivateURL = c.BaseURL + cloud.PathTo
// Try to set short URL to the d entity
if err = c.setPrivateURL(requestPath, &cloud); err != nil {
return &cloud, nil
if err := c.setPrivateURL(requestPath, &cloud); err != nil {
return &cloud, err
}
}
return &cloud, err
return &cloud, nil
}
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).
Send("PROPFIND", requestPath)
if err != nil {
return err
if resp.Err != nil {
return resp.Err
}
id := helpers.GetFileIDFromRespBody(resp.Bytes())
if id != 0 {
cloud.PrivateURL = c.BaseURL + "/f/" + strconv.Itoa(id)
return nil
if id == 0 {
return fmt.Errorf("unable to get fileid")
}
return err
cloud.PrivateURL = c.BaseURL + "/f/" + strconv.Itoa(id)
return nil
}
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
git.Private = true
resp, err := gb.R().
resp, _ := gb.R().
SetBody(&payload).
SetSuccessResult(&git).
Post("/user/repos")
if err != nil {
log.Print(resp)
if resp.Err != nil {
log.Print(resp.Err)
return nil, resp.Err
}
return &git, err
return &git, nil
}
func (gb *Git) AppsAsCollaboratorTo(git *domain.Git) (*domain.Git, error) {
payloadPermission := permissionRequest{
payload := permissionRequest{
Perm: "admin",
}
resp, err := gb.R().
SetBody(&payloadPermission).
Put("/repos/" + os.Getenv("GIT_USER") + "/" + git.Name + "/collaborators/apps")
respURL := "/repos/" + os.Getenv("GIT_USER") + "/" + git.Name + "/collaborators/apps"
if err != nil {
log.Print(resp)
resp, _ := gb.R().
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
_, err := yt.R().
resp, _ := yt.R().
EnableDump().
SetQueryParam("fields", "id,name,shortName").
SetSuccessResult(&projects).
Get("/admin/projects")
// Check if the request failed;
if err != nil {
return nil, fmt.Errorf("some problem with YT request. error message: %v", err)
if resp.Err != nil {
return nil, fmt.Errorf("some problem with YT request. error message: %v", resp.Err)
}
return projects, nil
@ -71,15 +71,15 @@ func (yt *youtrack) CreateIssue(projectID, name string) (*d.IssueCreateRequest,
}
// Push issue to the YT;
_, err := yt.R().
resp, _ := yt.R().
SetQueryParam("fields", "idReadable,id").
SetBody(&issue).
SetSuccessResult(&issue).
Post("/issues")
// Check if the request failed;
if err != nil {
return nil, fmt.Errorf("some problem with YT request. error message: %v", err)
if resp.Err != nil {
return nil, fmt.Errorf("some problem with YT request. error message: %v", resp.Err)
}
return &issue, nil
@ -109,14 +109,14 @@ func (yt *youtrack) UpdateIssue(issue *d.IssueCreateRequest, folder, git, gitBui
}
// Push issue update to YT
resp, err := yt.R().
resp, _ := yt.R().
SetBody(&update).
SetSuccessResult(&issue).
Post("/issues/" + issue.Key)
// Check if the request failed;
if err != nil {
return nil, fmt.Errorf("some problem with YT request. error message: %v", err)
if resp.Err != nil {
return nil, fmt.Errorf("some problem with YT request. error message: %v", resp.Err)
}
if !resp.IsSuccessState() {

View File

@ -6,6 +6,7 @@ import (
"fmt"
"strings"
"ticket-pimp/controller"
d "ticket-pimp/domain"
"github.com/mr-linch/go-tg"
"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)
}
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(
tg.HTML.Line(
"Repo ",
name,
tg.HTML.Link(g.name, g.url),
"has been created!",
),
)
@ -50,13 +69,16 @@ func (h *Handler) NewRepoHandler(ctx context.Context, mu *tgb.MessageUpdate) err
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 {
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 {