#[cfg(feature = "oauth")]
fn main() -> Result<(), Box<dyn std::error::Error>> {
use gouqi::{Credentials, Jira};
use std::env;
use std::fs;
let host = env::var("JIRA_HOST").expect("JIRA_HOST environment variable not set");
let consumer_key = env::var("JIRA_OAUTH_CONSUMER_KEY")
.expect("JIRA_OAUTH_CONSUMER_KEY environment variable not set");
let private_key_path = env::var("JIRA_OAUTH_PRIVATE_KEY_PATH")
.expect("JIRA_OAUTH_PRIVATE_KEY_PATH environment variable not set");
let access_token = env::var("JIRA_OAUTH_ACCESS_TOKEN")
.expect("JIRA_OAUTH_ACCESS_TOKEN environment variable not set");
let access_token_secret = env::var("JIRA_OAUTH_ACCESS_TOKEN_SECRET")
.expect("JIRA_OAUTH_ACCESS_TOKEN_SECRET environment variable not set");
let private_key_pem =
fs::read_to_string(&private_key_path).expect("Failed to read private key file");
println!("Setting up OAuth 1.0a authentication...");
println!("Host: {}", host);
println!("Consumer Key: {}", consumer_key);
println!("Private Key Path: {}", private_key_path);
let credentials = Credentials::OAuth1a {
consumer_key,
private_key_pem,
access_token,
access_token_secret,
};
let jira = Jira::new(host, credentials)?;
println!("\nTesting OAuth authentication...");
match jira.session() {
Ok(session) => {
println!("✓ Authentication successful!");
println!(" Logged in as: {}", session.name);
}
Err(e) => {
eprintln!("✗ Authentication failed: {}", e);
eprintln!("\nTroubleshooting:");
eprintln!("1. Verify your OAuth credentials are correct");
eprintln!("2. Ensure the access token hasn't expired");
eprintln!("3. Check that the consumer key matches your Jira application link");
eprintln!("4. Verify the private key is in correct PEM format");
return Err(e.into());
}
}
println!("\nSearching for issues...");
match jira
.search()
.list("order by created DESC", &Default::default())
{
Ok(results) => {
println!("✓ Found {} issues", results.total);
if let Some(first_issue) = results.issues.first() {
let summary = first_issue
.summary()
.unwrap_or_else(|| "No summary".to_string());
println!(" Latest issue: {} - {}", first_issue.key, summary);
}
}
Err(e) => {
eprintln!("✗ Search failed: {}", e);
}
}
Ok(())
}
#[cfg(not(feature = "oauth"))]
fn main() {
eprintln!("This example requires the 'oauth' feature to be enabled.");
eprintln!("Run with: cargo run --example oauth_example --features oauth");
std::process::exit(1);
}