Skip to content

Commit

Permalink
style: review use option
Browse files Browse the repository at this point in the history
  • Loading branch information
baerwang committed Apr 25, 2024
1 parent c34a597 commit aaea284
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 26 deletions.
24 changes: 9 additions & 15 deletions src-tauri/src/console/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ use crate::conf::config::ConfigData;
use crate::console::model::{Org, Repo};
use crate::console::Rest;
use crate::dispatch;
use crate::plugins::{client, get_api};
use crate::plugins::{get_api, get_client};

#[tauri::command]
pub async fn create(conf: ConfigData) -> String {
Expand All @@ -34,28 +34,22 @@ pub async fn create(conf: ConfigData) -> String {

#[tauri::command]
pub async fn repos(conf: ConfigData) -> Rest<Vec<Repo>> {
let api = get_api(
conf.plugin.as_str(),
conf.owners.name.clone(),
conf.reviews(),
);
Rest::from_result(client::<Vec<Repo>>(api.repos(), api.headers(conf.token.as_str())).await)
let api = get_api(conf.plugin.as_str(), conf.owners.name.clone(), None);
Rest::from_result(get_client::<Vec<Repo>>(api.repos(), api.headers(conf.token.as_str())).await)
}

#[tauri::command]
pub async fn orgs(conf: ConfigData) -> Rest<Vec<Org>> {
let api = get_api(conf.plugin.as_str(), "".to_string(), conf.reviews());
Rest::from_result(client::<Vec<Org>>(api.orgs(), api.headers(conf.token.as_str())).await)
let api = get_api(conf.plugin.as_str(), "".to_string(), None);
Rest::from_result(get_client::<Vec<Org>>(api.orgs(), api.headers(conf.token.as_str())).await)
}

#[tauri::command]
pub async fn org_repos(conf: ConfigData) -> Rest<Vec<Repo>> {
let api = get_api(
conf.plugin.as_str(),
conf.owners.name.clone(),
conf.reviews(),
);
Rest::from_result(client::<Vec<Repo>>(api.org_repos(), api.headers(conf.token.as_str())).await)
let api = get_api(conf.plugin.as_str(), conf.owners.name.clone(), None);
Rest::from_result(
get_client::<Vec<Repo>>(api.org_repos(), api.headers(conf.token.as_str())).await,
)
}

#[cfg(test)]
Expand Down
4 changes: 2 additions & 2 deletions src-tauri/src/console/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@
* limitations under the License.
*/

use serde::{Deserialize, Serialize};

pub mod api;
mod model;

use serde::{Deserialize, Serialize};

#[derive(Debug, Serialize, Deserialize)]
pub struct Rest<T> {
data: Option<T>,
Expand Down
7 changes: 4 additions & 3 deletions src-tauri/src/plugins/github.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ use serde::Deserialize;
use crate::notification::notify::notify;
use crate::plugins::api::Api;
use crate::plugins::api::PullRequest as PR;
use crate::plugins::client;
use crate::plugins::get_client;

pub struct GitHub {
pub owner: String,
Expand All @@ -37,10 +37,11 @@ impl GitHub {
}

pub async fn execute(&self, token: &str, repo: &str) -> Result<(), anyhow::Error> {
let prs = client::<Vec<PullRequest>>(self.pull_requests(repo), self.headers(token)).await?;
let prs =
get_client::<Vec<PullRequest>>(self.pull_requests(repo), self.headers(token)).await?;
for pr in prs {
let reviews =
client::<Reviews>(self.reviews(repo, pr.number), self.headers(token)).await?;
get_client::<Reviews>(self.reviews(repo, pr.number), self.headers(token)).await?;
reviews.users.iter().for_each(|user| {
if self.reviews.contains_key(user.login.as_str()) {
self.notify(
Expand Down
11 changes: 5 additions & 6 deletions src-tauri/src/plugins/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,26 +15,25 @@
* limitations under the License.
*/

use reqwest::Error;
use std::collections::HashMap;

use reqwest::header::HeaderMap;
use reqwest::Error;
use serde::de::DeserializeOwned;

use crate::plugins::api::Api;

use serde::de::DeserializeOwned;

mod api;
pub mod github;

pub fn get_api(api: &str, owner: String, reviews: HashMap<String, ()>) -> Box<dyn Api> {
pub fn get_api(api: &str, owner: String, reviews: Option<HashMap<String, ()>>) -> Box<dyn Api> {
match api {
"github" => Box::new(github::GitHub::new(owner, reviews)),
"github" => Box::new(github::GitHub::new(owner, reviews.unwrap_or_default())),
_ => panic!("Unsupported"),
}
}

pub async fn client<T>(url: String, headers: HeaderMap) -> Result<T, Error>
pub async fn get_client<T>(url: String, headers: HeaderMap) -> Result<T, Error>
where
T: DeserializeOwned,
{
Expand Down

0 comments on commit aaea284

Please sign in to comment.