From afe7bb94551881472105806c9d2cdf0bf18f6147 Mon Sep 17 00:00:00 2001 From: Miguel de la Cruz Date: Thu, 5 Mar 2020 22:57:01 +0100 Subject: [PATCH] Add a dry-run flag to the publish commands --- cmd/publish.go | 6 ++++-- cmd/standalone.go | 4 +++- jira/jira.go | 25 ++++++++++++++++++------- 3 files changed, 25 insertions(+), 10 deletions(-) diff --git a/cmd/publish.go b/cmd/publish.go index 7417d29..c2f3c85 100644 --- a/cmd/publish.go +++ b/cmd/publish.go @@ -20,6 +20,7 @@ func JiraPublishCmd() *cobra.Command { cmd.Flags().BoolP("all", "a", false, "Publish all the tickets of the campaign") cmd.Flags().IntP("batch", "b", 0, "Number of tickets to publish") + cmd.Flags().Bool("dry-run", false, "Print the tickets information instead of publishing them") return cmd } @@ -40,6 +41,7 @@ func PublishCmd() *cobra.Command { func jiraPublishCmdF(cmd *cobra.Command, _ []string) error { all, _ := cmd.Flags().GetBool("all") batch, _ := cmd.Flags().GetInt("batch") + dryRun, _ := cmd.Flags().GetBool("dry-run") if !all && batch == 0 { return fmt.Errorf("One of --all or --batch flags is required") @@ -61,13 +63,13 @@ func jiraPublishCmdF(cmd *cobra.Command, _ []string) error { } if all { - count, err := jiraClient.PublishAll(cmp) + count, err := jiraClient.PublishAll(cmp, dryRun) 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 { + if err := jiraClient.PublishBatch(cmp, batch, dryRun); err != nil { ErrorAndExit(cmd, err) } cmd.Printf("Batch of %d tickets successfully published in jira\n", batch) diff --git a/cmd/standalone.go b/cmd/standalone.go index 3aa03f9..3da854b 100644 --- a/cmd/standalone.go +++ b/cmd/standalone.go @@ -46,6 +46,7 @@ func CreateJiraTicketStandaloneCmd() *cobra.Command { cmd.Flags().String("username", "", "The jira username") cmd.Flags().String("token", "", "The jira token") cmd.Flags().StringSliceP("vars", "v", []string{}, "The variables to use in the template") + cmd.Flags().Bool("dry-run", false, "Print the ticket information instead of creating it") return cmd } @@ -87,6 +88,7 @@ func createJiraTicketStandaloneCmdF(cmd *cobra.Command, _ []string) error { summary, _ := cmd.Flags().GetString("summary") template, _ := cmd.Flags().GetString("template") vars, _ := cmd.Flags().GetStringSlice("vars") + dryRun, _ := cmd.Flags().GetBool("dry-run") if username == "" || token == "" { cfg, err := config.ReadConfig() @@ -120,7 +122,7 @@ func createJiraTicketStandaloneCmdF(cmd *cobra.Command, _ []string) error { } ticket := &model.Ticket{Data: varMap} - issue, err := jiraClient.PublishTicket(ticket, campaign) + issue, err := jiraClient.PublishTicket(ticket, campaign, dryRun) if err != nil { ErrorAndExit(cmd, err) } diff --git a/jira/jira.go b/jira/jira.go index a03685f..5b0e32a 100644 --- a/jira/jira.go +++ b/jira/jira.go @@ -2,6 +2,7 @@ package jira import ( "bytes" + "encoding/json" "fmt" "text/template" @@ -87,12 +88,18 @@ func (c *JiraClient) GetIssueFromTicket(ticket *model.Ticket, cmp *model.Campaig return issue, nil } -func (c *JiraClient) PublishTicket(ticket *model.Ticket, cmp *model.Campaign) (*jira.Issue, error) { +func (c *JiraClient) PublishTicket(ticket *model.Ticket, cmp *model.Campaign, dryRun bool) (*jira.Issue, error) { issue, err := c.GetIssueFromTicket(ticket, cmp) if err != nil { return nil, err } + if dryRun { + b, _ := json.MarshalIndent(issue, "", " ") + fmt.Println(string(b)) + return issue, nil + } + newIssue, _, err := c.Issue.Create(issue) if err != nil { return nil, err @@ -109,17 +116,21 @@ func (c *JiraClient) GetIssue(issueNo string) (*jira.Issue, error) { return issue, nil } -func (c *JiraClient) PublishNextTicket(cmp *model.Campaign) (bool, error) { +func (c *JiraClient) PublishNextTicket(cmp *model.Campaign, dryRun bool) (bool, error) { ticket := cmp.NextUnpublishedTicket() if ticket == nil { return false, nil } - issue, err := c.PublishTicket(ticket, cmp) + issue, err := c.PublishTicket(ticket, cmp, dryRun) if err != nil { return false, err } + if dryRun { + return true, nil + } + ticket.JiraLink = issue.Key if err := campaign.Save(cmp); err != nil { return false, err @@ -127,10 +138,10 @@ func (c *JiraClient) PublishNextTicket(cmp *model.Campaign) (bool, error) { return true, nil } -func (c *JiraClient) PublishAll(cmp *model.Campaign) (int, error) { +func (c *JiraClient) PublishAll(cmp *model.Campaign, dryRun bool) (int, error) { count := 0 for { - next, err := c.PublishNextTicket(cmp) + next, err := c.PublishNextTicket(cmp, dryRun) if err != nil { return count, err } @@ -142,9 +153,9 @@ func (c *JiraClient) PublishAll(cmp *model.Campaign) (int, error) { return count, nil } -func (c *JiraClient) PublishBatch(cmp *model.Campaign, batch int) error { +func (c *JiraClient) PublishBatch(cmp *model.Campaign, batch int, dryRun bool) error { for i := 0; i <= batch; i++ { - next, err := c.PublishNextTicket(cmp) + next, err := c.PublishNextTicket(cmp, dryRun) if err != nil { return err }