|
| 1 | +package create |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "errors" |
| 6 | + "fmt" |
| 7 | + "time" |
| 8 | + |
| 9 | + "github.com/AlecAivazis/survey/v2" |
| 10 | + "github.com/spf13/cobra" |
| 11 | + "github.com/spf13/viper" |
| 12 | + |
| 13 | + "github.com/ankitpokhrel/jira-cli/api" |
| 14 | + "github.com/ankitpokhrel/jira-cli/internal/cmdutil" |
| 15 | + "github.com/ankitpokhrel/jira-cli/internal/query" |
| 16 | + "github.com/ankitpokhrel/jira-cli/pkg/jira" |
| 17 | +) |
| 18 | + |
| 19 | +const ( |
| 20 | + helpText = `Create a new sprint.` |
| 21 | + examples = `$ jira sprint create MySprint |
| 22 | +
|
| 23 | +# Add a start and end date to the sprint: |
| 24 | +$ jira sprint create --start 2025-08-25 --end 2025-08-31 MySprint |
| 25 | +
|
| 26 | +# Also add a goal for the sprint: |
| 27 | +$ jira sprint create --start 2025-08-25 --end 2025-08-31 --goal "Fix all bugs" MySprint |
| 28 | +
|
| 29 | +# Omit some parameters on purpose: |
| 30 | +$ jira sprint create --no-input MySprint |
| 31 | +
|
| 32 | +# Get JSON output: |
| 33 | +$ jira sprint create --raw MySprint |
| 34 | +` |
| 35 | +) |
| 36 | + |
| 37 | +// NewCmdCreate is a create command. |
| 38 | +func NewCmdCreate() *cobra.Command { |
| 39 | + cmd := cobra.Command{ |
| 40 | + Use: "create SPRINT-NAME", |
| 41 | + Short: "Create new sprint", |
| 42 | + Long: helpText, |
| 43 | + Example: examples, |
| 44 | + Annotations: map[string]string{ |
| 45 | + "help:args": "SPRINT_NAME\t\tThe name of the sprint to be created", |
| 46 | + }, |
| 47 | + Run: create, |
| 48 | + } |
| 49 | + |
| 50 | + cmd.Flags().Bool("raw", false, "Print output in JSON format") |
| 51 | + cmd.Flags().Bool("no-input", false, "Disable prompt for non-required fields") |
| 52 | + cmd.Flags().StringP("start", "s", "", "Start date (YYYY-MM-DD)") |
| 53 | + cmd.Flags().StringP("end", "e", "", "End date (YYYY-MM-DD)") |
| 54 | + cmd.Flags().StringP("goal", "g", "", "Goal of the sprint") |
| 55 | + |
| 56 | + return &cmd |
| 57 | +} |
| 58 | + |
| 59 | +func create(cmd *cobra.Command, args []string) { |
| 60 | + params := parseFlags(cmd.Flags(), args) |
| 61 | + client := api.DefaultClient(params.Debug) |
| 62 | + |
| 63 | + qs := getQuestions(params) |
| 64 | + if len(qs) > 0 { |
| 65 | + ans := struct { |
| 66 | + SprintName string |
| 67 | + StartDate string |
| 68 | + EndDate string |
| 69 | + Goal string |
| 70 | + }{} |
| 71 | + err := survey.Ask(qs, &ans) |
| 72 | + cmdutil.ExitIfError(err) |
| 73 | + |
| 74 | + if params.SprintName == "" { |
| 75 | + params.SprintName = ans.SprintName |
| 76 | + } |
| 77 | + if params.StartDate == "" { |
| 78 | + params.StartDate = ans.StartDate |
| 79 | + } |
| 80 | + if params.EndDate == "" { |
| 81 | + params.EndDate = ans.EndDate |
| 82 | + } |
| 83 | + if params.Goal == "" { |
| 84 | + params.Goal = ans.Goal |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + if (params.StartDate != "" && params.EndDate == "") || (params.StartDate == "" && params.EndDate != "") { |
| 89 | + cmdutil.Failed("Either both start and end dates must be supplied, or none of them") |
| 90 | + } |
| 91 | + cr := jira.SprintCreateRequest{ |
| 92 | + Name: params.SprintName, |
| 93 | + StartDate: params.StartDate, |
| 94 | + EndDate: params.EndDate, |
| 95 | + Goal: params.Goal, |
| 96 | + OriginBoardID: viper.GetInt("board.id"), |
| 97 | + } |
| 98 | + sprint, err := client.CreateSprint(&cr) |
| 99 | + cmdutil.ExitIfError(err) |
| 100 | + |
| 101 | + if params.Raw { |
| 102 | + jsonData, err := json.Marshal(sprint) |
| 103 | + cmdutil.ExitIfError(err) |
| 104 | + fmt.Println(string(jsonData)) |
| 105 | + return |
| 106 | + } |
| 107 | + |
| 108 | + cmdutil.Success("Sprint '%s' with id '%d' created\n", sprint.Name, sprint.ID) |
| 109 | +} |
| 110 | + |
| 111 | +func parseFlags(flags query.FlagParser, args []string) *SprintCreateParams { |
| 112 | + var sprintName string |
| 113 | + |
| 114 | + if len(args) > 0 { |
| 115 | + sprintName = args[0] |
| 116 | + } |
| 117 | + |
| 118 | + start, err := flags.GetString("start") |
| 119 | + cmdutil.ExitIfError(err) |
| 120 | + if start != "" { |
| 121 | + if err = validateDate(start); err != nil { |
| 122 | + cmdutil.Failed("Invalid start date. Should be in YYYY-MM-DD format") |
| 123 | + } |
| 124 | + } |
| 125 | + |
| 126 | + end, err := flags.GetString("end") |
| 127 | + cmdutil.ExitIfError(err) |
| 128 | + if end != "" { |
| 129 | + if err = validateDate(end); err != nil { |
| 130 | + cmdutil.Failed("Invalid end date. Should be in YYYY-MM-DD format") |
| 131 | + } |
| 132 | + } |
| 133 | + |
| 134 | + goal, err := flags.GetString("goal") |
| 135 | + cmdutil.ExitIfError(err) |
| 136 | + |
| 137 | + debug, err := flags.GetBool("debug") |
| 138 | + cmdutil.ExitIfError(err) |
| 139 | + |
| 140 | + noInput, err := flags.GetBool("no-input") |
| 141 | + cmdutil.ExitIfError(err) |
| 142 | + |
| 143 | + raw, err := flags.GetBool("raw") |
| 144 | + cmdutil.ExitIfError(err) |
| 145 | + |
| 146 | + return &SprintCreateParams{ |
| 147 | + SprintName: sprintName, |
| 148 | + StartDate: start, |
| 149 | + EndDate: end, |
| 150 | + Goal: goal, |
| 151 | + Debug: debug, |
| 152 | + NoInput: noInput, |
| 153 | + Raw: raw, |
| 154 | + } |
| 155 | +} |
| 156 | + |
| 157 | +func getQuestions(params *SprintCreateParams) []*survey.Question { |
| 158 | + var qs []*survey.Question |
| 159 | + |
| 160 | + if params.SprintName == "" { |
| 161 | + qs = append(qs, &survey.Question{ |
| 162 | + Name: "SprintName", |
| 163 | + Prompt: &survey.Input{Message: "Sprint Name"}, |
| 164 | + Validate: survey.Required, |
| 165 | + }) |
| 166 | + } |
| 167 | + |
| 168 | + if params.NoInput { |
| 169 | + return qs |
| 170 | + } |
| 171 | + |
| 172 | + if params.StartDate == "" { |
| 173 | + qs = append(qs, &survey.Question{ |
| 174 | + Name: "StartDate", |
| 175 | + Prompt: &survey.Input{Message: "Start date (YYYY-MM-DDD)"}, |
| 176 | + Validate: validateDate, |
| 177 | + }) |
| 178 | + } |
| 179 | + |
| 180 | + if params.EndDate == "" { |
| 181 | + qs = append(qs, &survey.Question{ |
| 182 | + Name: "EndDate", |
| 183 | + Prompt: &survey.Input{Message: "End date (YYYY-MM-DDD)"}, |
| 184 | + Validate: validateDate, |
| 185 | + }) |
| 186 | + } |
| 187 | + |
| 188 | + if params.Goal == "" { |
| 189 | + qs = append(qs, &survey.Question{ |
| 190 | + Name: "Goal", |
| 191 | + Prompt: &survey.Input{Message: "Goal"}, |
| 192 | + }) |
| 193 | + } |
| 194 | + |
| 195 | + return qs |
| 196 | +} |
| 197 | + |
| 198 | +type SprintCreateParams struct { |
| 199 | + SprintName string |
| 200 | + StartDate string |
| 201 | + EndDate string |
| 202 | + Goal string |
| 203 | + Debug bool |
| 204 | + NoInput bool |
| 205 | + Raw bool |
| 206 | +} |
| 207 | + |
| 208 | +// Returns an error if the date is not in the form YYYY-MM-DD. Technically, the |
| 209 | +// JIRA API accepts other formats, but YYYY-MM-DD should be enough for most |
| 210 | +// users. |
| 211 | +func validateDate(val interface{}) error { |
| 212 | + // We allow empty dates for the start and end of the sprint |
| 213 | + if val.(string) == "" { |
| 214 | + return nil |
| 215 | + } |
| 216 | + |
| 217 | + _, err := time.Parse(time.DateOnly, val.(string)) |
| 218 | + if err != nil { |
| 219 | + return errors.New("Invalid date") |
| 220 | + } |
| 221 | + return nil |
| 222 | +} |
0 commit comments