Last active
March 21, 2023 01:26
-
-
Save chantra/e0f07534cf4306a1fdbd68c80bebac52 to your computer and use it in GitHub Desktop.
examples/get_artifact_for_run.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use std::env; | |
#[cfg(feature = "httpcache")] | |
use octorust::http_cache::FileBasedCache; | |
use octorust::{ | |
auth::{Credentials, InstallationTokenGenerator, JWTCredentials}, | |
Client, | |
}; | |
#[tokio::main] | |
async fn main() -> Result<(), Box<dyn std::error::Error>> { | |
let app_id_str = env::var("GH_APP_ID").unwrap(); | |
let app_id = app_id_str.parse::<i64>().unwrap(); | |
let app_installation_id_str = env::var("GH_INSTALLATION_ID").unwrap(); | |
let app_installation_id = app_installation_id_str.parse::<i64>().unwrap(); | |
let encoded_private_key = env::var("GH_PRIVATE_KEY").unwrap(); | |
let private_key = base64::decode(encoded_private_key).unwrap(); | |
// Decode the key. | |
let key = nom_pem::decode_block(&private_key).unwrap(); | |
// Get the JWT credentials. | |
let jwt = JWTCredentials::new(app_id, key.data).unwrap(); | |
let http = reqwest::Client::builder().redirect(reqwest::redirect::Policy::none()).build()?; | |
let retry_policy = | |
reqwest_retry::policies::ExponentialBackoff::builder().build_with_max_retries(3); | |
let client = reqwest_middleware::ClientBuilder::new(http) | |
// Trace HTTP requests. See the tracing crate to make use of these traces. | |
.with(reqwest_tracing::TracingMiddleware::default()) | |
// Retry failed requests. | |
.with(reqwest_retry::RetryTransientMiddleware::new_with_policy( | |
retry_policy, | |
)) | |
.build(); | |
// Create the HTTP cache. | |
#[cfg(feature = "httpcache")] | |
let mut dir = dirs::home_dir().expect("Expected a home dir"); | |
#[cfg(feature = "httpcache")] | |
dir.push(".cache/github"); | |
#[cfg(feature = "httpcache")] | |
let http_cache = Box::new(FileBasedCache::new(dir)); | |
let token_generator = InstallationTokenGenerator::new(app_installation_id, jwt); | |
#[cfg(not(feature = "httpcache"))] | |
let github = Client::custom( | |
concat!(env!("CARGO_PKG_NAME"), "/", env!("CARGO_PKG_VERSION")), | |
Credentials::InstallationToken(token_generator), | |
client, | |
); | |
#[cfg(feature = "httpcache")] | |
let github = Client::custom( | |
concat!(env!("CARGO_PKG_NAME"), "/", env!("CARGO_PKG_VERSION")), | |
Credentials::InstallationToken(token_generator), | |
client, | |
http_cache, | |
); | |
// List the repos for an org. | |
let artifact = github | |
.actions() | |
.download_artifact( | |
&env::var("GH_USER").unwrap(), | |
&env::var("GH_REPO").unwrap(), | |
env::var("GH_ARTIFACT_ID").unwrap().parse::<i64>().unwrap(), | |
"zip" | |
) | |
.await | |
.unwrap(); | |
println!("artifact header: {:?}", artifact.headers.get("Location")); | |
Ok(()) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment