tga 7.1.0

Developer productivity analytics — git commit collection, classification, and reporting
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
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
use super::*;
use crate::core::config::{
    AzureDevOpsConfig, BitbucketConfig, ClassificationConfig, GithubConfig, JiraConfig,
    OutputConfig, PmConfig, RepositoryConfig,
};
use std::path::PathBuf;

fn empty_config() -> Config {
    Config::default()
}

#[test]
fn empty_config_yields_no_errors() {
    let cfg = empty_config();
    let errors = ConfigValidator::new(&cfg).validate();
    assert!(errors.is_empty(), "got {errors:?}");
}

#[test]
fn missing_repo_path_reported() {
    let mut cfg = empty_config();
    cfg.repositories.push(RepositoryConfig {
        path: PathBuf::from("/nonexistent/path/definitely-not-there-12345"),
        ..Default::default()
    });
    let errors = ConfigValidator::new(&cfg).validate();
    assert!(
        errors
            .iter()
            .any(|e| matches!(e, ConfigError::RepoNotFound { .. })),
        "got {errors:?}"
    );
}

/// Create a unique temp directory for a test (avoids extra deps).
fn unique_tempdir(label: &str) -> PathBuf {
    let nanos = std::time::SystemTime::now()
        .duration_since(std::time::UNIX_EPOCH)
        .map(|d| d.as_nanos())
        .unwrap_or(0);
    let dir = std::env::temp_dir().join(format!(
        "tga-validator-{label}-{}-{nanos}",
        std::process::id()
    ));
    std::fs::create_dir_all(&dir).expect("create tempdir");
    dir
}

#[test]
fn existing_repo_path_passes() {
    let tmp = unique_tempdir("repo");
    let mut cfg = empty_config();
    cfg.repositories.push(RepositoryConfig {
        path: tmp.clone(),
        ..Default::default()
    });
    let errors = ConfigValidator::new(&cfg).validate();
    let _ = std::fs::remove_dir_all(&tmp);
    assert!(
        !errors
            .iter()
            .any(|e| matches!(e, ConfigError::RepoNotFound { .. })),
        "got {errors:?}"
    );
}

/// Build a `GithubConfig` with PR fetching on and the given config token.
fn github_fetching_prs(token: Option<&str>) -> GithubConfig {
    GithubConfig {
        token: token.map(str::to_string),
        org: None,
        orgs: vec![],
        repo: None,
        fetch_prs: true,
        fetch_pr_reviews: true,
        review_fetch_concurrency: 1,
        ticket_regex: None,
        fetch_on_reference: false,
        work_items_unavailable: None,
    }
}

/// Why: a config with `fetch_prs = true` and no token anywhere cannot fetch
/// PRs, so validation must say so before the run starts.
/// What: drive `github_token_missing` across every combination of config token
/// and `GITHUB_TOKEN` value, including whitespace-only ones.
/// Test: this test. #5313: it used to remove `GITHUB_TOKEN` process-wide to
/// reach the missing-token branch, which is `unsafe` under the 2024 edition and
/// races every other test thread. The env value is now an argument, so the
/// present-token branches are covered too — they were unreachable before.
#[test]
fn github_token_required_when_fetch_prs() {
    assert!(
        github_token_missing(&github_fetching_prs(None), None),
        "no config token and no env token means PRs cannot be fetched"
    );
    assert!(
        github_token_missing(&github_fetching_prs(Some("  ")), Some("\t")),
        "whitespace-only tokens count as absent"
    );
    assert!(
        !github_token_missing(&github_fetching_prs(Some("ghp_xxx")), None),
        "a config token satisfies the check"
    );
    assert!(
        !github_token_missing(&github_fetching_prs(None), Some("ghp_xxx")),
        "GITHUB_TOKEN satisfies the check"
    );

    let mut off = github_fetching_prs(None);
    off.fetch_prs = false;
    assert!(
        !github_token_missing(&off, None),
        "no token is required when fetch_prs is off"
    );
}

#[test]
fn github_token_in_config_satisfies() {
    let mut cfg = empty_config();
    cfg.github = Some(GithubConfig {
        token: Some("ghp_xxx".into()),
        org: None,
        orgs: vec![],
        repo: None,
        fetch_prs: true,
        fetch_pr_reviews: true,
        review_fetch_concurrency: 1,
        ticket_regex: None,
        fetch_on_reference: false,
        work_items_unavailable: None,
    });
    let errors = ConfigValidator::new(&cfg).validate();
    assert!(
        !errors
            .iter()
            .any(|e| matches!(e, ConfigError::MissingGitHubToken)),
        "got {errors:?}"
    );
}

