squawk_github/
actions.rs

1use log::info;
2
3use crate::app;
4use crate::app::SQUAWK_USER_AGENT;
5use crate::{Comment, DEFAULT_GITHUB_API_URL, GitHubApi, GithubError};
6
7pub struct GitHub {
8    github_api_url: String,
9    github_token: String,
10}
11impl GitHub {
12    #[must_use]
13    pub fn new(github_token: &str) -> Self {
14        Self::new_with_url(DEFAULT_GITHUB_API_URL, github_token)
15    }
16
17    #[must_use]
18    pub fn new_with_url(github_api_url: &str, github_token: &str) -> Self {
19        info!("github user agent {SQUAWK_USER_AGENT}");
20        GitHub {
21            github_api_url: github_api_url.to_string(),
22            github_token: github_token.to_string(),
23        }
24    }
25}
26impl GitHubApi for GitHub {
27    fn app_slug(&self) -> String {
28        "github-actions[bot]".to_string()
29    }
30    fn create_issue_comment(
31        &self,
32        owner: &str,
33        repo: &str,
34        issue_id: i64,
35        body: &str,
36    ) -> Result<(), GithubError> {
37        app::create_comment(
38            &self.github_api_url,
39            app::CommentArgs {
40                owner: owner.to_string(),
41                repo: repo.to_string(),
42                issue: issue_id,
43                body: body.to_string(),
44            },
45            &self.github_token,
46        )
47    }
48    fn list_issue_comments(
49        &self,
50        owner: &str,
51        repo: &str,
52        issue_id: i64,
53    ) -> Result<Vec<Comment>, GithubError> {
54        app::list_comments(
55            &self.github_api_url,
56            &app::PullRequest {
57                issue: issue_id,
58                owner: owner.to_string(),
59                repo: repo.to_string(),
60            },
61            &self.github_token,
62        )
63    }
64    fn update_issue_comment(
65        &self,
66        owner: &str,
67        repo: &str,
68        comment_id: i64,
69        body: &str,
70    ) -> Result<(), GithubError> {
71        app::update_comment(
72            &self.github_api_url,
73            owner,
74            repo,
75            comment_id,
76            body.to_string(),
77            &self.github_token,
78        )
79    }
80}