gitbucket-cli 0.9.0

A command-line tool for GitBucket, inspired by GitHub CLI
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
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
use clap::{Args, Subcommand};
use colored::Colorize;
use dialoguer::{Confirm, Input};

use crate::api::label::validate_label_segment;
use crate::cli::common::{create_web_session, RepoContext};
use crate::error::{GbError, Result};
use crate::models::label::{CreateLabel, UpdateLabel};
use crate::output;
use crate::output::table::print_table;
use crate::output::truncate;

#[derive(Args)]
pub struct LabelArgs {
    #[command(subcommand)]
    pub command: LabelCommand,
}

struct LabelEditRequest {
    new_name: Option<String>,
    color: Option<String>,
    description: Option<String>,
    remove_description: bool,
    web: bool,
}

#[derive(Subcommand)]
pub enum LabelCommand {
    /// List labels
    List {
        /// Output as JSON
        #[arg(long)]
        json: bool,
    },
    /// View a label
    View {
        /// Label name
        name: String,
    },
    /// Create a new label
    Create {
        /// Label name (prompts when omitted)
        name: Option<String>,
        /// Label color as 6-digit hex, with or without '#'
        #[arg(long, short)]
        color: Option<String>,
        /// Label description (unsupported on verified GitBucket versions)
        #[arg(long, short)]
        description: Option<String>,
    },
    /// Edit a label
    #[command(after_long_help = "Same-name color changes may need --web on GitBucket 4.44.")]
    Edit {
        /// Current label name
        name: String,
        /// New label name
        #[arg(long = "name")]
        new_name: Option<String>,
        /// New label color as 6-digit hex, with or without '#'
        #[arg(long, short)]
        color: Option<String>,
        /// Label description (unsupported on verified GitBucket versions)
        #[arg(long, short, conflicts_with = "remove_description")]
        description: Option<String>,
        /// Permit a verified Web color update if same-name REST PATCH is rejected
        #[arg(long)]
        web: bool,
        /// Clear the label description (unsupported on verified GitBucket versions)
        #[arg(long)]
        remove_description: bool,
    },
    /// Delete a label
    Delete {
        /// Label name
        name: String,
        /// Skip confirmation
        #[arg(long)]
        yes: bool,
    },
}

pub async fn run(
    args: LabelArgs,
    cli_hostname: &Option<String>,
    cli_repo: &Option<String>,
    cli_profile: &Option<String>,
) -> Result<()> {
    match args.command {
        LabelCommand::List { json } => list(cli_hostname, cli_repo, cli_profile, json).await,
        LabelCommand::View { name } => view(cli_hostname, cli_repo, cli_profile, &name).await,
        LabelCommand::Create {
            name,
            color,
            description,
        } => {
            create(
                cli_hostname,
                cli_repo,
                cli_profile,
                name,
                color,
                description,
            )
            .await
        }
        LabelCommand::Edit {
            name,
            new_name,
            color,
            description,
            remove_description,
            web,
        } => {
            edit(
                cli_hostname,
                cli_repo,
                cli_profile,
                &name,
                LabelEditRequest {
                    new_name,
                    color,
                    description,
                    remove_description,
                    web,
                },
            )
            .await
        }
        LabelCommand::Delete { name, yes } => {
            delete(cli_hostname, cli_repo, cli_profile, &name, yes).await
        }
    }
}

async fn list(
    hostname: &Option<String>,
    cli_repo: &Option<String>,
    cli_profile: &Option<String>,
    json: bool,
) -> Result<()> {
    let ctx = RepoContext::resolve(hostname, cli_repo, cli_profile)?;
    let labels = ctx.client.list_labels(&ctx.owner, &ctx.repo).await?;

    if json {
        return output::print_json(&labels);
    }

    let rows: Vec<Vec<String>> = labels
        .iter()
        .map(|label| {
            vec![
                label.name.clone(),
                label.color.as_deref().map(format_color).unwrap_or_default(),
                truncate(label.description.as_deref().unwrap_or(""), 60),
            ]
        })
        .collect();
    print_table(&["NAME", "COLOR", "DESCRIPTION"], &rows);
    Ok(())
}

async fn view(
    hostname: &Option<String>,
    cli_repo: &Option<String>,
    cli_profile: &Option<String>,
    name: &str,
) -> Result<()> {
    let ctx = RepoContext::resolve(hostname, cli_repo, cli_profile)?;
    let label = ctx.client.get_label(&ctx.owner, &ctx.repo, name).await?;

    println!("{}", label.name.bold());
    if let Some(color) = label.color.as_deref() {
        println!("Color: {}", format_color(color).dimmed());
    }
    if let Some(description) = label.description.as_deref() {
        if !description.is_empty() {
            println!("Description: {}", description);
        }
    }
    if let Some(url) = label.url.as_deref() {
        println!("API URL: {}", url);
    }

    Ok(())
}

