Skip to content

Commit

Permalink
feat: console implement repositories and organization
Browse files Browse the repository at this point in the history
  • Loading branch information
baerwang committed Apr 22, 2024
1 parent 07334d1 commit 256dada
Show file tree
Hide file tree
Showing 8 changed files with 48 additions and 22 deletions.
4 changes: 2 additions & 2 deletions ROADMAP.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

## Major features

- [ ] Repositories
- [ ] Organization
- [x] Repositories
- [x] Organization
- [ ] Specify certain repositories
- [ ] Find a good-looking icon
- [ ] Release version automatically packaged
27 changes: 13 additions & 14 deletions src-tauri/src/console/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,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};

Expand All @@ -32,31 +33,29 @@ pub async fn create(conf: ConfigData) -> String {
}

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

#[tauri::command]
#[allow(clippy::unused_unit)]
pub async fn orgs(conf: ConfigData) -> Result<Vec<Org>, anyhow::Error> {
pub async fn orgs(conf: ConfigData) -> Rest<Vec<Org>> {
let api = get_api(conf.plugin.as_str(), "".to_string(), conf.reviews());
Ok(client::<Vec<Org>>(api.orgs(), api.headers(conf.token.as_str())).await?)
Rest::from_result(client::<Vec<Org>>(api.orgs(), api.headers(conf.token.as_str())).await)
}

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

#[cfg(test)]
Expand Down Expand Up @@ -86,15 +85,15 @@ mod test {
},
))
.await;
assert!(result.is_ok());
assert_ne!(result.unwrap().len(), 0);
assert!(result.error.is_none());
assert_ne!(result.data.unwrap().len(), 0);
}

#[tokio::test]
async fn test_orgs() {
let result = orgs(ConfigData::new("github", token().as_str())).await;
assert!(result.is_ok());
assert_ne!(result.unwrap().len(), 0);
assert!(result.error.is_none());
assert_ne!(result.data.unwrap().len(), 0);
}

#[tokio::test]
Expand All @@ -108,7 +107,7 @@ mod test {
},
))
.await;
assert!(result.is_ok());
assert_ne!(result.unwrap().len(), 0);
assert!(result.error.is_none());
assert_ne!(result.data.unwrap().len(), 0);
}
}
21 changes: 21 additions & 0 deletions src-tauri/src/console/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,24 @@

pub mod api;
mod model;

use serde::{Deserialize, Serialize};

#[derive(Debug, Serialize, Deserialize)]
pub struct Rest<T> {
data: Option<T>,
error: Option<String>,
}

impl<T> Rest<T> {
pub fn new(data: Option<T>, error: Option<String>) -> Rest<T> {
Rest { data, error }
}

pub fn from_result(result: Result<T, reqwest::Error>) -> Rest<T> {
match result {
Ok(data) => Rest::new(Some(data), None),
Err(err) => Rest::new(None, Some(err.to_string())),
}
}
}
6 changes: 3 additions & 3 deletions src-tauri/src/console/model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,17 @@
* limitations under the License.
*/

use serde::Deserialize;
use serde::{Deserialize, Serialize};

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

#[derive(Debug, Deserialize)]
#[derive(Debug, Serialize, Deserialize)]
#[allow(dead_code)]
pub struct Org {
login: String,
Expand Down
7 changes: 6 additions & 1 deletion src-tauri/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,12 @@ mod tray;

fn main() {
tauri::Builder::default()
.invoke_handler(tauri::generate_handler![console::api::create])
.invoke_handler(tauri::generate_handler![
console::api::create,
console::api::repos,
console::api::orgs,
console::api::org_repos
])
.system_tray(tray::menu())
.on_system_tray_event(tray::handler)
.run(tauri::generate_context!())
Expand Down
2 changes: 1 addition & 1 deletion src-tauri/src/plugins/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ pub struct PullRequest {
pub number: i64,
}

pub trait Api {
pub trait Api: Send {
fn api(&self) -> &str;
fn headers(&self, token: &str) -> HeaderMap;
fn repo(&self, repo: &str) -> String;
Expand Down
1 change: 1 addition & 0 deletions src-tauri/src/plugins/github.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ use crate::plugins::api::Api;
use crate::plugins::api::PullRequest as PR;
use crate::plugins::client;

#[derive(Default)]
pub struct GitHub {
pub owner: String,
pub reviews: HashMap<String, ()>,
Expand Down
2 changes: 1 addition & 1 deletion src-tauri/src/plugins/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ use crate::plugins::api::Api;

use serde::de::DeserializeOwned;

mod api;
pub mod api;
pub mod github;

pub fn get_api(api: &str, owner: String, reviews: HashMap<String, ()>) -> Box<dyn Api> {
Expand Down

0 comments on commit 256dada

Please sign in to comment.