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

add get_readme for RepoHandler #465

Merged
merged 2 commits into from
Oct 8, 2023
Merged
Show file tree
Hide file tree
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
19 changes: 19 additions & 0 deletions src/api/repos.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ pub use stargazers::ListStarGazersBuilder;
pub use status::{CreateStatusBuilder, ListStatusesBuilder};
pub use tags::ListTagsBuilder;
pub use teams::ListTeamsBuilder;
use crate::repos::file::GetReadmeBuilder;

/// Handler for GitHub's repository API.
///
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
48 changes: 48 additions & 0 deletions src/api/repos/file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,54 @@ 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
Loading