pact-broker-cli 0.7.0

A Rust and CLI client for the Pact Broker. Publish and retrieve pacts and verification results.
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
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
use comfy_table::{Table, presets::UTF8_FULL};
use maplit::hashmap;
use serde_json::Value;

use crate::cli::pact_broker::main::HttpAuth;
use crate::cli::pact_broker::main::{
    HALClient, PactBrokerError,
    types::{OutputType, SslOptions},
    utils::{
        follow_broker_relation, follow_templated_broker_relation, get_auth, get_broker_relation,
        get_broker_url, get_custom_headers, get_ssl_options,
    },
};

pub fn describe_version(args: &clap::ArgMatches) -> Result<String, PactBrokerError> {
    let broker_url = get_broker_url(args).trim_end_matches('/').to_string();
    let auth = get_auth(args);
    let custom_headers = get_custom_headers(args);
    let ssl_options = get_ssl_options(args);

    let version: Option<&String> = args.try_get_one::<String>("version").unwrap();
    let latest: Option<&String> = args.try_get_one::<String>("latest").unwrap();
    let environment: Option<&String> = args.try_get_one::<String>("environment").unwrap();
    let deployed_only = args.get_flag("deployed");
    let released_only = args.get_flag("released");
    let output_type: OutputType = args
        .get_one::<String>("output")
        .and_then(|s| s.parse().ok())
        .unwrap_or(OutputType::Table);
    let pacticipant_name = args.get_one::<String>("pacticipant").unwrap();

    // If environment is specified, use environment-based queries
    if let Some(env_name) = environment {
        return describe_version_by_environment(
            &broker_url,
            &auth,
            &custom_headers,
            &ssl_options,
            pacticipant_name,
            env_name,
            deployed_only,
            released_only,
            output_type,
        );
    }

    let pb_relation_href = if latest.is_some() {
        "pb:latest-tagged-version".to_string()
    } else {
        "pb:latest-version".to_string()
    };

    let res = tokio::runtime::Runtime::new().unwrap().block_on(async {
        let hal_client: HALClient = HALClient::with_url(
            &broker_url,
            Some(auth.clone()),
            ssl_options.clone(),
            custom_headers.clone(),
        );
        let pb_version_href_path =
            get_broker_relation(hal_client.clone(), pb_relation_href, broker_url.to_string()).await;

        follow_templated_broker_relation(
            hal_client.clone(),
            "pb:pacticipant-version".to_string(),
            pb_version_href_path.unwrap(),
            hashmap! {
                "pacticipant".to_string() => pacticipant_name.to_string(),
                "version".to_string() => version.cloned().unwrap_or_default(),
                "tag".to_string() => latest.cloned().unwrap_or_default(),
            },
        )
        .await
    });

    match res {
        Ok(result) => match output_type {
            OutputType::Json => {
                let json: String = serde_json::to_string(&result).unwrap();
                println!("{}", json);
                return Ok(json);
            }
            OutputType::Table => {
                let mut table = Table::new();
                table
                    .load_preset(UTF8_FULL)
                    .set_header(vec!["NAME", "TAGS"]);

                let version_number = result.get("number").and_then(|v| v.as_str()).unwrap_or("-");

                let tags = result
                    .get("_embedded")
                    .and_then(|v| v.get("tags"))
                    .and_then(|v| v.as_array())
                    .map(|arr| {
                        arr.iter()
                            .filter_map(|tag| tag.get("name").and_then(|n| n.as_str()))
                            .collect::<Vec<_>>()
                            .join(", ")
                    })
                    .unwrap_or_else(|| "-".to_string());

                table.add_row(vec![version_number, &tags]);
                println!("{table}");
                return Ok(table.to_string());
            }

            OutputType::Text => {
                return Err(PactBrokerError::NotFound(
                    "Text output is not supported for describe versions".to_string(),
                ));
            }
            OutputType::Pretty => {
                let json: String = serde_json::to_string(&result).unwrap();
                println!("{}", json);
                return Ok(json);
            }
        },
        Err(PactBrokerError::NotFound(_)) => Err(PactBrokerError::NotFound(format!(
            "Pacticipant version not found"
        ))),

        Err(err) => Err(err),
    }
}

