Skip to content

Commit

Permalink
feat: console add orgs and client to add generics
Browse files Browse the repository at this point in the history
  • Loading branch information
baerwang committed Apr 20, 2024
1 parent ea4c020 commit 132513a
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 14 deletions.
14 changes: 14 additions & 0 deletions src-tauri/src/conf/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,20 @@ pub struct ConfigData {
}

impl ConfigData {
pub fn new(plugin: &str, token: &str) -> Self {
ConfigData {
plugin: plugin.to_string(),
token: token.to_string(),
reviews: vec![],
owners: Owner {
name: "".to_string(),
repos: vec![],
},
orgs: HashMap::new(),
dispatch: 0,
}
}

pub fn new_owner(plugin: &str, token: &str, owner: Owner) -> Self {
ConfigData {
plugin: plugin.to_string(),
Expand Down
33 changes: 27 additions & 6 deletions src-tauri/src/console/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
* limitations under the License.
*/

use crate::console::model::Repos;
use crate::console::model::{Org, Repo};
use crate::plugins::{client, get_api};
use crate::{conf, dispatch};

Expand All @@ -31,20 +31,30 @@ pub async fn create(conf: conf::config::ConfigData) -> String {
}

#[tauri::command]
pub async fn repos(conf: conf::config::ConfigData) -> Result<Vec<Repos>, anyhow::Error> {
pub async fn repos(conf: conf::config::ConfigData) -> Result<Vec<Repo>, anyhow::Error> {
let api = get_api(
conf.plugin.as_str(),
conf.owners.name.clone(),
conf.reviews(),
);
let rsp = client(api.repos(), api.headers(conf.token.as_str())).await?;
let repos: Vec<Repos> = serde_json::from_str(&rsp)?;
Ok(repos)
Ok(client::<Vec<Repo>>(api.repos(), api.headers(conf.token.as_str())).await?)
}

#[tauri::command]
#[allow(clippy::unused_unit)]
pub fn orgs(_conf: conf::config::ConfigData) -> () {}
pub async fn orgs(conf: conf::config::ConfigData) -> Result<Vec<Org>, anyhow::Error> {
let api = get_api(conf.plugin.as_str(), "".to_string(), conf.reviews());
let client = reqwest::Client::new();
// todo request There is a problem with populating access with headers
let request = client
.get(api.orgs())
.header("Authorization", format!("Bearer {}", conf.token))
.header("Accept", "application/vnd.github.v3+json")
.header("User-Agent", "Awesome-Octocat-App")
.build()?;
let organizations: Vec<Org> = client.execute(request).await?.json().await?;
Ok(organizations)
}

#[tauri::command]
#[allow(clippy::unused_unit)]
Expand All @@ -69,4 +79,15 @@ mod test {
assert!(result.is_ok());
assert_ne!(result.unwrap().len(), 0);
}

/*#[tokio::test]
async fn test_orgs() {
let result = orgs(ConfigData::new(
"github",
env::var("TOKEN").unwrap().as_str(),
))
.await;
assert!(result.is_ok());
assert_ne!(result.unwrap().len(), 0);
}*/
}
8 changes: 7 additions & 1 deletion src-tauri/src/console/model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,14 @@ use serde::Deserialize;

#[derive(Debug, Deserialize)]
#[allow(dead_code)]
pub struct Repos {
pub struct Repo {
name: String,
html_url: String,
updated_at: String,
}

#[derive(Debug, Deserialize)]
#[allow(dead_code)]
pub struct Org {
login: String,
}
1 change: 1 addition & 0 deletions src-tauri/src/plugins/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ pub trait Api {
fn repo(&self, repo: &str) -> String;
fn repos(&self) -> String;
fn org_repos(&self) -> String;
fn orgs(&self) -> String;
fn pull_requests(&self, repo: &str) -> String;
fn issues(&self, repo: &str) -> String;
fn reviews(&self, repo: &str, number: i64) -> String;
Expand Down
11 changes: 7 additions & 4 deletions src-tauri/src/plugins/github.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,10 @@ impl GitHub {
}

pub async fn execute(&self, token: &str, repo: &str) -> Result<(), anyhow::Error> {
let rsp = client(self.pull_requests(repo), self.headers(token)).await?;
let prs: Vec<PullRequest> = serde_json::from_str(&rsp)?;
let prs = client::<Vec<PullRequest>>(self.pull_requests(repo), self.headers(token)).await?;
for pr in prs {
let reviews_data = client(self.reviews(repo, pr.number), self.headers(token)).await?;
let reviews: Reviews = serde_json::from_str(&reviews_data)?;
let reviews =
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 Expand Up @@ -108,6 +107,10 @@ impl Api for GitHub {
)
}

fn orgs(&self) -> String {
format!("{}/user/orgs", self.api())
}

fn pull_requests(&self, repo: &str) -> String {
format!("{}/repos/{}/{repo}/pulls", self.api(), self.owner)
}
Expand Down
12 changes: 9 additions & 3 deletions src-tauri/src/plugins/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,15 @@
* limitations under the License.
*/

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

use reqwest::header::HeaderMap;

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

use serde::de::DeserializeOwned;

mod api;
pub mod github;

Expand All @@ -31,14 +34,17 @@ pub fn get_api(api: &str, owner: String, reviews: HashMap<String, ()>) -> Box<dy
}
}

pub async fn client(url: String, headers: HeaderMap) -> Result<String, reqwest::Error> {
pub async fn client<T>(url: String, headers: HeaderMap) -> Result<T, Error>
where
T: DeserializeOwned,
{
let resp = reqwest::Client::new()
.get(url)
.get(&url)
.headers(headers)
.timeout(std::time::Duration::from_secs(3))
.send()
.await?
.text()
.json::<T>()
.await?;
Ok(resp)
}

0 comments on commit 132513a

Please sign in to comment.