Skip to content

Commit

Permalink
feat(services/ghac): Allow explicitly setting ghac endpoint/token, no…
Browse files Browse the repository at this point in the history
…t just env vars (#3177)

* Allow explicitly setting ghac endpoint/token, not just env vars

* format
  • Loading branch information
huonw authored Sep 25, 2023
1 parent 17a91c4 commit d1f8de4
Showing 1 changed file with 55 additions and 18 deletions.
73 changes: 55 additions & 18 deletions core/src/services/ghac/backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<String>,
env_var_name: &str,
operation: &'static str,
) -> Result<String> {
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
Expand All @@ -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`
Expand Down Expand Up @@ -151,6 +171,8 @@ pub struct GhacBuilder {
root: Option<String>,
version: Option<String>,
enable_create_simulation: bool,
endpoint: Option<String>,
runtime_token: Option<String>,

http_client: Option<HttpClient>,
}
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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()
Expand Down

0 comments on commit d1f8de4

Please sign in to comment.