1pub mod cache;
2pub mod errors;
3
4use crate::errors::Error;
5use codecov::{
6 Client as CodecovClient, author::Author, branch_detail::BranchDetailAPIResponse,
7 branches::BranchesAPIResponse, commits::CommitsAPIResponse, owner::Owner, repos::Repo,
8};
9
10pub struct Client {
14 codecov_client: CodecovClient,
15 cache_client: cache::Client,
16}
17
18impl Client {
24 pub fn new_from_env() -> Result<Client, Error> {
25 let cache_dir = Client::resolve_cache_dir_root();
26 Ok(Client {
27 codecov_client: CodecovClient::new_from_env()?,
28 cache_client: cache::Client::new(cache_dir, "data.json".to_string()),
29 })
30 }
31
32 fn resolve_cache_dir_root() -> std::path::PathBuf {
33 match std::env::var("CODECOV_CACHE_DIR") {
34 Ok(path) => std::path::PathBuf::from(path),
35 Err(_) => Client::default_cache_dir_root(),
36 }
37 }
38
39 fn default_cache_dir_root() -> std::path::PathBuf {
40 let Some(mut path) = dirs::cache_dir() else {
41 panic!("Unsupported platform");
42 };
43 path.push("rust-codecov-cache");
44 path
45 }
46
47 pub fn new(token: String, cache_dir: std::path::PathBuf) -> Client {
48 Client {
49 codecov_client: CodecovClient::new(token),
50 cache_client: cache::Client::new(cache_dir, "data.json".to_string()),
51 }
52 }
53
54 pub fn get_all_repos(&self, owner: &Owner) -> Result<Vec<Repo>, Error> {
60 Ok(self.codecov_client.get_all_repos(owner)?)
61 }
62
63 pub fn get_commits(&self, author: &Author) -> Result<CommitsAPIResponse, Error> {
68 Ok(self.codecov_client.get_commits(author)?)
69 }
70
71 pub fn get_branches(&self, author: &Author) -> Result<BranchesAPIResponse, Error> {
76 Ok(self.codecov_client.get_branches(author)?)
77 }
78
79 pub fn get_branch_detail(
84 &self,
85 author: &Author,
86 branch_name: &str,
87 ) -> Result<BranchDetailAPIResponse, Error> {
88 Ok(self.codecov_client.get_branch_detail(author, branch_name)?)
89 }
90
91 pub fn get_branch_detail_with_commit_id(
96 &self,
97 author: &Author,
98 branch_name: &str,
99 commit_id: &str,
100 ) -> Result<BranchDetailAPIResponse, Error> {
101 let cache_key = &[
102 &author.service,
103 &author.username,
104 &author.name,
105 branch_name,
106 commit_id,
107 ];
108 if let Ok(data) = self.cache_client.load(cache_key)
110 && let Ok(value) = serde_json::from_slice(&data)
111 && let Ok(branch_detail) = serde_json::from_value(value)
112 {
113 return Ok(branch_detail);
114 }
115 let retrieved = self.codecov_client.get_branch_detail(author, branch_name)?;
117 if let BranchDetailAPIResponse::Success(detail) = &retrieved {
119 if let Ok(data) = serde_json::to_vec(&detail) {
120 let cache_key = &[
121 &author.service,
122 &author.username,
123 &author.name,
124 branch_name,
125 &detail.head_commit.commitid,
126 ];
127 if let Err(err) = self.cache_client.save(cache_key, &data) {
128 println!("Failed to save cache: {:?}", err);
129 }
130 }
131 return Ok(retrieved);
132 }
133 Ok(retrieved)
134 }
135}
136#[cfg(test)]
137mod tests {
138 use super::*;
139
140 #[test]
141 fn test_fetch_and_cache() {
142 let client = Client::new_from_env().unwrap(); let owner = Owner::new("github", "kitsuyui");
144 let author = owner.new_author("rust-codecov");
145
146 let detail = client.get_branch_detail(&author, "main").unwrap();
147
148 let BranchDetailAPIResponse::Success(detail) = detail else {
149 panic!("Unexpected response");
150 };
151 let commit_id = detail.head_commit.commitid.to_string();
152
153 client
154 .get_branch_detail_with_commit_id(&author, "main", &commit_id)
155 .unwrap();
156 assert!(client.cache_client.has(&[
157 "github",
158 "kitsuyui",
159 "rust-codecov",
160 "main",
161 &commit_id
162 ]));
163
164 let mut cache_dir = dirs::cache_dir().unwrap();
166 cache_dir.extend(&[
167 "rust-codecov-cache",
168 "github",
169 "kitsuyui",
170 "rust-codecov",
171 "main",
172 &commit_id,
173 ]);
174 assert!(cache_dir.exists());
175 }
176}