jirust_cli/args/
commands.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
use clap::{ArgAction, Args, Parser, Subcommand, ValueEnum};
use std::error::Error;

/// Command line arguments base
/// subcommands: Config, Version
#[derive(Parser, Debug)]
#[command(author, version, about, long_about = None)]
pub struct JirustCliArgs {
    /// Subcommands
    #[clap(subcommand)]
    pub subcmd: Commands,
}

/// Available CLI commands
/// Config, Issue, Project, Transition, Version
#[derive(Subcommand, Debug)]
pub enum Commands {
    /// Configuration management
    Config(ConfigArgs),
    /// Issue management
    Issue(IssueArgs),
    /// Project management
    Project(ProjectArgs),
    /// Transition management
    Transition(TransitionArgs),
    /// Version management
    Version(VersionArgs),
}

/// Available pagination command line arguments
///
/// * page_size: Option<i32>
/// * page_offset: Option<i64>
#[derive(Args, Debug)]
pub struct PaginationArgs {
    /// page size for lists
    #[clap(
        long,
        short = 'l',
        value_name = "page_size",
        help = "page size for lists"
    )]
    pub page_size: Option<i32>,
    /// page offset for list
    #[clap(
        long,
        short = 's',
        value_name = "page_offset",
        help = "page offset for list"
    )]
    pub page_offset: Option<i64>,
}

/// Available output values
/// Table, Json
#[derive(ValueEnum, Debug, Clone, Copy)]
#[value(rename_all = "kebab-case")]
pub enum OutputValues {
    /// Print output in table format
    #[value(name = "table", help = "Print output in table format")]
    Table,
    /// Print output in json format
    #[value(name = "json", help = "Print output in json format")]
    Json,
}

/// Available output values
///
/// * Table: Print output in table format
/// * Json: Print output in json format
#[derive(Args, Debug)]
pub struct OutputArgs {
    /// Output format
    #[clap(long, short = 'o', value_name = "table|json", help = "Output format")]
    pub output: Option<OutputValues>,
}

/// Available configuration command line arguments
/// cfg_act: ConfigActionValues
///    Auth, Jira, Setup, Show
///
#[derive(Args, Debug)]
pub struct ConfigArgs {
    /// Configuration action
    #[arg(
        value_name = "auth|jira|setup|show",
        help_heading = "Configuration management"
    )]
    pub cfg_act: ConfigActionValues,
}

/// Available configuration action values
/// Auth, Jira, Setup, Show
#[derive(ValueEnum, Debug, Clone, Copy)]
#[value(rename_all = "kebab-case")]
pub enum ConfigActionValues {
    /// Set Jira API authentication (username, apikey)
    #[value(name = "auth", help = "Set Jira API authentication (username, apikey)")]
    Auth,
    /// Set Jira API base URL
    #[value(name = "jira", help = "Set Jira API base URL")]
    Jira,
    /// Setup Jira API configuration (authentication data, jira base URL, etc.)
    #[value(
        name = "setup",
        help = "Setup Jira API configuration (authentication data, jira base URL, etc.)"
    )]
    Setup,
    /// Show current configuration
    #[value(name = "show", help = "Show current configuration")]
    Show,
}

