radicle-cli 0.20.0

Radicle CLI
Documentation
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
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
use std::str::FromStr;

use clap::{Parser, Subcommand};

use radicle::{
    cob::{Label, Reaction, Title},
    identity::{did::DidError, Did, RepoId},
    issue::{CloseReason, State},
};

use crate::{git::Rev, terminal::patch::Message};

#[derive(Default, Debug, Clone, PartialEq, Eq)]
pub enum Assigned {
    #[default]
    Me,
    Peer(Did),
}

#[derive(Parser, Debug)]
#[command(about = super::ABOUT, disable_version_flag = true)]
pub struct Args {
    #[command(subcommand)]
    pub(crate) command: Option<Command>,

    /// Do not print anything
    #[arg(short, long)]
    #[clap(global = true)]
    pub(crate) quiet: bool,

    /// Do not announce issue changes to the network
    #[arg(long)]
    #[arg(value_name = "no-announce")]
    #[clap(global = true)]
    pub(crate) no_announce: bool,

    /// Show only the issue header, hiding the comments
    #[arg(long)]
    #[clap(global = true)]
    pub(crate) header: bool,

    /// Operate on the given repository (default: cwd)
    #[arg(value_name = "RID")]
    #[arg(long, short)]
    #[clap(global = true)]
    pub(crate) repo: Option<RepoId>,

    /// Enable verbose output
    #[arg(long, short)]
    #[clap(global = true)]
    pub(crate) verbose: bool,

    /// Arguments for the empty subcommand.
    /// Will fall back to [`Command::List`].
    #[clap(flatten)]
    pub(crate) empty: EmptyArgs,
}

#[derive(Subcommand, Debug)]
pub(crate) enum Command {
    /// Add or delete assignees from an issue
    Assign {
        /// ID of the issue
        #[arg(value_name = "ISSUE_ID")]
        id: Rev,

        /// Add an assignee (may be specified multiple times, takes precedence over `--delete`)
        #[arg(long, short)]
        #[arg(value_name = "DID")]
        #[arg(action = clap::ArgAction::Append)]
        add: Vec<Did>,

        /// Delete an assignee (may be specified multiple times)
        #[arg(long, short)]
        #[arg(value_name = "DID")]
        #[arg(action = clap::ArgAction::Append)]
        delete: Vec<Did>,
    },
    /// Re-cache all issues that can be found in Radicle storage
    Cache {
        /// Optionally choose an issue to re-cache
        #[arg(value_name = "ISSUE_ID")]
        id: Option<Rev>,

        /// Operate on storage
        #[arg(long)]
        storage: bool,
    },
    /// Add a comment to an issue
    #[clap(long_about = include_str!("comment.txt"))]
    Comment(CommentArgs),
    /// Edit the title and description of an issue
    Edit {
        /// ID of the issue
        #[arg(value_name = "ISSUE_ID")]
        id: Rev,

        /// The new title to set
        #[arg(long, short)]
        title: Option<Title>,

        /// The new description to set
        #[arg(long, short)]
        description: Option<String>,
    },
    /// Delete an issue
    Delete {
        /// ID of the issue
        #[arg(value_name = "ISSUE_ID")]
        id: Rev,
    },
    /// Add or delete labels from an issue
    Label {
        /// ID of the issue
        #[arg(value_name = "ISSUE_ID")]
        id: Rev,

        /// Add a label (may be specified multiple times, takes precedence over `--delete`)
        #[arg(long, short)]
        #[arg(value_name = "label")]
        #[arg(action = clap::ArgAction::Append)]
        add: Vec<Label>,

        /// Delete a label (may be specified multiple times)
        #[arg(long, short)]
        #[arg(value_name = "label")]
        #[arg(action = clap::ArgAction::Append)]
        delete: Vec<Label>,
    },
    /// List issues, optionally filtering them
    List(ListArgs),
    /// Open a new issue
    Open {
        /// The title of the issue
        #[arg(long, short)]
        title: Option<Title>,

        /// The description of the issue
        #[arg(long, short)]
        description: Option<String>,

        /// A set of labels to associate with the issue
        #[arg(long)]
        labels: Vec<Label>,

        /// A set of DIDs to assign to the issue
        #[arg(value_name = "DID")]
        #[arg(long)]
        assignees: Vec<Did>,
    },
    /// Add a reaction emoji to an issue or comment
    React {
        /// ID of the issue
        #[arg(value_name = "ISSUE_ID")]
        id: Rev,

        /// The emoji reaction
        #[arg(long = "emoji")]
        #[arg(value_name = "CHAR")]
        reaction: Option<Reaction>,

        /// Optionally react to a comment
        #[arg(long = "to")]
        #[arg(value_name = "COMMENT_ID")]
        comment_id: Option<Rev>,
    },
    /// Show a specific issue
    Show {
        /// ID of the issue
        #[arg(value_name = "ISSUE_ID")]
        id: Rev,
    },
    /// Transition the state of an issue
    State {
        /// ID of the issue
        #[arg(value_name = "ISSUE_ID")]
        id: Rev,

        /// The desired target state
        #[clap(flatten)]
        target_state: StateArgs,
    },
}