async fn create(
    hostname: &Option<String>,
    cli_repo: &Option<String>,
    cli_profile: &Option<String>,
    name: Option<String>,
    color: Option<String>,
    description: Option<String>,
) -> Result<()> {
    if description.is_some() {
        return Err(description_unsupported());
    }
    let ctx = RepoContext::resolve(hostname, cli_repo, cli_profile)?;

    let name = match name {
        Some(name) => name,
        None => {
            crate::cancel::prompt(|| Input::new().with_prompt("Label name").interact_text()).await?
        }
    };

    validate_label_name(&name)?;
    let raw_color = match color {
        Some(color) => color,
        None => {
            crate::cancel::prompt(|| {
                Input::new()
                    .with_prompt("Label color (6-digit hex)")
                    .interact_text()
            })
            .await?
        }
    };
    let color = normalize_label_color(&raw_color)?;

    let label = ctx
        .client
        .create_label(
            &ctx.owner,
            &ctx.repo,
            &CreateLabel {
                name: name.clone(),
                color: color.clone(),
            },
        )
        .await?;

    verify_label(&label, &name, &color, None)?;
    let stored = ctx
        .client
        .get_label(&ctx.owner, &ctx.repo, &name)
        .await
        .map_err(|err| after_submission(err, "Label create"))?;
    verify_label(&stored, &name, &color, None)?;
    println!("✓ Created label {}", label.name);
    if let Some(color) = label.color.as_deref() {
        println!("Color: {}", format_color(color));
    }
    Ok(())
}

async fn edit(
    hostname: &Option<String>,
    cli_repo: &Option<String>,
    cli_profile: &Option<String>,
    name: &str,
    request: LabelEditRequest,
) -> Result<()> {
    if request.new_name.is_none()
        && request.color.is_none()
        && request.description.is_none()
        && !request.remove_description
    {
        return Err(GbError::Other(
            "No label changes requested. Pass at least one of --name, --color, --description, or --remove-description."
                .into(),
        ));
    }

    if request.description.is_some() || request.remove_description {
        return Err(description_unsupported());
    }
    validate_label_segment(name)?;
    if let Some(new_name) = &request.new_name {
        validate_label_name(new_name)?;
    }
    let requested_color = request
        .color
        .as_deref()
        .map(normalize_label_color)
        .transpose()?;
    let ctx = RepoContext::resolve(hostname, cli_repo, cli_profile)?;
    let current = ctx.client.get_label(&ctx.owner, &ctx.repo, name).await?;
    if current.name != name {
        return Err(GbError::Other(
            "Label lookup returned a different name. No changes were applied.".into(),
        ));
    }
    let old_color = current
        .color
        .as_deref()
        .map(normalize_label_color)
        .transpose()?;
    let new_name = request.new_name.unwrap_or_else(|| current.name.clone());
    let color = requested_color
        .or_else(|| old_color.clone())
        .ok_or_else(|| {
            GbError::Other(
                "Cannot preserve an unknown label color. Specify --color explicitly.".into(),
            )
        })?;
    let label = if new_name == current.name && old_color.as_deref() == Some(&color) {
        current.clone()
    } else {
        let response = ctx
            .client
            .update_label(
                &ctx.owner,
                &ctx.repo,
                name,
                &UpdateLabel {
                    name: new_name.clone(),
                    color: color.clone(),
                    description: current.description.clone(),
                },
            )
            .await;
        let mut stored_after_web = None;
        match response {
            Ok(label) => {
                verify_label(&label, &new_name, &color, current.description.as_deref())?;
            }
            Err(GbError::Api { status: 422, .. }) if new_name == current.name => {
                if !request.web {
                    return Err(GbError::Other("The same-name REST label update was rejected (GitBucket 4.44 limitation). Re-run with --web to permit an in-place color update; the label will not be deleted or recreated.".into()));
                }
                validate_web_label(&current)?;
                let old_color = old_color.as_deref().ok_or_else(|| GbError::Other("Cannot verify the Web form without the current label color. No Web mutation was attempted.".into()))?;
                eprintln!("Notice: Same-name REST label update was rejected; using the verified Web color route in place.");
                let mut progress = crate::operation::Progress::default();
                let step = progress.plan("label.color", crate::operation::Route::Web);
                let session = create_web_session(&ctx.client, &ctx.hostname, cli_profile)
                    .await
                    .map_err(|err| progress.error(err))?;
                progress.actor(session.principal());
                let target = session
                    .prepare_label_color(&ctx.owner, &ctx.repo, name, old_color)
                    .await
                    .map_err(|err| progress.error(err))?;
                stored_after_web = Some(
                    progress
                        .run(
                            step,
                            session.update_label_color(&target, &color),
                            |_| async {
                                let stored = ctx
                                    .client
                                    .get_label(&ctx.owner, &ctx.repo, &new_name)
                                    .await?;
                                verify_label(
                                    &stored,
                                    &new_name,
                                    &color,
                                    current.description.as_deref(),
                                )?;
                                Ok(stored)
                            },
                        )
                        .await?,
                );
            }
            Err(err) => return Err(err),
        }
        let stored = if let Some(stored) = stored_after_web {
            stored
        } else {
            ctx.client
                .get_label(&ctx.owner, &ctx.repo, &new_name)
                .await
                .map_err(|err| after_submission(err, "Label update"))?
        };
        verify_label(&stored, &new_name, &color, current.description.as_deref())?;
        stored
    };

    println!("✓ Updated label {}", label.name);
    if let Some(color) = label.color.as_deref() {
        println!("Color: {}", format_color(color));
    }
    if let Some(description) = label.description.as_deref() {
        if !description.is_empty() {
            println!("Description: {}", description);
        }
    }
    Ok(())
}

