WIP on integrate gitea

This commit is contained in:
Julien Fastré 2024-05-20 19:16:21 +02:00
parent 0c3320943e
commit 9aec267e0a
Signed by: julienfastre
GPG Key ID: BDE2190974723FCB
8 changed files with 125 additions and 0 deletions

View File

@ -3,6 +3,7 @@ use serde::Deserialize;
#[derive(Deserialize, Debug)] #[derive(Deserialize, Debug)]
pub(crate) struct Config { pub(crate) struct Config {
pub gitlab: Vec<GitlabConfig>, pub gitlab: Vec<GitlabConfig>,
pub gitea: Vec<GiteaConfig>,
pub openproject: OpenProjectConfig, pub openproject: OpenProjectConfig,
} }
@ -12,6 +13,12 @@ pub(crate) struct GitlabConfig {
pub domain: String, pub domain: String,
} }
#[derive(Deserialize, Debug)]
pub(crate) struct GiteaConfig {
pub token: String,
pub domain: String,
}
#[derive(Deserialize, Debug)] #[derive(Deserialize, Debug)]
pub(crate) struct OpenProjectConfig { pub(crate) struct OpenProjectConfig {
pub token: String, pub token: String,

View File

@ -1,5 +1,7 @@
use url::Url;
use crate::config::Config; use crate::config::Config;
use crate::error::GeneralError; use crate::error::GeneralError;
use crate::gitea::issue::Issue;
use crate::openproject::client::Client; use crate::openproject::client::Client;
use crate::openproject::root::RootClient; use crate::openproject::root::RootClient;
use crate::openproject::user::GetMe; use crate::openproject::user::GetMe;
@ -7,6 +9,14 @@ use crate::openproject::user::GetMe;
pub(crate) async fn debug(config: Config) -> Result<(), GeneralError> { pub(crate) async fn debug(config: Config) -> Result<(), GeneralError> {
println!("test"); 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); let open_project_client = Client::from_config(&config.openproject);
println!("base_url: {}", open_project_client.base_url); println!("base_url: {}", open_project_client.base_url);
println!("base_url: will get root"); println!("base_url: will get root");

View File

@ -1,4 +1,23 @@
use reqwest::header::InvalidHeaderValue;
use reqwest::Error;
#[derive(Debug)] #[derive(Debug)]
pub struct GeneralError { pub struct GeneralError {
pub(crate) description: String, pub(crate) description: String,
} }
impl From<InvalidHeaderValue> for GeneralError {
fn from(value: InvalidHeaderValue) -> Self {
GeneralError {
description: "Unable to convert the token into header value".to_string(),
}
}
}
impl From<reqwest::Error> for GeneralError {
fn from(value: Error) -> Self {
GeneralError {
description: format!("Unable to perform a request: {}", value.to_string()),
}
}
}

54
src/gitea/client.rs Normal file
View File

@ -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<T: DeserializeOwned>(&self, url: Url) -> Result<T, GeneralError> {
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()
),
}),
}
}
}

22
src/gitea/issue.rs Normal file
View File

@ -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<Issue>;
}
impl IssueClient for Client {
fn get_issue(owner_or_organisation: &String, repo: &String, number: &u64) -> Option<Issue> {
todo!()
}
}

3
src/gitea/mod.rs Normal file
View File

@ -0,0 +1,3 @@
pub mod client;
pub mod issue;
pub mod repository;

9
src/gitea/repository.rs Normal file
View File

@ -0,0 +1,9 @@
use serde::Deserialize;
#[derive(Debug, Deserialize)]
pub struct Repository {
id: u64,
name: String,
owner: String,
full_name: String,
}

View File

@ -7,6 +7,7 @@ mod cli;
mod config; mod config;
mod debug; mod debug;
mod error; mod error;
mod gitea;
mod gitlab; mod gitlab;
mod openproject; mod openproject;
mod planning; mod planning;