Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(services/ghac): Allow explicitly setting ghac endpoint/token, not just env vars #3177

Merged
merged 2 commits into from
Sep 25, 2023
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 51 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,8 @@ 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