jjj 0.4.1

Distributed project management and code review for Jujutsu
Documentation
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
//! End-to-end integration tests for GitHub sync against a real private repo (doug/jjjtest).
//!
//! These tests are gated behind `gh auth status` and `which jj` checks.
//! They use unique prefixes per run to avoid conflicts and clean up after themselves.

mod test_helpers;

use std::path::PathBuf;
use std::process::Command;
use std::time::{SystemTime, UNIX_EPOCH};
use tempfile::TempDir;
use test_helpers::run_jjj_success;

const TEST_REPO: &str = "doug/jjjtest";

/// Run an arbitrary command and return its Output.
fn run_cmd(dir: &std::path::Path, program: &str, args: &[&str]) -> std::process::Output {
    Command::new(program)
        .current_dir(dir)
        .args(args)
        .output()
        .unwrap_or_else(|e| panic!("Failed to execute {} {}: {}", program, args.join(" "), e))
}

/// Run gh CLI targeting the test repo. Returns stdout on success.
fn run_gh(dir: &std::path::Path, args: &[&str]) -> String {
    let output = Command::new("gh")
        .current_dir(dir)
        .args(args)
        .output()
        .expect("Failed to execute gh");
    assert!(
        output.status.success(),
        "gh {} failed.\nstdout: {}\nstderr: {}",
        args.join(" "),
        String::from_utf8_lossy(&output.stdout),
        String::from_utf8_lossy(&output.stderr)
    );
    String::from_utf8_lossy(&output.stdout).trim().to_string()
}

/// Generate a unique suffix for this test run (epoch millis).
fn unique_suffix() -> String {
    let ts = SystemTime::now()
        .duration_since(UNIX_EPOCH)
        .unwrap()
        .as_millis();
    format!("{}", ts)
}

/// Create a GitHub issue on the test repo, return the issue number.
/// Parses the issue number from the URL that `gh issue create` prints.
fn create_gh_issue(dir: &std::path::Path, title: &str, body: &str) -> u64 {
    let url = run_gh(
        dir,
        &[
            "issue", "create", "--repo", TEST_REPO, "--title", title, "--body", body,
        ],
    );
    // Output is a URL like https://github.com/doug/jjjtest/issues/42
    url.trim()
        .rsplit('/')
        .next()
        .expect("Failed to parse issue number from URL")
        .parse::<u64>()
        .unwrap_or_else(|_| panic!("Failed to parse issue number from URL: {}", url))
}

/// Check prerequisites: jj available, gh available and authenticated.
/// Returns false if any check fails (test should skip).
fn prerequisites_met() -> bool {
    // jj installed?
    if jjj::jj::find_executable("jj").is_none() {
        eprintln!("Skipping github_sync_e2e: jj not found");
        return false;
    }

    // gh installed?
    if jjj::jj::find_executable("gh").is_none() {
        eprintln!("Skipping github_sync_e2e: gh not found");
        return false;
    }

    // gh authenticated?
    let output = Command::new("gh")
        .args(["auth", "status"])
        .output()
        .expect("Failed to run gh auth status");
    if !output.status.success() {
        eprintln!("Skipping github_sync_e2e: gh not authenticated");
        return false;
    }

    true
}

/// Set up a temp jj repo (colocated with git), init jjj, add remote for doug/jjjtest.
fn setup_github_test_repo() -> TempDir {
    let dir = TempDir::new().expect("Failed to create temp dir");
    let path = dir.path();

    // jj git init --colocate
    let output = run_cmd(path, "jj", &["git", "init", "--colocate"]);
    assert!(
        output.status.success(),
        "jj git init --colocate failed: {}",
        String::from_utf8_lossy(&output.stderr)
    );

    // Configure user
    run_cmd(
        path,
        "jj",
        &["config", "set", "--repo", "user.name", "E2E Test"],
    );
    run_cmd(
        path,
        "jj",
        &["config", "set", "--repo", "user.email", "e2e@test.local"],
    );

    // Add git remote pointing to the test repo
    let remote_url = format!("https://github.com/{}.git", TEST_REPO);
    let output = run_cmd(path, "jj", &["git", "remote", "add", "origin", &remote_url]);
    assert!(
        output.status.success(),
        "Failed to add remote: {}",
        String::from_utf8_lossy(&output.stderr)
    );

    // Initialize jjj
    let stdout = run_jjj_success(path, &["init"]);
    assert!(
        stdout.contains("Initialized") || stdout.contains("jjj"),
        "jjj init output unexpected: {}",
        stdout
    );

    dir
}

/// Close a GitHub issue by number on the test repo (cleanup helper).
fn close_issue_on_repo(dir: &std::path::Path, number: u64) {
    let num_str = number.to_string();
    let _ = Command::new("gh")
        .current_dir(dir)
        .args(["issue", "close", &num_str, "--repo", TEST_REPO])
        .output();
}

// ---------------------------------------------------------------------------
// Tests
// ---------------------------------------------------------------------------