async fn delete(
    hostname: &Option<String>,
    cli_repo: &Option<String>,
    cli_profile: &Option<String>,
    name: &str,
    yes: bool,
) -> Result<()> {
    let ctx = RepoContext::resolve(hostname, cli_repo, cli_profile)?;

    if !yes {
        let prompt = format!(
            "Are you sure you want to delete label '{}' from {}/{}?",
            name, ctx.owner, ctx.repo
        );
        let confirmed = crate::cancel::prompt(move || {
            Confirm::new().with_prompt(prompt).default(false).interact()
        })
        .await?;
        if !confirmed {
            println!("Aborted.");
            return Ok(());
        }
    }

    ctx.client.delete_label(&ctx.owner, &ctx.repo, name).await?;
    println!("✓ Deleted label {}", name);
    Ok(())
}

fn description_unsupported() -> GbError {
    GbError::Other("Label description writes are unsupported by the verified GitBucket 4.44 API and Web contracts; this CLI cannot confirm support on other instances. No changes were applied.".into())
}

fn validate_label_name(name: &str) -> Result<()> {
    validate_label_segment(name)?;
    if name.trim_matches(|ch| ch <= '\u{20}').is_empty()
        || name.encode_utf16().count() > 100
        || name.starts_with(['_', '-'])
    {
        return Err(GbError::Other("Label names must be nonblank, at most 100 UTF-16 units, and cannot start with '_' or '-'.".into()));
    }
    Ok(())
}

fn validate_web_label(current: &crate::models::label::Label) -> Result<()> {
    validate_label_name(&current.name)?;
    if current.name.trim_matches(|ch| ch <= '\u{20}') != current.name
        || current.name.contains(',')
        || current
            .description
            .as_deref()
            .is_some_and(|value| !value.is_empty())
    {
        return Err(GbError::Other("The verified Web label form cannot preserve this name or description. No Web mutation was attempted; use a server-side REST fix.".into()));
    }
    Ok(())
}

fn after_submission(err: GbError, action: &str) -> GbError {
    GbError::Other(format!(
        "{action} was submitted, but verification failed: {err}. Inspect labels before retrying."
    ))
}

fn verify_label(
    label: &crate::models::label::Label,
    name: &str,
    color: &str,
    description: Option<&str>,
) -> Result<()> {
    if label.name != name
        || label
            .color
            .as_deref()
            .map(normalize_label_color)
            .transpose()?
            .as_deref()
            != Some(color)
        || label.description.as_deref().unwrap_or_default() != description.unwrap_or_default()
    {
        return Err(GbError::Other("Label mutation was submitted, but stored name/color/description does not match. Inspect labels and Issue associations before retrying.".into()));
    }
    Ok(())
}

fn normalize_label_color(value: &str) -> Result<String> {
    let trimmed = value.trim().trim_start_matches('#');
    if trimmed.len() != 6 || !trimmed.chars().all(|ch| ch.is_ascii_hexdigit()) {
        return Err(GbError::Other(format!(
            "Invalid label color '{}'. Expected a 6-digit hex value like ff0000.",
            value
        )));
    }
    Ok(trimmed.to_ascii_lowercase())
}

fn format_color(color: &str) -> String {
    format!("#{}", color.trim_start_matches('#'))
}

#[cfg(test)]
mod tests {
    use super::{normalize_label_color, validate_web_label};

    #[test]
    fn web_label_validation_preserves_non_ascii_spaces_like_java_trim() {
        let label =
            serde_json::from_value(serde_json::json!({"name":"\u{a0}", "color":"123abc"})).unwrap();
        validate_web_label(&label).unwrap();
    }

    #[test]
    fn normalize_label_color_accepts_hash_prefix() {
        assert_eq!(normalize_label_color("#A1B2C3").unwrap(), "a1b2c3");
    }

    #[test]
    fn normalize_label_color_rejects_invalid_values() {
        assert!(normalize_label_color("zzz").is_err());
        assert!(normalize_label_color("12345").is_err());
    }
}