linear-cli 0.1.0

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
use anyhow::Result;
use clap::Subcommand;
use colored::Colorize;
use serde_json::json;
use std::collections::HashSet;
use std::fs;
use std::path::Path;

use crate::api::{resolve_team_id, LinearClient};

/// Default directory to scan for local projects
const DEFAULT_CODE_DIR: &str = r"D:\code";

#[derive(Subcommand)]
pub enum SyncCommands {
    /// Compare local folders with Linear projects
    Status {
        /// Directory to scan for local projects (default: D:\code)
        #[arg(short, long)]
        directory: Option<String>,
        /// Show only missing projects (not in Linear)
        #[arg(short, long)]
        missing_only: bool,
    },
    /// Create Linear projects from local folders that don't exist in Linear
    Push {
        /// Directory to scan for local projects (default: D:\code)
        #[arg(short, long)]
        directory: Option<String>,
        /// Team name or ID to create projects in
        #[arg(short, long)]
        team: String,
        /// Only push specific folders (comma-separated)
        #[arg(short, long)]
        only: Option<String>,
        /// Dry run - show what would be created without creating
        #[arg(long)]
        dry_run: bool,
    },
}

/// Represents a local folder that could be a project
#[derive(Debug, Clone)]
struct LocalProject {
    name: String,
    path: String,
    has_git: bool,
}

/// Represents a Linear project for comparison
#[derive(Debug, Clone)]
struct LinearProject {
    #[allow(dead_code)]
    id: String,
    name: String,
    #[allow(dead_code)]
    url: Option<String>,
}

/// Sync status for a project
#[derive(Debug)]
enum SyncStatus {
    /// Exists in both local and Linear
    Synced { local: LocalProject, remote: LinearProject },
    /// Exists locally but not in Linear
    LocalOnly(LocalProject),
    /// Exists in Linear but not locally
    RemoteOnly(LinearProject),
}

pub async fn handle(cmd: SyncCommands) -> Result<()> {
    match cmd {
        SyncCommands::Status { directory, missing_only } => {
            status_command(directory, missing_only).await
        }
        SyncCommands::Push { directory, team, only, dry_run } => {
            push_command(directory, team, only, dry_run).await
        }
    }
}

/// Scan a directory for local project folders
fn scan_local_projects(dir: &str) -> Result<Vec<LocalProject>> {
    let path = Path::new(dir);

    if !path.exists() {
        anyhow::bail!("Directory does not exist: {}", dir);
    }

    if !path.is_dir() {
        anyhow::bail!("Path is not a directory: {}", dir);
    }

    let mut projects = Vec::new();

    for entry in fs::read_dir(path)? {
        let entry = entry?;
        let entry_path = entry.path();

        // Only consider directories
        if !entry_path.is_dir() {
            continue;
        }

        // Skip hidden directories
        let name = entry.file_name().to_string_lossy().to_string();
        if name.starts_with('.') {
            continue;
        }

        // Check if it has a .git folder
        let git_path = entry_path.join(".git");
        let has_git = git_path.exists();

        projects.push(LocalProject {
            name: name.clone(),
            path: entry_path.to_string_lossy().to_string(),
            has_git,
        });
    }

    // Sort alphabetically
    projects.sort_by(|a, b| a.name.to_lowercase().cmp(&b.name.to_lowercase()));

    Ok(projects)
}

/// Fetch all Linear projects
async fn fetch_linear_projects(client: &LinearClient) -> Result<Vec<LinearProject>> {
    let query = r#"
        query {
            projects(first: 250) {
                nodes {
                    id
                    name
                    url
                }
            }
        }
    "#;

    let result = client.query(query, None).await?;

    let projects = result["data"]["projects"]["nodes"]
        .as_array()
        .unwrap_or(&vec![])
        .iter()
        .map(|p| LinearProject {
            id: p["id"].as_str().unwrap_or("").to_string(),
            name: p["name"].as_str().unwrap_or("").to_string(),
            url: p["url"].as_str().map(|s| s.to_string()),
        })
        .collect();

    Ok(projects)
}

/// Compare local projects with Linear projects
fn compare_projects(
    local: Vec<LocalProject>,
    remote: Vec<LinearProject>,
) -> Vec<SyncStatus> {
    let mut statuses = Vec::new();

    // Create a set of remote project names (case-insensitive)
    let _remote_names: HashSet<String> = remote
        .iter()
        .map(|p| p.name.to_lowercase())
        .collect();

    // Create a map of local names for quick lookup
    let local_names: HashSet<String> = local
        .iter()
        .map(|p| p.name.to_lowercase())
        .collect();

    // Check local projects
    for local_proj in &local {
        let name_lower = local_proj.name.to_lowercase();

        if let Some(remote_proj) = remote.iter().find(|r| r.name.to_lowercase() == name_lower) {
            statuses.push(SyncStatus::Synced {
                local: local_proj.clone(),
                remote: remote_proj.clone(),
            });
        } else {
            statuses.push(SyncStatus::LocalOnly(local_proj.clone()));
        }
    }

    // Check for remote-only projects
    for remote_proj in &remote {
        let name_lower = remote_proj.name.to_lowercase();
        if !local_names.contains(&name_lower) {
            statuses.push(SyncStatus::RemoteOnly(remote_proj.clone()));
        }
    }

    statuses
}

