automatons_github/task/
list_check_suites.rs

1use anyhow::Context;
2use reqwest::Method;
3
4use automatons::Error;
5
6use crate::client::GitHubClient;
7use crate::resource::{CheckSuite, GitSha, Login, RepositoryName};
8
9/// List the check suites for a Git reference
10///
11/// Lists check suites for a commit `ref`. GitHub Apps must have the `checks:read` permission on a
12/// private repository or pull access to a public repository to list check suites. OAuth Apps and
13/// authenticated users must have the `repo` scope to get check suites in a private repository.
14///
15/// https://docs.github.com/en/rest/checks/suites#list-check-suites-for-a-git-reference
16#[derive(Copy, Clone, Debug)]
17pub struct ListCheckSuites<'a> {
18    github_client: &'a GitHubClient,
19    owner: &'a Login,
20    repository: &'a RepositoryName,
21    git_sha: &'a GitSha,
22}
23
24impl<'a> ListCheckSuites<'a> {
25    /// Initializes the task
26    pub fn new(
27        github_client: &'a GitHubClient,
28        owner: &'a Login,
29        repository: &'a RepositoryName,
30        git_sha: &'a GitSha,
31    ) -> Self {
32        Self {
33            github_client,
34            owner,
35            repository,
36            git_sha,
37        }
38    }
39
40    /// List the check suites for a Git reference
41    ///
42    /// Lists check suites for a commit `ref`.
43    pub async fn execute(&self) -> Result<Vec<CheckSuite>, Error> {
44        let url = format!(
45            "/repos/{}/{}/commits/{}/check-suites",
46            self.owner.get(),
47            self.repository.get(),
48            self.git_sha
49        );
50
51        let check_suites = self
52            .github_client
53            .paginate(Method::GET, &url, "check_suites")
54            .await
55            .context("failed to query check suites")?;
56
57        Ok(check_suites)
58    }
59}
60
61#[cfg(test)]
62mod tests {
63    use crate::resource::{GitSha, Login, RepositoryName};
64    use crate::testing::check_suite::mock_list_check_suites;
65    use crate::testing::client::github_client;
66    use crate::testing::token::mock_installation_access_tokens;
67
68    use super::ListCheckSuites;
69
70    #[tokio::test]
71    async fn task_returns_check_suites() {
72        let _token_mock = mock_installation_access_tokens();
73        let _content_mock = mock_list_check_suites();
74
75        let github_client = github_client();
76        let login = Login::new("github");
77        let repository = RepositoryName::new("hello-world");
78        let git_sha = GitSha::new("d6fde92930d4715a2b49857d24b940956b26d2d3");
79
80        let task = ListCheckSuites::new(&github_client, &login, &repository, &git_sha);
81
82        let check_suites = task.execute().await.unwrap();
83
84        assert_eq!(1, check_suites.len());
85    }
86
87    #[test]
88    fn trait_send() {
89        fn assert_send<T: Send>() {}
90        assert_send::<ListCheckSuites>();
91    }
92
93    #[test]
94    fn trait_sync() {
95        fn assert_sync<T: Sync>() {}
96        assert_sync::<ListCheckSuites>();
97    }
98}