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
use clap::{Args, Subcommand};
use serde_json::json;
use crate::client::LinearClient;
#[derive(Args, Debug)]
pub struct CyclesArgs {
#[command(subcommand)]
pub command: CyclesCommand,
}
#[derive(Subcommand, Debug)]
pub enum CyclesCommand {
/// List cycles for a team
List {
/// Team key or name
#[arg(long)]
team: String,
/// Filter type: current, previous, next, all
#[arg(long, default_value = "all")]
r#type: String,
},
/// Get cycle details
Get {
/// Cycle ID
cycle_id: String,
},
/// List issues in the active cycle
Issues {
/// Team key or name
#[arg(long)]
team: String,
},
/// Create a cycle
Create {
/// Team key or name
#[arg(long)]
team: String,
/// Cycle name
#[arg(long)]
name: Option<String>,
/// Start date (YYYY-MM-DD)
#[arg(long)]
start: String,
/// End date (YYYY-MM-DD)
#[arg(long)]
end: String,
},
/// Update a cycle
Update {
/// Cycle ID
cycle_id: String,
/// New name
#[arg(long)]
name: Option<String>,
/// New start date
#[arg(long)]
start: Option<String>,
/// New end date
#[arg(long)]
end: Option<String>,
},
/// Add an issue to the active cycle
Add {
/// Issue identifier (e.g., ENG-123)
issue_id: String,
/// Team key or name
#[arg(long)]
team: String,
},
/// Remove an issue from its cycle
Remove {
/// Issue identifier (e.g., ENG-123)
issue_id: String,
},
/// Archive a cycle
Archive {
/// Cycle ID
cycle_id: String,
},
}
pub async fn execute(args: &CyclesArgs, json: bool, debug: bool) -> anyhow::Result<()> {
let client = LinearClient::new(None, debug)?;
match &args.command {
CyclesCommand::List { team, r#type } => {
let team_id = client.get_team_id(team).await?;
let query = r#"
query($teamId: String!) {
team(id: $teamId) {
cycles(first: 50, orderBy: updatedAt) {
nodes {
id name number
startsAt endsAt
completedAt
}
}
activeCycle { id }
}
}
"#;
let variables = json!({ "teamId": team_id });
let result = client.query_raw(query, Some(variables)).await?;
if json {
crate::output::print_json(&result);
} else {
let active_id = result
.pointer("/data/team/activeCycle/id")
.and_then(|v| v.as_str())
.unwrap_or("");
let nodes = result
.pointer("/data/team/cycles/nodes")
.and_then(|v| v.as_array());
match nodes {
Some(cycles) if !cycles.is_empty() => {
let filtered: Vec<&serde_json::Value> = cycles
.iter()
.filter(|c| {
let id = c.get("id").and_then(|v| v.as_str()).unwrap_or("");
let is_active = id == active_id;
let completed = c.get("completedAt").is_some_and(|v| !v.is_null());
match r#type.as_str() {
"current" => is_active,
"previous" => completed,
"next" => !is_active && !completed,
_ => true,
}
})
.collect();
if filtered.is_empty() {
println!(" No cycles found.");
} else {
let rows: Vec<Vec<String>> = filtered
.iter()
.map(|c| {
let id = c.get("id").and_then(|v| v.as_str()).unwrap_or("-");
let name_or_number = c
.get("name")
.and_then(|v| v.as_str())
.map(|s| s.to_string())
.unwrap_or_else(|| {
c.get("number")
.and_then(|v| v.as_i64())
.map(|n| format!("Cycle {n}"))
.unwrap_or_else(|| "-".to_string())
});
let is_active = id == active_id;
let status = if is_active {
"Active".to_string()
} else if c.get("completedAt").is_some_and(|v| !v.is_null()) {
"Completed".to_string()
} else {
"Upcoming".to_string()
};
let start = c
.get("startsAt")
.and_then(|v| v.as_str())
.map(|s| s.chars().take(10).collect())
.unwrap_or_else(|| "-".to_string());
let end = c
.get("endsAt")
.and_then(|v| v.as_str())
.map(|s| s.chars().take(10).collect())
.unwrap_or_else(|| "-".to_string());
vec![name_or_number, status, start, end]
})
.collect();
crate::output::table::print_table(
&["Name", "Status", "Start", "End"],
&rows,
);
}
}
_ => println!(" No cycles found."),
}
}
}
CyclesCommand::Get { cycle_id } => {
let query = r#"
query($id: String!) {
cycle(id: $id) {
id name number
startsAt endsAt completedAt
team { key name }
issues {
nodes {
id identifier title
state { name }
assignee { displayName }
priority
team { key }
}
}
}
}
"#;
let variables = json!({ "id": cycle_id });
let result = client.query_raw(query, Some(variables)).await?;
if json {
crate::output::print_json(&result);
} else {
let cycle = result
.pointer("/data/cycle")
.ok_or_else(|| anyhow::anyhow!("Cycle not found: {cycle_id}"))?;
let name = cycle.get("name").and_then(|v| v.as_str()).unwrap_or({
// fallback handled below
"Cycle"
});
let number = cycle.get("number").and_then(|v| v.as_i64()).unwrap_or(0);
let display = if name == "Cycle" {
format!("Cycle {number}")
} else {
name.to_string()
};
println!("\n {}", crate::output::color::bold(&display));
println!();
if let Some(team_key) = cycle.pointer("/team/key").and_then(|v| v.as_str()) {
crate::output::detail::print_detail("Team", team_key, 0);
}
if let Some(start) = cycle.get("startsAt").and_then(|v| v.as_str()) {
crate::output::detail::print_detail("Start", &start[..10.min(start.len())], 0);
}
if let Some(end) = cycle.get("endsAt").and_then(|v| v.as_str()) {
crate::output::detail::print_detail("End", &end[..10.min(end.len())], 0);
}
if let Some(issues) = cycle.pointer("/issues/nodes").and_then(|v| v.as_array())
&& !issues.is_empty()
{
crate::output::detail::print_section("Issues");
for issue in issues {
crate::output::detail::print_issue_summary(issue);
}
}
}
}
CyclesCommand::Issues { team } => {
let team_id = client.get_team_id(team).await?;
let query = r#"
query($teamId: String!) {
team(id: $teamId) {
activeCycle {
id name number
issues {
nodes {
id identifier title
state { name }
assignee { displayName }
priority
team { key }
}
}
}
}
}
"#;
let variables = json!({ "teamId": team_id });
let result = client.query_raw(query, Some(variables)).await?;
if json {
crate::output::print_json(&result);
} else {
let nodes = result
.pointer("/data/team/activeCycle/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 in active cycle."),
}
}
}
CyclesCommand::Create {
team,
name,
start,
end,
} => {
let team_id = client.get_team_id(team).await?;
let query = r#"
mutation($input: CycleCreateInput!) {
cycleCreate(input: $input) {
success
cycle { id name number }
}
}
"#;
let mut input = json!({
"teamId": team_id,
"startsAt": start,
"endsAt": end,
});
if let Some(n) = name {
input["name"] = json!(n);
}
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/cycleCreate/success")
.and_then(|v| v.as_bool())
.unwrap_or(false);
if success {
println!(" {} Created cycle", crate::output::color::green("OK"),);
} else {
println!(
" {} Failed to create cycle",
crate::output::color::red("ERROR")
);
}
}
}
CyclesCommand::Update {
cycle_id,
name,
start,
end,
} => {
let query = r#"
mutation($id: String!, $input: CycleUpdateInput!) {
cycleUpdate(id: $id, input: $input) {
success
cycle { id name number }
}
}
"#;
let mut input = json!({});
if let Some(n) = name {
input["name"] = json!(n);
}
if let Some(s) = start {
input["startsAt"] = json!(s);
}
if let Some(e) = end {
input["endsAt"] = json!(e);
}
let variables = json!({ "id": cycle_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/cycleUpdate/success")
.and_then(|v| v.as_bool())
.unwrap_or(false);
if success {
println!(
" {} Updated cycle {}",
crate::output::color::green("OK"),
crate::output::color::bold(cycle_id),
);
} else {
println!(
" {} Failed to update cycle",
crate::output::color::red("ERROR")
);
}
}
}
CyclesCommand::Add { issue_id, team } => {
let team_id = client.get_team_id(team).await?;
// Get the active cycle ID
let cycle_query = r#"
query($teamId: String!) {
team(id: $teamId) {
activeCycle { id }
}
}
"#;
let cycle_result = client
.query_raw(cycle_query, Some(json!({ "teamId": team_id })))
.await?;
let cycle_id = cycle_result
.pointer("/data/team/activeCycle/id")
.and_then(|v| v.as_str())
.ok_or_else(|| anyhow::anyhow!("No active cycle for team {team}"))?;
let query = r#"
mutation($id: String!, $input: IssueUpdateInput!) {
issueUpdate(id: $id, input: $input) {
success
issue { id identifier }
}
}
"#;
let variables = json!({
"id": issue_id,
"input": { "cycleId": cycle_id },
});
let result = client.query_raw(query, Some(variables)).await?;
if json {
crate::output::print_json(&result);
} else {
let success = result
.pointer("/data/issueUpdate/success")
.and_then(|v| v.as_bool())
.unwrap_or(false);
if success {
println!(
" {} Added {} to active cycle",
crate::output::color::green("OK"),
crate::output::color::bold(issue_id),
);
} else {
println!(
" {} Failed to add issue to cycle",
crate::output::color::red("ERROR")
);
}
}
}
CyclesCommand::Remove { issue_id } => {
let query = r#"
mutation($id: String!, $input: IssueUpdateInput!) {
issueUpdate(id: $id, input: $input) {
success
issue { id identifier }
}
}
"#;
let variables = json!({
"id": issue_id,
"input": { "cycleId": null },
});
let result = client.query_raw(query, Some(variables)).await?;
if json {
crate::output::print_json(&result);
} else {
let success = result
.pointer("/data/issueUpdate/success")
.and_then(|v| v.as_bool())
.unwrap_or(false);
if success {
println!(
" {} Removed {} from cycle",
crate::output::color::green("OK"),
crate::output::color::bold(issue_id),
);
} else {
println!(
" {} Failed to remove issue from cycle",
crate::output::color::red("ERROR")
);
}
}
}
CyclesCommand::Archive { cycle_id } => {
let query = r#"
mutation($id: String!) {
cycleArchive(id: $id) {
success
}
}
"#;
let variables = json!({ "id": cycle_id });
let result = client.query_raw(query, Some(variables)).await?;
if json {
crate::output::print_json(&result);
} else {
let success = result
.pointer("/data/cycleArchive/success")
.and_then(|v| v.as_bool())
.unwrap_or(false);
if success {
println!(
" {} Archived cycle {}",
crate::output::color::green("OK"),
crate::output::color::bold(cycle_id),
);
} else {
println!(
" {} Failed to archive cycle",
crate::output::color::red("ERROR")
);
}
}
}
}
Ok(())
}