diff --git a/core/src/services/ghac/backend.rs b/core/src/services/ghac/backend.rs index e78fdb4055e3..f66f60d7ddca 100644 --- a/core/src/services/ghac/backend.rs +++ b/core/src/services/ghac/backend.rs @@ -60,6 +60,26 @@ const GITHUB_REPOSITORY: &str = "GITHUB_REPOSITORY"; /// The github API version that used by OpenDAL. const GITHUB_API_VERSION: &str = "2022-11-28"; +fn value_or_env( + explicit_value: Option, + env_var_name: &str, + operation: &'static str, +) -> Result { + if let Some(value) = explicit_value { + return Ok(value); + } + + env::var(env_var_name).map_err(|err| { + let text = format!( + "{} not found, maybe not in github action environment?", + env_var_name + ); + Error::new(ErrorKind::ConfigInvalid, &text) + .with_operation(operation) + .set_source(err) + }) +} + /// GitHub Action Cache Services support. /// /// # Capabilities @@ -83,8 +103,8 @@ const GITHUB_API_VERSION: &str = "2022-11-28"; /// /// Refer to [Caching dependencies to speed up workflows](https://docs.github.com/en/actions/using-workflows/caching-dependencies-to-speed-up-workflows) for more information. /// -/// To make this service work as expected, please make sure the following -/// environment has been setup correctly: +/// To make this service work as expected, please make sure to either call `endpoint` and `token` to +/// configure the URL and credentials, or that the following environment has been setup correctly: /// /// - `ACTIONS_CACHE_URL` /// - `ACTIONS_RUNTIME_TOKEN` @@ -151,6 +171,8 @@ pub struct GhacBuilder { root: Option, version: Option, enable_create_simulation: bool, + endpoint: Option, + runtime_token: Option, http_client: Option, } @@ -191,6 +213,31 @@ impl GhacBuilder { self } + /// Set the endpoint for ghac service. + /// + /// For example, this is provided as the `ACTIONS_CACHE_URL` environment variable by the GHA runner. + /// + /// Default: the value of the `ACTIONS_CACHE_URL` environment variable. + pub fn endpoint(&mut self, endpoint: &str) -> &mut Self { + if !endpoint.is_empty() { + self.endpoint = Some(endpoint.to_string()) + } + self + } + + /// Set the runtime token for ghac service. + /// + /// For example, this is provided as the `ACTIONS_RUNTIME_TOKEN` environment variable by the GHA + /// runner. + /// + /// Default: the value of the `ACTIONS_RUNTIME_TOKEN` environment variable. + pub fn runtime_token(&mut self, runtime_token: &str) -> &mut Self { + if !runtime_token.is_empty() { + self.runtime_token = Some(runtime_token.to_string()) + } + self + } + /// Specify the http client that used by this service. /// /// # Notes @@ -238,22 +285,12 @@ impl Builder for GhacBuilder { root, enable_create_simulation: self.enable_create_simulation, - cache_url: env::var(ACTIONS_CACHE_URL).map_err(|err| { - Error::new( - ErrorKind::ConfigInvalid, - "ACTIONS_CACHE_URL not found, maybe not in github action environment?", - ) - .with_operation("Builder::build") - .set_source(err) - })?, - catch_token: env::var(ACTIONS_RUNTIME_TOKEN).map_err(|err| { - Error::new( - ErrorKind::ConfigInvalid, - "ACTIONS_RUNTIME_TOKEN not found, maybe not in github action environment?", - ) - .with_operation("Builder::build") - .set_source(err) - })?, + cache_url: value_or_env(self.endpoint.take(), ACTIONS_CACHE_URL, "Builder::build")?, + catch_token: value_or_env( + self.runtime_token.take(), + ACTIONS_RUNTIME_TOKEN, + "Builder::build", + )?, version: self .version .clone()