impl Command {
    /// Returns `true` if the changes made by the command should announce to the
    /// network.
    pub(crate) fn should_announce_for(&self) -> bool {
        match self {
            Command::Open { .. }
            | Command::React { .. }
            | Command::State { .. }
            | Command::Delete { .. }
            | Command::Assign { .. }
            | Command::Label { .. }
            // Special handling for `--edit` will be removed in the future.
            | Command::Edit { .. } => true,
            Command::Comment(args) => !args.is_edit(),
            Command::Cache{..} | Command::Show { .. } | Command::List(_) => false,
        }
    }
}

/// Arguments for the empty subcommand.
#[derive(Parser, Debug, Default)]
pub(crate) struct EmptyArgs {
    #[arg(long, name = "DID")]
    #[arg(default_missing_value = "me")]
    #[arg(num_args = 0..=1)]
    #[arg(hide = true)]
    pub(crate) assigned: Option<Assigned>,

    #[clap(flatten)]
    pub(crate) state: EmptyStateArgs,
}

/// Counterpart to [`ListStateArgs`] for the empty subcommand.
#[derive(Parser, Debug, Default)]
#[group(multiple = false)]
pub(crate) struct EmptyStateArgs {
    #[arg(long, hide = true)]
    all: bool,

    #[arg(long, hide = true)]
    open: bool,

    #[arg(long, hide = true)]
    closed: bool,

    #[arg(long, hide = true)]
    solved: bool,
}

/// Arguments for the [`Command::List`] subcommand.
#[derive(Parser, Debug, Default)]
pub(crate) struct ListArgs {
    /// Filter for the list of issues that are assigned to '<DID>' (default: me)
    #[arg(long, name = "DID")]
    #[arg(default_missing_value = "me")]
    #[arg(num_args = 0..=1)]
    pub(crate) assigned: Option<Assigned>,

    #[clap(flatten)]
    pub(crate) state: ListStateArgs,
}

#[derive(Parser, Debug, Default)]
#[group(multiple = false)]
pub(crate) struct ListStateArgs {
    /// List all issues
    #[arg(long)]
    all: bool,

    /// List only open issues (default)
    #[arg(long)]
    open: bool,

    /// List only closed issues
    #[arg(long)]
    closed: bool,

    /// List only solved issues
    #[arg(long)]
    solved: bool,
}

impl From<&ListStateArgs> for Option<State> {
    fn from(args: &ListStateArgs) -> Self {
        match (args.all, args.open, args.closed, args.solved) {
            (true, false, false, false) => None,
            (false, true, false, false) | (false, false, false, false) => Some(State::Open),
            (false, false, true, false) => Some(State::Closed {
                reason: CloseReason::Other,
            }),
            (false, false, false, true) => Some(State::Closed {
                reason: CloseReason::Solved,
            }),
            _ => unreachable!(),
        }
    }
}

impl From<EmptyStateArgs> for ListStateArgs {
    fn from(args: EmptyStateArgs) -> Self {
        Self {
            all: args.all,
            open: args.open,
            closed: args.closed,
            solved: args.solved,
        }
    }
}

impl From<EmptyArgs> for ListArgs {
    fn from(args: EmptyArgs) -> Self {
        Self {
            assigned: args.assigned,
            state: ListStateArgs::from(args.state),
        }
    }
}