/// Available version command line arguments
/// version_act: VersionActionValues
///   Create, List, Update, Delete, Release, Archive
#[derive(Args, Debug)]
pub struct VersionArgs {
    /// Version action
    #[arg(
        value_name = "archive|create|delete|list|release|update",
        help_heading = "Jira Project version management"
    )]
    pub version_act: VersionActionValues,
    /// Jira Project key
    #[clap(
        long,
        short = 'k',
        value_name = "project_key",
        required = true,
        help = "Jira Project key"
    )]
    pub project_key: String,
    /// Jira Project ID
    #[clap(long, short = 'i', value_name = "project_id", help = "Jira Project ID")]
    pub project_id: Option<i64>,
    /// Jira Project version ID
    #[clap(
        long,
        short = 'v',
        value_name = "version_id",
        help = "Jira Project version ID"
    )]
    pub version_id: Option<String>,
    /// Jira Project version name
    #[clap(
        long,
        short = 'n',
        value_name = "version_name",
        help = "Jira Project version name"
    )]
    pub version_name: Option<String>,
    /// Jira Project version description
    #[clap(
        long,
        short = 'd',
        value_name = "version_description",
        help = "Jira Project version description"
    )]
    pub version_description: Option<String>,
    /// Jira Project version start date
    #[clap(
        long,
        value_name = "version_start_date",
        help = "Jira Project version start date"
    )]
    pub version_start_date: Option<String>,
    /// Jira Project version release date
    #[clap(
        long,
        value_name = "version_release_date",
        help = "Jira Project version release date"
    )]
    pub version_release_date: Option<String>,
    /// Jira Project version archived
    #[clap(
        long,
        short = 'a',
        value_name = "version_archived",
        help = "Jira Project version archived"
    )]
    pub version_archived: Option<bool>,
    /// Jira Project version released
    #[clap(
        long,
        short = 'm',
        action = ArgAction::SetTrue,
        value_name = "version_released",
        help = "Jira Project version released"
    )]
    pub version_released: Option<bool>,
    /// Jira Project version changelog file
    #[clap(
        long,
        short = 'c',
        value_name = "changelog_file",
        help = "changelog file path to be used for automatic description generation (if set the script detects automatically the first tagged block in the changelog and use it as description)"
    )]
    pub changelog_file: Option<String>,
    /// Jira Project version automatically transition issues in changelog
    #[clap(
        long,
        short = 'r',
        action = ArgAction::SetTrue,
        value_name = "resolve_issues",
        help = "if changelog is set and this flag is set, the script will transition all issues in the changelog of the current version release to the \"resolved\" status setting the version as \"fixVersion\""
    )]
    pub transition_issues: Option<bool>,
    /// Jira Project version transition assignee
    #[clap(
        long,
        short = 'u',
        value_name = "transition_assignee",
        help = "if changelog is set and the resolve_issues flag is set, the script will assigned all the resolved issue to the user specified in this field (if not set the assignee will not be changed)"
    )]
    pub transition_assignee: Option<String>,
    /// Jira Project version pagination
    #[clap(flatten)]
    pub pagination: PaginationArgs,
    /// Jira Project version actions result output format
    #[clap(flatten)]
    pub output: OutputArgs,
}

/// Available version action values
/// Archive, Create, Delete, List, Release, Update
#[derive(ValueEnum, Debug, Clone, Copy)]
#[value(rename_all = "kebab-case")]
pub enum VersionActionValues {
    /// Archive a Jira Project version
    #[value(name = "archive", help = "Archive a Jira Project version")]
    Archive,
    /// Create a Jira Project version
    #[value(name = "create", help = "Create a Jira Project version")]
    Create,
    /// Delete a Jira Project version
    #[value(name = "delete", help = "Delete a Jira Project version")]
    Delete,
    /// List Jira Project versions
    #[value(name = "list", help = "List Jira Project versions")]
    List,
    /// Release a Jira Project version
    #[value(name = "release", help = "Release a Jira Project version")]
    Release,
    /// Update a Jira Project version
    #[value(name = "update", help = "Update a Jira Project version")]
    Update,
}

/// Available project command line arguments
/// version_act: ProjectActionValues
/// GetIssueTypes, GetIssueTypeFields, List
#[derive(Args, Debug)]
pub struct ProjectArgs {
    /// Project action
    #[arg(
        value_name = "get-issue-types|get-issue-type-fields|list",
        help_heading = "Jira Project management",
        required = true
    )]
    pub project_act: ProjectActionValues,
    /// Jira Project key
    #[clap(
        long,
        short = 'k',
        value_name = "project_key",
        help = "Jira Project key",
        required = true
    )]
    pub project_key: Option<String>,
    /// Jira Project issue type ID
    #[clap(
        long,
        short = 'i',
        value_name = "project_issue_type",
        help = "Jira Project issue type ID"
    )]
    pub project_issue_type: Option<String>,
    /// Jira Project pagination
    #[clap(flatten)]
    pub pagination: PaginationArgs,
    /// Jira Project actions result output format
    #[clap(flatten)]
    pub output: OutputArgs,
}

/// Available project action values
/// GetIssueTypes, GetIssueTypeFields, List
#[derive(ValueEnum, Debug, Clone, Copy)]
#[value(rename_all = "kebab-case")]
pub enum ProjectActionValues {
    /// Get Jira Project issue types by Jira project key
    #[value(
        name = "get-issue-types",
        help = "Get Jira Project issue types by Jira project key"
    )]
    GetIssueTypes,
    /// Get Jira Project issue type fields by Jira project key and issue type ID
    #[value(
        name = "get-issue-type-fields",
        help = "Get Jira Project issue type fields by Jira project key and issue type ID"
    )]
    GetIssueTypeFields,
    /// List Jira Projects
    #[value(name = "list", help = "List Jira Projects")]
    List,
}

