allow multiple instances of gitlab to be configured
This commit is contained in:
parent
34f6eac006
commit
df71e1073c
@ -2,13 +2,14 @@ use serde::Deserialize;
|
|||||||
|
|
||||||
#[derive(Deserialize, Debug)]
|
#[derive(Deserialize, Debug)]
|
||||||
pub(crate) struct Config {
|
pub(crate) struct Config {
|
||||||
pub gitlab: GitlabConfig,
|
pub gitlab: Vec<GitlabConfig>,
|
||||||
pub openproject: OpenProjectConfig,
|
pub openproject: OpenProjectConfig,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Deserialize, Debug)]
|
#[derive(Deserialize, Debug)]
|
||||||
pub(crate) struct GitlabConfig {
|
pub(crate) struct GitlabConfig {
|
||||||
pub token: String,
|
pub token: String,
|
||||||
|
pub domain: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Deserialize, Debug)]
|
#[derive(Deserialize, Debug)]
|
||||||
|
35
src/gitlab/client.rs
Normal file
35
src/gitlab/client.rs
Normal file
@ -0,0 +1,35 @@
|
|||||||
|
use crate::config::Config;
|
||||||
|
use crate::error::GeneralError;
|
||||||
|
use gitlab::AsyncGitlab;
|
||||||
|
use gitlab::GitlabBuilder;
|
||||||
|
use url::Url;
|
||||||
|
|
||||||
|
pub trait ClientProviderTrait {
|
||||||
|
async fn client_for_url(url: &Url, config: &Config) -> Result<AsyncGitlab, GeneralError>;
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct ClientProvider {}
|
||||||
|
|
||||||
|
impl ClientProviderTrait for ClientProvider {
|
||||||
|
async fn client_for_url(url: &Url, config: &Config) -> Result<AsyncGitlab, GeneralError> {
|
||||||
|
for c in &config.gitlab {
|
||||||
|
if url.domain() == Some(c.domain.as_str()) {
|
||||||
|
let client = GitlabBuilder::new("gitlab.com", c.token.clone())
|
||||||
|
.build_async()
|
||||||
|
.await;
|
||||||
|
|
||||||
|
return match client {
|
||||||
|
Ok(new_client) => Ok(new_client),
|
||||||
|
Err(e) => {
|
||||||
|
let new_error = e.into();
|
||||||
|
Err(new_error)
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Err(GeneralError {
|
||||||
|
description: format!("No client available for this domain: {:?}", url.domain()),
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
@ -1,3 +1,4 @@
|
|||||||
|
pub mod client;
|
||||||
pub mod issue;
|
pub mod issue;
|
||||||
|
|
||||||
use crate::error::GeneralError;
|
use crate::error::GeneralError;
|
||||||
|
@ -3,22 +3,23 @@ use crate::error::GeneralError;
|
|||||||
use crate::openproject::work::{WorkPackage, WorkPackageWriter};
|
use crate::openproject::work::{WorkPackage, WorkPackageWriter};
|
||||||
use reqwest::Response;
|
use reqwest::Response;
|
||||||
use serde::Deserialize;
|
use serde::Deserialize;
|
||||||
|
use std::error::Error;
|
||||||
|
|
||||||
#[derive(Deserialize, Debug)]
|
#[derive(Deserialize, Debug)]
|
||||||
pub(crate) struct Error {
|
pub(crate) struct OpenProjectError {
|
||||||
pub(crate) description: String,
|
pub(crate) description: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl From<reqwest::Error> for Error {
|
impl From<reqwest::Error> for OpenProjectError {
|
||||||
fn from(value: reqwest::Error) -> Self {
|
fn from(value: reqwest::Error) -> Self {
|
||||||
Error {
|
OpenProjectError {
|
||||||
description: format!("Error while connecting to openproject instance: {}", value),
|
description: format!("Error while connecting to openproject instance: {}", value),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl From<Error> for GeneralError {
|
impl From<OpenProjectError> for GeneralError {
|
||||||
fn from(value: Error) -> GeneralError {
|
fn from(value: OpenProjectError) -> GeneralError {
|
||||||
GeneralError {
|
GeneralError {
|
||||||
description: value.description,
|
description: value.description,
|
||||||
}
|
}
|
||||||
@ -33,7 +34,7 @@ pub(crate) struct Client {
|
|||||||
pub async fn handle_response_status<T: for<'de> serde::Deserialize<'de>>(
|
pub async fn handle_response_status<T: for<'de> serde::Deserialize<'de>>(
|
||||||
response: Response,
|
response: Response,
|
||||||
error_message: &str,
|
error_message: &str,
|
||||||
) -> Result<T, Error> {
|
) -> Result<T, OpenProjectError> {
|
||||||
if !response.status().is_success() {
|
if !response.status().is_success() {
|
||||||
let status = response.status().to_string().clone();
|
let status = response.status().to_string().clone();
|
||||||
let content = response
|
let content = response
|
||||||
@ -42,7 +43,7 @@ pub async fn handle_response_status<T: for<'de> serde::Deserialize<'de>>(
|
|||||||
.unwrap_or_else(|_| "Impossible to decode".into())
|
.unwrap_or_else(|_| "Impossible to decode".into())
|
||||||
.clone();
|
.clone();
|
||||||
|
|
||||||
return Err(Error {
|
return Err(OpenProjectError {
|
||||||
description: format!(
|
description: format!(
|
||||||
"{}, status: {}, content: {}",
|
"{}, status: {}, content: {}",
|
||||||
error_message, status, content
|
error_message, status, content
|
||||||
@ -50,9 +51,18 @@ pub async fn handle_response_status<T: for<'de> serde::Deserialize<'de>>(
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
let t = response.json().await?;
|
let t = response.json::<T>().await;
|
||||||
|
|
||||||
Ok(t)
|
match t {
|
||||||
|
Ok(t) => Ok(t),
|
||||||
|
Err(e) => Err(OpenProjectError {
|
||||||
|
description: format!(
|
||||||
|
"Error while decoding json: {}, source: {:?}",
|
||||||
|
e.to_string(),
|
||||||
|
e.source()
|
||||||
|
),
|
||||||
|
}),
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Client {
|
impl Client {
|
||||||
@ -67,21 +77,22 @@ impl Client {
|
|||||||
&self,
|
&self,
|
||||||
work_package: &WorkPackageWriter,
|
work_package: &WorkPackageWriter,
|
||||||
project_id: &String,
|
project_id: &String,
|
||||||
) -> Result<WorkPackage, Error> {
|
) -> Result<WorkPackage, OpenProjectError> {
|
||||||
let client = reqwest::Client::new();
|
let client = reqwest::Client::new();
|
||||||
|
|
||||||
let work_package: WorkPackage = client
|
let response = client
|
||||||
.post(format!(
|
.post(format!(
|
||||||
"{}/api/v3/projects/{}/work_packages",
|
"{}/api/v3/projects/{}/work_packages",
|
||||||
self.base_url, project_id
|
self.base_url, project_id
|
||||||
))
|
))
|
||||||
.basic_auth("apikey", Some(&self.token))
|
.basic_auth("apikey", Some(&self.token))
|
||||||
.json(&work_package)
|
.json(work_package)
|
||||||
.send()
|
.send()
|
||||||
.await?
|
|
||||||
.json()
|
|
||||||
.await?;
|
.await?;
|
||||||
|
|
||||||
|
let work_package =
|
||||||
|
handle_response_status(response, "error while retrieving work package").await?;
|
||||||
|
|
||||||
Ok(work_package)
|
Ok(work_package)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
use crate::openproject::client::{handle_response_status, Client, Error};
|
use crate::openproject::client::{handle_response_status, Client, OpenProjectError};
|
||||||
use crate::openproject::hal::HalEntity;
|
use crate::openproject::hal::HalEntity;
|
||||||
use serde::Deserialize;
|
use serde::Deserialize;
|
||||||
|
|
||||||
@ -22,11 +22,11 @@ pub struct Root {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub trait RootClient {
|
pub trait RootClient {
|
||||||
async fn root(&self) -> Result<Root, Error>;
|
async fn root(&self) -> Result<Root, OpenProjectError>;
|
||||||
}
|
}
|
||||||
|
|
||||||
impl RootClient for Client {
|
impl RootClient for Client {
|
||||||
async fn root(&self) -> Result<Root, Error> {
|
async fn root(&self) -> Result<Root, OpenProjectError> {
|
||||||
let client = reqwest::Client::new();
|
let client = reqwest::Client::new();
|
||||||
|
|
||||||
let response = client
|
let response = client
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
use crate::openproject::client::{handle_response_status, Client, Error};
|
use crate::openproject::client::{handle_response_status, Client, OpenProjectError};
|
||||||
use crate::openproject::hal::Link;
|
use crate::openproject::hal::Link;
|
||||||
use crate::openproject::root::RootClient;
|
use crate::openproject::root::RootClient;
|
||||||
use serde::Deserialize;
|
use serde::Deserialize;
|
||||||
@ -19,11 +19,11 @@ pub struct User {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub trait GetMe {
|
pub trait GetMe {
|
||||||
async fn me(&self) -> Result<User, Error>;
|
async fn me(&self) -> Result<User, OpenProjectError>;
|
||||||
}
|
}
|
||||||
|
|
||||||
impl GetMe for Client {
|
impl GetMe for Client {
|
||||||
async fn me(&self) -> Result<User, Error> {
|
async fn me(&self) -> Result<User, OpenProjectError> {
|
||||||
let r = self.root().await?;
|
let r = self.root().await?;
|
||||||
let client = reqwest::Client::new();
|
let client = reqwest::Client::new();
|
||||||
|
|
||||||
|
@ -2,7 +2,7 @@ use serde::{Deserialize, Serialize};
|
|||||||
|
|
||||||
#[derive(Serialize, Debug)]
|
#[derive(Serialize, Debug)]
|
||||||
pub struct WorkPackageWriterAssignee {
|
pub struct WorkPackageWriterAssignee {
|
||||||
pub(crate) href: String,
|
pub(crate) href: Option<String>,
|
||||||
}
|
}
|
||||||
#[derive(Serialize, Debug)]
|
#[derive(Serialize, Debug)]
|
||||||
pub struct WorkPackageWriter {
|
pub struct WorkPackageWriter {
|
||||||
@ -10,7 +10,7 @@ pub struct WorkPackageWriter {
|
|||||||
#[serde(alias = "type")]
|
#[serde(alias = "type")]
|
||||||
pub(crate) work_type: String,
|
pub(crate) work_type: String,
|
||||||
pub(crate) description: DescriptionWriter,
|
pub(crate) description: DescriptionWriter,
|
||||||
pub assignee: Option<WorkPackageWriterAssignee>,
|
pub assignee: WorkPackageWriterAssignee,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Serialize, Debug)]
|
#[derive(Serialize, Debug)]
|
||||||
|
@ -1,12 +1,13 @@
|
|||||||
use crate::cli::Issue2Work;
|
use crate::cli::Issue2Work;
|
||||||
use crate::config::Config;
|
use crate::config::Config;
|
||||||
use crate::error::GeneralError;
|
use crate::error::GeneralError;
|
||||||
|
use crate::gitlab::client::{ClientProvider, ClientProviderTrait};
|
||||||
use crate::gitlab::issue::IssueBundle;
|
use crate::gitlab::issue::IssueBundle;
|
||||||
use crate::openproject::client::Client;
|
use crate::openproject::client::Client;
|
||||||
use crate::openproject::user::{GetMe, User};
|
use crate::openproject::user::{GetMe, User};
|
||||||
use crate::openproject::work::WorkPackageWriterAssignee;
|
use crate::openproject::work::WorkPackageWriterAssignee;
|
||||||
use gitlab::api::{issues, projects, AsyncQuery};
|
use gitlab::api::{issues, projects, AsyncQuery};
|
||||||
use gitlab::{GitlabBuilder, Issue, Project};
|
use gitlab::{Issue, Project};
|
||||||
use url::Url;
|
use url::Url;
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
@ -22,17 +23,6 @@ struct Issue2WorkPackageDTO {
|
|||||||
pub assign_to: Option<User>,
|
pub assign_to: Option<User>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl From<&Issue2WorkPackageDTO> for Option<WorkPackageWriterAssignee> {
|
|
||||||
fn from(value: &Issue2WorkPackageDTO) -> Self {
|
|
||||||
match &value.assign_to {
|
|
||||||
None => None,
|
|
||||||
Some(w) => Some(WorkPackageWriterAssignee {
|
|
||||||
href: w.clone().d_links.d_self.href,
|
|
||||||
}),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl From<&Issue2WorkPackageDTO> for crate::openproject::work::WorkPackageWriter {
|
impl From<&Issue2WorkPackageDTO> for crate::openproject::work::WorkPackageWriter {
|
||||||
fn from(value: &Issue2WorkPackageDTO) -> Self {
|
fn from(value: &Issue2WorkPackageDTO) -> Self {
|
||||||
crate::openproject::work::WorkPackageWriter {
|
crate::openproject::work::WorkPackageWriter {
|
||||||
@ -47,7 +37,12 @@ impl From<&Issue2WorkPackageDTO> for crate::openproject::work::WorkPackageWriter
|
|||||||
format: "markdown".into(),
|
format: "markdown".into(),
|
||||||
raw: format!("From gitlab: {}", value.issue.issue.web_url),
|
raw: format!("From gitlab: {}", value.issue.issue.web_url),
|
||||||
},
|
},
|
||||||
assignee: value.into(),
|
assignee: WorkPackageWriterAssignee {
|
||||||
|
href: match &value.assign_to {
|
||||||
|
None => None,
|
||||||
|
Some(w) => Some(w.clone().d_links.d_self.href),
|
||||||
|
},
|
||||||
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -56,9 +51,7 @@ pub(crate) async fn issue2work(config: Config, args: &Issue2Work) -> Result<(),
|
|||||||
let url = Url::parse(&*args.issue_url).expect("issue_url is not valid");
|
let url = Url::parse(&*args.issue_url).expect("issue_url is not valid");
|
||||||
let data = extract_issue_info(&url).unwrap();
|
let data = extract_issue_info(&url).unwrap();
|
||||||
|
|
||||||
let client = GitlabBuilder::new("gitlab.com", config.gitlab.token)
|
let client = ClientProvider::client_for_url(&url, &config).await?;
|
||||||
.build_async()
|
|
||||||
.await?;
|
|
||||||
|
|
||||||
let endpoint = issues::ProjectIssues::builder()
|
let endpoint = issues::ProjectIssues::builder()
|
||||||
.iid(data.iid)
|
.iid(data.iid)
|
||||||
|
Loading…
Reference in New Issue
Block a user