ticket-pimp/bot/controller/controller.go

212 lines
4.2 KiB
Go

package controller
import (
"encoding/csv"
"fmt"
"io"
"strings"
"sync"
"ticket-pimp/internal/domain"
"ticket-pimp/internal/services"
"github.com/imroc/req/v3"
)
type WorkflowController struct {
iGit services.IGit
iCloud services.ICloud
iYouTrack services.IYouTrack
additionalYT services.IYouTrack
iCoda services.ICoda
}
func NewWorkflowController(
git services.IGit,
cloud services.ICloud,
devyt services.IYouTrack,
farmyt services.IYouTrack,
coda services.ICoda,
) *WorkflowController {
return &WorkflowController{
iGit: git,
iCloud: cloud,
iYouTrack: devyt,
additionalYT: farmyt,
iCoda: coda,
}
}
type IWorkflowController interface {
Workflow(name string) (string, error)
NewTask(summ, desc, c, cLink string) *Task
CreateTask(t *Task) (*Task, error)
ThrowConversions(f io.ReadCloser, appID string, token string) *domain.ConversionLog
}
func (wc *WorkflowController) ThrowConversions(f io.ReadCloser, appID string, token string) *domain.ConversionLog {
c := req.C().
SetBaseURL("https://graph.facebook.com/v15.0/").
DevMode()
const currency = "USD"
r := csv.NewReader(f)
conversionLog := domain.ConversionLog{}
for {
record, err := r.Read()
if err == io.EOF {
break
}
if err != nil {
return nil
}
advertiser := strings.Split(record[0], ";")[0]
params := map[string]string{
"advertiser_id": advertiser,
"event": "CUSTOM_APP_EVENTS",
"application_tracking_enabled": "1",
"advertiser_tracking_enabled": "1",
"custom_events": `[{"_eventName":"fb_mobile_purchase"}]`,
}
res, _ := c.R().
SetQueryString(token).
SetQueryParams(params).
Post(appID + "/activities")
if res.Err != nil {
conversionLog.Advertiser = append(conversionLog.Advertiser, advertiser)
}
}
return &conversionLog
}
type Task struct {
Summary string
Description string
Creator string
CreatorLink string
Key string
URL string
}
func (wc *WorkflowController) NewTask(summ, desc, c, cLink string) *Task {
return &Task{
Summary: summ,
Description: desc,
Creator: c,
CreatorLink: cLink,
}
}
func (wc *WorkflowController) CreateTask(t *Task) (*Task, error) {
yt := wc.additionalYT
projectID, err := yt.GetProjectIDByName("E")
if err != nil {
return nil, err
}
t.Description += fmt.Sprintf("\n\n Created by: [%s](%s)", t.Creator, t.CreatorLink)
issue, err := yt.CreateIssue(projectID, t.Creator+" | "+t.Summary, t.Description)
if err != nil {
return nil, err
}
t.Key = issue.Key
t.URL = fmt.Sprintf("https://mobmarlerino.youtrack.cloud/issue/%s", issue.Key)
return t, nil
}
func (wc *WorkflowController) Workflow(name string) (string, error) {
// coda := wc.iCoda
yt := wc.iYouTrack
projectID, err := yt.GetProjectIDByName("tst")
if err != nil {
return "", err
}
// Create an issue at the available project with the provided name
issue, err := yt.CreateIssue(projectID, name, "")
if err != nil {
return "", err
}
if issue != nil {
var (
git, gitBuild *domain.Git
cloud *domain.Folder
)
var wg sync.WaitGroup
wg.Add(3)
go func(ref **domain.Git) {
defer wg.Done()
*ref, _ = wc.iGit.CreateRepo(issue.Key)
}(&git)
go func(ref **domain.Git) {
defer wg.Done()
*ref, _ = wc.iGit.CreateRepo(issue.Key + "-build")
}(&gitBuild)
go func(ref **domain.Folder) {
defer wg.Done()
*ref, _ = wc.iCloud.CreateFolder(issue.Key + " - " + issue.Summary)
}(&cloud)
wg.Wait()
var gitResult, gitBuildResult, cloudResult string
if git == nil {
gitResult = "cannot create git"
} else {
gitResult = git.HtmlUrl
}
if gitBuild == nil {
gitBuildResult = "cannot create git"
} else {
gitBuildResult = fmt.Sprintf("ssh://%s/%s.git", gitBuild.SshUrl, gitBuild.FullName)
}
if cloud == nil {
cloudResult = "cannot create folder"
} else {
cloudResult = cloud.PrivateURL
}
wc.iCoda.CreateApp(domain.CodaIssue{
ID: issue.Key,
Summary: strings.TrimSpace(name),
Git: gitResult,
GitBuild: gitBuildResult,
Folder: cloudResult,
})
yt.UpdateIssue(
issue,
cloudResult,
gitResult,
gitBuildResult,
)
}
return issue.Key, nil
}