Skip to content

Commit

Permalink
feat(services/github): make access_token optional (#4404)
Browse files Browse the repository at this point in the history
  • Loading branch information
hoslo authored Mar 28, 2024
1 parent b6cdd68 commit 126cb05
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 16 deletions.
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

0 comments on commit 126cb05

Please sign in to comment.