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        tracing::debug!(
47            org = self.repo.org.org,
48            repo = self.repo.repo,
49            limit,
50            "listing branches"
51        );
52        let config = self.repo.org.config;
53        let org = self.repo.org.org;
54        let repo = self.repo.repo;
55
56        paginate(limit, move |cursor, lim| async move {
57            branches_api::get_by_org_by_repo_branches(config, org, repo, cursor.as_deref(), lim)
58                .await
59        })
60    }
61
62    /// Create a new branch from an existing ref.
63    ///
64    /// # Errors
65    ///
66    /// Returns an error if the API request fails.
67    #[tracing::instrument(skip(self, request), fields(org = self.repo.org.org, repo = self.repo.repo), err(Debug))]
68    pub async fn create(
69        &self,
70        request: models::PostByOrgByRepoBranchesRequest,
71    ) -> Result<
72        models::PostByOrgByRepoBranches201Response,
73        Error<branches_api::PostByOrgByRepoBranchesError>,
74    > {
75        branches_api::post_by_org_by_repo_branches(
76            self.repo.org.config,
77            self.repo.org.org,
78            self.repo.repo,
79            Some(request),
80        )
81        .await
82    }
83
84    /// Delete a branch by name.
85    ///
86    /// # Errors
87    ///
88    /// Returns an error if the API request fails.
89    #[tracing::instrument(skip(self), fields(org = self.repo.org.org, repo = self.repo.repo), err(Debug))]
90    pub async fn delete(
91        &self,
92        branch: &str,
93    ) -> Result<
94        models::DeleteByOrgApiKeysById200Response,
95        Error<branches_api::DeleteByOrgByRepoBranchesByBranchError>,
96    > {
97        branches_api::delete_by_org_by_repo_branches_by_branch(
98            self.repo.org.config,
99            self.repo.org.org,
100            self.repo.repo,
101            Some(branch),
102        )
103        .await
104    }
105}