linear-cli 0.3.22

A powerful CLI for Linear.app - manage issues, projects, cycles, and more from your terminal
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
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
use anyhow::Result;
use clap::Subcommand;
use colored::Colorize;
use serde_json::json;
use tabled::{Table, Tabled};

use crate::api::{resolve_team_id, resolve_view_id, LinearClient};
use crate::display_options;
use crate::output::{
    ensure_non_empty, filter_values, print_json, print_json_owned, sort_values, OutputOptions,
};
use crate::pagination::paginate_nodes;
use crate::text::truncate;
use crate::types::CustomView;

#[derive(Subcommand)]
pub enum ViewCommands {
    /// List custom views
    #[command(alias = "ls")]
    #[command(after_help = r#"EXAMPLES:
    linear views list                       # List all custom views
    linear v list --shared                  # List shared views only
    linear v list --team ENG                # Filter by team"#)]
    List {
        /// Filter by team name or ID
        #[arg(short, long)]
        team: Option<String>,
        /// Show only shared views
        #[arg(long)]
        shared: bool,
    },
    /// Get custom view details
    #[command(after_help = r#"EXAMPLES:
    linear views get "My View"              # Get by name
    linear v get VIEW_ID                    # Get by ID"#)]
    Get {
        /// View name or ID
        name_or_id: String,
    },
    /// Create a custom view
    #[command(after_help = r#"EXAMPLES:
    linear views create "Bug Triage"        # Create a personal view
    linear v create "Sprint View" --shared  # Create a shared view
    linear v create "Team Bugs" -t ENG --filter-json filters.json"#)]
    Create {
        /// View name
        name: String,
        /// View description
        #[arg(short, long)]
        description: Option<String>,
        /// Team name or ID to scope the view to
        #[arg(short, long)]
        team: Option<String>,
        /// Make the view shared (visible to all workspace members)
        #[arg(long)]
        shared: bool,
        /// Path to JSON file with filter data, or "-" for stdin
        #[arg(long)]
        filter_json: Option<String>,
        /// View icon
        #[arg(long)]
        icon: Option<String>,
        /// View color (hex)
        #[arg(long)]
        color: Option<String>,
    },
    /// Update a custom view
    #[command(after_help = r#"EXAMPLES:
    linear views update "My View" --name "New Name"
    linear v update VIEW_ID --shared --description "Updated""#)]
    Update {
        /// View name or ID
        name_or_id: String,
        /// New name
        #[arg(short, long)]
        name: Option<String>,
        /// New description
        #[arg(short, long)]
        description: Option<String>,
        /// Set shared visibility
        #[arg(long)]
        shared: Option<bool>,
        /// Path to JSON file with filter data, or "-" for stdin
        #[arg(long)]
        filter_json: Option<String>,
    },
    /// Delete a custom view
    #[command(after_help = r#"EXAMPLES:
    linear views delete "My View"           # Delete with confirmation
    linear v delete VIEW_ID --force         # Delete without confirmation"#)]
    Delete {
        /// View name or ID
        name_or_id: String,
        /// Skip confirmation
        #[arg(long)]
        force: bool,
    },
}

#[derive(Tabled)]
struct ViewRow {
    #[tabled(rename = "Name")]
    name: String,
    #[tabled(rename = "Shared")]
    shared: String,
    #[tabled(rename = "Owner")]
    owner: String,
    #[tabled(rename = "Team")]
    team: String,
    #[tabled(rename = "Updated")]
    updated: String,
    #[tabled(rename = "ID")]
    id: String,
}

pub async fn handle(cmd: ViewCommands, output: &OutputOptions) -> Result<()> {
    match cmd {
        ViewCommands::List { team, shared } => list_views(team, shared, output).await,
        ViewCommands::Get { name_or_id } => get_view(&name_or_id, output).await,
        ViewCommands::Create {
            name,
            description,
            team,
            shared,
            filter_json,
            icon,
            color,
        } => {
            create_view(
                &name,
                description,
                team,
                shared,
                filter_json,
                icon,
                color,
                output,
            )
            .await
        }
        ViewCommands::Update {
            name_or_id,
            name,
            description,
            shared,
            filter_json,
        } => update_view(&name_or_id, name, description, shared, filter_json, output).await,
        ViewCommands::Delete { name_or_id, force } => delete_view(&name_or_id, force, output).await,
    }
}