/// Display sync status
async fn status_command(directory: Option<String>, missing_only: bool) -> Result<()> {
    let dir = directory.unwrap_or_else(|| DEFAULT_CODE_DIR.to_string());

    println!("{}", "Sync Status".bold());
    println!("{}", "─".repeat(60));
    println!("Scanning: {}", dir.cyan());
    println!();

    // Scan local projects
    let local_projects = scan_local_projects(&dir)?;
    println!("Found {} local folders", local_projects.len());

    // Fetch Linear projects
    let client = LinearClient::new()?;
    let linear_projects = fetch_linear_projects(&client).await?;
    println!("Found {} Linear projects", linear_projects.len());
    println!();

    // Compare
    let statuses = compare_projects(local_projects, linear_projects);

    // Count stats
    let synced_count = statuses.iter().filter(|s| matches!(s, SyncStatus::Synced { .. })).count();
    let local_only_count = statuses.iter().filter(|s| matches!(s, SyncStatus::LocalOnly(_))).count();
    let remote_only_count = statuses.iter().filter(|s| matches!(s, SyncStatus::RemoteOnly(_))).count();

    // Display results
    if !missing_only {
        // Show synced projects
        let synced: Vec<_> = statuses.iter()
            .filter_map(|s| match s {
                SyncStatus::Synced { local, remote } => Some((local, remote)),
                _ => None,
            })
            .collect();

        if !synced.is_empty() {
            println!("{} {} Synced:", "[OK]".green(), synced.len());
            for (local, _remote) in synced {
                let git_indicator = if local.has_git { "[git]".dimmed() } else { "".dimmed() };
                println!("  {} {} {}", "+".green(), local.name, git_indicator);
            }
            println!();
        }
    }

    // Show local-only projects (missing from Linear)
    let local_only: Vec<_> = statuses.iter()
        .filter_map(|s| match s {
            SyncStatus::LocalOnly(local) => Some(local),
            _ => None,
        })
        .collect();

    if !local_only.is_empty() {
        println!("{} {} Local only (not in Linear):", "[MISSING]".yellow(), local_only.len());
        for local in local_only {
            let git_indicator = if local.has_git { "[git]".dimmed() } else { "".dimmed() };
            println!("  {} {} {}", "!".yellow(), local.name.yellow(), git_indicator);
        }
        println!();
    }

    // Show remote-only projects (not found locally)
    if !missing_only {
        let remote_only: Vec<_> = statuses.iter()
            .filter_map(|s| match s {
                SyncStatus::RemoteOnly(remote) => Some(remote),
                _ => None,
            })
            .collect();

        if !remote_only.is_empty() {
            println!("{} {} Linear only (not found locally):", "[REMOTE]".blue(), remote_only.len());
            for remote in remote_only {
                println!("  {} {}", "-".blue(), remote.name.blue());
            }
            println!();
        }
    }

    // Summary
    println!("{}", "─".repeat(60));
    println!(
        "Summary: {} synced, {} local only, {} remote only",
        synced_count.to_string().green(),
        local_only_count.to_string().yellow(),
        remote_only_count.to_string().blue()
    );

    if local_only_count > 0 {
        println!();
        println!(
            "{} Run {} to create Linear projects for local folders",
            "Tip:".dimmed(),
            "linear sync push --team <TEAM>".cyan()
        );
    }

    Ok(())
}

