Skip to content

Commit 5d4c776

Browse files
committed
rust: move release artifact upload to own function
In preparation of introducing a new caller outside the existing loop.
1 parent 92207b5 commit 5d4c776

File tree

1 file changed

+48
-36
lines changed

1 file changed

+48
-36
lines changed

src/github.rs

Lines changed: 48 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,10 @@ use {
77
anyhow::{anyhow, Result},
88
clap::ArgMatches,
99
futures::StreamExt,
10-
octocrab::{models::workflows::WorkflowListArtifact, Octocrab, OctocrabBuilder},
10+
octocrab::{
11+
models::{repos::Release, workflows::WorkflowListArtifact},
12+
Octocrab, OctocrabBuilder,
13+
},
1114
rayon::prelude::*,
1215
std::{
1316
collections::{BTreeMap, BTreeSet},
@@ -26,6 +29,48 @@ async fn fetch_artifact(client: &Octocrab, artifact: WorkflowListArtifact) -> Re
2629
Ok(res.bytes().await?)
2730
}
2831

32+
async fn upload_release_artifact(
33+
client: &Octocrab,
34+
release: &Release,
35+
filename: &str,
36+
data: Vec<u8>,
37+
dry_run: bool,
38+
) -> Result<()> {
39+
if release.assets.iter().any(|asset| asset.name == filename) {
40+
println!("release asset {} already present; skipping", filename);
41+
return Ok(());
42+
}
43+
44+
let mut url = release.upload_url.clone();
45+
let path = url.path().to_string();
46+
47+
if let Some(path) = path.strip_suffix("%7B") {
48+
url.set_path(path);
49+
}
50+
51+
url.query_pairs_mut().clear().append_pair("name", filename);
52+
53+
println!("uploading to {}", url);
54+
55+
let request = client
56+
.request_builder(url, reqwest::Method::POST)
57+
.header("Content-Length", data.len())
58+
.header("Content-Type", "application/x-tar")
59+
.body(data);
60+
61+
if dry_run {
62+
return Ok(());
63+
}
64+
65+
let response = client.execute(request).await?;
66+
67+
if !response.status().is_success() {
68+
return Err(anyhow!("HTTP {}", response.status()));
69+
}
70+
71+
Ok(())
72+
}
73+
2974
pub async fn command_fetch_release_distributions(args: &ArgMatches) -> Result<()> {
3075
let dest_dir = PathBuf::from(args.value_of("dest").expect("dest directory should be set"));
3176
let org = args
@@ -287,42 +332,9 @@ pub async fn command_upload_release_distributions(args: &ArgMatches) -> Result<(
287332
continue;
288333
}
289334

290-
if release.assets.iter().any(|asset| asset.name == dest) {
291-
println!("release asset {} already present; skipping", dest);
292-
continue;
293-
}
294-
295-
let path = dist_dir.join(&source);
296-
let file_data = std::fs::read(&path)?;
297-
298-
let mut url = release.upload_url.clone();
299-
let path = url.path().to_string();
300-
301-
if let Some(path) = path.strip_suffix("%7B") {
302-
url.set_path(path);
303-
}
304-
305-
url.query_pairs_mut()
306-
.clear()
307-
.append_pair("name", dest.as_str());
335+
let file_data = std::fs::read(dist_dir.join(&source))?;
308336

309-
println!("uploading {} to {}", source, url);
310-
311-
let request = client
312-
.request_builder(url, reqwest::Method::POST)
313-
.header("Content-Length", file_data.len())
314-
.header("Content-Type", "application/x-tar")
315-
.body(file_data);
316-
317-
if dry_run {
318-
continue;
319-
}
320-
321-
let response = client.execute(request).await?;
322-
323-
if !response.status().is_success() {
324-
return Err(anyhow!("HTTP {}", response.status()));
325-
}
337+
upload_release_artifact(&client, &release, &dest, file_data, dry_run).await?;
326338
}
327339

328340
Ok(())

0 commit comments

Comments
 (0)