async fn list_views(team: Option<String>, shared_only: bool, output: &OutputOptions) -> Result<()> {
    let client = LinearClient::new()?;

    let query = r#"
        query($first: Int, $after: String) {
            customViews(first: $first, after: $after) {
                nodes {
                    id name description icon color shared slugId modelName
                    filterData projectFilterData
                    owner { id name }
                    team { id key name }
                    createdAt updatedAt
                }
                pageInfo { hasNextPage endCursor }
            }
        }
    "#;

    let vars = serde_json::Map::new();
    let pagination = output.pagination.with_default_limit(100);
    let mut views = paginate_nodes(
        &client,
        query,
        vars,
        &["data", "customViews", "nodes"],
        &["data", "customViews", "pageInfo"],
        &pagination,
        100,
    )
    .await?;

    // Filter by shared
    if shared_only {
        views.retain(|v| v["shared"].as_bool() == Some(true));
    }

    // Filter by team
    if let Some(ref team_filter) = team {
        let team_id = resolve_team_id(&client, team_filter, &output.cache).await?;
        views.retain(|v| v["team"]["id"].as_str() == Some(team_id.as_str()));
    }

    if output.is_json() || output.has_template() {
        print_json_owned(json!(views), output)?;
        return Ok(());
    }

    filter_values(&mut views, &output.filters);

    if let Some(sort_key) = output.json.sort.as_deref() {
        sort_values(&mut views, sort_key, output.json.order);
    }

    ensure_non_empty(&views, output)?;
    if views.is_empty() {
        println!("No custom views found.");
        return Ok(());
    }

    let width = display_options().max_width(30);
    let rows: Vec<ViewRow> = views
        .iter()
        .map(|v| {
            let updated = v["updatedAt"]
                .as_str()
                .unwrap_or("")
                .chars()
                .take(10)
                .collect::<String>();

            ViewRow {
                name: truncate(v["name"].as_str().unwrap_or(""), width),
                shared: if v["shared"].as_bool() == Some(true) {
                    "Yes".to_string()
                } else {
                    "No".to_string()
                },
                owner: truncate(v["owner"]["name"].as_str().unwrap_or("-"), width),
                team: truncate(v["team"]["name"].as_str().unwrap_or("-"), width),
                updated,
                id: v["id"].as_str().unwrap_or("").to_string(),
            }
        })
        .collect();

    let table = Table::new(rows).to_string();
    println!("{}", table);
    println!("\n{} views", views.len());

    Ok(())
}

async fn get_view(name_or_id: &str, output: &OutputOptions) -> Result<()> {
    let client = LinearClient::new()?;
    let view_id = resolve_view_id(&client, name_or_id, &output.cache).await?;

    let query = r#"
        query($id: String!) {
            customView(id: $id) {
                id name description icon color shared slugId modelName
                filterData projectFilterData
                owner { id name }
                team { id key name }
                createdAt updatedAt
            }
        }
    "#;

    let result = client.query(query, Some(json!({ "id": view_id }))).await?;
    let view = &result["data"]["customView"];

    if view.is_null() {
        anyhow::bail!("Custom view not found: {}", name_or_id);
    }

    if output.is_json() || output.has_template() {
        print_json(view, output)?;
        return Ok(());
    }

    let cv: CustomView = serde_json::from_value(view.clone())?;

    println!("{}", cv.name.bold());
    println!("{}", "-".repeat(40));

    if let Some(desc) = &cv.description {
        println!("Description: {}", desc);
    }

    println!("Shared: {}", if cv.shared { "Yes" } else { "No" });

    if let Some(owner) = &cv.owner {
        println!("Owner: {}", owner.name);
    }

    if let Some(team) = &cv.team {
        println!("Team: {} ({})", team.name, team.key);
    }

    if let Some(icon) = &cv.icon {
        println!("Icon: {}", icon);
    }

    if let Some(color) = &cv.color {
        println!("Color: {}", color);
    }

    if let Some(model) = &cv.model_name {
        println!("Model: {}", model);
    }

    println!("ID: {}", cv.id);

    if let Some(created) = &cv.created_at {
        println!("Created: {}", created.chars().take(10).collect::<String>());
    }

    if let Some(updated) = &cv.updated_at {
        println!("Updated: {}", updated.chars().take(10).collect::<String>());
    }

    if let Some(ref filter) = cv.filter_data {
        if !filter.is_null() {
            println!("\n{}", "Filter Data".bold());
            println!("{}", "-".repeat(40));
            println!("{}", serde_json::to_string_pretty(filter)?);
        }
    }

    if let Some(ref filter) = cv.project_filter_data {
        if !filter.is_null() {
            println!("\n{}", "Project Filter Data".bold());
            println!("{}", "-".repeat(40));
            println!("{}", serde_json::to_string_pretty(filter)?);
        }
    }

    Ok(())
}

