forked from microsoft/azure-devops-rust-api
-
Notifications
You must be signed in to change notification settings - Fork 0
/
git_repo_get.rs
86 lines (73 loc) · 2.46 KB
/
git_repo_get.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
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
// git_repo_get.rs
// Repository get example.
use anyhow::Result;
use azure_devops_rust_api::git;
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 repo_name = env::args()
.nth(1)
.expect("Usage: git_repo_get <repository-name>");
// Create a "git" client
let git_client = git::ClientBuilder::new(credential).build();
// Get the specified repo
let repo = git_client
.repositories_client()
.get_repository(&organization, &repo_name, &project)
.into_future()
.await?;
println!("{:#?}", repo);
// Get up to 10 pull requests on the specified repo
let prs = git_client
.pull_requests_client()
.get_pull_requests(&organization, &repo.id, &project)
.top(10)
.into_future()
.await?
.value;
println!("\nFound {} pull requests", prs.len());
for pr in &prs {
println!("{:<8}{}", pr.pull_request_id, pr.title.as_ref().unwrap());
}
if let Some(pr) = prs.iter().next() {
println!("\nExample PR struct:");
println!("{:#?}", pr);
}
// Get up to 10 refs on the specified repo
let git_refs = git_client
.refs_client()
.list(&organization, &repo.id, &project)
.top(10)
.into_future()
.await?
.value;
println!("\nGot {} refs", git_refs.len());
for git_ref in &git_refs {
println!("{:<50}{}", git_ref.name, git_ref.object_id);
}
if let Some(git_ref) = git_refs.iter().next() {
println!("\nExample ref struct:");
println!("{:#?}", git_ref);
}
Ok(())
}