bzr 0.2.0

A CLI for Bugzilla, inspired by gh
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
use std::io::{self, Write};

use serde::Serialize;

use super::formatting::{mask_api_key, print_field, print_formatted, print_optional_field};
use crate::config::{CredentialSource, CredentialSourceKind};
use crate::types::{AuthMethod, OutputFormat};

fn auth_display(auth_method: Option<&AuthMethod>) -> String {
    auth_method.map_or_else(
        || "auto (not yet detected)".to_string(),
        ToString::to_string,
    )
}

#[derive(Serialize)]
#[non_exhaustive]
pub struct ServerDisplayInfo {
    url: String,
    email: Option<String>,
    api_key: String,
    api_key_source: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    auth_method: Option<AuthMethod>,
    #[serde(skip_serializing_if = "std::ops::Not::not")]
    tls_insecure: bool,
    #[serde(skip_serializing_if = "Option::is_none")]
    tls_ca_cert: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    tls_pin: Option<String>,
}

impl ServerDisplayInfo {
    fn from_config(srv: &crate::config::ServerConfig) -> Self {
        let (api_key, api_key_source) = match srv.credential_source() {
            Ok(CredentialSource::Inline(api_key)) => {
                (mask_api_key(api_key), CredentialSourceKind::Inline.as_str())
            }
            Ok(CredentialSource::EnvVar(var_name)) => {
                (var_name.to_string(), CredentialSourceKind::Env.as_str())
            }
            Ok(CredentialSource::Keyring { service, account }) => {
                let display = if account.is_empty() {
                    format!("{service}/<server-name>")
                } else {
                    format!("{service}/{account}")
                };
                (display, CredentialSourceKind::Keyring.as_str())
            }
            Err(_) => ("[invalid config]".to_string(), "invalid"),
        };
        Self {
            url: srv.url.clone(),
            email: srv.email.clone(),
            api_key,
            api_key_source: api_key_source.to_string(),
            auth_method: srv.auth_method,
            tls_insecure: srv.tls_insecure,
            tls_ca_cert: srv.tls_ca_cert.as_ref().map(|p| p.display().to_string()),
            tls_pin: srv.tls_pin_sha256.as_ref().map(|pin| {
                if let Some(issuer) = &srv.tls_pin_issuer {
                    format!("{pin} ({issuer})")
                } else {
                    pin.clone()
                }
            }),
        }
    }
}

#[derive(Serialize)]
#[non_exhaustive]
pub struct ConfigView {
    pub config_file: String,
    pub default_server: Option<String>,
    pub servers: std::collections::BTreeMap<String, ServerDisplayInfo>,
}

impl ConfigView {
    pub fn from_config(config: &crate::config::Config, path: &std::path::Path) -> Self {
        let servers = config
            .servers
            .iter()
            .map(|(name, srv)| (name.clone(), ServerDisplayInfo::from_config(srv)))
            .collect();
        Self {
            config_file: path.to_string_lossy().into_owned(),
            default_server: config.default_server.clone(),
            servers,
        }
    }
}

fn print_api_key(s: &ServerDisplayInfo) {
    let label = match s.api_key_source.as_str() {
        "env" => "API Key Env",
        "keyring" => "Keyring",
        _ => "API Key",
    };
    print_field(label, &s.api_key);
}

fn print_server(name: &str, s: &ServerDisplayInfo) {
    writeln!(io::stdout(), "\n[{name}]").expect("write to output");
    print_field("URL", &s.url);
    print_optional_field("Email", s.email.as_deref());
    print_api_key(s);
    print_field("API Key Source", &s.api_key_source);
    print_field("Auth", &auth_display(s.auth_method.as_ref()));
    if s.tls_insecure {
        print_field("TLS", "insecure (certificate verification disabled)");
    }
    if let Some(ca) = &s.tls_ca_cert {
        print_field("TLS CA Cert", ca);
    }
    if let Some(pin) = &s.tls_pin {
        print_field("TLS Pin", pin);
    }
}

pub fn print_config(view: &ConfigView, format: OutputFormat) {
    print_formatted(view, format, |v| {
        let mut out = io::stdout();
        writeln!(out, "Config file: {}\n", v.config_file).expect("write to output");
        if let Some(ref def) = v.default_server {
            writeln!(out, "Default server: {def}").expect("write to output");
        }
        if v.servers.is_empty() {
            writeln!(out, "No servers configured.").expect("write to output");
            return;
        }
        for (name, s) in &v.servers {
            print_server(name, s);
        }
    });
}

#[cfg(test)]
#[expect(clippy::unwrap_used)]
mod tests {
    use super::*;
    use crate::config::{Config, ServerConfig};
    use std::collections::HashMap;
    use std::path::Path;

