azure-devops 0.1.0

Idiomatic Rust client for Azure DevOps REST APIs
Documentation
use azure_devops::work_items::models::{JsonPatchOperation, Wiql};
use azure_devops::AzureDevOps;

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    let pat =
        std::env::var("AZURE_DEVOPS_PAT").expect("AZURE_DEVOPS_PAT environment variable not set");
    let org = std::env::var("AZURE_DEVOPS_ORG").unwrap_or_else(|_| "my-company".to_string());
    let project = std::env::var("AZURE_DEVOPS_PROJECT").unwrap_or_else(|_| "MyProject".to_string());

    let client = AzureDevOps::builder()
        .organization(&org)
        .pat(&pat)
        .build()?;

    // ── Get a work item ─────────────────────────────────────────────
    // Replace 123 with a real work item ID
    // let wi = client.work_items().get(123).await?;
    // println!("Work Item: {} — {:?}", wi.id.unwrap(), wi.fields);

    // ── WIQL query ──────────────────────────────────────────────────
    let query = Wiql::new(format!(
        "SELECT [System.Id], [System.Title] FROM WorkItems \
         WHERE [System.TeamProject] = '{project}' ORDER BY [System.Id] DESC"
    ));
    let result = client.work_items().query_by_wiql(&project, &query).await?;
    println!(
        "Found {} work items",
        result.work_items.as_ref().map_or(0, |w| w.len())
    );
    for wi_ref in result.work_items.iter().flatten().take(5) {
        println!(
            "  • #{} — {}",
            wi_ref.id.unwrap_or(0),
            wi_ref.url.as_deref().unwrap_or("")
        );
    }

    // ── Work item types ─────────────────────────────────────────────
    let types = client.work_items().get_work_item_types(&project).await?;
    println!("\nWork item types in {project}:");
    for t in &types {
        println!(
            "  • {} ({})",
            t.name.as_deref().unwrap_or("?"),
            t.reference_name.as_deref().unwrap_or("?")
        );
    }

    // ── Create a work item ──────────────────────────────────────────
    let patch = vec![
        JsonPatchOperation::add("/fields/System.Title", "Test bug from azure-devops crate"),
        JsonPatchOperation::add(
            "/fields/System.Description",
            "This bug was created by the Rust API client example.",
        ),
    ];
    let wi = client.work_items().create(&project, "Bug", &patch).await?;
    println!("\nCreated work item #{}", wi.id.unwrap());

    Ok(())
}