/// Arguments for the [`Command::Comment`] subcommand.
#[derive(Parser, Debug)]
pub(crate) struct CommentArgs {
    /// ID of the issue
    #[arg(value_name = "ISSUE_ID")]
    id: Rev,

    /// The body of the comment
    #[arg(long, short)]
    #[arg(value_name = "MESSAGE")]
    message: Option<Message>,

    /// Optionally, the comment to reply to. If not specified, the comment
    /// will be in reply to the issue itself
    #[arg(long, value_name = "COMMENT_ID")]
    #[arg(conflicts_with = "edit")]
    reply_to: Option<Rev>,

    /// Edit a comment by specifying its ID
    #[arg(long, value_name = "COMMENT_ID")]
    #[arg(conflicts_with = "reply_to")]
    edit: Option<Rev>,
}

impl CommentArgs {
    // TODO(finto): this is only needed to avoid announcing edits for the time
    // being
    /// If the comment is editing an existing comment
    pub(crate) fn is_edit(&self) -> bool {
        self.edit.is_some()
    }
}

/// Arguments for the [`Command::State`] subcommand.
#[derive(Parser, Debug)]
#[group(required = true, multiple = false)]
pub(crate) struct StateArgs {
    /// Change the state to 'open'
    #[arg(long)]
    pub(crate) open: bool,

    /// Change the state to 'closed'
    #[arg(long)]
    pub(crate) closed: bool,

    /// Change the state to 'solved'
    #[arg(long)]
    pub(crate) solved: bool,
}

impl From<StateArgs> for StateArg {
    fn from(state: StateArgs) -> Self {
        // These are mutually exclusive, guaranteed by clap grouping
        match (state.open, state.closed, state.solved) {
            (true, _, _) => StateArg::Open,
            (_, true, _) => StateArg::Closed,
            (_, _, true) => StateArg::Solved,
            _ => unreachable!(),
        }
    }
}

/// Argument value for transition an issue to the given [`State`].
#[derive(Clone, Copy, Debug)]
pub(crate) enum StateArg {
    /// Open issues.
    /// Maps to [`State::Open`].
    Open,
    /// Closed issues.
    /// Maps to [`State::Closed`] and [`CloseReason::Other`].
    Closed,
    /// Solved issues.
    /// Maps to [`State::Closed`] and [`CloseReason::Solved`].
    Solved,
}

impl From<StateArg> for State {
    fn from(value: StateArg) -> Self {
        match value {
            StateArg::Open => Self::Open,
            StateArg::Closed => Self::Closed {
                reason: CloseReason::Other,
            },
            StateArg::Solved => Self::Closed {
                reason: CloseReason::Solved,
            },
        }
    }
}

impl FromStr for Assigned {
    type Err = DidError;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        if s == "me" {
            Ok(Assigned::Me)
        } else {
            let value = s.parse::<Did>()?;
            Ok(Assigned::Peer(value))
        }
    }
}

/// The action that should be performed based on the supplied [`CommentArgs`].
pub(crate) enum CommentAction {
    /// Comment to the main issue thread.
    Comment {
        /// ID of the issue
        id: Rev,
        /// The message of the comment.
        message: Message,
    },
    /// Reply to a specific comment in the issue.
    Reply {
        /// ID of the issue
        id: Rev,
        /// The message that is being used to reply to the comment.
        message: Message,
        /// The comment ID that is being replied to.
        reply_to: Rev,
    },
    /// Edit a specific comment in the issue.
    Edit {
        /// ID of the issue
        id: Rev,
        /// The message that is being used to edit the comment.
        message: Message,
        /// The comment ID that is being edited.
        to_edit: Rev,
    },
}

impl From<CommentArgs> for CommentAction {
    fn from(
        CommentArgs {
            id,
            message,
            reply_to,
            edit,
        }: CommentArgs,
    ) -> Self {
        let message = message.unwrap_or(Message::Edit);
        match (reply_to, edit) {
            (Some(_), Some(_)) => {
                unreachable!("the argument '--reply-to' cannot be used with '--edit'")
            }
            (Some(reply_to), None) => Self::Reply {
                id,
                message,
                reply_to,
            },
            (None, Some(to_edit)) => Self::Edit {
                id,
                message,
                to_edit,
            },
            (None, None) => Self::Comment { id, message },
        }
    }
}