/// Describe versions deployed/released to a specific environment
fn describe_version_by_environment(
    broker_url: &str,
    auth: &HttpAuth,
    custom_headers: &Option<crate::cli::pact_broker::main::CustomHeaders>,
    ssl_options: &SslOptions,
    pacticipant_name: &str,
    environment_name: &str,
    deployed_only: bool,
    released_only: bool,
    output_type: OutputType,
) -> Result<String, PactBrokerError> {
    tokio::runtime::Runtime::new().unwrap().block_on(async {
        let hal_client: HALClient = HALClient::with_url(
            broker_url,
            Some(auth.clone()),
            ssl_options.clone(),
            custom_headers.clone(),
        );

        // First, get the environment UUID
        let environments_href = get_broker_relation(
            hal_client.clone(),
            "pb:environments".to_string(),
            broker_url.to_string(),
        )
        .await?;

        let environments_response = follow_broker_relation(
            hal_client.clone(),
            "pb:environments".to_string(),
            environments_href,
        )
        .await?;

        // Find the environment UUID by name
        let environment_uuid = find_environment_uuid(&environments_response, environment_name)
            .ok_or_else(|| {
                PactBrokerError::NotFound(format!("Environment '{}' not found", environment_name))
            })?;

        // Query endpoints based on flags
        let mut all_versions = Vec::new();

        if deployed_only || (!deployed_only && !released_only) {
            // Get currently deployed versions
            let deployed_path = format!(
                "/environments/{}/deployed-versions/currently-deployed",
                environment_uuid
            );

            if let Ok(deployed_response) = hal_client
                .clone()
                .fetch(&format!("{}{}", broker_url, deployed_path))
                .await
            {
                let deployed_versions =
                    filter_versions_by_pacticipant(&deployed_response, pacticipant_name);
                all_versions.extend(deployed_versions);
            }
        }

        if released_only || (!deployed_only && !released_only) {
            // Get currently supported released versions
            let released_path = format!(
                "/environments/{}/released-versions/currently-supported",
                environment_uuid
            );

            if let Ok(released_response) = hal_client
                .clone()
                .fetch(&format!("{}{}", broker_url, released_path))
                .await
            {
                let released_versions =
                    filter_versions_by_pacticipant(&released_response, pacticipant_name);
                all_versions.extend(released_versions);
            }
        }

        format_environment_versions_output(all_versions, environment_name, output_type)
    })
}

/// Find environment UUID by name from environments response
fn find_environment_uuid(environments_response: &Value, environment_name: &str) -> Option<String> {
    environments_response
        .get("_embedded")
        .and_then(|e| e.get("environments"))
        .and_then(|envs| envs.as_array())
        .and_then(|envs| {
            envs.iter().find(|env| {
                env.get("name")
                    .and_then(|n| n.as_str())
                    .map(|n| n == environment_name)
                    .unwrap_or(false)
            })
        })
        .and_then(|env| env.get("uuid"))
        .and_then(|uuid| uuid.as_str())
        .map(|s| s.to_string())
}

/// Filter versions by pacticipant name
fn filter_versions_by_pacticipant(versions_response: &Value, pacticipant_name: &str) -> Vec<Value> {
    versions_response
        .get("_embedded")
        .and_then(|e| {
            e.get("deployedVersions")
                .or_else(|| e.get("releasedVersions"))
                .or_else(|| e.get("currentlyDeployedVersions"))
                .or_else(|| e.get("currentlySupportedVersions"))
        })
        .and_then(|versions| versions.as_array())
        .map(|versions| {
            versions
                .iter()
                .filter(|version| {
                    version
                        .get("_embedded")
                        .and_then(|e| e.get("pacticipant"))
                        .and_then(|p| p.get("name"))
                        .and_then(|n| n.as_str())
                        .map(|n| n == pacticipant_name)
                        .unwrap_or(false)
                })
                .cloned()
                .collect()
        })
        .unwrap_or_default()
}

