forked from microsoft/azure-devops-rust-api
-
Notifications
You must be signed in to change notification settings - Fork 0
/
wit.rs
77 lines (65 loc) · 2.31 KB
/
wit.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
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
// wit.rs
// Work Item example.
use anyhow::Result;
use azure_devops_rust_api::wit;
use azure_devops_rust_api::wit::models::WorkItemRelation;
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 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 work_item_id: i32 = env::args()
.nth(1)
.expect("Usage: wit <work_item_id>")
.parse()
.expect("integer id");
// Create a wit client
let wit_client = wit::ClientBuilder::new(credential).build();
// Get work items
let work_item = wit_client
.work_items_client()
.get_work_item(&organization, work_item_id, &project)
.expand("All")
.into_future()
.await?;
println!("{:#?}", work_item);
const CHILD_LINK_TYPE: &str = "System.LinkTypes.Hierarchy-Forward";
let children: Vec<&WorkItemRelation> = work_item
.relations
.iter()
.filter(|relation| relation.link.rel == CHILD_LINK_TYPE)
.collect();
println!("\n{} child work items found", children.len());
for child in children.iter() {
println!("{}", child.link.url);
}
const ARTIFACT_LINK_TYPE: &str = "ArtifactLink";
let artifacts: Vec<&WorkItemRelation> = work_item
.relations
.iter()
.filter(|relation| relation.link.rel == ARTIFACT_LINK_TYPE)
.collect();
println!("\n{} related artifacts found", artifacts.len());
for artifact in artifacts.iter() {
println!("{}", artifact.link.url);
}
Ok(())
}