Add a dry-run flag to the publish commands

This commit is contained in:
Miguel de la Cruz 2020-03-05 22:57:01 +01:00
parent e5ed57c3d8
commit afe7bb9455
3 changed files with 25 additions and 10 deletions

View file

@ -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)

View file

@ -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)
}

View file

@ -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
}