/// Format environment versions output
fn format_environment_versions_output(
    versions: Vec<Value>,
    environment_name: &str,
    output_type: OutputType,
) -> Result<String, PactBrokerError> {
    match output_type {
        OutputType::Json => {
            let json = serde_json::to_string(&versions)
                .map_err(|e| PactBrokerError::ContentError(e.to_string()))?;
            println!("{}", json);
            Ok(json)
        }
        OutputType::Table => {
            let mut table = Table::new();
            table.load_preset(UTF8_FULL).set_header(vec![
                "VERSION",
                "STATUS",
                "ENVIRONMENT",
                "APPLICATION INSTANCE",
            ]);

            for version in &versions {
                let version_number = version
                    .get("_embedded")
                    .and_then(|e| e.get("version"))
                    .and_then(|v| v.get("number"))
                    .and_then(|n| n.as_str())
                    .unwrap_or("-");

                let status = if version
                    .get("currentlyDeployed")
                    .and_then(|v| v.as_bool())
                    .unwrap_or(false)
                {
                    "Deployed"
                } else if version
                    .get("currentlyReleased")
                    .and_then(|v| v.as_bool())
                    .unwrap_or(false)
                {
                    "Released"
                } else {
                    "Unknown"
                };

                let application_instance = version
                    .get("applicationInstance")
                    .and_then(|ai| ai.as_str())
                    .unwrap_or("-");

                table.add_row(vec![
                    version_number,
                    status,
                    environment_name,
                    application_instance,
                ]);
            }

            let table_str = table.to_string();
            println!("{}", table_str);
            Ok(table_str)
        }
        OutputType::Text => Err(PactBrokerError::NotFound(
            "Text output is not supported for environment versions".to_string(),
        )),
        OutputType::Pretty => {
            let json = serde_json::to_string_pretty(&versions)
                .map_err(|e| PactBrokerError::ContentError(e.to_string()))?;
            println!("{}", json);
            Ok(json)
        }
    }
}

#[cfg(test)]
mod describe_version_tests {
    use super::describe_version;
    use crate::cli::pact_broker::main::subcommands::add_describe_version_subcommand;
    use pact_consumer::prelude::*;
    use pact_models::PactSpecification;
    use serde_json::json;

    #[test]
    fn describe_version_returns_version_and_tags_as_table() {
        // Arrange
        let config = MockServerConfig {
            pact_specification: PactSpecification::V2,
            ..MockServerConfig::default()
        };

        let pacticipant_name = "Condor";
        let version_number = "1.3.0";
        let tag_name = "prod";

        let version_path = format!(
            "/pacticipants/{}/versions/{}",
            pacticipant_name, version_number
        );

        let pact_broker_service = PactBuilder::new("pact-broker-cli", "Pact Broker")
            .interaction("a request for the index resource for records_undeployment_successfully", "", |mut i| {
                i.given("the pb:pacticipant-version relation exists in the index resource");
                i.request
                    .path("/")
                    .header("Accept", "application/hal+json")
                    .header("Accept", "application/json");
                i.response
                    .header("Content-Type", "application/hal+json;charset=utf-8")
                    .json_body(json_pattern!({
                        "_links": {
                            "pb:pacticipant-version": {
                                "href": term!("http:\\/\\/.*",format!("http://localhost{}",version_path)),
                            },
                            "pb:latest-version": {
                                "href": term!("http:\\/\\/.*",format!("http://localhost{}",version_path)),
                            },
                            "pb:latest-tagged-version": {
                                "href": term!("http:\\/\\/.*",format!("http://localhost{}",version_path)),
                            }
                        }
                    }));
                i
            })
            .interaction("get pacticipant version", "", |mut i| {
                i.given(format!(
                    "'{}' exists in the pact-broker with version {}, tagged with '{}'",
                    pacticipant_name, version_number, tag_name
                ));
                i.request
                    .method("GET")
                    .path(version_path.clone())
                    .header("Accept", "application/hal+json")
                    .header("Accept", "application/json");
                i.response
                    .status(200)
                    .header("Content-Type", "application/hal+json;charset=utf-8")
                    .json_body(json!({
                        "number": version_number,
                        "_embedded": {
                            "tags": [
                                { "name": tag_name }
                            ]
                        }
                    }));
                i
            })
            .start_mock_server(None, Some(config));

        let mock_server_url = pact_broker_service.url();

        // Arrange CLI args
        let matches = add_describe_version_subcommand().get_matches_from(vec![
            "describe-versions",
            "-b",
            mock_server_url.as_str(),
            "--pacticipant",
            pacticipant_name,
            "--version",
            version_number,
            "--output",
            "table",
        ]);

        // Act
        let result = describe_version(&matches);

        // Assert
        assert!(result.is_ok());
        let output = result.unwrap();
        println!("Output: {}", output);
        assert!(output.contains(version_number));
        assert!(output.contains(tag_name));
    }

