Skip to main content

mesa_dev/client/
branches.rs

1use crate::low_level::apis::{branches_api, Error};
2use crate::models;
3
4use super::pagination::{paginate, PaginatedResponse};
5use super::RepoClient;
6
7use futures_core::Stream;
8
9impl PaginatedResponse for models::GetByOrgByRepoBranches200Response {
10    type Item = models::GetByOrgByRepoBranches200ResponseBranchesInner;
11
12    fn items(self) -> Vec<Self::Item> {
13        self.branches
14    }
15
16    fn next_cursor(&self) -> Option<&str> {
17        self.next_cursor.as_deref()
18    }
19
20    fn has_more(&self) -> bool {
21        self.has_more
22    }
23}
24
25/// Client for branch operations (`/{org}/{repo}/branches`).
26#[derive(Clone, Debug)]
27pub struct BranchesClient<'a> {
28    pub(super) repo: &'a RepoClient<'a>,
29}
30
31impl<'a> BranchesClient<'a> {
32    /// List all branches in the repository.
33    ///
34    /// Returns an async stream that automatically paginates through all results,
35    /// yielding one branch at a time. Pass `limit` to control the page size of
36    /// each underlying API request, or `None` for the server default.
37    pub fn list(
38        &self,
39        limit: Option<u8>,
40    ) -> impl Stream<
41        Item = Result<
42            models::GetByOrgByRepoBranches200ResponseBranchesInner,
43            Error<branches_api::GetByOrgByRepoBranchesError>,
44        >,
45    > + 'a {
46        let config = self.repo.org.config;
47        let org = self.repo.org.org;
48        let repo = self.repo.repo;
49
50        paginate(limit, move |cursor, lim| async move {
51            branches_api::get_by_org_by_repo_branches(config, org, repo, cursor.as_deref(), lim)
52                .await
53        })
54    }
55
56    /// Create a new branch from an existing ref.
57    ///
58    /// # Errors
59    ///
60    /// Returns an error if the API request fails.
61    pub async fn create(
62        &self,
63        request: models::PostByOrgByRepoBranchesRequest,
64    ) -> Result<
65        models::PostByOrgByRepoBranches201Response,
66        Error<branches_api::PostByOrgByRepoBranchesError>,
67    > {
68        branches_api::post_by_org_by_repo_branches(
69            self.repo.org.config,
70            self.repo.org.org,
71            self.repo.repo,
72            Some(request),
73        )
74        .await
75    }
76
77    /// Delete a branch by name.
78    ///
79    /// # Errors
80    ///
81    /// Returns an error if the API request fails.
82    pub async fn delete(
83        &self,
84        branch: &str,
85    ) -> Result<
86        models::DeleteByOrgApiKeysById200Response,
87        Error<branches_api::DeleteByOrgByRepoBranchesByBranchError>,
88    > {
89        branches_api::delete_by_org_by_repo_branches_by_branch(
90            self.repo.org.config,
91            self.repo.org.org,
92            self.repo.repo,
93            branch,
94        )
95        .await
96    }
97}