#[test]
fn partial_jira_config_reports_each_missing_field() {
    let mut cfg = empty_config();
    cfg.jira = Some(JiraConfig {
        url: Some("https://x.atlassian.net".into()),
        // username & token missing
        ..Default::default()
    });
    let errors = ConfigValidator::new(&cfg).validate();
    let missing: Vec<&str> = errors
        .iter()
        .filter_map(|e| match e {
            ConfigError::IncompleteJiraConfig { field } => Some(field.as_str()),
            _ => None,
        })
        .collect();
    assert!(missing.contains(&"username"), "got {errors:?}");
    assert!(missing.contains(&"token"), "got {errors:?}");
}

#[test]
fn empty_jira_block_is_fine() {
    let mut cfg = empty_config();
    cfg.jira = Some(JiraConfig::default());
    let errors = ConfigValidator::new(&cfg).validate();
    assert!(
        !errors
            .iter()
            .any(|e| matches!(e, ConfigError::IncompleteJiraConfig { .. })),
        "got {errors:?}"
    );
}

/// Build a `ClassificationConfig` with LLM classification on.
fn llm_enabled(provider: &str, config_key: Option<&str>) -> ClassificationConfig {
    ClassificationConfig {
        use_llm: true,
        llm_provider: provider.into(),
        openrouter_api_key: config_key.map(str::to_string),
        ..Default::default()
    }
}

/// An `env_lookup` that answers only for the named key.
fn only(key: &'static str, value: &'static str) -> impl Fn(&str) -> Option<String> {
    move |k| (k == key).then(|| value.to_string())
}

/// Why: a config with `use_llm = true` and no API key anywhere cannot classify,
/// so validation must say so before the run starts.
/// What: drive `llm_key_missing` across every provider and every source a key
/// can come from, with `env_lookup` supplied per case.
/// Test: this test. #5725: it used to remove `OPENROUTER_API_KEY` and
/// `OPENAI_API_KEY` process-wide to reach the missing-key branch. That is
/// `unsafe` under the 2024 edition, and its restore raced
/// `profile::batch_reviewer::tests::from_slug_with_store_errors_when_no_credential_resolves` —
/// putting the key back mid-resolution and failing that test 40 runs out of 40.
/// The env lookup is now an argument, so the present-key branches are covered
/// too; they were unreachable before.
#[test]
fn llm_key_missing_across_providers_and_env() {
    let none = |_: &str| None;

    assert_eq!(
        llm_key_missing(&llm_enabled("openrouter", None), none),
        Some("openrouter".to_string()),
        "no config key and no env key means classification cannot run"
    );
    assert_eq!(
        llm_key_missing(&llm_enabled("openrouter", Some("   ")), |_| Some(
            "\t".to_string()
        )),
        Some("openrouter".to_string()),
        "whitespace-only keys count as absent"
    );
    assert_eq!(
        llm_key_missing(&llm_enabled("openrouter", Some("sk-or-xxx")), none),
        None,
        "a config key satisfies the check"
    );
    assert_eq!(
        llm_key_missing(
            &llm_enabled("openrouter", None),
            only("OPENROUTER_API_KEY", "sk-or-xxx")
        ),
        None,
        "OPENROUTER_API_KEY satisfies the check"
    );
    assert_eq!(
        llm_key_missing(
            &llm_enabled("openrouter", None),
            only("OPENAI_API_KEY", "sk-oa-xxx")
        ),
        Some("openrouter".to_string()),
        "the wrong provider's key does not satisfy openrouter"
    );

    // `openai` reads no config field — its key must come from the environment.
    assert_eq!(
        llm_key_missing(&llm_enabled("openai", Some("sk-or-xxx")), none),
        Some("openai".to_string()),
        "the openrouter config key is not an openai key"
    );
    assert_eq!(
        llm_key_missing(
            &llm_enabled("openai", None),
            only("OPENAI_API_KEY", "sk-oa-xxx")
        ),
        None
    );

    // `auto` accepts either provider's key.
    assert_eq!(
        llm_key_missing(
            &llm_enabled("auto", None),
            only("OPENAI_API_KEY", "sk-oa-xxx")
        ),
        None
    );
    assert_eq!(
        llm_key_missing(&llm_enabled("auto", None), none),
        Some("auto".to_string())
    );

    // Bedrock uses the AWS credential chain — no API-key check applies.
    assert_eq!(llm_key_missing(&llm_enabled("bedrock", None), none), None);

    // LLM off means no key is required.
    let mut off = llm_enabled("openrouter", None);
    off.use_llm = false;
    assert_eq!(llm_key_missing(&off, none), None);
}

#[test]
fn confidence_threshold_out_of_range_reported() {
    let mut cfg = empty_config();
    cfg.classification = Some(ClassificationConfig {
        confidence_threshold: 1.5,
        ..Default::default()
    });
    let errors = ConfigValidator::new(&cfg).validate();
    assert!(
        errors
            .iter()
            .any(|e| matches!(e, ConfigError::Conflict { .. })),
        "got {errors:?}"
    );
}

