Skip to content

Commit

Permalink
add get_readme for RepoHandler (#465)
Browse files Browse the repository at this point in the history
  • Loading branch information
ImSingee authored Oct 8, 2023
1 parent 643fcd3 commit f91541b
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 0 deletions.
19 changes: 19 additions & 0 deletions src/api/repos.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ mod tags;
mod teams;

use crate::error::HttpSnafu;
use crate::repos::file::GetReadmeBuilder;
use crate::{models, params, Octocrab, Result};
pub use branches::ListBranchesBuilder;
pub use collaborators::ListCollaboratorsBuilder;
Expand Down Expand Up @@ -223,6 +224,24 @@ impl<'octo> RepoHandler<'octo> {
GetContentBuilder::new(self)
}

/// Get repository readme.
/// ```no_run
/// # async fn run() -> octocrab::Result<()> {
///
/// octocrab::instance()
/// .repos("owner", "repo")
/// .get_readme()
/// .path("path/to/file")
/// .r#ref("main")
/// .send()
/// .await?;
/// # Ok(())
/// # }
/// ```
pub fn get_readme(&self) -> GetReadmeBuilder<'_, '_> {
GetReadmeBuilder::new(self)
}

/// Creates a new file in the repository.
/// ```no_run
/// # async fn run() -> octocrab::Result<()> {
Expand Down
46 changes: 46 additions & 0 deletions src/api/repos/file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,52 @@ impl<'octo, 'r> GetContentBuilder<'octo, 'r> {
}
}

#[derive(serde::Serialize)]
pub struct GetReadmeBuilder<'octo, 'r> {
#[serde(skip)]
handler: &'r RepoHandler<'octo>,
#[serde(skip)]
path: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
r#ref: Option<String>,
}

impl<'octo, 'r> GetReadmeBuilder<'octo, 'r> {
pub(crate) fn new(handler: &'r RepoHandler<'octo>) -> Self {
Self {
handler,
path: None,
r#ref: None,
}
}

/// The content path.
/// Default: none (the repository's root directory)
pub fn path(mut self, path: impl Into<String>) -> Self {
self.path = Some(path.into());
self
}

/// The name of the commit/branch/tag.
/// Default: the repository’s default branch (usually `master)
pub fn r#ref(mut self, r#ref: impl Into<String>) -> Self {
self.r#ref = Some(r#ref.into());
self
}

/// Sends the actual request.
pub async fn send(self) -> Result<models::repos::Content> {
let path = self.path.clone().unwrap_or(String::from(""));
let route = format!(
"/repos/{owner}/{repo}/readme/{path}",
owner = self.handler.owner,
repo = self.handler.repo,
path = path,
);
self.handler.crab.get(route, Some(&self)).await
}
}

#[derive(serde::Serialize)]
pub struct UpdateFileBuilder<'octo, 'r> {
#[serde(skip)]
Expand Down

0 comments on commit f91541b

Please sign in to comment.