/// Push local projects to Linear
async fn push_command(
    directory: Option<String>,
    team: String,
    only: Option<String>,
    dry_run: bool,
) -> Result<()> {
    let dir = directory.unwrap_or_else(|| DEFAULT_CODE_DIR.to_string());

    // Create client early to resolve team ID
    let client = LinearClient::new()?;

    // Resolve team key/name to UUID
    let team_id = resolve_team_id(&client, &team).await?;

    if dry_run {
        println!("{}", "[DRY RUN] No projects will be created".yellow().bold());
        println!();
    }

    println!("{}", "Push to Linear".bold());
    println!("{}", "─".repeat(60));
    println!("Source: {}", dir.cyan());
    println!("Team: {}", team.cyan());
    println!();

    // Scan local projects
    let local_projects = scan_local_projects(&dir)?;

    // Fetch Linear projects
    let linear_projects = fetch_linear_projects(&client).await?;

    // Compare to find missing
    let statuses = compare_projects(local_projects, linear_projects);

    // Get local-only projects
    let mut to_create: Vec<&LocalProject> = statuses.iter()
        .filter_map(|s| match s {
            SyncStatus::LocalOnly(local) => Some(local),
            _ => None,
        })
        .collect();

    // Filter by --only if specified
    if let Some(only_list) = &only {
        let only_names: HashSet<String> = only_list
            .split(',')
            .map(|s| s.trim().to_lowercase())
            .collect();

        to_create.retain(|p| only_names.contains(&p.name.to_lowercase()));
    }

    if to_create.is_empty() {
        println!("{} All local projects already exist in Linear", "[OK]".green());
        return Ok(());
    }

    println!("Projects to create: {}", to_create.len());
    println!();

    let mut created_count = 0;
    let mut failed_count = 0;

    for project in to_create {
        print!("  {} {} ... ", ">".cyan(), project.name);

        if dry_run {
            println!("{}", "[would create]".yellow());
            created_count += 1;
            continue;
        }

        match create_linear_project(&client, &project.name, &team_id, &project.path).await {
            Ok(url) => {
                println!("{}", "[created]".green());
                if let Some(url) = url {
                    println!("    {}", url.dimmed());
                }
                created_count += 1;
            }
            Err(e) => {
                println!("{} {}", "[failed]".red(), e.to_string().red());
                failed_count += 1;
            }
        }
    }

    println!();
    println!("{}", "─".repeat(60));

    if dry_run {
        println!(
            "Would create {} projects",
            created_count.to_string().green()
        );
    } else {
        println!(
            "Created: {}, Failed: {}",
            created_count.to_string().green(),
            failed_count.to_string().red()
        );
    }

    Ok(())
}

/// Create a single Linear project
async fn create_linear_project(
    client: &LinearClient,
    name: &str,
    team: &str,
    local_path: &str,
) -> Result<Option<String>> {
    let description = format!("Local project synced from: {}", local_path);

    let input = json!({
        "name": name,
        "teamIds": [team],
        "description": description
    });

    let mutation = r#"
        mutation($input: ProjectCreateInput!) {
            projectCreate(input: $input) {
                success
                project {
                    id
                    name
                    url
                }
            }
        }
    "#;

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

    if result["data"]["projectCreate"]["success"].as_bool() == Some(true) {
        let url = result["data"]["projectCreate"]["project"]["url"]
            .as_str()
            .map(|s| s.to_string());
        Ok(url)
    } else {
        let errors = &result["errors"];
        if !errors.is_null() {
            anyhow::bail!("API error: {}", errors);
        }
        anyhow::bail!("Failed to create project");
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_compare_projects_synced() {
        let local = vec![
            LocalProject {
                name: "my-project".to_string(),
                path: "/code/my-project".to_string(),
                has_git: true,
            },
        ];

        let remote = vec![
            LinearProject {
                id: "123".to_string(),
                name: "my-project".to_string(),
                url: Some("https://linear.app/...".to_string()),
            },
        ];

        let statuses = compare_projects(local, remote);
        assert_eq!(statuses.len(), 1);
        assert!(matches!(statuses[0], SyncStatus::Synced { .. }));
    }

    #[test]
    fn test_compare_projects_local_only() {
        let local = vec![
            LocalProject {
                name: "new-project".to_string(),
                path: "/code/new-project".to_string(),
                has_git: false,
            },
        ];

        let remote = vec![];

        let statuses = compare_projects(local, remote);
        assert_eq!(statuses.len(), 1);
        assert!(matches!(statuses[0], SyncStatus::LocalOnly(_)));
    }

    #[test]
    fn test_compare_projects_remote_only() {
        let local = vec![];

        let remote = vec![
            LinearProject {
                id: "456".to_string(),
                name: "archived-project".to_string(),
                url: None,
            },
        ];

        let statuses = compare_projects(local, remote);
        assert_eq!(statuses.len(), 1);
        assert!(matches!(statuses[0], SyncStatus::RemoteOnly(_)));
    }

    #[test]
    fn test_compare_projects_case_insensitive() {
        let local = vec![
            LocalProject {
                name: "MyProject".to_string(),
                path: "/code/MyProject".to_string(),
                has_git: true,
            },
        ];

        let remote = vec![
            LinearProject {
                id: "789".to_string(),
                name: "myproject".to_string(),
                url: None,
            },
        ];

        let statuses = compare_projects(local, remote);
        // Should match case-insensitively
        let synced = statuses.iter().filter(|s| matches!(s, SyncStatus::Synced { .. })).count();
        assert_eq!(synced, 1);
    }
}