#[test]
fn nonexistent_output_dir_is_created_and_passes() {
    let tmp = unique_tempdir("output");
    let nested = tmp.join("a/b/c");
    let mut cfg = empty_config();
    cfg.output = Some(OutputConfig {
        directory: Some(nested.clone()),
        ..Default::default()
    });
    let errors = ConfigValidator::new(&cfg).validate();
    let exists = nested.exists();
    let _ = std::fs::remove_dir_all(&tmp);
    assert!(
        !errors
            .iter()
            .any(|e| matches!(e, ConfigError::OutputNotWritable { .. })),
        "got {errors:?}"
    );
    assert!(exists, "validator should have created the dir");
}

/// Build a `BitbucketConfig` with PR fetching on and the given auth fields.
fn bitbucket_fetching_prs(
    token: Option<&str>,
    username: Option<&str>,
    app_password: Option<&str>,
) -> BitbucketConfig {
    BitbucketConfig {
        token: token.map(str::to_string),
        username: username.map(str::to_string),
        app_password: app_password.map(str::to_string),
        workspace: Some("acme".into()),
        repo_slug: Some("widgets".into()),
        fetch_prs: true,
        ..Default::default()
    }
}

/// Why: Bitbucket PR fetching needs one of two auth modes, and either half of
/// either mode can arrive from the config file or the environment. Validation
/// must reject only the combinations that leave no usable mode.
/// What: drive `bitbucket_auth_missing` across bearer-token and basic-auth
/// sources, including whitespace-only values and the partial-basic case.
/// Test: this test. #5725: the four Bitbucket tests below used to clear
/// `BITBUCKET_TOKEN` and `BITBUCKET_APP_PASSWORD` process-wide through an
/// `EnvVarGuard`, which is `unsafe` under the 2024 edition and races every other
/// test thread. Both env values are now arguments, so the env-supplied branches
/// are covered too; they were unreachable before.
#[test]
fn bitbucket_auth_missing_across_config_and_env() {
    assert!(
        bitbucket_auth_missing(&bitbucket_fetching_prs(None, None, None), None, None),
        "no auth in config and none in the environment"
    );
    assert!(
        !bitbucket_auth_missing(
            &bitbucket_fetching_prs(Some("workspace-access-token"), None, None),
            None,
            None
        ),
        "a config bearer token satisfies the check"
    );
    assert!(
        !bitbucket_auth_missing(
            &bitbucket_fetching_prs(None, None, None),
            Some("env-token"),
            None
        ),
        "BITBUCKET_TOKEN satisfies the check"
    );
    assert!(
        !bitbucket_auth_missing(
            &bitbucket_fetching_prs(None, Some("alice"), Some("abcd")),
            None,
            None
        ),
        "a config username/app-password pair satisfies the check"
    );
    assert!(
        !bitbucket_auth_missing(
            &bitbucket_fetching_prs(None, Some("alice"), None),
            None,
            Some("env-pwd")
        ),
        "BITBUCKET_APP_PASSWORD pairs with a config username"
    );
    assert!(
        bitbucket_auth_missing(
            &bitbucket_fetching_prs(None, Some("alice"), None),
            None,
            None
        ),
        "a username with no password anywhere is partial auth, not auth"
    );
    assert!(
        bitbucket_auth_missing(
            &bitbucket_fetching_prs(None, None, Some("abcd")),
            None,
            None
        ),
        "an app password with no username is partial auth, not auth"
    );
    assert!(
        bitbucket_auth_missing(
            &bitbucket_fetching_prs(Some("  "), Some("alice"), Some("\t")),
            Some(" "),
            Some("")
        ),
        "whitespace-only values count as absent in every position"
    );

    let mut off = bitbucket_fetching_prs(None, None, None);
    off.fetch_prs = false;
    assert!(
        !bitbucket_auth_missing(&off, None, None),
        "no auth is required when fetch_prs is off"
    );
}

/// Why: the workspace and repo_slug checks are separate from auth and consult
/// no environment, so they are provable through the whole validator.
/// What: a Bitbucket block with PR fetching on and neither field set reports
/// both as incomplete.
/// Test: this test itself.
#[test]
fn bitbucket_requires_workspace_and_repo_slug_when_fetch_prs() {
    let mut cfg = empty_config();
    cfg.bitbucket = Some(BitbucketConfig {
        token: Some("bearer".into()),
        fetch_prs: true,
        ..Default::default()
    });
    let errors = ConfigValidator::new(&cfg).validate();
    let missing: Vec<&str> = errors
        .iter()
        .filter_map(|e| match e {
            ConfigError::IncompleteBitbucketConfig { field } => Some(field.as_str()),
            _ => None,
        })
        .collect();
    assert!(missing.contains(&"workspace"), "got {errors:?}");
    assert!(missing.contains(&"repo_slug"), "got {errors:?}");
}