fn read_filter_json(path: &str) -> Result<serde_json::Value> {
    let content = if path == "-" {
        use std::io::Read;
        let mut buf = String::new();
        std::io::stdin().read_to_string(&mut buf)?;
        buf
    } else {
        std::fs::read_to_string(path)?
    };
    let value: serde_json::Value = serde_json::from_str(&content)?;
    Ok(value)
}

#[allow(clippy::too_many_arguments)]
async fn create_view(
    name: &str,
    description: Option<String>,
    team: Option<String>,
    shared: bool,
    filter_json: Option<String>,
    icon: Option<String>,
    color: Option<String>,
    output: &OutputOptions,
) -> Result<()> {
    let client = LinearClient::new()?;

    let mut input = json!({
        "name": name,
        "shared": shared,
    });

    if let Some(desc) = description {
        input["description"] = json!(desc);
    }

    if let Some(ref t) = team {
        let team_id = resolve_team_id(&client, t, &output.cache).await?;
        input["teamId"] = json!(team_id);
    }

    if let Some(ref path) = filter_json {
        let filter = read_filter_json(path)?;
        input["filterData"] = filter;
    }

    if let Some(i) = icon {
        input["icon"] = json!(i);
    }

    if let Some(c) = color {
        input["color"] = json!(c);
    }

    if output.dry_run {
        if output.is_json() || output.has_template() {
            print_json_owned(
                json!({
                    "dry_run": true,
                    "would_create": { "input": input }
                }),
                output,
            )?;
        } else {
            println!("{}", "[DRY RUN] Would create custom view:".yellow().bold());
            println!("  Name: {}", name);
        }
        return Ok(());
    }

    let mutation = r#"
        mutation($input: CustomViewCreateInput!) {
            customViewCreate(input: $input) {
                success
                customView { id name shared }
            }
        }
    "#;

    let result = client
        .mutate(mutation, Some(json!({ "input": input })))
        .await?;

    if result["data"]["customViewCreate"]["success"].as_bool() == Some(true) {
        let view = &result["data"]["customViewCreate"]["customView"];
        if output.is_json() || output.has_template() {
            print_json(view, output)?;
            return Ok(());
        }
        println!(
            "{} Created custom view: {}",
            "+".green(),
            view["name"].as_str().unwrap_or("")
        );
        println!("  ID: {}", view["id"].as_str().unwrap_or(""));
        println!(
            "  Shared: {}",
            if view["shared"].as_bool() == Some(true) {
                "Yes"
            } else {
                "No"
            }
        );
    } else {
        anyhow::bail!("Failed to create custom view");
    }

    Ok(())
}