    #[test]
    fn describe_version_returns_json_output() {
        // Arrange
        let config = MockServerConfig {
            pact_specification: PactSpecification::V2,
            ..MockServerConfig::default()
        };

        let pacticipant_name = "Condor";
        let version_number = "1.2.3";

        let version_path = format!(
            "/pacticipants/{}/versions/{}",
            pacticipant_name, version_number
        );

        let pact_broker_service = PactBuilder::new("pact-broker-cli", "Pact Broker")
              .interaction("a request for the index resource for records_undeployment_successfully", "", |mut i| {
                i.given("the pb:pacticipant-version relation exists in the index resource");
                i.request
                    .path("/")
                    .header("Accept", "application/hal+json")
                    .header("Accept", "application/json");
                i.response
                    .header("Content-Type", "application/hal+json;charset=utf-8")
                    .json_body(json_pattern!({
                        "_links": {
                            "pb:pacticipant-version": {
                                "href": term!("http:\\/\\/.*",format!("http://localhost{}",version_path)),
                            },
                            "pb:latest-version": {
                                "href": term!("http:\\/\\/.*",format!("http://localhost{}",version_path)),
                            },
                            "pb:latest-tagged-version": {
                                "href": term!("http:\\/\\/.*",format!("http://localhost{}",version_path)),
                            }
                        }
                    }));
                i
            })
            .interaction("get pacticipant version for json", "", |mut i| {
                i.given(format!(
                    "'{}' exists in the pact-broker with the latest version {}",
                    pacticipant_name, version_number
                ));
                i.request
                    .method("GET")
                    .path(version_path.clone())
                    .header("Accept", "application/hal+json")
                    .header("Accept", "application/json");
                i.response
                    .status(200)
                    .header("Content-Type", "application/hal+json;charset=utf-8")
                    .json_body(json!({
                        "number": version_number,
                        "_embedded": {
                            "tags": []
                        }
                    }));
                i
            })
            .start_mock_server(None, Some(config));

        let mock_server_url = pact_broker_service.url();

        // Arrange CLI args
        let matches = add_describe_version_subcommand().get_matches_from(vec![
            "describe-versions",
            "-b",
            mock_server_url.as_str(),
            "--pacticipant",
            pacticipant_name,
            "--version",
            version_number,
            "--output",
            "json",
        ]);

        // Act
        let result = describe_version(&matches);

        // Assert
        assert!(result.is_ok());
        let output = result.unwrap();
        println!("Output: {}", output);
        assert!(output.contains(version_number));
        assert!(output.contains("\"tags\":[]"));
    }

    #[test]
    fn describe_version_returns_not_found_error() {
        // Arrange
        let config = MockServerConfig {
            pact_specification: PactSpecification::V2,
            ..MockServerConfig::default()
        };

        let pacticipant_name = "Baz";
        let version_number = "9.9.9";

        let version_path = format!(
            "/pacticipants/{}/versions/{}",
            pacticipant_name, version_number
        );

        let pact_broker_service = PactBuilder::new("pact-broker-cli", "Pact Broker")
              .interaction("a request for the index resource for records_undeployment_successfully", "", |mut i| {
                i.given("the pb:pacticipant-version relation exists in the index resource");
                i.request
                    .path("/")
                    .header("Accept", "application/hal+json")
                    .header("Accept", "application/json");
                i.response
                    .header("Content-Type", "application/hal+json;charset=utf-8")
                    .json_body(json_pattern!({
                        "_links": {
                            "pb:pacticipant-version": {
                                "href": term!("http:\\/\\/.*",format!("http://localhost{}",version_path)),
                            },
                            "pb:latest-version": {
                                "href": term!("http:\\/\\/.*",format!("http://localhost{}",version_path)),
                            },
                            "pb:latest-tagged-version": {
                                "href": term!("http:\\/\\/.*",format!("http://localhost{}",version_path)),
                            }
                        }
                    }));
                i
            })
            .interaction("get non-existent pacticipant version", "", |mut i| {
                // i.given(format!(
                //     "no pacticipant with name {} and version {} exists",
                //     pacticipant_name, version_number
                // ));
                i.request
                    .method("GET")
                    .path(version_path.clone())
                    .header("Accept", "application/hal+json")
                    .header("Accept", "application/json");
                i.response
                    .status(404)
                    .header("Content-Type", "application/json;charset=utf-8")
                    .json_body(json!({
                        "error": "The requested document was not found on this server."
                    }));
                i
            })
            .start_mock_server(None, Some(config));

        let mock_server_url = pact_broker_service.url();

        // Arrange CLI args
        let matches = add_describe_version_subcommand().get_matches_from(vec![
            "describe-versions",
            "-b",
            mock_server_url.as_str(),
            "--pacticipant",
            pacticipant_name,
            "--version",
            version_number,
            "--output",
            "table",
        ]);

        // Act
        let result = describe_version(&matches);

        // Assert
        assert!(result.is_err());
        let err = result.unwrap_err();
        let err_str = format!("{:?}", err);
        println!("Error: {}", err_str);
        assert!(err_str.contains("Pacticipant version not found"));
    }
}