From f91541bd8b1fb303e5a9c456bb0d8d9ad1974fc1 Mon Sep 17 00:00:00 2001 From: Bryan Date: Sun, 8 Oct 2023 16:09:57 +0800 Subject: [PATCH] add get_readme for RepoHandler (#465) --- src/api/repos.rs | 19 ++++++++++++++++++ src/api/repos/file.rs | 46 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 65 insertions(+) diff --git a/src/api/repos.rs b/src/api/repos.rs index a2543651..4a9cc31f 100644 --- a/src/api/repos.rs +++ b/src/api/repos.rs @@ -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; @@ -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<()> { diff --git a/src/api/repos/file.rs b/src/api/repos/file.rs index 27c805c1..5b772213 100644 --- a/src/api/repos/file.rs +++ b/src/api/repos/file.rs @@ -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, + #[serde(skip_serializing_if = "Option::is_none")] + r#ref: Option, +} + +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) -> 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) -> Self { + self.r#ref = Some(r#ref.into()); + self + } + + /// Sends the actual request. + pub async fn send(self) -> Result { + 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)]