cyrup_release 0.5.4

Production-quality release management for Rust workspaces
Documentation
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
//! Cargo publishing operations with retry logic and error handling.
//!
//! This module provides robust cargo publish operations with rate limiting,
//! retry logic, and comprehensive error handling for crates.io publishing.

use crate::error::{Result, PublishError};
use crate::workspace::PackageInfo;
use semver::Version;
use std::process::Stdio;
use std::time::Duration;
use tokio::process::Command;
use tokio::time::{sleep, timeout};

/// Cargo publisher with retry logic and rate limiting
#[derive(Debug, Clone)]
pub struct CargoPublisher {
    /// Maximum number of retry attempts
    max_retries: usize,
    /// Base delay between retries (exponential backoff)
    base_retry_delay: Duration,
    /// Timeout for individual operations
    operation_timeout: Duration,
    /// Whether to use dry-run validation
    dry_run_validation: bool,
}

impl Default for CargoPublisher {
    fn default() -> Self {
        Self {
            max_retries: 3,
            base_retry_delay: Duration::from_secs(5),
            operation_timeout: Duration::from_secs(300), // 5 minutes
            dry_run_validation: true,
        }
    }
}

/// Result of a cargo publish operation
#[derive(Debug, Clone)]
pub struct PublishResult {
    /// Package name that was published
    pub package_name: String,
    /// Version that was published
    pub version: Version,
    /// Duration of the publish operation
    pub duration: Duration,
    /// Number of retry attempts made
    pub retry_attempts: usize,
    /// Any warnings from cargo publish
    pub warnings: Vec<String>,
    /// Whether this was a dry run
    pub dry_run: bool,
}

/// Result of a cargo yank operation
#[derive(Debug, Clone)]
pub struct YankResult {
    /// Package name that was yanked
    pub package_name: String,
    /// Version that was yanked
    pub version: Version,
    /// Duration of the yank operation
    pub duration: Duration,
    /// Whether the package was successfully yanked
    pub success: bool,
}

/// Configuration for publishing operations
#[derive(Debug, Clone)]
pub struct PublishConfig {
    /// Registry to publish to (defaults to crates.io)
    pub registry: Option<String>,
    /// Whether to perform dry run validation first
    pub dry_run_first: bool,
    /// Whether to allow dirty working directory
    pub allow_dirty: bool,
    /// Additional cargo publish arguments
    pub additional_args: Vec<String>,
    /// Token for authentication (if not using cargo login)
    pub token: Option<String>,
}

impl Default for PublishConfig {
    fn default() -> Self {
        Self {
            registry: None,
            dry_run_first: true,
            allow_dirty: false,
            additional_args: Vec::new(),
            token: None,
        }
    }
}

impl CargoPublisher {
    /// Create a new cargo publisher with default configuration
    pub fn new() -> Self {
        Self::default()
    }

    /// Create a cargo publisher with custom retry configuration
    pub fn with_retry_config(
        max_retries: usize,
        base_delay: Duration,
        timeout: Duration,
    ) -> Self {
        Self {
            max_retries,
            base_retry_delay: base_delay,
            operation_timeout: timeout,
            dry_run_validation: true,
        }
    }

    /// Publish a package to crates.io
    pub async fn publish_package(
        &self,
        package_info: &PackageInfo,
        config: &PublishConfig,
    ) -> Result<PublishResult> {
        let start_time = std::time::Instant::now();
        let package_name = &package_info.name;
        let version = Version::parse(&package_info.version)
            .map_err(|e| PublishError::PublishFailed {
                package: package_name.clone(),
                reason: format!("Invalid version '{}': {}", package_info.version, e),
            })?;

        let mut warnings = Vec::new();
        let mut retry_attempts = 0;

        // Perform dry run validation if requested
        if config.dry_run_first {
            self.validate_package_dry_run(package_info, config).await?;
        }

        // Attempt publish with retry logic
        let _result = self.retry_with_backoff(
            || self.attempt_publish(package_info, config),
            &mut retry_attempts,
            &mut warnings,
        ).await?;

        let duration = start_time.elapsed();

        Ok(PublishResult {
            package_name: package_name.clone(),
            version,
            duration,
            retry_attempts,
            warnings,
            dry_run: false,
        })
    }