/// Available version command line arguments
/// version_act: VersionActionValues
///   Create, List, Update, Delete, Release, Archive
#[derive(Args, Debug)]
pub struct IssueArgs {
    /// Issue action
    #[arg(
        value_name = "assign|create|delete|get|transition|update",
        help_heading = "Jira Project issue management"
    )]
    pub issue_act: IssueActionValues,
    /// Jira Project key
    #[clap(
        long,
        short = 'p',
        value_name = "project_key",
        help = "Jira Project key",
        required = true
    )]
    pub project_key: String,
    /// Jira Project issue key
    #[clap(
        long,
        short = 'i',
        value_name = "issue_key",
        help = "Jira Project issue key",
        required = true
    )]
    pub issue_key: Option<String>,
    /// Jira Project issue fields
    #[clap(long,
        short = 'f',
        value_name = "issue_fields",
        value_parser = parse_key_val::<String, String>,
        help = "Jira Project issue fields (field_name=value)")]
    pub issue_fields: Option<Vec<(String, String)>>,
    /// Jira Project issue transition
    #[clap(
        long,
        short = 't',
        value_name = "transition_to",
        help = "Jira Project issue transition to"
    )]
    pub transition_to: Option<String>,
    /// Jira Project issue assignee
    #[clap(
        long,
        short = 'a',
        value_name = "assignee",
        help = "Jira Project issue assignee"
    )]
    pub assignee: Option<String>,
    /// Jira Project issue pagination
    #[clap(flatten)]
    pub pagination: PaginationArgs,
    /// Jira Project issue actions result output format
    #[clap(flatten)]
    pub output: OutputArgs,
}

/// Available issue action values
/// Create, List, Update, Delete, Release, Archive
#[derive(ValueEnum, Debug, Clone, Copy)]
#[value(rename_all = "kebab-case")]
pub enum IssueActionValues {
    /// Assign a Jira Project issue
    #[value(name = "assign", help = "Assign a Jira Project issue")]
    Assign,
    /// Create a Jira Project issue
    #[value(name = "create", help = "Create a Jira Project issue")]
    Create,
    /// Delete a Jira Project issue
    #[value(name = "delete", help = "Delete a Jira Project issue")]
    Delete,
    /// Get a specific Jira Project issue
    #[value(name = "get", help = "Get a specific Jira Project issue")]
    Get,
    /// Transition a Jira Project issue
    #[value(name = "transition", help = "Transition a Jira Project issue")]
    Transition,
    /// Update a Jira Project issue
    #[value(name = "update", help = "Update a Jira Project issue")]
    Update,
}

/// Available transition command line arguments
/// transition_act: TransitionActionValues
/// List
#[derive(Args, Debug)]
pub struct TransitionArgs {
    /// Transition action
    #[arg(value_name = "list", help_heading = "Jira issue transition list")]
    pub transition_act: TransitionActionValues,
    /// Jira issue key
    #[clap(
        long,
        short = 'i',
        value_name = "issue_key",
        help = "Jira Project issue key",
        required = true
    )]
    pub issue_key: String,
    /// Jira issue output format
    #[clap(flatten)]
    pub output: OutputArgs,
}

/// Available transition action values
/// List
#[derive(ValueEnum, Debug, Clone, Copy)]
#[value(rename_all = "kebab-case")]
pub enum TransitionActionValues {
    /// List Jira issue available transitions
    #[value(name = "list", help = "List Jira issue available transitions")]
    List,
}

/// Parse a single key-value pair
/// Thanks to the example from the clap documentation (https://github.com/clap-rs/clap/blob/master/examples/typed-derive.rs)
fn parse_key_val<T, U>(s: &str) -> Result<(T, U), Box<dyn Error + Send + Sync + 'static>>
where
    T: std::str::FromStr,
    T::Err: Error + Send + Sync + 'static,
    U: std::str::FromStr,
    U::Err: Error + Send + Sync + 'static,
{
    let pos = s
        .find('=')
        .ok_or_else(|| format!("invalid KEY=value: no `=` found in `{s}`"))?;
    Ok((s[..pos].parse()?, s[pos + 1..].parse()?))
}