/// Full end-to-end: create local problem, create GH issue, import, close, reopen, status, cleanup.
#[test]
fn test_github_sync_e2e_full_flow() {
    if !prerequisites_met() {
        return;
    }

    let suffix = unique_suffix();
    let dir = setup_github_test_repo();
    let path = dir.path();

    // Track issue numbers we create so we can clean up on any failure path.
    let mut created_issue_numbers: Vec<u64> = Vec::new();

    // Helper closure-style cleanup (we'll call at the end and on panics via Drop).
    // We do explicit cleanup at the end; the Drop guard is just in case.
    struct CleanupGuard {
        dir: PathBuf,
        issues: Vec<u64>,
    }
    impl Drop for CleanupGuard {
        fn drop(&mut self) {
            for &num in &self.issues {
                close_issue_on_repo(&self.dir, num);
            }
        }
    }

    let mut guard = CleanupGuard {
        dir: path.to_path_buf(),
        issues: Vec::new(),
    };

    // -----------------------------------------------------------------------
    // Step 1: Create a local problem
    // -----------------------------------------------------------------------
    let problem_title = format!("E2E Test Problem {}", suffix);
    let stdout = run_jjj_success(path, &["problem", "new", &problem_title]);
    assert!(
        stdout.contains(&problem_title),
        "Expected title in output: {}",
        stdout
    );

    // -----------------------------------------------------------------------
    // Step 2: Create a GitHub issue directly via gh, then import it
    // -----------------------------------------------------------------------
    let gh_issue_title = format!("GH E2E Import Test {}", suffix);
    let gh_issue_body = "Automated test issue created by github_sync_e2e_test. Safe to delete.";

    // Create issue on the real repo
    let issue_number = create_gh_issue(path, &gh_issue_title, gh_issue_body);
    created_issue_numbers.push(issue_number);
    guard.issues.push(issue_number);

    eprintln!("Created GitHub issue #{} on {}", issue_number, TEST_REPO);

    // Wait for GitHub's GraphQL API to replicate the newly created issue
    std::thread::sleep(std::time::Duration::from_secs(2));

    // Import the issue via jjj github import
    let issue_ref = format!("#{}", issue_number);
    let stdout = run_jjj_success(path, &["github", "import", &issue_ref]);
    assert!(
        stdout.contains("Imported") || stdout.contains(&gh_issue_title),
        "Expected import confirmation: {}",
        stdout
    );

    // -----------------------------------------------------------------------
    // Step 3: Verify the imported problem has github_issue set
    // -----------------------------------------------------------------------
    // List problems in JSON and find the imported one
    let stdout = run_jjj_success(path, &["problem", "list", "--json"]);
    let problems: serde_json::Value =
        serde_json::from_str(&stdout).expect("Failed to parse problem list JSON");
    let problems_arr = problems.as_array().expect("Expected JSON array");

    let imported = problems_arr
        .iter()
        .find(|p| {
            p["title"].as_str().map_or(false, |t| {
                t.contains(&gh_issue_title) || t == gh_issue_title
            })
        })
        .expect("Imported problem not found in problem list");

    let imported_id = imported["id"].as_str().expect("Expected id string");
    let github_issue_field = imported.get("github_issue");
    assert!(
        github_issue_field.is_some() && github_issue_field.unwrap().as_u64() == Some(issue_number),
        "Expected github_issue={} on imported problem, got {:?}",
        issue_number,
        github_issue_field
    );

    // Also verify via `problem show --json`
    let stdout = run_jjj_success(path, &["problem", "show", imported_id, "--json"]);
    let show_json: serde_json::Value =
        serde_json::from_str(&stdout).expect("Failed to parse problem show JSON");
    assert_eq!(
        show_json["github_issue"].as_u64(),
        Some(issue_number),
        "problem show --json should have github_issue={}",
        issue_number
    );

    // -----------------------------------------------------------------------
    // Step 4: Close the issue via jjj github close
    // -----------------------------------------------------------------------
    let stdout = run_jjj_success(path, &["github", "close", imported_id]);
    assert!(
        stdout.contains("Closed") || stdout.contains("close"),
        "Expected close confirmation: {}",
        stdout
    );

    // Verify the issue is closed on GitHub
    let gh_state = run_gh(
        path,
        &[
            "issue",
            "view",
            &issue_number.to_string(),
            "--repo",
            TEST_REPO,
            "--json",
            "state",
            "--jq",
            ".state",
        ],
    );
    assert_eq!(
        gh_state.trim(),
        "CLOSED",
        "Issue #{} should be CLOSED on GitHub, got: {}",
        issue_number,
        gh_state
    );

    // -----------------------------------------------------------------------
    // Step 5: Reopen the issue via jjj github reopen
    // -----------------------------------------------------------------------
    let stdout = run_jjj_success(path, &["github", "reopen", imported_id]);
    assert!(
        stdout.contains("Reopened") || stdout.contains("reopen"),
        "Expected reopen confirmation: {}",
        stdout
    );

    // Verify the issue is open on GitHub
    let gh_state = run_gh(
        path,
        &[
            "issue",
            "view",
            &issue_number.to_string(),
            "--repo",
            TEST_REPO,
            "--json",
            "state",
            "--jq",
            ".state",
        ],
    );
    assert_eq!(
        gh_state.trim(),
        "OPEN",
        "Issue #{} should be OPEN after reopen, got: {}",
        issue_number,
        gh_state
    );

    // -----------------------------------------------------------------------
    // Step 6: Check sync status
    // -----------------------------------------------------------------------
    let stdout = run_jjj_success(path, &["github", "status"]);
    // Status should mention the repo and the linked issue
    assert!(
        stdout.contains("jjjtest") || stdout.contains(TEST_REPO),
        "Status should mention the repo: {}",
        stdout
    );
    assert!(
        stdout.contains(&format!("#{}", issue_number)) || stdout.contains(&gh_issue_title),
        "Status should mention the linked issue: {}",
        stdout
    );

    // -----------------------------------------------------------------------
    // Step 7: Cleanup - close all test issues
    // -----------------------------------------------------------------------
    for &num in &created_issue_numbers {
        close_issue_on_repo(path, num);
    }

    // Verify cleanup: issue should be closed
    let gh_state = run_gh(
        path,
        &[
            "issue",
            "view",
            &issue_number.to_string(),
            "--repo",
            TEST_REPO,
            "--json",
            "state",
            "--jq",
            ".state",
        ],
    );
    assert_eq!(
        gh_state.trim(),
        "CLOSED",
        "Cleanup: issue #{} should be CLOSED, got: {}",
        issue_number,
        gh_state
    );

    // Clear guard so it doesn't double-close
    guard.issues.clear();
}