    /// Perform dry run validation of a package
    pub async fn validate_package_dry_run(
        &self,
        package_info: &PackageInfo,
        config: &PublishConfig,
    ) -> Result<PublishResult> {
        let start_time = std::time::Instant::now();
        let package_name = &package_info.name;
        let version = Version::parse(&package_info.version)
            .map_err(|e| PublishError::DryRunFailed {
                package: package_name.clone(),
                reason: format!("Invalid version '{}': {}", package_info.version, e),
            })?;

        let mut cmd = self.build_publish_command(package_info, config);
        cmd.arg("--dry-run");

        let output = timeout(self.operation_timeout, cmd.output()).await
            .map_err(|_| PublishError::DryRunFailed {
                package: package_name.clone(),
                reason: "Dry run timed out".to_string(),
            })?
            .map_err(|e| PublishError::DryRunFailed {
                package: package_name.clone(),
                reason: format!("Failed to execute cargo publish: {}", e),
            })?;

        if !output.status.success() {
            let stderr = String::from_utf8_lossy(&output.stderr);
            return Err(PublishError::DryRunFailed {
                package: package_name.clone(),
                reason: stderr.to_string(),
            }.into());
        }

        let stdout = String::from_utf8_lossy(&output.stdout);
        let warnings = self.extract_warnings(&stdout);

        let duration = start_time.elapsed();

        Ok(PublishResult {
            package_name: package_name.clone(),
            version,
            duration,
            retry_attempts: 0,
            warnings,
            dry_run: true,
        })
    }

    /// Yank a published package version
    pub async fn yank_package(
        &self,
        package_name: &str,
        version: &Version,
        config: &PublishConfig,
    ) -> Result<YankResult> {
        let start_time = std::time::Instant::now();

        let mut cmd = Command::new("cargo");
        cmd.arg("yank")
            .arg("--vers")
            .arg(version.to_string())
            .arg(package_name)
            .stdout(Stdio::piped())
            .stderr(Stdio::piped());

        // Add registry if specified
        if let Some(ref registry) = config.registry {
            cmd.arg("--registry").arg(registry);
        }

        // Add token if specified
        if let Some(ref token) = config.token {
            cmd.arg("--token").arg(token);
        }

        let output = timeout(self.operation_timeout, cmd.output()).await
            .map_err(|_| PublishError::YankFailed {
                package: package_name.to_string(),
                version: version.to_string(),
                reason: "Yank operation timed out".to_string(),
            })?
            .map_err(|e| PublishError::YankFailed {
                package: package_name.to_string(),
                version: version.to_string(),
                reason: format!("Failed to execute cargo yank: {}", e),
            })?;

        let success = output.status.success();
        if !success {
            let stderr = String::from_utf8_lossy(&output.stderr);
            return Err(PublishError::YankFailed {
                package: package_name.to_string(),
                version: version.to_string(),
                reason: stderr.to_string(),
            }.into());
        }

        let duration = start_time.elapsed();

        Ok(YankResult {
            package_name: package_name.to_string(),
            version: version.clone(),
            duration,
            success,
        })
    }

