lmrc-gitlab 0.3.16

GitLab API client library for the LMRC Stack - comprehensive Rust library for programmatic control of GitLab via its API
Documentation
//! Basic usage example for the gitlab-manager library.
//!
//! This example demonstrates the core features of the library including:
//! - Creating a client
//! - Listing pipelines with filters
//! - Getting pipeline details
//! - Retrying and canceling pipelines
//!
//! To run this example:
//! ```bash
//! export GITLAB_TOKEN="your-token"
//! cargo run --example basic_usage
//! ```

use lmrc_gitlab::{GitLabClient, Result, models::PipelineStatus};

#[tokio::main]
async fn main() -> Result<()> {
    // Get configuration from environment
    let gitlab_url =
        std::env::var("GITLAB_URL").unwrap_or_else(|_| "https://gitlab.com".to_string());
    let gitlab_token =
        std::env::var("GITLAB_TOKEN").expect("GITLAB_TOKEN environment variable must be set");
    let project =
        std::env::var("GITLAB_PROJECT").unwrap_or_else(|_| "gitlab-org/gitlab".to_string());

    println!("Connecting to GitLab at {}", gitlab_url);

    // Create the client
    let client = GitLabClient::new(&gitlab_url, &gitlab_token)?;

    println!("\n=== Example 1: List all pipelines ===");
    let all_pipelines = client.pipelines(&project).limit(5).list().await?;

    println!("Found {} pipelines:", all_pipelines.len());
    for pipeline in &all_pipelines {
        println!(
            "  - ID: {}, Status: {}, Ref: {}, Created: {}",
            pipeline.id, pipeline.status, pipeline.ref_name, pipeline.created_at
        );
    }

    println!("\n=== Example 2: List failed pipelines on main branch ===");
    let failed_pipelines = client
        .pipelines(&project)
        .status(PipelineStatus::Failed)
        .ref_name("main")
        .limit(3)
        .list()
        .await?;

    println!("Found {} failed pipelines on main:", failed_pipelines.len());
    for pipeline in &failed_pipelines {
        println!("  - ID: {}, Created: {}", pipeline.id, pipeline.created_at);
    }

    println!("\n=== Example 3: Get detailed pipeline information ===");
    if let Some(pipeline_basic) = all_pipelines.first() {
        let pipeline_id = pipeline_basic.id;
        println!("Getting details for pipeline #{}...", pipeline_id);

        let pipeline = client.pipeline(&project, pipeline_id).get().await?;

        println!("Pipeline Details:");
        println!("  ID: {}", pipeline.id);
        println!("  Status: {}", pipeline.status);
        println!("  Ref: {}", pipeline.ref_name);
        println!("  SHA: {}", pipeline.sha);
        println!("  Created: {}", pipeline.created_at);
        println!("  Updated: {}", pipeline.updated_at);

        if let Some(started_at) = pipeline.started_at {
            println!("  Started: {}", started_at);
        }

        if let Some(finished_at) = pipeline.finished_at {
            println!("  Finished: {}", finished_at);
        }

        if let Some(duration) = pipeline.duration {
            println!("  Duration: {:.2}s", duration);
        }

        if let Some(user) = pipeline.user {
            println!("  Triggered by: {} (@{})", user.name, user.username);
        }

        println!("  Web URL: {}", pipeline.web_url);
    }

    println!("\n=== Example 4: Check pipeline status ===");
    if let Some(pipeline_basic) = all_pipelines.first() {
        let status = pipeline_basic.status;
        println!("Pipeline #{} status: {}", pipeline_basic.id, status);
        println!("  Is finished? {}", status.is_finished());
        println!("  Is active? {}", status.is_active());
        println!("  Is successful? {}", status.is_successful());
        println!("  Is failed? {}", status.is_failed());
    }

    // Uncomment to try retrying a failed pipeline
    // println!("\n=== Example 5: Retry a failed pipeline ===");
    // if let Some(failed) = failed_pipelines.first() {
    //     println!("Retrying pipeline #{}...", failed.id);
    //     let retried = client.pipeline(&project, failed.id).retry().await?;
    //     println!("Pipeline retried! New status: {}", retried.status);
    // }

    // Uncomment to try canceling a running pipeline
    // println!("\n=== Example 6: Cancel a running pipeline ===");
    // let running = client
    //     .pipelines(&project)
    //     .status(PipelineStatus::Running)
    //     .limit(1)
    //     .list()
    //     .await?;
    //
    // if let Some(running_pipeline) = running.first() {
    //     println!("Canceling pipeline #{}...", running_pipeline.id);
    //     let canceled = client.pipeline(&project, running_pipeline.id).cancel().await?;
    //     println!("Pipeline canceled! Status: {}", canceled.status);
    // }

    println!("\n✅ Examples completed successfully!");

    Ok(())
}