From 9aec267e0a7ef24787a354106476b7b41f1f9b8a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julien=20Fastr=C3=A9?= Date: Mon, 20 May 2024 19:16:21 +0200 Subject: [PATCH] WIP on integrate gitea --- src/config.rs | 7 ++++++ src/debug.rs | 10 ++++++++ src/error.rs | 19 +++++++++++++++ src/gitea/client.rs | 54 +++++++++++++++++++++++++++++++++++++++++ src/gitea/issue.rs | 22 +++++++++++++++++ src/gitea/mod.rs | 3 +++ src/gitea/repository.rs | 9 +++++++ src/main.rs | 1 + 8 files changed, 125 insertions(+) create mode 100644 src/gitea/client.rs create mode 100644 src/gitea/issue.rs create mode 100644 src/gitea/mod.rs create mode 100644 src/gitea/repository.rs diff --git a/src/config.rs b/src/config.rs index 468a551..9cd9fdd 100644 --- a/src/config.rs +++ b/src/config.rs @@ -3,6 +3,7 @@ use serde::Deserialize; #[derive(Deserialize, Debug)] pub(crate) struct Config { pub gitlab: Vec, + pub gitea: Vec, pub openproject: OpenProjectConfig, } @@ -12,6 +13,12 @@ pub(crate) struct GitlabConfig { pub domain: String, } +#[derive(Deserialize, Debug)] +pub(crate) struct GiteaConfig { + pub token: String, + pub domain: String, +} + #[derive(Deserialize, Debug)] pub(crate) struct OpenProjectConfig { pub token: String, diff --git a/src/debug.rs b/src/debug.rs index 68362b1..6a9f8b2 100644 --- a/src/debug.rs +++ b/src/debug.rs @@ -1,5 +1,7 @@ +use url::Url; use crate::config::Config; use crate::error::GeneralError; +use crate::gitea::issue::Issue; use crate::openproject::client::Client; use crate::openproject::root::RootClient; use crate::openproject::user::GetMe; @@ -7,6 +9,14 @@ use crate::openproject::user::GetMe; pub(crate) async fn debug(config: Config) -> Result<(), GeneralError> { println!("test"); + + let gitea_client = crate::gitea::client::Client::from_config(config.gitea.first().unwrap()); + let issue: Issue = gitea_client.get(Url::parse("https://gitea.champs-libres.be/api/v1/repos/julienfastre/test/issues/6").unwrap()).await?; + + println!("issue: {:?}", issue); + + return Ok(()); + let open_project_client = Client::from_config(&config.openproject); println!("base_url: {}", open_project_client.base_url); println!("base_url: will get root"); diff --git a/src/error.rs b/src/error.rs index c39d1aa..fdf9bc0 100644 --- a/src/error.rs +++ b/src/error.rs @@ -1,4 +1,23 @@ +use reqwest::header::InvalidHeaderValue; +use reqwest::Error; + #[derive(Debug)] pub struct GeneralError { pub(crate) description: String, } + +impl From for GeneralError { + fn from(value: InvalidHeaderValue) -> Self { + GeneralError { + description: "Unable to convert the token into header value".to_string(), + } + } +} + +impl From for GeneralError { + fn from(value: Error) -> Self { + GeneralError { + description: format!("Unable to perform a request: {}", value.to_string()), + } + } +} diff --git a/src/gitea/client.rs b/src/gitea/client.rs new file mode 100644 index 0000000..a3abd12 --- /dev/null +++ b/src/gitea/client.rs @@ -0,0 +1,54 @@ +use crate::error::GeneralError; +use reqwest::header::{HeaderMap, AUTHORIZATION, ACCEPT}; +use reqwest::{ClientBuilder, StatusCode}; +use serde::de::DeserializeOwned; +use url::Url; +use crate::config::GiteaConfig; + +#[derive(Debug)] +pub struct Client { + token: String, + base_uri: String, +} + +impl Client { + + pub fn from_config(config: &GiteaConfig) -> Self { + Self::new(&config.token, &config.domain) + } + + pub fn new(token: &String, domain: &String) -> Self { + Client { + token: token.clone(), + base_uri: format!("https://{}", domain.clone()), + } + } + + pub async fn get(&self, url: Url) -> Result { + let mut headers = HeaderMap::new(); + headers.append(AUTHORIZATION, self.token.parse()?); + headers.append(ACCEPT, "application/json".parse()?); + + let client = ClientBuilder::new() + .default_headers(headers) + .build() + .unwrap(); + + let response = client.get(url.clone()).send().await?; + + match response.status() { + StatusCode::OK => { + let result: T = response.json().await?; + + return Ok(result); + } + _ => Err(GeneralError { + description: format!( + "Could not call GET on {:?}, error code {}", + url, + response.status() + ), + }), + } + } +} diff --git a/src/gitea/issue.rs b/src/gitea/issue.rs new file mode 100644 index 0000000..ad7fc57 --- /dev/null +++ b/src/gitea/issue.rs @@ -0,0 +1,22 @@ +use crate::gitea::client::Client; +use serde::Deserialize; +use crate::gitea::repository::Repository; + +#[derive(Debug, Deserialize)] +pub struct Issue { + id: u64, + number: u64, + title: String, + body: String, + repository: Repository +} + +pub trait IssueClient { + fn get_issue(owner_or_organisation: &String, repo: &String, number: &u64) -> Option; +} + +impl IssueClient for Client { + fn get_issue(owner_or_organisation: &String, repo: &String, number: &u64) -> Option { + todo!() + } +} diff --git a/src/gitea/mod.rs b/src/gitea/mod.rs new file mode 100644 index 0000000..c47c4ac --- /dev/null +++ b/src/gitea/mod.rs @@ -0,0 +1,3 @@ +pub mod client; +pub mod issue; +pub mod repository; diff --git a/src/gitea/repository.rs b/src/gitea/repository.rs new file mode 100644 index 0000000..0e0ba74 --- /dev/null +++ b/src/gitea/repository.rs @@ -0,0 +1,9 @@ +use serde::Deserialize; + +#[derive(Debug, Deserialize)] +pub struct Repository { + id: u64, + name: String, + owner: String, + full_name: String, +} diff --git a/src/main.rs b/src/main.rs index 19789f7..fc7ff5f 100644 --- a/src/main.rs +++ b/src/main.rs @@ -7,6 +7,7 @@ mod cli; mod config; mod debug; mod error; +mod gitea; mod gitlab; mod openproject; mod planning;