    /// Attempt a single publish operation
    async fn attempt_publish(
        &self,
        package_info: &PackageInfo,
        config: &PublishConfig,
    ) -> Result<()> {
        let mut cmd = self.build_publish_command(package_info, config);

        let output = timeout(self.operation_timeout, cmd.output()).await
            .map_err(|_| PublishError::PublishFailed {
                package: package_info.name.clone(),
                reason: "Publish operation timed out".to_string(),
            })?
            .map_err(|e| PublishError::PublishFailed {
                package: package_info.name.clone(),
                reason: format!("Failed to execute cargo publish: {}", e),
            })?;

        if !output.status.success() {
            let stderr = String::from_utf8_lossy(&output.stderr);
            
            // Check for specific error types
            if stderr.contains("rate limit") || stderr.contains("too many requests") {
                return Err(PublishError::RateLimitExceeded {
                    retry_after_seconds: 60, // Default retry after 1 minute
                }.into());
            }
            
            if stderr.contains("already published") {
                return Err(PublishError::AlreadyPublished {
                    package: package_info.name.clone(),
                    version: package_info.version.clone(),
                }.into());
            }
            
            if stderr.contains("authentication") || stderr.contains("unauthorized") {
                return Err(PublishError::AuthenticationError.into());
            }

            return Err(PublishError::PublishFailed {
                package: package_info.name.clone(),
                reason: stderr.to_string(),
            }.into());
        }

        Ok(())
    }

    /// Build cargo publish command
    fn build_publish_command(
        &self,
        package_info: &PackageInfo,
        config: &PublishConfig,
    ) -> Command {
        let mut cmd = Command::new("cargo");
        cmd.arg("publish")
            .arg("--manifest-path")
            .arg(&package_info.cargo_toml_path)
            .stdout(Stdio::piped())
            .stderr(Stdio::piped());

        // Add registry if specified
        if let Some(ref registry) = config.registry {
            cmd.arg("--registry").arg(registry);
        }

        // Add token if specified
        if let Some(ref token) = config.token {
            cmd.arg("--token").arg(token);
        }

        // Add allow dirty if specified
        if config.allow_dirty {
            cmd.arg("--allow-dirty");
        }

        // Add additional arguments
        for arg in &config.additional_args {
            cmd.arg(arg);
        }

        cmd
    }

    /// Retry operation with exponential backoff
    async fn retry_with_backoff<F, Fut>(
        &self,
        mut operation: F,
        retry_attempts: &mut usize,
        warnings: &mut Vec<String>,
    ) -> Result<()>
    where
        F: FnMut() -> Fut,
        Fut: std::future::Future<Output = Result<()>>,
    {
        let mut delay = self.base_retry_delay;

        for attempt in 0..=self.max_retries {
            *retry_attempts = attempt;

            match operation().await {
                Ok(()) => return Ok(()),
                Err(e) if attempt == self.max_retries => return Err(e),
                Err(e) => {
                    // Check if this is a retryable error
                    if !self.is_retryable_error(&e) {
                        return Err(e);
                    }

                    warnings.push(format!(
                        "Attempt {} failed: {}. Retrying in {:.1}s...",
                        attempt + 1,
                        e,
                        delay.as_secs_f64()
                    ));

                    sleep(delay).await;
                    delay = std::cmp::min(delay * 2, Duration::from_secs(300)); // Cap at 5 minutes
                }
            }
        }

        unreachable!("Loop should have returned or errored")
    }

    /// Check if an error is retryable
    fn is_retryable_error(&self, error: &crate::error::ReleaseError) -> bool {
        match error {
            crate::error::ReleaseError::Publish(publish_error) => {
                matches!(
                    publish_error,
                    PublishError::NetworkError { .. } |
                    PublishError::RateLimitExceeded { .. }
                )
            }
            _ => false,
        }
    }

    /// Extract warnings from cargo output
    fn extract_warnings(&self, output: &str) -> Vec<String> {
        output
            .lines()
            .filter(|line| line.contains("warning:"))
            .map(|line| line.trim().to_string())
            .collect()
    }

