diff --git a/Cargo.lock b/Cargo.lock index fde62a8..11a4cb1 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -43,23 +43,24 @@ dependencies = [ [[package]] name = "anstream" -version = "0.6.4" +version = "0.6.14" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2ab91ebe16eb252986481c5b62f6098f3b698a45e34b5b98200cf20dd2484a44" +checksum = "418c75fa768af9c03be99d17643f93f79bbba589895012a80e3452a19ddda15b" dependencies = [ "anstyle", "anstyle-parse", "anstyle-query", "anstyle-wincon", "colorchoice", + "is_terminal_polyfill", "utf8parse", ] [[package]] name = "anstyle" -version = "1.0.4" +version = "1.0.7" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7079075b41f533b8c61d2a4d073c4676e1f8b249ff94a393b0595db304e0dd87" +checksum = "038dfcf04a5feb68e9c60b21c9625a54c2c0616e79b72b0fd87075a056ae1d1b" [[package]] name = "anstyle-parse" @@ -116,7 +117,7 @@ checksum = "a66537f1bb974b254c98ed142ff995236e81b9d0fe4db0575f46612cb15eb0f9" dependencies = [ "proc-macro2", "quote", - "syn", + "syn 2.0.66", ] [[package]] @@ -238,7 +239,7 @@ dependencies = [ "heck", "proc-macro2", "quote", - "syn", + "syn 2.0.66", ] [[package]] @@ -256,11 +257,13 @@ dependencies = [ "base64", "clap", "git2", + "gql_client", "insta", "itertools", "lazy_static", "log", "md5", + "nestify", "rand", "regex", "reqwest", @@ -268,6 +271,7 @@ dependencies = [ "reqwest-retry", "serde", "serde_json", + "serde_yaml", "sha256", "simplelog", "temp-env", @@ -509,7 +513,7 @@ checksum = "53b153fd91e4b0147f4aced87be237c98248656bb01050b96bf3ee89220a8ddb" dependencies = [ "proc-macro2", "quote", - "syn", + "syn 2.0.66", ] [[package]] @@ -586,6 +590,17 @@ dependencies = [ "url", ] +[[package]] +name = "gql_client" +version = "1.0.7" +source = "git+https://github.com/adriencaccia/gql-client-rs#3d2fe081c197c2244c6a6f5537ac2bac95fa673d" +dependencies = [ + "log", + "reqwest", + "serde", + "serde_json", +] + [[package]] name = "h2" version = "0.3.21" @@ -745,9 +760,9 @@ dependencies = [ [[package]] name = "indexmap" -version = "2.1.0" +version = "2.2.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d530e1a18b1cb4c484e6e34556a0d948706958449fca0cab753d649f2bce3d1f" +checksum = "168fb715dda47215e360912c096649d23d58bf392ac62f73919e831745e40f26" dependencies = [ "equivalent", "hashbrown 0.14.3", @@ -787,6 +802,12 @@ version = "2.9.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8f518f335dce6725a761382244631d86cf0ccb2863413590b31338feb467f9c3" +[[package]] +name = "is_terminal_polyfill" +version = "1.70.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f8478577c03552c21db0e2724ffb8986a5ce7af88107e6be5d2ee6e158c12800" + [[package]] name = "itertools" version = "0.11.0" @@ -798,9 +819,9 @@ dependencies = [ [[package]] name = "itoa" -version = "1.0.9" +version = "1.0.11" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "af150ab688ff2122fcef229be89cb50dd66af9e01a4ff320cc137eecc9bacc38" +checksum = "49f1f14873335454500d59611f1cf4a4b0f786f9ac11f4312a78e4cf2566695b" [[package]] name = "jobserver" @@ -896,9 +917,9 @@ dependencies = [ [[package]] name = "log" -version = "0.4.20" +version = "0.4.21" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b5e6163cb8c49088c2c36f57875e58ccd8c87c7427f7fbd50ea6710b2f3f2e8f" +checksum = "90ed8c1e510134f979dbc4f070f87d4313098b704861a105fe34231c70a3901c" [[package]] name = "md5" @@ -908,9 +929,9 @@ checksum = "490cc448043f947bae3cbee9c203358d62dbee0db12107a74be5c30ccfd09771" [[package]] name = "memchr" -version = "2.6.4" +version = "2.7.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f665ee40bc4a3c5590afb1e9677db74a508659dfd71e126420da8274909a0167" +checksum = "6c8640c5d730cb13ebd907d8d04b52f55ac9a2eec55b440c8892f40d56c76c1d" [[package]] name = "mime" @@ -966,6 +987,18 @@ dependencies = [ "tempfile", ] +[[package]] +name = "nestify" +version = "0.3.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "41d7249f7122d4e8a40f3b1b1850b763d2f864bf8e4b712427f024f8a167ea17" +dependencies = [ + "proc-macro-error", + "proc-macro2", + "quote", + "syn 2.0.66", +] + [[package]] name = "num-conv" version = "0.1.0" @@ -1019,7 +1052,7 @@ checksum = "a948666b637a0f465e8564c73e89d4dde00d72d4d473cc972f390fc3dcee7d9c" dependencies = [ "proc-macro2", "quote", - "syn", + "syn 2.0.66", ] [[package]] @@ -1135,7 +1168,7 @@ dependencies = [ "pest_meta", "proc-macro2", "quote", - "syn", + "syn 2.0.66", ] [[package]] @@ -1179,20 +1212,44 @@ version = "0.2.17" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "5b40af805b3121feab8a3c29f04d8ad262fa8e0561883e7653e024ae4479e6de" +[[package]] +name = "proc-macro-error" +version = "1.0.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "da25490ff9892aab3fcf7c36f08cfb902dd3e71ca0f9f9517bea02a73a5ce38c" +dependencies = [ + "proc-macro-error-attr", + "proc-macro2", + "quote", + "syn 1.0.109", + "version_check", +] + +[[package]] +name = "proc-macro-error-attr" +version = "1.0.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a1be40180e52ecc98ad80b184934baf3d0d29f979574e439af5a55274b35f869" +dependencies = [ + "proc-macro2", + "quote", + "version_check", +] + [[package]] name = "proc-macro2" -version = "1.0.69" +version = "1.0.83" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "134c189feb4956b20f6f547d2cf727d4c0fe06722b20a0eec87ed445a97f92da" +checksum = "0b33eb56c327dec362a9e55b3ad14f9d2f0904fb5a5b03b513ab5465399e9f43" dependencies = [ "unicode-ident", ] [[package]] name = "quote" -version = "1.0.33" +version = "1.0.36" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5267fca4496028628a95160fc423a33e8b2e6af8a5302579e322e4b520293cae" +checksum = "0fa76aaf39101c457836aec0ce2316dbdc3ab723cdda1c6bd4e6ad4208acaca7" dependencies = [ "proc-macro2", ] @@ -1394,9 +1451,9 @@ dependencies = [ [[package]] name = "ryu" -version = "1.0.15" +version = "1.0.18" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1ad4cc8da4ef723ed60bced201181d83791ad433213d8c24efffda1eec85d741" +checksum = "f3cb5ba0dc43242ce17de99c180e96db90b235b8a9fdc9543c96d2209116bd9f" [[package]] name = "schannel" @@ -1438,31 +1495,31 @@ dependencies = [ [[package]] name = "serde" -version = "1.0.192" +version = "1.0.202" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "bca2a08484b285dcb282d0f67b26cadc0df8b19f8c12502c13d966bf9482f001" +checksum = "226b61a0d411b2ba5ff6d7f73a476ac4f8bb900373459cd00fab8512828ba395" dependencies = [ "serde_derive", ] [[package]] name = "serde_derive" -version = "1.0.192" +version = "1.0.202" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d6c7207fbec9faa48073f3e3074cbe553af6ea512d7c21ba46e434e70ea9fbc1" +checksum = "6048858004bcff69094cd972ed40a32500f153bd3be9f716b2eed2e8217c4838" dependencies = [ "proc-macro2", "quote", - "syn", + "syn 2.0.66", ] [[package]] name = "serde_json" -version = "1.0.108" +version = "1.0.117" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3d1c7e3eac408d115102c4c24ad393e0821bb3a5df4d506a80f85f7a742a526b" +checksum = "455182ea6142b14f93f4bc5320a2b31c1f266b66a4a5c858b013302a5d8cbfc3" dependencies = [ - "indexmap 2.1.0", + "indexmap 2.2.6", "itoa", "ryu", "serde", @@ -1480,6 +1537,19 @@ dependencies = [ "serde", ] +[[package]] +name = "serde_yaml" +version = "0.9.34+deprecated" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6a8b1a1a2ebf674015cc02edccce75287f1a0130d394307b36743c2f5d504b47" +dependencies = [ + "indexmap 2.2.6", + "itoa", + "ryu", + "serde", + "unsafe-libyaml", +] + [[package]] name = "sha2" version = "0.10.8" @@ -1517,6 +1587,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "acee08041c5de3d5048c8b3f6f13fafb3026b24ba43c6a695a0c76179b844369" dependencies = [ "log", + "termcolor", "time", ] @@ -1563,9 +1634,19 @@ checksum = "73473c0e59e6d5812c5dfe2a064a6444949f089e20eec9a2e5506596494e4623" [[package]] name = "syn" -version = "2.0.39" +version = "1.0.109" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "72b64191b275b66ffe2469e8af2c1cfe3bafa67b529ead792a6d0160888b4237" +dependencies = [ + "proc-macro2", + "unicode-ident", +] + +[[package]] +name = "syn" +version = "2.0.66" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "23e78b90f2fcf45d3e842032ce32e3f2d1545ba6636271dcbf24fa306d87be7a" +checksum = "c42f3f41a2de00b01c0aaad383c5a45241efc8b2d1eda5661812fda5f3cdcff5" dependencies = [ "proc-macro2", "quote", @@ -1624,6 +1705,15 @@ dependencies = [ "windows-sys 0.52.0", ] +[[package]] +name = "termcolor" +version = "1.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bab24d30b911b2376f3a13cc2cd443142f0c81dda04c118693e35b3835757755" +dependencies = [ + "winapi-util", +] + [[package]] name = "thiserror" version = "1.0.50" @@ -1641,7 +1731,7 @@ checksum = "266b2e40bc00e5a6c09c3584011e08b06f123c00362c92b975ba9843aaaa14b8" dependencies = [ "proc-macro2", "quote", - "syn", + "syn 2.0.66", ] [[package]] @@ -1714,7 +1804,7 @@ checksum = "5b8a1e28f2deaa14e508979454cb3a223b10b938b45af148bc0986de36f1923b" dependencies = [ "proc-macro2", "quote", - "syn", + "syn 2.0.66", ] [[package]] @@ -1792,7 +1882,7 @@ checksum = "34704c8d6ebcbc939824180af020566b01a7c01f80641264eba0999f6c2b6be7" dependencies = [ "proc-macro2", "quote", - "syn", + "syn 2.0.66", ] [[package]] @@ -1852,6 +1942,12 @@ dependencies = [ "tinyvec", ] +[[package]] +name = "unsafe-libyaml" +version = "0.2.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "673aac59facbab8a9007c7f6108d11f63b603f7cabff99fabf650fea5c32b861" + [[package]] name = "url" version = "2.4.1" @@ -1917,7 +2013,7 @@ dependencies = [ "once_cell", "proc-macro2", "quote", - "syn", + "syn 2.0.66", "wasm-bindgen-shared", ] @@ -1951,7 +2047,7 @@ checksum = "c5353b8dab669f5e10f5bd76df26a9360c748f054f862ff5f3f8aae0c7fb3907" dependencies = [ "proc-macro2", "quote", - "syn", + "syn 2.0.66", "wasm-bindgen-backend", "wasm-bindgen-shared", ] @@ -2016,6 +2112,15 @@ version = "0.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" +[[package]] +name = "winapi-util" +version = "0.1.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4d4cc384e1e73b93bafa6fb4f1df8c41695c8a91cf9c4c64358067d15a7b6c6b" +dependencies = [ + "windows-sys 0.52.0", +] + [[package]] name = "winapi-x86_64-pc-windows-gnu" version = "0.4.0" diff --git a/Cargo.toml b/Cargo.toml index 51d5b94..8cf72b2 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -30,9 +30,14 @@ tokio-tar = "0.3.1" md5 = "0.7.0" base64 = "0.21.0" async-compression = { version = "0.4.5", features = ["tokio", "gzip"] } -simplelog = { version = "0.12.1", default-features = false } +simplelog = { version = "0.12.1", default-features = false, features = [ + "termcolor", +] } tempfile = "3.10.0" git2 = "0.18.3" +nestify = "0.3.3" +gql_client = { git = "https://github.com/adriencaccia/gql-client-rs" } +serde_yaml = "0.9.34" [dev-dependencies] temp-env = { version = "0.3.6", features = ["async_closure"] } diff --git a/src/app.rs b/src/app.rs index 2ac8ee2..84e784f 100644 --- a/src/app.rs +++ b/src/app.rs @@ -1,4 +1,4 @@ -use crate::{prelude::*, run}; +use crate::{auth, prelude::*, run}; use clap::{Parser, Subcommand}; #[derive(Parser, Debug)] @@ -11,6 +11,8 @@ struct Cli { enum Commands { /// Run the bench command and upload the results to CodSpeed Run(run::RunArgs), + /// Commands related to authentication with CodSpeed + Auth(auth::AuthArgs), } pub async fn run() -> Result<()> { @@ -18,6 +20,7 @@ pub async fn run() -> Result<()> { match cli.command { Commands::Run(args) => run::run(args).await?, + Commands::Auth(args) => auth::run(args).await?, } Ok(()) } diff --git a/src/auth.rs b/src/auth.rs new file mode 100644 index 0000000..1e0b934 --- /dev/null +++ b/src/auth.rs @@ -0,0 +1,166 @@ +use std::time::Duration; + +use crate::{config::Config, logger::get_local_logger, prelude::*}; +use clap::{Args, Subcommand}; +use gql_client::{Client as GQLClient, ClientConfig}; +use nestify::nest; +use serde::{Deserialize, Serialize}; +use simplelog::CombinedLogger; +use tokio::time::{sleep, Instant}; + +#[derive(Debug, Args)] +pub struct AuthArgs { + /// The URL of the CodSpeed GraphQL API + #[arg(long, env = "CODSPEED_API_URL", global = true, hide = true)] + api_url: Option, + + #[command(subcommand)] + command: AuthCommands, +} + +#[derive(Debug, Subcommand)] +enum AuthCommands { + /// Login to CodSpeed + Login, +} + +// TODO: tweak the logger to make it more user-friendly +fn init_logger() -> Result<()> { + let logger = get_local_logger(); + CombinedLogger::init(vec![logger])?; + Ok(()) +} + +pub async fn run(args: AuthArgs) -> Result<()> { + init_logger()?; + let api_client = CodSpeedAPIClient::from(&args); + + match args.command { + AuthCommands::Login => login(api_client).await?, + } + Ok(()) +} + +nest! { + #[derive(Debug, Deserialize, Serialize)]* + #[serde(rename_all = "camelCase")]* + struct CreateLoginSessionData { + create_login_session: struct CreateLoginSessionPayload { + callback_url: String, + session_id: String, + } + } +} + +nest! { + #[derive(Debug, Deserialize, Serialize)]* + #[serde(rename_all = "camelCase")]* + struct ConsumeLoginSessionData { + consume_login_session: struct ConsumeLoginSessionPayload { + token: Option + } + } +} + +#[derive(Serialize)] +#[serde(rename_all = "camelCase")] +struct ConsumeLoginSessionVars { + session_id: String, +} + +struct CodSpeedAPIClient { + gql_client: GQLClient, +} + +impl From<&AuthArgs> for CodSpeedAPIClient { + fn from(args: &AuthArgs) -> Self { + Self { + gql_client: build_gql_api_client(args.api_url.clone()), + } + } +} + +const CODSPEED_GRAPHQL_ENDPOINT: &str = "https://gql.codspeed.io/"; + +fn build_gql_api_client(api_url: Option) -> GQLClient { + let endpoint = api_url.unwrap_or_else(|| CODSPEED_GRAPHQL_ENDPOINT.to_string()); + + GQLClient::new_with_config(ClientConfig { + endpoint, + timeout: Some(10), + headers: Default::default(), + proxy: None, + }) +} + +impl CodSpeedAPIClient { + async fn create_login_session(&self) -> Result { + let response = self + .gql_client + .query_unwrap::(include_str!("queries/CreateLoginSession.gql")) + .await; + match response { + Ok(response) => Ok(response.create_login_session), + Err(err) => bail!("Failed to create login session: {}", err), + } + } + + async fn consume_login_session(&self, session_id: &str) -> Result { + let response = self + .gql_client + .query_with_vars_unwrap::( + include_str!("queries/ConsumeLoginSession.gql"), + ConsumeLoginSessionVars { + session_id: session_id.to_string(), + }, + ) + .await; + match response { + Ok(response) => Ok(response.consume_login_session), + Err(err) => bail!("Failed to use login session: {}", err), + } + } +} + +const LOGIN_SESSION_MAX_DURATION: Duration = Duration::from_secs(60 * 5); // 5 minutes + +async fn login(api_client: CodSpeedAPIClient) -> Result<()> { + debug!("Login to CodSpeed"); + debug!("Creating login session..."); + let login_session_payload = api_client.create_login_session().await?; + info!( + "Login session created, open the following URL in your browser: {}", + login_session_payload.callback_url + ); + + info!("Waiting for the login to be completed..."); + let token; + let start = Instant::now(); + loop { + if LOGIN_SESSION_MAX_DURATION < start.elapsed() { + bail!("Login session expired, please try again"); + } + + match api_client + .consume_login_session(&login_session_payload.session_id) + .await? + .token + { + Some(token_from_api) => { + token = token_from_api; + break; + } + None => sleep(Duration::from_secs(5)).await, + } + } + debug!("Login completed"); + + let mut config = Config::load().await?; + config.auth.token = token; + config.persist().await?; + debug!("Token saved to configuration file"); + + info!("Login successful, your are now authenticated on CodSpeed"); + + Ok(()) +} diff --git a/src/config.rs b/src/config.rs new file mode 100644 index 0000000..7cff667 --- /dev/null +++ b/src/config.rs @@ -0,0 +1,73 @@ +use std::{env, path::PathBuf}; + +use crate::prelude::*; +use nestify::nest; +use serde::{Deserialize, Serialize}; + +nest! { + #[derive(Debug, Deserialize, Serialize)]* + #[serde(rename_all = "kebab-case")]* + pub struct Config { + pub auth: pub struct AuthConfig { + pub token: String, + } + } +} + +/// Get the path to the configuration file, following the XDG Base Directory Specification +/// at https://specifications.freedesktop.org/basedir-spec/basedir-spec-latest.html +fn get_configuration_file_path() -> PathBuf { + let config_dir = env::var("XDG_CONFIG_HOME") + .map(PathBuf::from) + .unwrap_or_else(|_| { + let home = env::var("HOME").expect("HOME env variable not set"); + PathBuf::from(home).join(".config") + }); + let config_dir = config_dir.join("codspeed"); + config_dir.join("config.yaml") +} + +impl Default for Config { + fn default() -> Self { + Self { + auth: AuthConfig { token: "".into() }, + } + } +} + +impl Config { + /// Load the configuration. If it does not exist, store and return a default configuration + pub async fn load() -> Result { + let config_path = get_configuration_file_path(); + + match tokio::fs::read(&config_path).await { + Ok(config_str) => { + let config = serde_yaml::from_slice(&config_str).context(format!( + "Failed to parse CodSpeed config at {}", + config_path.display() + ))?; + debug!("Config loaded from {}", config_path.display()); + Ok(config) + } + Err(e) if e.kind() == std::io::ErrorKind::NotFound => { + debug!("Config file not found at {}", config_path.display()); + let config = Config::default(); + config.persist().await?; + Ok(config) + } + Err(e) => bail!("Failed to load config: {}", e), + } + } + + /// Persist changes to the configuration + pub async fn persist(&self) -> Result<()> { + let config_path = get_configuration_file_path(); + tokio::fs::create_dir_all(config_path.parent().unwrap()).await?; + + let config_str = serde_yaml::to_string(self)?; + tokio::fs::write(&config_path, config_str).await?; + debug!("Config written to {}", config_path.display()); + + Ok(()) + } +} diff --git a/src/logger.rs b/src/logger.rs new file mode 100644 index 0000000..a027304 --- /dev/null +++ b/src/logger.rs @@ -0,0 +1,21 @@ +use std::env; + +use simplelog::{ConfigBuilder, SharedLogger}; + +pub fn get_local_logger() -> Box { + let log_level = env::var("CODSPEED_LOG") + .ok() + .and_then(|log_level| log_level.parse::().ok()) + .unwrap_or(log::LevelFilter::Info); + + let config = ConfigBuilder::new() + .set_time_level(log::LevelFilter::Debug) + .build(); + + simplelog::TermLogger::new( + log_level, + config, + simplelog::TerminalMode::Mixed, + simplelog::ColorChoice::Auto, + ) +} diff --git a/src/main.rs b/src/main.rs index 91113c6..4e7ba4f 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,4 +1,7 @@ mod app; +mod auth; +mod config; +mod logger; mod prelude; mod request_client; mod run; @@ -15,10 +18,12 @@ pub const VALGRIND_CODSPEED_VERSION: &str = "3.21.0-0codspeed1"; async fn main() { let res = crate::app::run().await; if let Err(err) = res { - if log_enabled!(log::Level::Error) { - error!("Error {}", err); - } else { - eprintln!("Error {}", err); + for cause in err.chain() { + if log_enabled!(log::Level::Error) { + error!("Error {}", cause); + } else { + eprintln!("Error {}", cause); + } } if log_enabled!(log::Level::Debug) { for e in err.chain().skip(1) { diff --git a/src/queries/ConsumeLoginSession.gql b/src/queries/ConsumeLoginSession.gql new file mode 100644 index 0000000..b246172 --- /dev/null +++ b/src/queries/ConsumeLoginSession.gql @@ -0,0 +1,5 @@ +mutation ConsumeLoginSession($sessionId: String!) { + consumeLoginSession(sessionId: $sessionId) { + token + } +} diff --git a/src/queries/CreateLoginSession.gql b/src/queries/CreateLoginSession.gql new file mode 100644 index 0000000..77f2dbb --- /dev/null +++ b/src/queries/CreateLoginSession.gql @@ -0,0 +1,6 @@ +mutation CreateLoginSession { + createLoginSession { + callbackUrl + sessionId + } +} diff --git a/src/run/ci_provider/github_actions/provider.rs b/src/run/ci_provider/github_actions/provider.rs index 3cd2cb5..cfa6b5c 100644 --- a/src/run/ci_provider/github_actions/provider.rs +++ b/src/run/ci_provider/github_actions/provider.rs @@ -23,7 +23,6 @@ pub struct GitHubActionsProvider { pub ref_: String, pub head_ref: Option, pub base_ref: Option, - pub commit_hash: String, pub gh_data: GhData, pub event: RunEvent, pub repository_root_path: String, @@ -49,7 +48,7 @@ impl TryFrom<&Config> for GitHubActionsProvider { let (owner, repository) = Self::get_owner_and_repository()?; let ref_ = get_env_variable("GITHUB_REF")?; let is_pr = PR_REF_REGEX.is_match(&ref_); - let (head_ref, commit_hash) = if is_pr { + let head_ref = if is_pr { let github_event_path = get_env_variable("GITHUB_EVENT_PATH")?; let github_event = fs::read_to_string(github_event_path)?; let github_event: Value = serde_json::from_str(&github_event) @@ -70,12 +69,9 @@ impl TryFrom<&Config> for GitHubActionsProvider { } else { pull_request["head"]["ref"].as_str().unwrap().to_owned() }; - ( - Some(head_ref), - pull_request["head"]["sha"].as_str().unwrap().to_owned(), - ) + Some(head_ref) } else { - (None, get_env_variable("GITHUB_SHA")?) + None }; let github_event_name = get_env_variable("GITHUB_EVENT_NAME")?; @@ -95,7 +91,6 @@ impl TryFrom<&Config> for GitHubActionsProvider { owner, repository: repository.clone(), ref_, - commit_hash, head_ref, event, gh_data: GhData { @@ -187,7 +182,6 @@ mod tests { ("GITHUB_REF", Some("refs/heads/main")), ("GITHUB_REPOSITORY", Some("owner/repository")), ("GITHUB_RUN_ID", Some("1234567890")), - ("GITHUB_SHA", Some("1234567890abcdef")), ], || { let config = Config { @@ -200,7 +194,6 @@ mod tests { assert_eq!(github_actions_provider.ref_, "refs/heads/main"); assert_eq!(github_actions_provider.base_ref, Some("main".into())); assert_eq!(github_actions_provider.head_ref, None); - assert_eq!(github_actions_provider.commit_hash, "1234567890abcdef"); assert_eq!(github_actions_provider.event, RunEvent::Push); assert_eq!(github_actions_provider.gh_data.job, "job"); assert_eq!(github_actions_provider.gh_data.run_id, 1234567890); @@ -245,10 +238,6 @@ mod tests { ("GITHUB_REF", Some("refs/pull/22/merge")), ("GITHUB_REPOSITORY", Some("my-org/adrien-python-test")), ("GITHUB_RUN_ID", Some("6957110437")), - ( - "GITHUB_SHA", - Some("5bd77cb0da72bef094893ed45fb793ff16ecfbe3"), - ), ("VERSION", Some("0.1.0")), ], || { @@ -293,10 +282,6 @@ mod tests { ("GITHUB_REF", Some("refs/pull/22/merge")), ("GITHUB_REPOSITORY", Some("my-org/adrien-python-test")), ("GITHUB_RUN_ID", Some("6957110437")), - ( - "GITHUB_SHA", - Some("5bd77cb0da72bef094893ed45fb793ff16ecfbe3"), - ), ("VERSION", Some("0.1.0")), ], || {