Skip to main content

mesa_dev/resources/
branches.rs

1//! Branch operations.
2//!
3//! Access via [`MesaClient::branches`](crate::MesaClient::branches):
4//!
5//! ```rust,no_run
6//! # use mesa_dev::{Mesa, MesaError, models::CreateBranchRequest};
7//! # async fn run() -> Result<(), MesaError> {
8//! # let client = Mesa::new("key");
9//! let branch = client.branches("my-org", "my-repo").create(&CreateBranchRequest {
10//!     name: "feature/new".to_owned(),
11//!     from: "main".to_owned(),
12//! }).await?;
13//! # Ok(())
14//! # }
15//! ```
16
17use std::sync::Arc;
18
19use http::Method;
20
21use crate::client::MesaClient;
22use crate::error::MesaError;
23use crate::http_client::HttpClient;
24use crate::models::{
25    Branch, CreateBranchRequest, ListBranchesResponse, PaginationParams, SuccessResponse,
26};
27use crate::pagination::PageStream;
28
29/// Operations on branches within a repository.
30pub struct BranchesResource<'c, C: HttpClient> {
31    client: &'c MesaClient<C>,
32    org: String,
33    repo: String,
34}
35
36impl<'c, C: HttpClient> BranchesResource<'c, C> {
37    pub(crate) fn new(client: &'c MesaClient<C>, org: String, repo: String) -> Self {
38        Self { client, org, repo }
39    }
40
41    /// Create a new branch.
42    ///
43    /// # Errors
44    ///
45    /// Returns [`MesaError`] if the API request fails.
46    pub async fn create(&self, req: &CreateBranchRequest) -> Result<Branch, MesaError> {
47        let path = format!("/{}/{}/branches", self.org, self.repo);
48        self.client
49            .request(Method::POST, &path, &[], Some(req))
50            .await
51    }
52
53    /// List branches with pagination parameters.
54    ///
55    /// # Errors
56    ///
57    /// Returns [`MesaError`] if the API request fails.
58    pub async fn list(&self, params: &PaginationParams) -> Result<ListBranchesResponse, MesaError> {
59        let path = format!("/{}/{}/branches", self.org, self.repo);
60        let mut query = Vec::new();
61        if let Some(ref cursor) = params.cursor {
62            query.push(("cursor", cursor.as_str()));
63        }
64        if let Some(limit) = params.limit {
65            let limit_str = limit.to_string();
66            return self
67                .client
68                .request::<ListBranchesResponse>(
69                    Method::GET,
70                    &path,
71                    &[&query[..], &[("limit", &limit_str)]].concat(),
72                    None::<&()>,
73                )
74                .await;
75        }
76        self.client
77            .request(Method::GET, &path, &query, None::<&()>)
78            .await
79    }
80
81    /// Return a [`PageStream`] that iterates over all branches.
82    #[must_use]
83    pub fn list_all(&self) -> PageStream<C, ListBranchesResponse> {
84        let path = format!("/{}/{}/branches", self.org, self.repo);
85        PageStream::new(Arc::clone(&self.client.inner), path, Vec::new())
86    }
87
88    /// Delete a branch.
89    ///
90    /// # Errors
91    ///
92    /// Returns [`MesaError`] if the API request fails.
93    pub async fn delete(&self, branch: &str) -> Result<SuccessResponse, MesaError> {
94        let path = format!("/{}/{}/branches/{branch}", self.org, self.repo);
95        self.client
96            .request(Method::DELETE, &path, &[], None::<&()>)
97            .await
98    }
99}