    /// Check if a package is already published
    pub async fn is_package_published(
        &self,
        package_name: &str,
        version: &Version,
    ) -> Result<bool> {
        let mut cmd = Command::new("cargo");
        cmd.arg("search")
            .arg(package_name)
            .arg("--limit")
            .arg("1")
            .stdout(Stdio::piped())
            .stderr(Stdio::piped());

        let output = timeout(Duration::from_secs(30), cmd.output()).await
            .map_err(|_| PublishError::NetworkError {
                reason: "Search operation timed out".to_string(),
            })?
            .map_err(|e| PublishError::NetworkError {
                reason: format!("Failed to execute cargo search: {}", e),
            })?;

        if !output.status.success() {
            return Ok(false); // If search fails, assume not published
        }

        let stdout = String::from_utf8_lossy(&output.stdout);
        
        // Parse search output to check if exact version exists
        for line in stdout.lines() {
            if line.starts_with(package_name) && line.contains(&format!("= \"{}\"", version)) {
                return Ok(true);
            }
        }

        Ok(false)
    }

    /// Get published versions of a package
    pub async fn get_published_versions(&self, package_name: &str) -> Result<Vec<Version>> {
        let mut cmd = Command::new("cargo");
        cmd.arg("search")
            .arg(package_name)
            .arg("--limit")
            .arg("1")
            .stdout(Stdio::piped())
            .stderr(Stdio::piped());

        let output = timeout(Duration::from_secs(30), cmd.output()).await
            .map_err(|_| PublishError::NetworkError {
                reason: "Search operation timed out".to_string(),
            })?
            .map_err(|e| PublishError::NetworkError {
                reason: format!("Failed to execute cargo search: {}", e),
            })?;

        if !output.status.success() {
            return Ok(Vec::new());
        }

        let stdout = String::from_utf8_lossy(&output.stdout);
        let mut versions = Vec::new();

        // Parse versions from search output
        for line in stdout.lines() {
            if line.starts_with(package_name) {
                if let Some(version_part) = line.split("= \"").nth(1) {
                    if let Some(version_str) = version_part.split('"').next() {
                        if let Ok(version) = Version::parse(version_str) {
                            versions.push(version);
                        }
                    }
                }
            }
        }

        Ok(versions)
    }

    /// Update publisher configuration
    pub fn set_max_retries(&mut self, max_retries: usize) {
        self.max_retries = max_retries;
    }

    /// Update retry delay
    pub fn set_retry_delay(&mut self, delay: Duration) {
        self.base_retry_delay = delay;
    }

    /// Update operation timeout
    pub fn set_timeout(&mut self, timeout: Duration) {
        self.operation_timeout = timeout;
    }

    /// Enable or disable dry run validation
    pub fn set_dry_run_validation(&mut self, enabled: bool) {
        self.dry_run_validation = enabled;
    }
}

impl PublishResult {
    /// Check if the publish was successful
    pub fn is_successful(&self) -> bool {
        !self.dry_run // Real publishes are successful if they complete
    }

    /// Get a summary of the publish operation
    pub fn summary(&self) -> String {
        let status = if self.dry_run { "validated" } else { "published" };
        let retry_info = if self.retry_attempts > 0 {
            format!(" (after {} retries)", self.retry_attempts)
        } else {
            String::new()
        };

        format!(
            "Successfully {} {}@{} in {:.2}s{}",
            status,
            self.package_name,
            self.version,
            self.duration.as_secs_f64(),
            retry_info
        )
    }

    /// Format detailed report
    pub fn format_report(&self) -> String {
        let mut report = self.summary();
        
        if !self.warnings.is_empty() {
            report.push_str("\nWarnings:\n");
            for warning in &self.warnings {
                report.push_str(&format!("  - {}\n", warning));
            }
        }

        report
    }
}

impl YankResult {
    /// Format yank result for display
    pub fn format_result(&self) -> String {
        if self.success {
            format!(
                "✅ Successfully yanked {}@{} in {:.2}s",
                self.package_name,
                self.version,
                self.duration.as_secs_f64()
            )
        } else {
            format!(
                "❌ Failed to yank {}@{}",
                self.package_name,
                self.version
            )
        }
    }
}