forked from microsoft/azure-devops-rust-api
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pipelines.rs
95 lines (82 loc) · 3.16 KB
/
pipelines.rs
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
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
// pipelines.rs
// Pipelines example.
use anyhow::Result;
use azure_devops_rust_api::pipelines;
use azure_devops_rust_api::pipelines::models::Pipeline;
use azure_devops_rust_api::Credential;
use std::env;
use std::sync::Arc;
#[tokio::main]
async fn main() -> Result<()> {
// Initialize logging
env_logger::init();
// Get authentication credential either from a PAT ("ADO_TOKEN") or via the az cli.
let credential = match env::var("ADO_TOKEN") {
Ok(token) => {
println!("Authenticate using PAT provided via $ADO_TOKEN");
Credential::from_pat(token)
}
Err(_) => {
println!("Authenticate using Azure CLI");
Credential::from_token_credential(Arc::new(azure_identity::AzureCliCredential::new()))
}
};
// Get ADO server configuration via environment variables
let organization = env::var("ADO_ORGANIZATION").expect("Must define ADO_ORGANIZATION");
let project = env::var("ADO_PROJECT").expect("Must define ADO_PROJECT");
let pipeline_name = env::args().nth(1).expect("Usage: pipelines <name>");
// Create a pipelines client
let pipelines_client = pipelines::ClientBuilder::new(credential).build();
// List all pipelines in the specified organization/project
let pipelines = pipelines_client
.pipelines_client()
.list(&organization, &project)
.into_future()
.await?
.value;
println!("Total pipelines: {}", pipelines.len());
// Filter the pipelines to just those that contain the specified name
println!("\nMatched pipelines:");
let matched_pipelines: Vec<Pipeline> = pipelines
.iter()
.filter(|pipeline| pipeline.name.contains(&pipeline_name))
.cloned()
.collect();
for pipeline in matched_pipelines.iter() {
println!("{:4} {}", pipeline.id, pipeline.name);
}
if let Some(pipeline) = matched_pipelines.iter().next() {
println!("\nExample pipeline struct from list:");
println!("{:#?}", pipeline);
// The pipeline struct returned from list is different from that returned by get.
// Query and display the struct returned by get for comparison.
let pipeline = pipelines_client
.pipelines_client()
.get(&organization, &project, pipeline.id)
.into_future()
.await?;
println!("\nExample pipeline struct from get:");
println!("{:#?}", pipeline);
// Use the client to list all runs of the selected pipeline
let runs = pipelines_client
.runs_client()
.list(&organization, &project, pipeline.id)
.into_future()
.await?
.value;
println!("\nPipeline runs: {}", runs.len());
// Display [result, state] for each pipeline run
for run in runs.iter() {
println!(
"{:8} {:16} {:14} {:14}",
run.run_reference.id,
run.run_reference.name,
format!("{:?}", run.result),
format!("{:?}", run.state)
);
}
}
Ok(())
}