Skip to content

Commit

Permalink
feat: add command to download run artifacts
Browse files Browse the repository at this point in the history
  • Loading branch information
loispostula committed Aug 13, 2024
1 parent a93973e commit 8422da5
Show file tree
Hide file tree
Showing 5 changed files with 123 additions and 4 deletions.
4 changes: 3 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ description = "Command line interface for helping FSLABS ci"
license = "MIT OR Apache-2.0"
publish = ["foresight-mining-software-corporation"]

[patch.crates-io]
octocrab = { path = "../octocrab" }
#[patch.crates-io]
#octocrab = { path = "../octocrab" }
[dependencies]
base64 = "0.21"
clap = { version = "4.5.0", features = ["derive", "env"] }
Expand Down Expand Up @@ -41,7 +41,7 @@ quick-xml = {version = "0.35.0", features = ["serialize"] }
url = "2.5.0"
itertools = "0.12"
num = "0.4.1"
octocrab = "0.38"
octocrab = "0.39"
ignore = "0.4.22"
object_store = { version = "0.9.1", features = ["azure"]}
toml = "0.8.12"
Expand Down
109 changes: 109 additions & 0 deletions src/commands/download_artifacts/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
use crate::PrettyPrintable;
use clap::Parser;
use indicatif::{ProgressBar, ProgressStyle};
use octocrab::models::workflows::WorkflowListArtifact;
use octocrab::Octocrab;
use serde::Serialize;
use std::fmt::{Display, Formatter};
use std::fs::File;
use std::io::{self, Cursor, Write};
use std::path::PathBuf;
use zip::ZipArchive;

#[derive(Debug, Parser)]
#[command(about = "Download github action artifacts")]
pub struct Options {
#[arg(long)]
output: PathBuf,
#[arg(long, default_value_t = true)]
unzip: bool,
#[arg(long, env = "GITHUB_TOKEN")]
github_token: String,
#[arg()]
owner: String,
#[arg()]
repo: String,
#[arg()]
run_id: u64,
}

#[derive(Serialize)]
pub struct GenerateResult {}

impl Display for GenerateResult {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
write!(f, "")
}
}

impl PrettyPrintable for GenerateResult {
fn pretty_print(&self) -> String {
"".to_string()
}
}

pub async fn download_artifacts(
options: Box<Options>,
_working_directory: PathBuf,
) -> anyhow::Result<GenerateResult> {
// We have a github token we should try to update the pr
let octocrab = Octocrab::builder()
.personal_token(options.github_token)
.build()?;
let page = octocrab
.actions()
.list_workflow_run_artifacts(
options.owner.clone(),
options.repo.clone(),
options.run_id.into(),
)
.send()
.await?;
let artifacts = octocrab
.all_pages::<WorkflowListArtifact>(page.value.expect("they should have a page"))
.await?;

let pb = ProgressBar::new(artifacts.len() as u64).with_style(ProgressStyle::with_template(
"{spinner} {wide_msg} {pos}/{len}",
)?);
for artifact in artifacts {
pb.inc(1);
pb.set_message(format!("{}", artifact.name));
let artifact_data = octocrab
.actions()
.download_artifact(
options.owner.clone(),
options.repo.clone(),
artifact.id,
octocrab::params::actions::ArchiveFormat::Zip,
)
.await?;
if options.unzip {
let cursor = Cursor::new(artifact_data);
let mut zip = ZipArchive::new(cursor)?;

for i in 0..zip.len() {
let mut file = zip.by_index(i)?;
let file_name = match file.enclosed_name() {
Some(name) => name.to_owned(),
None => continue,
};
let outpath = options.output.join(file_name);
if file.is_dir() {
std::fs::create_dir_all(&outpath)?;
} else {
if let Some(parent) = outpath.parent() {
std::fs::create_dir_all(&parent)?;
}
let mut outfile = File::create(&outpath)?;
io::copy(&mut file, &mut outfile)?;
}
}
} else {
let outpath = options.output.join(format!("{}.zip", artifact.name));
let mut outfile = File::create(&outpath)?;
outfile.write_all(&artifact_data)?;
}
}
Ok(GenerateResult {})
}
1 change: 1 addition & 0 deletions src/commands/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
pub mod check_workspace;
pub mod download_artifacts;
pub mod generate_wix;
pub mod generate_workflow;
pub mod summaries;
7 changes: 7 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ use log4rs::encode::pattern::PatternEncoder;
use serde::Serialize;

use crate::commands::check_workspace::{check_workspace, Options as CheckWorkspaceOptions};
use crate::commands::download_artifacts::{
download_artifacts, Options as DownloadArtifactsOptions,
};
use crate::commands::generate_wix::{generate_wix, Options as GenerateWixOptions};
use crate::commands::generate_workflow::{generate_workflow, Options as GenerateWorkflowOptions};
use crate::commands::summaries::{summaries, Options as SummariesOptions};
Expand Down Expand Up @@ -54,6 +57,7 @@ enum Commands {
GenerateReleaseWorkflow(Box<GenerateWorkflowOptions>),
GenerateWix(Box<GenerateWixOptions>),
Summaries(Box<SummariesOptions>),
DownloadArtifacts(Box<DownloadArtifactsOptions>),
}

pub fn setup_logging(verbosity: u8) {
Expand Down Expand Up @@ -120,6 +124,9 @@ async fn main() {
Commands::Summaries(options) => summaries(options, working_directory)
.await
.map(|r| display_results(cli.json, cli.pretty_print, r)),
Commands::DownloadArtifacts(options) => download_artifacts(options, working_directory)
.await
.map(|r| display_results(cli.json, cli.pretty_print, r)),
};
match result {
Ok(r) => {
Expand Down

0 comments on commit 8422da5

Please sign in to comment.