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
use clap::{Args, Subcommand};
use serde_json::json;
use crate::client::LinearClient;
#[derive(Args, Debug)]
pub struct ViewsArgs {
#[command(subcommand)]
pub command: ViewsCommand,
}
#[derive(Subcommand, Debug)]
pub enum ViewsCommand {
/// List custom views
List {
/// Filter by team key
#[arg(long)]
team: Option<String>,
/// Max results
#[arg(long, default_value = "50")]
limit: i32,
},
/// Get view details
Get {
/// View ID
view_id: String,
},
/// Create a custom view
Create {
/// View name
#[arg(long)]
name: String,
/// Description
#[arg(long)]
description: Option<String>,
/// Team key
#[arg(long)]
team: Option<String>,
/// Make shared (team/org visible)
#[arg(long)]
shared: bool,
/// Make personal (only you)
#[arg(long)]
personal: bool,
/// Filter JSON string
#[arg(long)]
filter: Option<String>,
/// Icon name
#[arg(long)]
icon: Option<String>,
},
/// Update a custom view
Update {
/// View ID
view_id: String,
/// New name
#[arg(long)]
name: Option<String>,
/// New description
#[arg(long)]
description: Option<String>,
/// Make shared
#[arg(long)]
shared: bool,
/// Make personal
#[arg(long)]
personal: bool,
/// New filter JSON string
#[arg(long)]
filter: Option<String>,
/// New icon
#[arg(long)]
icon: Option<String>,
},
/// Delete a custom view
Delete {
/// View ID
view_id: String,
},
/// List issues matching a view's filters
Issues {
/// View ID
view_id: String,
/// Max results
#[arg(long, default_value = "50")]
limit: i32,
},
}
/// Resolve a view identifier to a UUID. If it already looks like a UUID, return it as-is.
/// Otherwise, list all custom views and fuzzy-match by name (case-insensitive substring).
async fn resolve_view_id(client: &LinearClient, id_or_name: &str) -> anyhow::Result<String> {
if crate::util::looks_like_uuid(id_or_name) {
return Ok(id_or_name.to_string());
}
// Not a UUID — search by name
let query = r#"
query {
customViews(first: 250) {
nodes { id name }
}
}
"#;
let result = client.query_raw(query, None).await?;
let nodes = result
.pointer("/data/customViews/nodes")
.and_then(|v| v.as_array())
.ok_or_else(|| anyhow::anyhow!("Could not fetch custom views"))?;
let needle = id_or_name.to_lowercase();
// Try exact match first, then substring
let found = nodes
.iter()
.find(|v| {
v.get("name")
.and_then(|n| n.as_str())
.map(|n| n.to_lowercase() == needle)
.unwrap_or(false)
})
.or_else(|| {
nodes.iter().find(|v| {
v.get("name")
.and_then(|n| n.as_str())
.map(|n| n.to_lowercase().contains(&needle))
.unwrap_or(false)
})
});
match found {
Some(view) => view
.get("id")
.and_then(|v| v.as_str())
.map(String::from)
.ok_or_else(|| anyhow::anyhow!("View matched but has no ID")),
None => Err(anyhow::anyhow!("View not found: {id_or_name}")),
}
}
pub async fn execute(
args: &ViewsArgs,
json: bool,
debug: bool,
workspace: Option<&str>,
) -> anyhow::Result<()> {
let client = LinearClient::new(None, debug, workspace)?;
match &args.command {
ViewsCommand::List { team, limit } => {
let resolved_team = match team {
Some(t) => Some(t.clone()),
None if crate::output::interactive::is_interactive() => {
let teams = client.get_teams().await?;
let items: Vec<String> = teams
.iter()
.map(|t| format!("{} ({})", t.name, t.key))
.collect();
let idx = crate::output::interactive::fuzzy_select("Select team", &items)?;
Some(teams[idx].key.clone())
}
None => None,
};
let query = r#"
query($first: Int!) {
customViews(first: $first) {
nodes {
id name description shared
team { key }
creator { displayName }
}
}
}
"#;
let variables = json!({ "first": limit });
let result = client.query_raw(query, Some(variables)).await?;
if json {
crate::output::print_json(&result);
} else {
let nodes = result
.pointer("/data/customViews/nodes")
.and_then(|v| v.as_array());
match nodes {
Some(views) if !views.is_empty() => {
let filtered: Vec<&serde_json::Value> =
if let Some(team_key) = &resolved_team {
views
.iter()
.filter(|v| {
v.pointer("/team/key")
.and_then(|k| k.as_str())
.map(|k| k.eq_ignore_ascii_case(team_key))
.unwrap_or(false)
})
.collect()
} else {
views.iter().collect()
};
if filtered.is_empty() {
println!(" No views found.");
} else {
let rows: Vec<Vec<String>> = filtered
.iter()
.map(|v| {
vec![
v.get("id")
.and_then(|x| x.as_str())
.unwrap_or("-")
.to_string(),
v.get("name")
.and_then(|x| x.as_str())
.unwrap_or("-")
.to_string(),
if v.get("shared")
.and_then(|x| x.as_bool())
.unwrap_or(false)
{
"Shared".to_string()
} else {
"Personal".to_string()
},
v.pointer("/team/key")
.and_then(|x| x.as_str())
.unwrap_or("-")
.to_string(),
v.pointer("/creator/displayName")
.and_then(|x| x.as_str())
.unwrap_or("-")
.to_string(),
]
})
.collect();
crate::output::table::print_table(
&["ID", "Name", "Visibility", "Team", "Creator"],
&rows,
);
}
}
_ => println!(" No views found."),
}
}
}
ViewsCommand::Get { view_id } => {
let view_id = resolve_view_id(&client, view_id).await?;
let query = r#"
query($id: String!) {
customView(id: $id) {
id name description shared
team { key name }
creator { displayName }
filterData
icon
createdAt updatedAt
}
}
"#;
let variables = json!({ "id": view_id });
let result = client.query_raw(query, Some(variables)).await?;
if json {
crate::output::print_json(&result);
} else {
let view = result
.pointer("/data/customView")
.ok_or_else(|| anyhow::anyhow!("View not found: {view_id}"))?;
let name = view.get("name").and_then(|v| v.as_str()).unwrap_or("?");
println!("\n {}", crate::output::color::bold(name));
println!();
if let Some(desc) = view.get("description").and_then(|v| v.as_str())
&& !desc.is_empty()
{
crate::output::detail::print_detail("Description", desc, 0);
}
let vis = if view
.get("shared")
.and_then(|v| v.as_bool())
.unwrap_or(false)
{
"Shared"
} else {
"Personal"
};
crate::output::detail::print_detail("Visibility", vis, 0);
if let Some(team_name) = view.pointer("/team/name").and_then(|v| v.as_str()) {
crate::output::detail::print_detail("Team", team_name, 0);
}
if let Some(creator) = view
.pointer("/creator/displayName")
.and_then(|v| v.as_str())
{
crate::output::detail::print_detail("Creator", creator, 0);
}
if let Some(icon) = view.get("icon").and_then(|v| v.as_str()) {
crate::output::detail::print_detail("Icon", icon, 0);
}
if let Some(filter) = view.get("filterData")
&& !filter.is_null()
{
crate::output::detail::print_section("Filter Data");
let pretty =
serde_json::to_string_pretty(filter).unwrap_or_else(|_| "?".into());
for line in pretty.lines() {
println!(" {line}");
}
}
}
}
ViewsCommand::Create {
name,
description,
team,
shared,
personal: _,
filter,
icon,
} => {
let query = r#"
mutation($input: CustomViewCreateInput!) {
customViewCreate(input: $input) {
success
customView { id name }
}
}
"#;
let mut input = json!({ "name": name });
if let Some(desc) = description {
input["description"] = json!(desc);
}
if let Some(team_key) = team {
let team_id = client.get_team_id(team_key).await?;
input["teamId"] = json!(team_id);
}
input["shared"] = json!(*shared);
if let Some(f) = filter {
let filter_val: serde_json::Value =
serde_json::from_str(f).unwrap_or_else(|_| json!(f));
input["filterData"] = filter_val;
}
if let Some(i) = icon {
input["icon"] = json!(i);
}
let variables = json!({ "input": input });
let result = client.query_raw(query, Some(variables)).await?;
if json {
crate::output::print_json(&result);
} else {
let success = result
.pointer("/data/customViewCreate/success")
.and_then(|v| v.as_bool())
.unwrap_or(false);
if success {
let view_id = result
.pointer("/data/customViewCreate/customView/id")
.and_then(|v| v.as_str())
.unwrap_or("?");
println!(
" {} Created view {} ({})",
crate::output::color::green("OK"),
crate::output::color::bold(name),
crate::output::color::dim(view_id),
);
} else {
println!(
" {} Failed to create view",
crate::output::color::red("ERROR")
);
}
}
}
ViewsCommand::Update {
view_id,
name,
description,
shared,
personal: _,
filter,
icon,
} => {
let view_id = resolve_view_id(&client, view_id).await?;
let query = r#"
mutation($id: String!, $input: CustomViewUpdateInput!) {
customViewUpdate(id: $id, input: $input) {
success
customView { id name }
}
}
"#;
let mut input = json!({});
if let Some(n) = name {
input["name"] = json!(n);
}
if let Some(desc) = description {
input["description"] = json!(desc);
}
if *shared {
input["shared"] = json!(true);
}
if let Some(f) = filter {
let filter_val: serde_json::Value =
serde_json::from_str(f).unwrap_or_else(|_| json!(f));
input["filterData"] = filter_val;
}
if let Some(i) = icon {
input["icon"] = json!(i);
}
let variables = json!({ "id": view_id, "input": input });
let result = client.query_raw(query, Some(variables)).await?;
if json {
crate::output::print_json(&result);
} else {
let success = result
.pointer("/data/customViewUpdate/success")
.and_then(|v| v.as_bool())
.unwrap_or(false);
if success {
println!(
" {} Updated view {}",
crate::output::color::green("OK"),
crate::output::color::bold(&view_id),
);
} else {
println!(
" {} Failed to update view",
crate::output::color::red("ERROR")
);
}
}
}
ViewsCommand::Delete { view_id } => {
let view_id = resolve_view_id(&client, view_id).await?;
if crate::output::interactive::is_interactive()
&& !crate::output::interactive::confirm(&format!("Delete view {}?", view_id))?
{
println!("Cancelled.");
return Ok(());
}
let query = r#"
mutation($id: String!) {
customViewDelete(id: $id) {
success
}
}
"#;
let variables = json!({ "id": view_id });
let result = client.query_raw(query, Some(variables)).await?;
if json {
crate::output::print_json(&result);
} else {
let success = result
.pointer("/data/customViewDelete/success")
.and_then(|v| v.as_bool())
.unwrap_or(false);
if success {
println!(
" {} Deleted view {}",
crate::output::color::green("OK"),
crate::output::color::bold(&view_id),
);
} else {
println!(
" {} Failed to delete view",
crate::output::color::red("ERROR")
);
}
}
}
ViewsCommand::Issues { view_id, limit } => {
let view_id = resolve_view_id(&client, view_id).await?;
// Get the view's filter data, then query issues with those filters
let view_query = r#"
query($id: String!) {
customView(id: $id) {
filterData
}
}
"#;
let view_result = client
.query_raw(view_query, Some(json!({ "id": view_id })))
.await?;
let filter_data = view_result
.pointer("/data/customView/filterData")
.cloned()
.unwrap_or(json!({}));
let query = r#"
query($filter: IssueFilter, $first: Int!) {
issues(filter: $filter, first: $first, orderBy: updatedAt) {
nodes {
id identifier title
state { name }
assignee { displayName }
priority
team { key }
}
}
}
"#;
let variables = json!({ "filter": filter_data, "first": limit });
let result = client.query_raw(query, Some(variables)).await?;
if json {
crate::output::print_json(&result);
} else {
let nodes = result
.pointer("/data/issues/nodes")
.and_then(|v| v.as_array());
match nodes {
Some(issues) if !issues.is_empty() => {
for issue in issues {
crate::output::detail::print_issue_summary(issue);
}
}
_ => println!(" No issues found."),
}
}
}
}
Ok(())
}