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/github): make access_token optional #4404

Merged
merged 1 commit into from
Mar 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
15 changes: 5 additions & 10 deletions core/src/services/github/backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,10 @@ pub struct GithubConfig {
pub root: Option<String>,
/// Github access_token.
///
/// required.
pub token: String,
/// optional.
/// If not provided, the backend will only support read operations for public repositories.
/// And rate limit will be limited to 60 requests per hour.
pub token: Option<String>,
/// Github repo owner.
///
/// required.
Expand Down Expand Up @@ -105,7 +107,7 @@ impl GithubBuilder {
///
/// required.
pub fn token(&mut self, token: &str) -> &mut Self {
self.config.token = token.to_string();
self.config.token = Some(token.to_string());

self
}
Expand Down Expand Up @@ -168,13 +170,6 @@ impl Builder for GithubBuilder {
let root = normalize_root(&self.config.root.clone().unwrap_or_default());
debug!("backend use root {}", &root);

// Handle token.
if self.config.token.is_empty() {
return Err(Error::new(ErrorKind::ConfigInvalid, "token is empty")
.with_operation("Builder::build")
.with_context("service", Scheme::Github));
}

// Handle owner.
if self.config.owner.is_empty() {
return Err(Error::new(ErrorKind::ConfigInvalid, "owner is empty")
Expand Down
25 changes: 19 additions & 6 deletions core/src/services/github/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ pub struct GithubCore {
/// The root of this core.
pub root: String,
/// Github access_token.
pub token: String,
pub token: Option<String>,
/// Github repo owner.
pub owner: String,
/// Github repo name.
Expand All @@ -65,19 +65,32 @@ impl GithubCore {
}

pub fn sign(&self, req: request::Builder) -> Result<request::Builder> {
let req = req
let mut req = req
.header(header::USER_AGENT, format!("opendal-{}", VERSION))
.header("X-GitHub-Api-Version", "2022-11-28");

Ok(req.header(
header::AUTHORIZATION,
format_authorization_by_bearer(&self.token)?,
))
// Github access_token is optional.
if let Some(token) = &self.token {
req = req.header(
header::AUTHORIZATION,
format_authorization_by_bearer(token)?,
)
}

Ok(req)
}
}

impl GithubCore {
pub async fn get_file_sha(&self, path: &str) -> Result<Option<String>> {
// if the token is not set, we shhould not try to get the sha of the file.
if self.token.is_none() {
return Err(Error::new(
ErrorKind::PermissionDenied,
"Github access_token is not set",
));
}

let resp = self.stat(path).await?;

match resp.status() {
Expand Down
Loading