From 21238205867747afd7f50341c5fc0754d3984ff2 Mon Sep 17 00:00:00 2001 From: Miguel de la Cruz Date: Thu, 5 Mar 2020 22:37:01 +0100 Subject: [PATCH] Add publish all method --- cmd/publish.go | 44 +++++++++++-- cmd/standalone.go | 2 +- jira/jira.go | 158 ++++++++++++++++++++++++++++------------------ model/campaign.go | 9 +++ 4 files changed, 146 insertions(+), 67 deletions(-) diff --git a/cmd/publish.go b/cmd/publish.go index 2000cab..7417d29 100644 --- a/cmd/publish.go +++ b/cmd/publish.go @@ -1,6 +1,12 @@ package cmd import ( + "fmt" + + "git.ctrlz.es/mgdelacroix/campaigner/campaign" + "git.ctrlz.es/mgdelacroix/campaigner/config" + "git.ctrlz.es/mgdelacroix/campaigner/jira" + "github.com/spf13/cobra" ) @@ -32,14 +38,40 @@ func PublishCmd() *cobra.Command { } func jiraPublishCmdF(cmd *cobra.Command, _ []string) error { - /* - all, _ := cmd.Flags().GetBool("all") - batch, _ := cmd.Flags().GetInt("batch") + all, _ := cmd.Flags().GetBool("all") + batch, _ := cmd.Flags().GetInt("batch") - if !all && batch == 0 { - return fmt.Errorf("One of --all or --batch flags is required") + if !all && batch == 0 { + return fmt.Errorf("One of --all or --batch flags is required") + } + + cfg, err := config.ReadConfig() + if err != nil { + ErrorAndExit(cmd, err) + } + + cmp, err := campaign.Read() + if err != nil { + ErrorAndExit(cmd, err) + } + + jiraClient, err := jira.NewClient(cmp.Url, cfg.JiraUsername, cfg.JiraToken) + if err != nil { + ErrorAndExit(cmd, err) + } + + if all { + count, err := jiraClient.PublishAll(cmp) + if err != nil { + ErrorAndExit(cmd, err) } - */ + cmd.Printf("All %d tickets successfully published in jira\n", count) + } else { + if err := jiraClient.PublishBatch(cmp, batch); err != nil { + ErrorAndExit(cmd, err) + } + cmd.Printf("Batch of %d tickets successfully published in jira\n", batch) + } return nil } diff --git a/cmd/standalone.go b/cmd/standalone.go index d392047..3aa03f9 100644 --- a/cmd/standalone.go +++ b/cmd/standalone.go @@ -120,7 +120,7 @@ func createJiraTicketStandaloneCmdF(cmd *cobra.Command, _ []string) error { } ticket := &model.Ticket{Data: varMap} - issue, err := jiraClient.CreateTicket(ticket, campaign) + issue, err := jiraClient.PublishTicket(ticket, campaign) if err != nil { ErrorAndExit(cmd, err) } diff --git a/jira/jira.go b/jira/jira.go index 33e39ac..884e956 100644 --- a/jira/jira.go +++ b/jira/jira.go @@ -5,6 +5,7 @@ import ( "fmt" "text/template" + "git.ctrlz.es/mgdelacroix/campaigner/campaign" "git.ctrlz.es/mgdelacroix/campaigner/model" jira "gopkg.in/andygrunwald/go-jira.v1" @@ -14,64 +15,6 @@ type JiraClient struct { *jira.Client } -func (c *JiraClient) GetIssueFromTicket(ticket *model.Ticket, campaign *model.Campaign) (*jira.Issue, error) { - summaryTmpl, err := template.New("").Parse(campaign.Summary) - if err != nil { - return nil, err - } - - var summaryBytes bytes.Buffer - if err := summaryTmpl.Execute(&summaryBytes, ticket.Data); err != nil { - return nil, err - } - summary := summaryBytes.String() - - descriptionTemplate, err := template.ParseFiles(campaign.Template) - if err != nil { - return nil, err - } - - var descriptionBytes bytes.Buffer - if err := descriptionTemplate.Execute(&descriptionBytes, ticket.Data); err != nil { - return nil, err - } - description := descriptionBytes.String() - - data := map[string]string{ - "Description": description, - "Summary": summary, - "Project": campaign.Project, - "Issue Type": campaign.IssueType, - "Epic Link": campaign.Epic, - } - - if team, ok := ticket.Data["team"]; ok { - data["team"] = team.(string) - } - - createMetaInfo, _, err := c.Issue.GetCreateMeta(campaign.Project) - if err != nil { - return nil, err - } - - project := createMetaInfo.GetProjectWithKey(campaign.Project) - if project == nil { - return nil, fmt.Errorf("Error retrieving project with key %s", campaign.Project) - } - - issueType := project.GetIssueTypeWithName(campaign.IssueType) - if issueType == nil { - return nil, fmt.Errorf("Error retrieving issue type with name Story") - } - - issue, err := jira.InitIssueWithMetaAndFields(project, issueType, data) - if err != nil { - return nil, err - } - - return issue, nil -} - func NewClient(url, username, token string) (*JiraClient, error) { tp := jira.BasicAuthTransport{ Username: username, @@ -86,8 +29,66 @@ func NewClient(url, username, token string) (*JiraClient, error) { return &JiraClient{client}, nil } -func (c *JiraClient) CreateTicket(ticket *model.Ticket, campaign *model.Campaign) (*jira.Issue, error) { - issue, err := c.GetIssueFromTicket(ticket, campaign) +func (c *JiraClient) GetIssueFromTicket(ticket *model.Ticket, cmp *model.Campaign) (*jira.Issue, error) { + summaryTmpl, err := template.New("").Parse(cmp.Summary) + if err != nil { + return nil, err + } + + var summaryBytes bytes.Buffer + if err := summaryTmpl.Execute(&summaryBytes, ticket.Data); err != nil { + return nil, err + } + summary := summaryBytes.String() + + descriptionTemplate, err := template.ParseFiles(cmp.Template) + if err != nil { + return nil, err + } + + var descriptionBytes bytes.Buffer + if err := descriptionTemplate.Execute(&descriptionBytes, ticket.Data); err != nil { + return nil, err + } + description := descriptionBytes.String() + + data := map[string]string{ + "Description": description, + "Summary": summary, + "Project": cmp.Project, + "Issue Type": cmp.IssueType, + "Epic Link": cmp.Epic, + } + + if team, ok := ticket.Data["team"]; ok { + data["team"] = team.(string) + } + + createMetaInfo, _, err := c.Issue.GetCreateMeta(cmp.Project) + if err != nil { + return nil, err + } + + project := createMetaInfo.GetProjectWithKey(cmp.Project) + if project == nil { + return nil, fmt.Errorf("Error retrieving project with key %s", cmp.Project) + } + + issueType := project.GetIssueTypeWithName(cmp.IssueType) + if issueType == nil { + return nil, fmt.Errorf("Error retrieving issue type with name Story") + } + + issue, err := jira.InitIssueWithMetaAndFields(project, issueType, data) + if err != nil { + return nil, err + } + + return issue, nil +} + +func (c *JiraClient) PublishTicket(ticket *model.Ticket, cmp *model.Campaign) (*jira.Issue, error) { + issue, err := c.GetIssueFromTicket(ticket, cmp) if err != nil { return nil, err } @@ -107,3 +108,40 @@ func (c *JiraClient) GetIssue(issueNo string) (*jira.Issue, error) { } return issue, nil } + +func (c *JiraClient) PublishNextTicket(cmp *model.Campaign) (bool, error) { + ticket := cmp.NextUnpublishedTicket() + if ticket == nil { + return false, nil + } + + issue, err := c.PublishTicket(ticket, cmp) + if err != nil { + return false, err + } + + ticket.JiraLink = issue.Key + if err := campaign.Save(cmp); err != nil { + return false, err + } + return true, nil +} + +func (c *JiraClient) PublishAll(cmp *model.Campaign) (int, error) { + count := 0 + for { + next, err := c.PublishNextTicket(cmp) + if err != nil { + return count, err + } + if !next { + break + } + count++ + } + return count, nil +} + +func (c *JiraClient) PublishBatch(cmp *model.Campaign, batch int) error { + return nil +} diff --git a/model/campaign.go b/model/campaign.go index d9e9ca2..68f6339 100644 --- a/model/campaign.go +++ b/model/campaign.go @@ -10,3 +10,12 @@ type Campaign struct { Template string `json:"template"` Tickets []*Ticket `json:"tickets,omitempty"` } + +func (c *Campaign) NextUnpublishedTicket() *Ticket { + for _, ticket := range c.Tickets { + if ticket.JiraLink == "" { + return ticket + } + } + return nil +}