Skip to main content

mesa_dev/client/
commits.rs

1use crate::low_level::apis::{commits_api, Error};
2use crate::low_level::commits;
3use crate::models;
4
5use super::pagination::{paginate, PaginatedResponse};
6use super::RepoClient;
7
8use futures_core::Stream;
9
10impl PaginatedResponse for models::GetByOrgByRepoCommits200Response {
11    type Item = models::GetByOrgByRepoCommits200ResponseCommitsInner;
12
13    fn items(self) -> Vec<Self::Item> {
14        self.commits
15    }
16
17    fn next_cursor(&self) -> Option<&str> {
18        self.next_cursor.as_deref()
19    }
20
21    fn has_more(&self) -> bool {
22        self.has_more
23    }
24}
25
26/// Client for commit operations (`/{org}/{repo}/commits`).
27#[derive(Clone, Debug)]
28pub struct CommitsClient<'a> {
29    pub(super) repo: &'a RepoClient<'a>,
30}
31
32impl<'a> CommitsClient<'a> {
33    /// List commits, optionally filtered by ref.
34    ///
35    /// Returns an async stream that automatically paginates through all results,
36    /// yielding one commit at a time. Pass `limit` to control the page size of
37    /// each underlying API request, or `None` for the server default.
38    pub fn list(
39        &self,
40        r#ref: Option<&'a str>,
41        limit: Option<u8>,
42    ) -> impl Stream<
43        Item = Result<
44            models::GetByOrgByRepoCommits200ResponseCommitsInner,
45            Error<commits_api::GetByOrgByRepoCommitsError>,
46        >,
47    > + 'a {
48        let config = self.repo.org.config;
49        let org = self.repo.org.org;
50        let repo = self.repo.repo;
51
52        paginate(limit, move |cursor, lim| async move {
53            commits_api::get_by_org_by_repo_commits(
54                config,
55                org,
56                repo,
57                cursor.as_deref(),
58                lim,
59                r#ref,
60            )
61            .await
62        })
63    }
64
65    /// Get a specific commit by its SHA.
66    ///
67    /// # Errors
68    ///
69    /// Returns an error if the API request fails.
70    pub async fn get(
71        &self,
72        sha: &str,
73    ) -> Result<
74        models::GetByOrgByRepoCommitsBySha200Response,
75        Error<commits_api::GetByOrgByRepoCommitsByShaError>,
76    > {
77        commits_api::get_by_org_by_repo_commits_by_sha(
78            self.repo.org.config,
79            self.repo.org.org,
80            self.repo.repo,
81            sha,
82        )
83        .await
84    }
85
86    /// Create a commit with file operations.
87    ///
88    /// Uses the hand-written implementation to work around the missing
89    /// `Upsert` action variant in the generated code.
90    ///
91    /// # Errors
92    ///
93    /// Returns an error if the API request fails.
94    #[allow(clippy::too_many_arguments)]
95    pub async fn create(
96        &self,
97        branch: &str,
98        message: &str,
99        author: &commits::CommitAuthor,
100        files: &[commits::CommitFile],
101        base_sha: Option<&str>,
102    ) -> Result<commits::CommitResponse, Error<commits::CreateCommitError>> {
103        commits::create_commit(
104            self.repo.org.config,
105            self.repo.org.org,
106            self.repo.repo,
107            branch,
108            message,
109            author,
110            files,
111            base_sha,
112        )
113        .await
114    }
115}