Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Compress files into a single coverage.zip and update upload logic accordingly #1528

Merged
merged 9 commits into from
Feb 27, 2025
23 changes: 14 additions & 9 deletions qlty-cloud/src/export/coverage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,18 +70,23 @@ impl CoverageExport {
.cloned()
.collect();

compress_files(raw_file_paths, &directory.join("raw_files.zip"))
compress_files(raw_file_paths, &directory.join("raw_files.zip"))?;

let files_to_zip = vec![
"report_files.json.gz",
"file_coverages.json.gz",
"metadata.json",
"raw_files.zip",
]
.iter()
.map(|file| directory.join(file).to_string_lossy().into_owned())
.collect();

compress_files(files_to_zip, &directory.join("coverage.zip"))
}

pub fn total_size_bytes(&self) -> Result<u64> {
let mut bytes: u64 = 0;

bytes += self.read_file("report_files.json.gz")?.len() as u64;
bytes += self.read_file("file_coverages.json.gz")?.len() as u64;
bytes += self.read_file("metadata.json")?.len() as u64;
bytes += self.read_file("raw_files.zip")?.len() as u64;

Ok(bytes)
Ok(self.read_file("coverage.zip")?.len() as u64)
}

pub fn read_file<P: AsRef<Path>>(&self, filename: P) -> Result<Vec<u8>> {
Expand Down
76 changes: 8 additions & 68 deletions qlty-coverage/src/publish/upload.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,63 +12,24 @@ const LEGACY_API_URL: &str = "https://qlty.sh/api";
pub struct Upload {
pub id: String,
pub project_id: String,
pub file_coverages_url: String,
pub report_files_url: String,
pub metadata_url: String,
pub raw_files_url: String,
pub coverage_url: String,
}

impl Upload {
pub fn prepare(token: &str, report: &mut Report) -> Result<Self> {
let response = Self::request_api(&report.metadata, token)?;

let file_coverages_url = response
let coverage_url = response
.get("data")
.and_then(|data| data.get("file_coverages.json.gz"))
.and_then(|data| data.get("coverage.zip"))
.and_then(|upload_url| upload_url.as_str())
.with_context(|| {
format!(
"Unable to find file coverages URL in response body: {:?}",
"Unable to find coverage URL in response body: {:?}",
response
)
})
.context("Failed to extract file coverages URL from response")?;

let report_files_url = response
.get("data")
.and_then(|data| data.get("report_files.json.gz"))
.and_then(|upload_url| upload_url.as_str())
.with_context(|| {
format!(
"Unable to find report files URL in response body: {:?}",
response
)
})
.context("Failed to extract report files URL from response")?;

let metadata_url = response
.get("data")
.and_then(|data| data.get("metadata.json"))
.and_then(|upload_url| upload_url.as_str())
.with_context(|| {
format!(
"Unable to find metadata URL in response body: {:?}",
response
)
})
.context("Failed to extract metadata URL from response")?;

let raw_files_url = response
.get("data")
.and_then(|data| data.get("raw_files.zip"))
.and_then(|upload_url| upload_url.as_str())
.with_context(|| {
format!(
"Unable to find metadata URL in response body: {:?}",
response
)
})
.context("Failed to extract metadata URL from response")?;
.context("Failed to extract coverage URL from response")?;

let id = response
.get("data")
Expand All @@ -90,36 +51,15 @@ impl Upload {
Ok(Self {
id: id.to_string(),
project_id: project_id.to_string(),
file_coverages_url: file_coverages_url.to_string(),
report_files_url: report_files_url.to_string(),
metadata_url: metadata_url.to_string(),
raw_files_url: raw_files_url.to_string(),
coverage_url: coverage_url.to_string(),
})
}

pub fn upload(&self, export: &CoverageExport) -> Result<()> {
self.upload_data(
&self.file_coverages_url,
"application/gzip",
export.read_file(PathBuf::from("file_coverages.json.gz"))?,
)?;

self.upload_data(
&self.report_files_url,
"application/gzip",
export.read_file(PathBuf::from("report_files.json.gz"))?,
)?;

self.upload_data(
&self.metadata_url,
"application/json",
export.read_file(PathBuf::from("metadata.json"))?,
)?;

self.upload_data(
&self.raw_files_url,
&self.coverage_url,
"application/zip",
export.read_file(PathBuf::from("raw_files.zip"))?,
export.read_file(PathBuf::from("coverage.zip"))?,
)?;

Ok(())
Expand Down
Loading