/// Test: creating a local problem does NOT auto-create a GH issue (auto_push is off by default).
#[test]
fn test_github_sync_local_problem_no_auto_push() {
    if !prerequisites_met() {
        return;
    }

    let suffix = unique_suffix();
    let dir = setup_github_test_repo();
    let path = dir.path();

    let title = format!("Local Only E2E {}", suffix);
    run_jjj_success(path, &["problem", "new", &title]);

    // The problem should NOT have a github_issue (auto_push is off)
    let stdout = run_jjj_success(path, &["problem", "list", "--json"]);
    let problems: serde_json::Value = serde_json::from_str(&stdout).expect("Failed to parse JSON");
    let problems_arr = problems.as_array().unwrap();

    let local = problems_arr
        .iter()
        .find(|p| p["title"].as_str().map_or(false, |t| t == title))
        .expect("Local problem not found");

    let gh_issue = local.get("github_issue");
    assert!(
        gh_issue.is_none() || gh_issue.unwrap().is_null(),
        "Local-only problem should not have github_issue, got: {:?}",
        gh_issue
    );
}

/// Test: importing an already-imported issue is idempotent.
#[test]
fn test_github_sync_import_idempotent() {
    if !prerequisites_met() {
        return;
    }

    let suffix = unique_suffix();
    let dir = setup_github_test_repo();
    let path = dir.path();

    // Create a GH issue
    let title = format!("Idempotent Import E2E {}", suffix);
    let body = "Automated test issue for idempotency check. Safe to delete.";

    let issue_number = create_gh_issue(path, &title, body);

    struct CleanupGuard {
        dir: PathBuf,
        issue: u64,
    }
    impl Drop for CleanupGuard {
        fn drop(&mut self) {
            close_issue_on_repo(&self.dir, self.issue);
        }
    }
    let _guard = CleanupGuard {
        dir: path.to_path_buf(),
        issue: issue_number,
    };

    let issue_ref = format!("#{}", issue_number);

    // Wait for GitHub's GraphQL API to replicate the newly created issue
    std::thread::sleep(std::time::Duration::from_secs(2));

    // First import
    let stdout = run_jjj_success(path, &["github", "import", &issue_ref]);
    assert!(
        stdout.contains("Imported"),
        "First import should say Imported: {}",
        stdout
    );

    // Second import should be a no-op (already linked)
    let stdout = run_jjj_success(path, &["github", "import", &issue_ref]);
    assert!(
        stdout.contains("already linked") || stdout.contains("already"),
        "Second import should report already linked: {}",
        stdout
    );

    // Should still have exactly one problem with that github_issue
    let stdout = run_jjj_success(path, &["problem", "list", "--json"]);
    let problems: serde_json::Value = serde_json::from_str(&stdout).unwrap();
    let matching: Vec<_> = problems
        .as_array()
        .unwrap()
        .iter()
        .filter(|p| p["github_issue"].as_u64() == Some(issue_number))
        .collect();
    assert_eq!(
        matching.len(),
        1,
        "Expected exactly 1 problem linked to issue #{}, got {}",
        issue_number,
        matching.len()
    );
}