    #[test]
    fn config_view_json_serialization() {
        let view = ConfigView {
            config_file: "/etc/bzr/config.toml".into(),
            default_server: Some("prod".into()),
            servers: std::collections::BTreeMap::new(),
        };
        let json: serde_json::Value = serde_json::to_value(&view).unwrap();
        assert_eq!(json["config_file"], "/etc/bzr/config.toml");
        assert_eq!(json["default_server"], "prod");
    }

    #[test]
    fn config_view_no_default_server() {
        let view = ConfigView {
            config_file: "/tmp/config.toml".into(),
            default_server: None,
            servers: std::collections::BTreeMap::new(),
        };
        let json: serde_json::Value = serde_json::to_value(&view).unwrap();
        assert!(json["default_server"].is_null());
    }

    #[test]
    fn auth_display_formats_detected_and_undetected_states() {
        assert_eq!(auth_display(None), "auto (not yet detected)");
        assert_eq!(auth_display(Some(&AuthMethod::Header)), "header");
    }

    #[test]
    fn config_view_from_config_masks_key_and_includes_flags() {
        let mut servers = HashMap::new();
        servers.insert(
            "prod".into(),
            ServerConfig {
                url: "https://bugzilla.example".into(),
                email: Some("admin@example.com".into()),
                api_key: Some("1234567890abcdef".into()),
                api_key_env: None,
                api_key_keyring: None,
                auth_method: Some(AuthMethod::Header),
                api_mode: None,
                server_version: None,
                tls_insecure: true,
                tls_ca_cert: None,
                tls_pin_sha256: None,
                tls_pin_issuer: None,
                tls_pin_issuer_der: None,
            },
        );
        let config = Config {
            default_server: Some("prod".into()),
            servers,
            queries: HashMap::new(),
            templates: HashMap::new(),
        };

        let view = ConfigView::from_config(&config, Path::new("/tmp/bzr/config.toml"));
        let server = &view.servers["prod"];
        let json: serde_json::Value = serde_json::to_value(server).unwrap();

        assert_eq!(json["url"], "https://bugzilla.example");
        assert_eq!(json["email"], "admin@example.com");
        assert_eq!(json["api_key"], "12345678...");
        assert_eq!(json["api_key_source"], "inline");
        assert_eq!(json["auth_method"], "header");
        assert_eq!(json["tls_insecure"], true);
    }

    #[test]
    fn config_view_from_config_shows_env_backed_keys_without_resolving() {
        let mut servers = HashMap::new();
        servers.insert(
            "prod".into(),
            ServerConfig {
                url: "https://bugzilla.example".into(),
                email: None,
                api_key: None,
                api_key_env: Some("BZR_API_KEY".into()),
                api_key_keyring: None,
                auth_method: None,
                api_mode: None,
                server_version: None,
                tls_insecure: false,
                tls_ca_cert: None,
                tls_pin_sha256: None,
                tls_pin_issuer: None,
                tls_pin_issuer_der: None,
            },
        );
        let config = Config {
            default_server: Some("prod".into()),
            servers,
            queries: HashMap::new(),
            templates: HashMap::new(),
        };

        let view = ConfigView::from_config(&config, Path::new("/tmp/bzr/config.toml"));
        let server = &view.servers["prod"];
        let json: serde_json::Value = serde_json::to_value(server).unwrap();

        assert_eq!(json["api_key"], "BZR_API_KEY");
        assert_eq!(json["api_key_source"], "env");
    }

    #[test]
    fn server_display_info_keyring_source() {
        let srv = ServerConfig {
            url: "https://example.com".into(),
            api_key: None,
            api_key_env: None,
            api_key_keyring: Some(crate::config::KeyringRef {
                service: Some("bzr".into()),
                account: Some("prod".into()),
            }),
            email: None,
            auth_method: None,
            api_mode: None,
            server_version: None,
            tls_insecure: false,
            tls_ca_cert: None,
            tls_pin_sha256: None,
            tls_pin_issuer: None,
            tls_pin_issuer_der: None,
        };
        let info = ServerDisplayInfo::from_config(&srv);
        assert_eq!(info.api_key_source, "keyring");
        assert!(info.api_key.contains("bzr"));
        assert!(info.api_key.contains("prod"));
    }

