Clear publish progress bar on retry (#7921)

This commit is contained in:
konsti 2024-10-04 13:15:26 +02:00 committed by GitHub
parent d80139698d
commit fc495876cf
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 17 additions and 14 deletions

View File

@ -22,7 +22,7 @@ use std::{env, fmt, io};
use thiserror::Error; use thiserror::Error;
use tokio::io::AsyncReadExt; use tokio::io::AsyncReadExt;
use tokio_util::io::ReaderStream; use tokio_util::io::ReaderStream;
use tracing::{debug, enabled, trace, warn, Level}; use tracing::{debug, enabled, trace, Level};
use url::Url; use url::Url;
use uv_client::UvRetryableStrategy; use uv_client::UvRetryableStrategy;
use uv_configuration::{KeyringProviderType, TrustedPublishing}; use uv_configuration::{KeyringProviderType, TrustedPublishing};
@ -30,7 +30,7 @@ use uv_distribution_filename::{DistFilename, SourceDistExtension, SourceDistFile
use uv_fs::{ProgressReader, Simplified}; use uv_fs::{ProgressReader, Simplified};
use uv_metadata::read_metadata_async_seek; use uv_metadata::read_metadata_async_seek;
use uv_pypi_types::{Metadata23, MetadataError}; use uv_pypi_types::{Metadata23, MetadataError};
use uv_warnings::warn_user_once; use uv_warnings::{warn_user, warn_user_once};
pub use trusted_publishing::TrustedPublishingToken; pub use trusted_publishing::TrustedPublishingToken;
@ -95,7 +95,7 @@ pub trait Reporter: Send + Sync + 'static {
fn on_progress(&self, name: &str, id: usize); fn on_progress(&self, name: &str, id: usize);
fn on_download_start(&self, name: &str, size: Option<u64>) -> usize; fn on_download_start(&self, name: &str, size: Option<u64>) -> usize;
fn on_download_progress(&self, id: usize, inc: u64); fn on_download_progress(&self, id: usize, inc: u64);
fn on_download_complete(&self); fn on_download_complete(&self, id: usize);
} }
impl PublishSendError { impl PublishSendError {
@ -298,7 +298,7 @@ pub async fn upload(
let mut attempt = 0; let mut attempt = 0;
loop { loop {
attempt += 1; attempt += 1;
let request = build_request( let (request, idx) = build_request(
file, file,
filename, filename,
registry, registry,
@ -312,8 +312,9 @@ pub async fn upload(
.map_err(|err| PublishError::PublishPrepare(file.to_path_buf(), Box::new(err)))?; .map_err(|err| PublishError::PublishPrepare(file.to_path_buf(), Box::new(err)))?;
let result = request.send().await; let result = request.send().await;
if attempt <= retries && UvRetryableStrategy.handle(&result) == Some(Retryable::Transient) { if attempt < retries && UvRetryableStrategy.handle(&result) == Some(Retryable::Transient) {
warn!("Transient request failure for {}, retrying", registry); reporter.on_download_complete(idx);
warn_user!("Transient request failure for {}, retrying", registry);
continue; continue;
} }
@ -478,6 +479,9 @@ async fn form_metadata(
Ok(form_metadata) Ok(form_metadata)
} }
/// Build the upload request.
///
/// Returns the request and the reporter progress bar id.
async fn build_request( async fn build_request(
file: &Path, file: &Path,
filename: &DistFilename, filename: &DistFilename,
@ -487,7 +491,7 @@ async fn build_request(
password: Option<&str>, password: Option<&str>,
form_metadata: &[(&'static str, String)], form_metadata: &[(&'static str, String)],
reporter: Arc<impl Reporter>, reporter: Arc<impl Reporter>,
) -> Result<RequestBuilder, PublishPrepareError> { ) -> Result<(RequestBuilder, usize), PublishPrepareError> {
let mut form = reqwest::multipart::Form::new(); let mut form = reqwest::multipart::Form::new();
for (key, value) in form_metadata { for (key, value) in form_metadata {
form = form.text(*key, value.clone()); form = form.text(*key, value.clone());
@ -534,7 +538,7 @@ async fn build_request(
let credentials = BASE64_STANDARD.encode(format!("{username}:{password}")); let credentials = BASE64_STANDARD.encode(format!("{username}:{password}"));
request = request.header(AUTHORIZATION, format!("Basic {credentials}")); request = request.header(AUTHORIZATION, format!("Basic {credentials}"));
} }
Ok(request) Ok((request, idx))
} }
/// Returns `true` if the file was newly uploaded and `false` if it already existed. /// Returns `true` if the file was newly uploaded and `false` if it already existed.
@ -636,7 +640,7 @@ mod tests {
0 0
} }
fn on_download_progress(&self, _id: usize, _inc: u64) {} fn on_download_progress(&self, _id: usize, _inc: u64) {}
fn on_download_complete(&self) {} fn on_download_complete(&self, _id: usize) {}
} }
/// Snapshot the data we send for an upload request for a source distribution. /// Snapshot the data we send for an upload request for a source distribution.
@ -700,7 +704,7 @@ mod tests {
project_urls: Source, https://github.com/unknown/tqdm project_urls: Source, https://github.com/unknown/tqdm
"###); "###);
let request = build_request( let (request, _) = build_request(
&file, &file,
&filename, &filename,
&Url::parse("https://example.org/upload").unwrap(), &Url::parse("https://example.org/upload").unwrap(),
@ -843,7 +847,7 @@ mod tests {
project_urls: wiki, https://github.com/tqdm/tqdm/wiki project_urls: wiki, https://github.com/tqdm/tqdm/wiki
"###); "###);
let request = build_request( let (request, _) = build_request(
&file, &file,
&filename, &filename,
&Url::parse("https://example.org/upload").unwrap(), &Url::parse("https://example.org/upload").unwrap(),

View File

@ -522,9 +522,8 @@ impl uv_publish::Reporter for PublishReporter {
self.reporter.on_download_progress(id, inc); self.reporter.on_download_progress(id, inc);
} }
fn on_download_complete(&self) { fn on_download_complete(&self, id: usize) {
self.reporter.root.set_message(""); self.reporter.on_download_complete(id);
self.reporter.root.finish_and_clear();
} }
} }