/// Why: the two complete auth modes must pass the whole validator, not just the
/// decision function.
/// What: a bearer token and a username/app-password pair each yield no
/// Bitbucket error. An ambient `BITBUCKET_*` value could only ADD auth, so
/// these assertions need no environment control (#5725).
/// Test: this test itself.
#[test]
fn bitbucket_complete_auth_passes_validation() {
    for bb in [
        bitbucket_fetching_prs(None, Some("alice"), Some("abcd")),
        bitbucket_fetching_prs(Some("workspace-access-token"), None, None),
    ] {
        let mut cfg = empty_config();
        cfg.bitbucket = Some(bb);
        let errors = ConfigValidator::new(&cfg).validate();
        assert!(
            !errors.iter().any(|e| matches!(
                e,
                ConfigError::MissingBitbucketAuth | ConfigError::IncompleteBitbucketConfig { .. }
            )),
            "got {errors:?}"
        );
    }
}

#[test]
fn config_validator_rejects_ado_with_no_projects() {
    // Regression for the preflight gap surfaced after issue #91:
    // ConfigValidator must reject a `pm.azure_devops` block whose
    // `project` is None and `projects` is empty. Otherwise the CLI's
    // `--validate-only` would pass, the collection would proceed, and
    // every ADO PR fetch would silently return Ok(None).
    let mut cfg = empty_config();
    cfg.pm = Some(PmConfig {
        azure_devops: Some(AzureDevOpsConfig {
            organization_url: "https://dev.azure.com/myorg".into(),
            pat: "secret-pat".into(),
            project: None,
            projects: vec![],
            ticket_regex: r"AB#(\d+)".into(),
            team_keys: vec![],
            fetch_on_reference: true,
            fetch_prs: true,
        }),
    });
    let errors = ConfigValidator::new(&cfg).validate();
    assert!(
        errors
            .iter()
            .any(|e| matches!(e, ConfigError::InvalidAzureDevOpsConfig { .. })),
        "expected InvalidAzureDevOpsConfig, got: {errors:?}"
    );
}

/// Why (#5220): `workspaces` is the second way to name a repository set, and a
/// config that uses it owes no `workspace`/`repo_slug` pair — the repository
/// list comes from `GET /2.0/repositories/{workspace}` instead of the file.
/// Before this, the validator rejected exactly the config `tga install --host
/// bitbucket` generates.
/// What: a Bitbucket block with PR fetching on, one workspace to discover, and
/// neither singular field set, reports no incomplete-config error.
/// Test: this test itself.
#[test]
fn bitbucket_workspaces_satisfy_the_workspace_and_repo_slug_checks() {
    let mut cfg = empty_config();
    cfg.bitbucket = Some(BitbucketConfig {
        token: Some("bearer".into()),
        workspaces: vec!["acme".into()],
        fetch_prs: true,
        ..Default::default()
    });
    let errors = ConfigValidator::new(&cfg).validate();
    assert!(
        !errors
            .iter()
            .any(|e| matches!(e, ConfigError::IncompleteBitbucketConfig { .. })),
        "workspace discovery needs neither workspace nor repo_slug, got {errors:?}"
    );
}

/// A whitespace-only workspaces entry is not a repository set (#5220).
#[test]
fn blank_bitbucket_workspaces_still_require_the_singular_pair() {
    let mut cfg = empty_config();
    cfg.bitbucket = Some(BitbucketConfig {
        token: Some("bearer".into()),
        workspaces: vec!["   ".into()],
        fetch_prs: true,
        ..Default::default()
    });
    let errors = ConfigValidator::new(&cfg).validate();
    let missing: Vec<&str> = errors
        .iter()
        .filter_map(|e| match e {
            ConfigError::IncompleteBitbucketConfig { field } => Some(field.as_str()),
            _ => None,
        })
        .collect();
    assert!(missing.contains(&"workspace"), "got {errors:?}");
    assert!(missing.contains(&"repo_slug"), "got {errors:?}");
}

#[test]
fn bitbucket_block_off_is_fine() {
    // `fetch_prs` defaults off, so the check returns before it reads anything —
    // no environment control needed (#5725).
    let mut cfg = empty_config();
    cfg.bitbucket = Some(BitbucketConfig::default());
    let errors = ConfigValidator::new(&cfg).validate();
    assert!(
        !errors.iter().any(|e| matches!(
            e,
            ConfigError::MissingBitbucketAuth | ConfigError::IncompleteBitbucketConfig { .. }
        )),
        "got {errors:?}"
    );
}