    #[test]
    fn display_server_with_tls_pin() {
        let mut servers = HashMap::new();
        servers.insert(
            "pinned".into(),
            ServerConfig {
                url: "https://bugzilla.example".into(),
                email: None,
                api_key: Some("1234567890abcdef".into()),
                api_key_env: None,
                api_key_keyring: None,
                auth_method: Some(AuthMethod::Header),
                api_mode: None,
                server_version: None,
                tls_insecure: false,
                tls_ca_cert: None,
                tls_pin_sha256: Some("sha256//abc123".into()),
                tls_pin_issuer: Some("CN=Test CA".into()),
                tls_pin_issuer_der: None,
            },
        );
        let config = Config {
            default_server: Some("pinned".into()),
            servers,
            queries: HashMap::new(),
            templates: HashMap::new(),
        };

        let view = ConfigView::from_config(&config, Path::new("/tmp/bzr/config.toml"));
        let server = &view.servers["pinned"];
        let json: serde_json::Value = serde_json::to_value(server).unwrap();

        assert_eq!(json["tls_pin"], "sha256//abc123 (CN=Test CA)");
        assert!(json.get("tls_insecure").is_none());
    }

    fn make_display_info(
        url: &str,
        api_key: &str,
        source: &str,
        tls_insecure: bool,
    ) -> ServerDisplayInfo {
        ServerDisplayInfo {
            url: url.into(),
            email: None,
            api_key: api_key.into(),
            api_key_source: source.into(),
            auth_method: None,
            tls_insecure,
            tls_ca_cert: None,
            tls_pin: None,
        }
    }

    async fn capture_print_config(view: &ConfigView) -> String {
        let ((), output) = crate::test_helpers::capture_stdout(async {
            print_config(view, OutputFormat::Table);
        })
        .await;
        output
    }

    #[tokio::test]
    async fn print_config_renders_table_for_inline_server() {
        // Exercises print_config / print_server / print_api_key with the
        // inline credential branch.
        let _lock = crate::ENV_LOCK.lock().await;
        let mut info = make_display_info("https://bugzilla.example", "12345678...", "inline", true);
        info.email = Some("admin@example.com".into());
        info.auth_method = Some(AuthMethod::Header);
        let mut servers = std::collections::BTreeMap::new();
        servers.insert("prod".into(), info);
        let view = ConfigView {
            config_file: "/tmp/bzr/config.toml".into(),
            default_server: Some("prod".into()),
            servers,
        };
        let output = capture_print_config(&view).await;
        for needle in [
            "Config file: /tmp/bzr/config.toml",
            "Default server: prod",
            "[prod]",
            "https://bugzilla.example",
            "admin@example.com",
            "API Key",
            "12345678...",
            "insecure",
        ] {
            assert!(output.contains(needle), "missing {needle:?} in output");
        }
    }

    #[tokio::test]
    async fn print_config_renders_env_and_keyring_labels() {
        // Exercises both non-inline branches of print_api_key in one run.
        let _lock = crate::ENV_LOCK.lock().await;
        let mut servers = std::collections::BTreeMap::new();
        servers.insert(
            "env-srv".into(),
            make_display_info("https://env.example", "BZR_API_KEY", "env", false),
        );
        servers.insert(
            "kr-srv".into(),
            make_display_info("https://kr.example", "bzr/kr-srv", "keyring", false),
        );
        let view = ConfigView {
            config_file: "/tmp/bzr/config.toml".into(),
            default_server: None,
            servers,
        };
        let output = capture_print_config(&view).await;
        for needle in ["API Key Env", "BZR_API_KEY", "Keyring", "bzr/kr-srv"] {
            assert!(output.contains(needle), "missing {needle:?} in output");
        }
    }

    #[tokio::test]
    async fn print_config_renders_empty_servers_message() {
        let _lock = crate::ENV_LOCK.lock().await;
        let view = ConfigView {
            config_file: "/tmp/bzr/config.toml".into(),
            default_server: None,
            servers: std::collections::BTreeMap::new(),
        };
        let output = capture_print_config(&view).await;
        assert!(output.contains("No servers configured."));
    }

    #[test]
    fn server_display_info_keyring_source_default_account() {
        // account=None should render with `<server-name>` placeholder per the
        // Task 3 implementation.
        let srv = ServerConfig {
            url: "https://example.com".into(),
            api_key: None,
            api_key_env: None,
            api_key_keyring: Some(crate::config::KeyringRef {
                service: None,
                account: None,
            }),
            email: None,
            auth_method: None,
            api_mode: None,
            server_version: None,
            tls_insecure: false,
            tls_ca_cert: None,
            tls_pin_sha256: None,
            tls_pin_issuer: None,
            tls_pin_issuer_der: None,
        };
        let info = ServerDisplayInfo::from_config(&srv);
        assert_eq!(info.api_key_source, "keyring");
        assert!(info.api_key.contains("bzr"));
        // The display shows the placeholder, not a real server name (since
        // ServerDisplayInfo doesn't know the server name — that's higher
        // up in ConfigView).
        assert!(info.api_key.contains("<server-name>"));
    }
}