async fn update_view(
    name_or_id: &str,
    name: Option<String>,
    description: Option<String>,
    shared: Option<bool>,
    filter_json: Option<String>,
    output: &OutputOptions,
) -> Result<()> {
    let client = LinearClient::new()?;
    let view_id = resolve_view_id(&client, name_or_id, &output.cache).await?;

    let mut input = json!({});
    if let Some(n) = name {
        input["name"] = json!(n);
    }
    if let Some(d) = description {
        input["description"] = json!(d);
    }
    if let Some(s) = shared {
        input["shared"] = json!(s);
    }
    if let Some(ref path) = filter_json {
        let filter = read_filter_json(path)?;
        input["filterData"] = filter;
    }

    if input.as_object().map(|o| o.is_empty()).unwrap_or(true) {
        println!("No updates specified.");
        return Ok(());
    }

    if output.dry_run {
        if output.is_json() || output.has_template() {
            print_json_owned(
                json!({
                    "dry_run": true,
                    "would_update": {
                        "id": view_id,
                        "input": input,
                    }
                }),
                output,
            )?;
        } else {
            println!("{}", "[DRY RUN] Would update custom view:".yellow().bold());
            println!("  ID: {}", view_id);
        }
        return Ok(());
    }

    let mutation = r#"
        mutation($id: String!, $input: CustomViewUpdateInput!) {
            customViewUpdate(id: $id, input: $input) {
                success
                customView { id name }
            }
        }
    "#;

    let result = client
        .mutate(mutation, Some(json!({ "id": view_id, "input": input })))
        .await?;

    if result["data"]["customViewUpdate"]["success"].as_bool() == Some(true) {
        if output.is_json() || output.has_template() {
            print_json(&result["data"]["customViewUpdate"]["customView"], output)?;
            return Ok(());
        }
        println!("{} Custom view updated", "+".green());
    } else {
        anyhow::bail!("Failed to update custom view");
    }

    Ok(())
}

async fn delete_view(name_or_id: &str, force: bool, output: &OutputOptions) -> Result<()> {
    let client = LinearClient::new()?;
    let view_id = resolve_view_id(&client, name_or_id, &output.cache).await?;

    if output.dry_run {
        if output.is_json() || output.has_template() {
            print_json_owned(
                json!({
                    "dry_run": true,
                    "would_delete": { "id": view_id }
                }),
                output,
            )?;
        } else {
            println!("{}", "[DRY RUN] Would delete custom view:".yellow().bold());
            println!("  ID: {}", view_id);
        }
        return Ok(());
    }

    if !force && !crate::is_yes() {
        use dialoguer::Confirm;
        let confirm = Confirm::new()
            .with_prompt(format!("Delete custom view {}?", name_or_id))
            .default(false)
            .interact()?;
        if !confirm {
            println!("Cancelled.");
            return Ok(());
        }
    }

    let mutation = r#"
        mutation($id: String!) {
            customViewDelete(id: $id) {
                success
            }
        }
    "#;

    let result = client
        .mutate(mutation, Some(json!({ "id": view_id })))
        .await?;

    if result["data"]["customViewDelete"]["success"].as_bool() == Some(true) {
        if output.is_json() || output.has_template() {
            print_json_owned(json!({ "deleted": true, "id": view_id }), output)?;
            return Ok(());
        }
        println!("{} Custom view deleted: {}", "-".red(), name_or_id);
    } else {
        anyhow::bail!("Failed to delete custom view");
    }

    Ok(())
}

/// Fetch the filterData for a custom view (used by issues list --view).
pub async fn fetch_view_filter(
    client: &LinearClient,
    view_name_or_id: &str,
    cache_opts: &crate::cache::CacheOptions,
) -> Result<serde_json::Value> {
    let view_id = resolve_view_id(client, view_name_or_id, cache_opts).await?;

    let query = r#"
        query($id: String!) {
            customView(id: $id) {
                filterData
            }
        }
    "#;

    let result = client.query(query, Some(json!({ "id": view_id }))).await?;
    let filter = &result["data"]["customView"]["filterData"];

    if filter.is_null() {
        anyhow::bail!("Custom view '{}' has no filter data", view_name_or_id);
    }

    Ok(filter.clone())
}

/// Fetch the projectFilterData for a custom view (used by projects list --view).
pub async fn fetch_view_project_filter(
    client: &LinearClient,
    view_name_or_id: &str,
    cache_opts: &crate::cache::CacheOptions,
) -> Result<serde_json::Value> {
    let view_id = resolve_view_id(client, view_name_or_id, cache_opts).await?;

    let query = r#"
        query($id: String!) {
            customView(id: $id) {
                projectFilterData
            }
        }
    "#;

    let result = client.query(query, Some(json!({ "id": view_id }))).await?;
    let filter = &result["data"]["customView"]["projectFilterData"];

    if filter.is_null() {
        anyhow::bail!(
            "Custom view '{}' has no project filter data",
            view_name_or_id
        );
    }

    Ok(filter.clone())
}