Skip to main content

mesa_dev/client/
commits.rs

1use crate::low_level::apis::{commits_api, Error};
2use crate::models;
3
4use super::pagination::{paginate, PaginatedResponse};
5use super::RepoClient;
6
7use futures_core::Stream;
8
9impl PaginatedResponse for models::GetByOrgByRepoCommits200Response {
10    type Item = models::GetByOrgByRepoCommits200ResponseCommitsInner;
11
12    fn items(self) -> Vec<Self::Item> {
13        self.commits
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 commit operations (`/{org}/{repo}/commits`).
26#[derive(Clone, Debug)]
27pub struct CommitsClient<'a> {
28    pub(super) repo: &'a RepoClient<'a>,
29}
30
31impl<'a> CommitsClient<'a> {
32    /// List commits, optionally filtered by ref.
33    ///
34    /// Returns an async stream that automatically paginates through all results,
35    /// yielding one commit 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        r#ref: Option<&'a str>,
40        limit: Option<u8>,
41    ) -> impl Stream<
42        Item = Result<
43            models::GetByOrgByRepoCommits200ResponseCommitsInner,
44            Error<commits_api::GetByOrgByRepoCommitsError>,
45        >,
46    > + 'a {
47        tracing::debug!(
48            org = self.repo.org.org,
49            repo = self.repo.repo,
50            r#ref,
51            limit,
52            "listing commits"
53        );
54        let config = self.repo.org.config;
55        let org = self.repo.org.org;
56        let repo = self.repo.repo;
57
58        paginate(limit, move |cursor, lim| async move {
59            commits_api::get_by_org_by_repo_commits(
60                config,
61                org,
62                repo,
63                cursor.as_deref(),
64                lim,
65                r#ref,
66            )
67            .await
68        })
69    }
70
71    /// Get a specific commit by its SHA.
72    ///
73    /// # Errors
74    ///
75    /// Returns an error if the API request fails.
76    #[tracing::instrument(skip(self), fields(org = self.repo.org.org, repo = self.repo.repo), err(Debug))]
77    pub async fn get(
78        &self,
79        sha: &str,
80    ) -> Result<
81        models::GetByOrgByRepoCommitsBySha200Response,
82        Error<commits_api::GetByOrgByRepoCommitsByShaError>,
83    > {
84        commits_api::get_by_org_by_repo_commits_by_sha(
85            self.repo.org.config,
86            self.repo.org.org,
87            self.repo.repo,
88            Some(sha),
89        )
90        .await
91    }
92}